Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:mfriedel/minc-stuffs into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mcvaneede committed Jul 13, 2016
2 parents 25621f6 + 6b94f44 commit 914a884
Show file tree
Hide file tree
Showing 11 changed files with 852 additions and 120 deletions.
13 changes: 13 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
New in Version 0.1.14
=====================
* completely remove the multiprocessing machinery from pmincaverage since this caused extra processes
to be spawned even when running single-threaded, causing large memory usage to be attributed to
running pmincaverage instances. If necessary, this machinery could be reinstated
behind some conditional logic.
* small upgrades to voxel_vote: better help messages and argument parsing,
ability to vote on a single file, small code improvements, --(no-)clobber respected
* sge_batch has been removed; use pipetone/qbatch instead
* atlas_to_atlas.pl has been added, though use of Pydpiper's MAGeT.py is recommended in general
* tagtoxfm and some supporting code have been added to allow their use in environments without
a build of the visual tools from minc-toolkit

New in Version 0.1.13
=====================
* code changes to ensure python3 compatibility
Expand Down
2 changes: 1 addition & 1 deletion perl/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bin_SCRIPTS = lin_from_nlin scale_voxels add_noise_to_volume grid_object_manipulator ray_trace_crosshair concatenate_tags sge_batch
bin_SCRIPTS = lin_from_nlin scale_voxels add_noise_to_volume grid_object_manipulator ray_trace_crosshair concatenate_tags

grid_object_manipulator: grid_object_manipulator
sed < $< > $@-t \
Expand Down
92 changes: 0 additions & 92 deletions python/cython_functions.pyx

This file was deleted.

17 changes: 8 additions & 9 deletions python/pmincaverage
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ from numpy import *
from argparse import ArgumentParser
import os.path
import sys
import multiprocessing as mp
try:
import queue
except:
import Queue as queue

def getslice(volhandle, slice, q,nslices):
"""collects hyperslabs for a particular volume handle. Designed to
Expand Down Expand Up @@ -77,8 +80,6 @@ if __name__ == "__main__":
volhandles[0].sizes[3]))

# loop over all slices
p = mp.Pool(processes=1)
manager = mp.Manager()
for i in range(0,volhandles[0].sizes[sliceIndex],nslices):
# last set might have less than n-slices - adjust.
if (i + nslices) > volhandles[0].sizes[sliceIndex]:
Expand All @@ -93,14 +94,12 @@ if __name__ == "__main__":
nslices,
volhandles[0].sizes[2],
volhandles[0].sizes[3]))


# without multiprocessing, this queueing stuff probably doesn't make sense -- simplify
print("SLICE: %i" % i)
# use parallel processing to get the slice data
q = manager.Queue()
q = queue.Queue()
for j in range(nfiles):
t = p.apply_async(getslice, (j,i,q,nslices))

t.get() # seems to be necessary as a sort of waitpid call ...
t = getslice(j,i,q,nslices)

# retrieve the data from the queue
while not q.empty():
Expand Down
79 changes: 75 additions & 4 deletions python/replace_label_with_nearest_valid_label
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

from pyminc.volumes.factory import *
import numpy as np
import scipy.stats as scipy_stats
from optparse import OptionParser
import os
import os.path
import cython_functions
import datetime as datetime
import sys

SDTYPE = np.uint16

################################################################################
################################################################################
################################################################################
Expand All @@ -27,6 +29,75 @@ def is_minc_2_file(filename):
################################################################################
################################################################################
################################################################################

#
# This function takes an input data structure which
# contains label values and a list of unwanted/undesired
# labels. The output data structure contains the input
# labels where the unwanted/undesired labels have been
# replaced by the closest valid label as follows:
#
# For an unwanted label:
#
# 1) all 26 neighbors of a voxel are considered to have
# the same distance. If 1 or more of these 26
# neighbors has a valid label (not 0, nor one of the
# values in the unwanted list), it will be used
# to re-label the unwanted label (using the
# most occuring label)
#
# Input: inlabels, the current set of labels
# outlables, a copy of the current set of labels, to be edited
# unwantedlabels, an array containing the labels numbers that need to be replaced
#
# Output: the function will return the number of labels that could not be replaced,
# and outlabels will be updated as described above
#
def substitute_labels_using_26_nearest_neighbors(labels,
outlabels,
unwantedlabels):

# make sure that the input files have the appropriate types:
assert inlabels.dtype == SDTYPE and outlabels.dtype == SDTYPE and unwantedlabels.dtype == SDTYPE

# get dimension information from the minc volume
nv0 = inlabels.shape[0]
nv1 = inlabels.shape[1]
nv2 = inlabels.shape[2]

# the number of unwanted labels
nlabels = unwantedlabels.shape[0]

# number of unwanted labels that could not be replaced using its 26 neighbors
nunresolved = 0

# from each dimension exclude the first and last element, because these have
# a number of undefined neighbors: the ones that fall outside of the file
for v0 in range(1, nv0 - 1):
for v1 in range(1, nv1 - 1):
for v2 in range(1, nv2 - 1):
# first check whether this label value should be replaced
if( np.rint(inlabels[v0,v1,v2]) in unwantedlabels):
# create array of possible substitues
possible_labels = []
# we can safely look at all 27 voxels in the 3*3 block, because
# the voxel in question will not be added to the list of
# possible labels
for i0 in (v0-1, v0, v0+1):
for i1 in (v1-1, v1, v1+1):
for i2 in (v2-1, v2, v2+1):
if( not(np.rint(inlabels[i0,i1,i2]) == 0) and not(np.rint(inlabels[i0,i1,i2]) in unwantedlabels)):
possible_labels.append(np.rint(inlabels[i0,i1,i2]))
# potentially we did not find any valid substitues
if len(possible_labels) == 0:
nunresolved += 1
else: # we have candidates, simply take the most occuring label number
outlabels[v0,v1,v2] = np.rint(scipy_stats.mode(possible_labels)[0])

return nunresolved



if __name__ == "__main__":

usage = "usage: %prog [options] input_labels.mnc updated_labels.mnc\n"
Expand Down Expand Up @@ -94,13 +165,13 @@ only desired label numbers.\n"""
array_undesired_labels = np.fromstring(options.undesired_labels, dtype=np.uint16, sep=',')

previous_unresolved = -1
current_unresolved = cython_functions.substitute_labels_using_26_nearest_neighbors(inlabels.data, outlabels.data, array_undesired_labels)
current_unresolved = substitute_labels_using_26_nearest_neighbors(inlabels.data, outlabels.data, array_undesired_labels)
if(options.verbose):
print("Number of unresolved labels: %f \n" % (current_unresolved))
while( current_unresolved > 0 and not(previous_unresolved == current_unresolved)):
previous_unresolved = current_unresolved
copy_of_data = outlabels.data[::]
current_unresolved = cython_functions.substitute_labels_using_26_nearest_neighbors(copy_of_data, outlabels.data, array_undesired_labels)
current_unresolved = substitute_labels_using_26_nearest_neighbors(copy_of_data, outlabels.data, array_undesired_labels)
if(options.verbose):
print("Number of unresolved labels: %f \n" % (current_unresolved))

Expand Down Expand Up @@ -181,4 +252,4 @@ only desired label numbers.\n"""
#####ndx = distance.argsort() # indirect sort
#####print("Current point <%s> " % (point))
#####print("closest point <%s> " % (label_coordinates[ndx[:1]]))
#####label_iterator_main.iternext()
#####label_iterator_main.iternext()
16 changes: 3 additions & 13 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
#!/usr/bin/env python

from setuptools import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy

# setuptools DWIM monkey-patch madness
# http://mail.python.org/pipermail/distutils-sig/2007-September/thread.html#8204
import sys
if 'setuptools.extension' in sys.modules:
m = sys.modules['setuptools.extension']
m.Extension.__dict__ = m._Extension.__dict__

setup(name="python-stuffs",
version='0.1.13',
version='0.1.14',
scripts=["python/TFCE",
"python/smooth_vector",
"python/measure_xcorr",
Expand All @@ -23,8 +15,6 @@
"python/volumes_from_labels_only",
"python/voxel_vote",
"python/replace_label_with_nearest_valid_label",
"python/rotational_minctracc.py",
"minc-scripts-jscholz/vtk_meshconvert.py"],
cmdclass = {'build_ext': build_ext},
ext_modules = [Extension("cython_functions", ["python/cython_functions.pyx"], include_dirs=[numpy.get_include()])]
"python/rotational_minctracc.py"]
# "minc-scripts-jscholz/vtk_meshconvert.py"],
)
4 changes: 3 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
bin_PROGRAMS = minc_displacement label_volumes_from_jacobians xfm2tag compute_counts_for_labels
bin_PROGRAMS = minc_displacement label_volumes_from_jacobians xfm2tag compute_counts_for_labels tagtoxfm

minc_displacement_SOURCES = minc_displacement.c
label_volumes_from_jacobians_SOURCES = label_volumes_from_jacobians.c
xfm2tag_SOURCES = xfm2tag.c
compute_counts_for_labels_SOURCES= compute_counts_for_labels.c
tagtoxfm_SOURCES = tagtoxfm.c tagtoxfm.h ParseArgv.c ParseArgv.h


Loading

0 comments on commit 914a884

Please sign in to comment.