def clean_ct(CT_original, CT_clean): ct_ori, imageInfo= nrrd.read(CT_original) ct_cl, nr_objects = ndimage.label(ct_ori > -700) ct_ori[ct_cl != 1]=-1024 nrrd.write(CT_clean, np.squeeze(ct_ori))
def cutObj(imagefile, labelfile, labels=None): if labels is None: print 'no labels to be cut' else: print 'Loading image %s...' % (imagefile) data1, header1 = nrrd.read(imagefile) print 'Loading mask %s...' % (labelfile) data2, header2 = nrrd.read(labelfile) print 'Cutting objects with label(s) %s' % str(labels) for i in labels: try: print 'Cutting object ' + str(i) data1[data2 == int(i)] = np.uint8(0) except: print '---' v = np.max(data1) print "Saving result over " + imagefile header1['encoding'] = 'gzip' if v > 256: header1['type'] = 'uint16' nrrd.write(imagefile, np.uint16(data1), options=header1) else: header1['type'] = 'uint8' nrrd.write(imagefile, np.uint8(data1), options=header1)
def align(floatingImage, xform=floatingImage.replace('.nrrd', '_warp.xform'), imageOUT=floatingImage.replace('.nrrd', '_aligned.nrrd'), template=template, settings=''): if 'default' in xform: xform = floatingImage.replace('.nrrd', '_warp.xform') if 'default' in imageOUT: imageOUT = floatingImage.replace('.nrrd', '_aligned.nrrd') if settings == None or 'None' in settings: settings = '' if (os.path.exists(xform) and os.path.exists(template) and os.path.exists(floatingImage)): if os.path.exists(imageOUT): print 'removing old alignment %s' % (imageOUT) os.remove(imageOUT) print 'nice %sreformatx %s -o %s --floating %s %s %s' % ( cmtkdir, settings, imageOUT, floatingImage, template, xform) r = subprocess.call("nice %sreformatx %s -o '%s' --floating '%s' '%s' '%s'" % ( cmtkdir, settings, imageOUT, floatingImage, template, xform), shell=True) try: data1, header1 = nrrd.read(template) data1, header2 = nrrd.read(imageOUT) header1['encoding'] = 'gzip' if header1['space directions'] == ['none', 'none', 'none']: header1.pop("space directions", None) if os.path.exists(imageOUT): os.remove(imageOUT) nrrd.write(imageOUT, data1, options=header1) os.chmod(imageOUT, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) except: pass else: r = 99 return imageOUT, r
def rateOne(file,results="./OverlapResults.csv", methord=slicescore.OverlapCoeff, template=template): if os.path.isfile(template): data1, header1 = nrrd.read(template) else: print 'Template file missing!' data1 = [] if not data1==[]: ctemplate = xscore.xslice(data1) ztemplate = slicescore.zsampleslice(data1) ytemplate = slicescore.ysampleslice(data1) xtemplate = slicescore.xsampleslice(data1) del data1, header1 r=np.float128(0.0) if os.path.isfile(file): print "testing:" + os.path.basename(file) data2, header2 = nrrd.read(file) calignment = xscore.xslice(data2) zalignment = slicescore.zsampleslice(data2) yalignment = slicescore.ysampleslice(data2) xalignment = slicescore.xsampleslice(data2) del data2, header2 r = np.fmin(np.array([xscore.symTest(slicescore.OverlapCoeff,calignment),methord(ctemplate,calignment),methord(ztemplate,zalignment),methord(ytemplate,yalignment),methord(xtemplate,xalignment)])) if not results==None: with open(results, 'a') as csvfile: spamwriter = csv.writer(csvfile) spamwriter.writerow([path,r[-1][0],"[Symmetry,Diagonal,Zsample,Ysample,Xsample,Final]score",r[-1][1],r[-1][2],r[-1][3],r[-1][4],r[-1][5],min(r[-1][1:])]) del xalignment return r
def compute_ct_mask_similarity(input_labelmap_filename, input_ctfilename , output_maskfilename, dilation_value): cropped_data_temp, imageInfo= nrrd.read(input_labelmap_filename) above_zero = cropped_data_temp>0 belowthresh = cropped_data_temp<17000 #fat is 17944 #threshold slice to contain only all pec data cropped_data_temp[above_zero & belowthresh ] = 1 cropped_data_temp[cropped_data_temp>1] = 0 #dilate #cropped_data_temp = np.array(ndimage.binary_dilation(cropped_data_temp,\ # iterations = 10)).astype(np.int) #10 # cropped_data = np.squeeze(cropped_data_temp) if (dilation_value > 0): cropped_data_temp = np.array(ndimage.binary_dilation(cropped_data_temp,\ iterations = dilation_value)).astype(np.int) #10 is the last functional value cropped_data = np.squeeze(cropped_data_temp) print(np.shape(cropped_data)) #now find boundingbox #b = np.where(cropped_data>0) #cropped_data[min(b[0]):max(b[0])+1, min(b[1]):max(b[1])+1 ] = 1 #remove lung tissue ct_data_temp, info = nrrd.read (input_ctfilename) ct_data = np.squeeze(ct_data_temp) print(np.shape(ct_data)) lung_indeces = np.where(ct_data < (-1022)) cropped_data[lung_indeces] = 0 nrrd.write(output_maskfilename,np.squeeze(cropped_data))
def cropObj(imagefile, labelfile, labels=None): if labels is None: print 'no labels to crop to' else: print 'Loading image %s...' % (imagefile) data1, header1 = nrrd.read(imagefile) print 'Loading mask %s...' % (labelfile) data2, header2 = nrrd.read(labelfile) print 'Croping to objects with label(s) %s' % str(labels) mask = np.ones(np.shape(data1), dtype=bool) for i in labels: try: print 'Cropping to object ' + str(i) mask[data2 == int(i)] = False except: print '---' data1[mask] = 0 v = np.max(data1) print "Saving result over " + imagefile header1['encoding'] = 'gzip' if v > 256: header1['type'] = 'uint16' nrrd.write(imagefile, np.uint16(data1), options=header1) else: header1['type'] = 'uint8' nrrd.write(imagefile, np.uint8(data1), options=header1)
def compute_dice_with_transfo(img_fixed, img_moving, transfo): #first transform toolsPaths = ['CIP_PATH']; path=dict() for path_name in toolsPaths: path[path_name]=os.environ.get(path_name,False) if path[path_name] == False: print path_name + " environment variable is not set" exit() temp_out = "/Users/rolaharmouche/Documents/Data/temp_reg.nrrd" resamplecall = os.path.join(path['CIP_PATH'], "ResampleCT") sys_call = resamplecall+" -d "+img_fixed+" -r "+ temp_out+\ " -t "+transfo+" -l "+img_moving os.system(sys_call) print(" computing ssd between "+img_fixed+" and registered"+ img_moving) img1_data, info = nrrd.read(temp_out) img2_data, info = nrrd.read(img_fixed) #careful reference image has labels =2 and 3 added_images = img1_data np.bitwise_and(img1_data, img2_data, added_images) Dice_calculation = sum(added_images[:])*2.0/(sum(img1_data[:])+sum(img2_data[:])) return Dice_calculation
def _run_interface(self, runtime): lm, lm_header = nrrd.read(self.inputs.in_lm) ct, ct_header = nrrd.read(self.inputs.in_ct) spacing = np.zeros(3) spacing[0] = ct_header['space directions'][0][0] spacing[1] = ct_header['space directions'][1][1] spacing[2] = ct_header['space directions'][2][2] regions = None print(self.inputs.chest_regions) if hasattr(self, 'inputs.chest_regions'): #if self.inputs.chest_regions is not '_Undefined': regions = self.inputs.chest_regions.split(',') types = None print(self.inputs.chest_types) if hasattr(self, 'inputs.chest_types'): #if self.inputs.chest_types is not '_Undefined': types = self.inputs.chest_types.split(',') pairs = None if hasattr(self, 'inputs.pairs'): #if self.inputs.options.pairs is not None: tmp = pairs.split(',') assert len(tmp)%2 == 0, 'Specified pairs not understood' pairs = [] for i in xrange(0, len(tmp)/2): pairs.append([tmp[2*i], tmp[2*i+1]]) pheno_names = None if self.inputs.pheno_names is not None: pheno_names = self.inputs.pheno_names.split(',') paren_pheno = ParenchymaPhenotypes(chest_regions=regions, #self.inputs.chest_regions, chest_types=types, pairs=pairs, pheno_names=pheno_names) df = paren_pheno.execute(ct, lm, self.inputs.cid, spacing) if self.inputs.out_csv is not None: df.to_csv(self.inputs.out_csv, index=False) #pdb.set_trace() #fname = self.inputs.volume # img = nb.load(fname) # data = np.array(img.get_data()) # active_map = data > self.inputs.threshold # thresholded_map = np.zeros(data.shape) # thresholded_map[active_map] = data[active_map] # new_img = nb.Nifti1Image(thresholded_map, img.get_affine(), img.get_header()) # _, base, _ = split_filename(fname) # nb.save(new_img, base + '_thresholded.nii') return runtime
def createTiff(RedCh=None, GreenCh=None, BlueCh=None, Output="merged.tif"): if not RedCh == None: print "Loading red channel: " + str(RedCh) dataR, headerR = nrrd.read(str(RedCh)) sh = np.shape(dataR) header = headerR print "..." if not GreenCh == None: print "Loading green channel: " + str(GreenCh) dataG, headerG = nrrd.read(str(GreenCh)) sh = np.shape(dataG) header = headerG print "..." if not BlueCh == None: print "Loading blue channel: " + str(BlueCh) dataB, headerB = nrrd.read(str(BlueCh)) sh = np.shape(dataB) header = headerB print "..." if RedCh == None: dataR = np.zeros(sh, dtype=np.uint8) headerR = header if GreenCh == None: dataG = np.zeros(sh, dtype=np.uint8) headerG = header if BlueCh == None: dataB = np.zeros(sh, dtype=np.uint8) headerB = header if not (np.shape(dataR) == np.shape(dataG) == np.shape(dataB)): print "Error: images not the same size!" return 0 else: print "Merging..." dataR = np.swapaxes(dataR, 0, -1) dataR = np.expand_dims(dataR, axis=0) dataR = np.expand_dims(dataR, axis=2) dataG = np.swapaxes(dataG, 0, -1) dataG = np.expand_dims(dataG, axis=0) dataG = np.expand_dims(dataG, axis=2) out = np.concatenate((dataR, dataG), axis=2) del dataR, dataG, headerR, headerG dataB = np.swapaxes(dataB, 0, -1) dataB = np.expand_dims(dataB, axis=0) dataB = np.expand_dims(dataB, axis=2) out = np.concatenate((out, dataB), axis=2) del dataB, headerB print "Saving merged tif file: " + str(Output) imsave(str(Output), out) return 1
def register_2d_ct(moving_CT_original, fixed_CT_original, output_transfo_name): """ Function that registers a moving 2D ct image to a fixed image, and saves the resulting transformation in a .tfm file moving_CT_original : inout moving CT image fixed_CT_original : input fixed ct image output_transfo_name : filename of the output transformation to be saved. (Mask files will be saved in the same directory as the input files) """ toolsPaths = ['CIP_PATH']; path=dict() for path_name in toolsPaths: path[path_name]=os.environ.get(path_name,False) if path[path_name] == False: print path_name + " environment variable is not set" exit() """ Remove cruft from images before performing registration """ moving_CT= moving_CT_original.split('.')[0]+"_cleaned."+\ moving_CT_original.split('.')[1] fixed_CT= fixed_CT_original.split('.')[0]+"_cleaned."+\ fixed_CT_original.split('.')[1] input_moving_mask_rigid= '_'.join(moving_CT_original.split("_")[0:-1])+"_pecsSubqFatClosedSlice."+\ moving_CT_original.split('.')[1] moving_mask_rigid= '_'.join(moving_CT_original.split("_")[0:-1])+"_pecsSubqFatClosedSlice_thresholded."+\ moving_CT_original.split('.')[1] mask, imageInfo= nrrd.read(input_moving_mask_rigid) above_zero = mask>0 belowthresh = mask<17000 #fat is 17944 #threshold slice to contain only all pec data mask[above_zero & belowthresh ] = 1 mask[mask>1] = 0 nrrd.write(moving_mask_rigid, np.squeeze(mask)) clean_ct(moving_CT_original, moving_CT) clean_ct(fixed_CT_original, fixed_CT) registerCall = os.path.join(path['CIP_PATH'],"RegisterCT2D") register_call = registerCall+" -m "+moving_CT+" -f "+\ fixed_CT+ " --outputTransform "+output_transfo_name+\ " --isIntensity --movingLabelmapFileName "+moving_mask_rigid print(register_call) os.system(register_call)
def get_injection_density(self, experiment_id, file_name=None): """ Read an injection density volume for a single experiment. Download it first if it doesn't exist. Injection density is the proportion of projecting pixels in a grid voxel only including pixels that are part of the injection site in [0,1]. Parameters ---------- experiment_id: int ID of the experiment to download/read. This corresponds to section_data_set_id in the API. file_name: string File name to store the template volume. If it already exists, it will be read from this file. If file_name is None, the file_name will be pulled out of the manifest. Default is None. """ file_name = self.get_cache_path(file_name, self.INJECTION_DENSITY_KEY, experiment_id, self.resolution) self.api.download_injection_density( file_name, experiment_id, self.resolution, strategy='lazy') return nrrd.read(file_name)
def read_nrrd_atlas(nrrd_file): """ Download atlas files from: http://help.brain-map.org/display/mouseconnectivity/API#API-DownloadAtlas """ import nrrd data, header = nrrd.read(nrrd_file) # convert to ubyte to compress a bit np.multiply(data, 255./data.max(), out=data, casting='unsafe') data = data.astype('ubyte') # data must have axes (anterior, dorsal, right) # rearrange axes to fit -- CCF data comes in (posterior, inferior, right) order. data = data[::-1, ::-1, :] # voxel size in um vxsize = 1e-6 * float(header['space directions'][0][0]) info = [ {'name': 'anterior', 'values': np.arange(data.shape[0]) * vxsize, 'units': 'm'}, {'name': 'dorsal', 'values': np.arange(data.shape[1]) * vxsize, 'units': 'm'}, {'name': 'right', 'values': np.arange(data.shape[2]) * vxsize, 'units': 'm'}, {'vxsize': vxsize} ] ma = metaarray.MetaArray(data, info=info) return ma
def download_annotation_volume(self, ccf_version, resolution, file_name): ''' Download the annotation volume at a particular resolution. Parameters ---------- resolution: int Desired resolution to download in microns. Must be 10, 25, 50, or 100. file_name: string Where to save the annotation volume. ''' if ccf_version is None: ccf_version = MouseConnectivityApi.CCF_VERSION_DEFAULT try: os.makedirs(os.path.dirname(file_name)) except: pass self.download_volumetric_data(ccf_version, 'annotation_%d.nrrd' % resolution, save_file_path=file_name) annotation_data, annotation_image = nrrd.read(file_name) return annotation_data, annotation_image
def download_template_volume(self, resolution, file_name): ''' Download the registration template volume at a particular resolution. Parameters ---------- resolution: int Desired resolution to download in microns. Must be 10, 25, 50, or 100. file_name: string Where to save the registration template volume. ''' try: os.makedirs(os.path.dirname(file_name)) except: pass self.download_volumetric_data(MouseConnectivityApi.AVERAGE_TEMPLATE, 'average_template_%d.nrrd' % resolution, save_file_path=file_name) annotation_data, annotation_image = nrrd.read(file_name) return annotation_data, annotation_image
def download_annotation_volume(self, resolution, file_name): ''' Download the annotation volume at a particular resolution. Parameters ---------- resolution: int Desired resolution to download in microns. Must be 10, 25, 50, or 100. file_name: string Where to save the annotation volume. ''' try: os.makedirs(os.path.dirname(file_name)) except: pass self.download_volumetric_data('annotation/ccf_2015', 'annotation_%d.nrrd' % resolution, save_file_path=file_name) annotation_data, annotation_image = nrrd.read(file_name) return annotation_data, annotation_image
def get_NRRD_image(self): """ Wrapper around nrrd.read so as to return the transpose of the array. """ CT_scan_labels, CT_scan_nrrd_header = nrrd.read(self.parameters["NRRD_path"]) CT_scan_labels = np.transpose(CT_scan_labels, (1,0,2)) + 1 return CT_scan_labels, CT_scan_nrrd_header
def blur_image(input_filename, sigma, output_filename): """ blur an image using a soecific sigma """ toolsPaths = ['CIP_PATH', 'TEEM_PATH']; path=dict() for path_name in toolsPaths: path[path_name]=os.environ.get(path_name,False) if path[path_name] == False: print path_name + " environment variable is not set" exit() unuCall = os.path.join(path['TEEM_PATH'], \ "unu") image, info = nrrd.read(input_filename) if(info['dimension'] == 3): blur_call = unuCall+" resample -s x1 x1 x1 -k gauss:"+str(sigma)+","+str(sigma)+" -i "+\ input_filename+" -o "+ output_filename else: blur_call = unuCall+" resample -s x1 x1 -k gauss:"+str(sigma)+","+str(sigma)+" -i "+\ input_filename+" -o "+ output_filename print(blur_call) os.system(blur_call)
def test_read_header_and_gz_compressed_data_with_lineskip3(self): expected_header = self.expected_header expected_header[u'encoding'] = 'gzip' expected_header[u'line skip'] = 3 data, header = nrrd.read(GZ_LINESKIP_NRRD_FILE_PATH) self.assertEqual(self.expected_header, header) self.assertEqual(self.expected_data, data.tostring(order='F'))
def labelObj(imagefile, labelfile, t=20, ms=1000, sl=np.ones((3, 3, 3))): print 'Loading image %s...' % (imagefile) data1, header1 = nrrd.read(imagefile) header1.pop("endian", None) print 'Labeling objects with any voxel intensity above %s' % str(t) data1[data1 < t] = 0 print 'identifying distinct objects...' labels, features = ndimage.label(data1, structure=sl) print str(features) + ' distinct objects found' val, lab = np.histogram(labels, bins=range(1, features + 1)) print 'Removing any objects with a volume of less than ' + str(ms) + ' voxels.' print 'New label(s):' data = np.zeros(np.shape(data1)) v = 1 for i in range(0, features - 1): if val[i] > ms: data[labels == lab[i]] = v print str(v) + ' = ' + str(lab[i]) v = v + 1 print str(v - 1) + ' distinct objects still indexed' print "Saving result to " + labelfile header1['encoding'] = 'gzip' if v > 256: header1['type'] = 'uint16' nrrd.write(labelfile, np.uint16(data), options=header1) else: header1['type'] = 'uint8' nrrd.write(labelfile, np.uint8(data), options=header1) return np.uint8(np.unique(data))
def compute_ct_mask_similarity_withlabel(input_ct_volume, input_labelmap_filename,\ output_ct_filename, output_maskfilename = None): cropped_data_temp, imageInfo= nrrd.read(input_labelmap_filename) above_zero = cropped_data_temp>0 belowthresh = cropped_data_temp<17000 #fat is 17944 #threshold slice to contain only all pec data cropped_data_temp[above_zero & belowthresh ] = 1 cropped_data_temp[cropped_data_temp>1] = 0 #dilate cropped_data_temp = np.array(ndimage.binary_dilation(cropped_data_temp, iterations = 20)).astype(np.int) #10 cropped_data = np.squeeze(cropped_data_temp) ##now find boundingbox b = np.where(cropped_data>0) cropped_data[:] = -1024 cropped_data[min(b[0]):max(b[0])+1, min(b[1]):max(b[1])+1 ] = \ input_ct_volume[min(b[0]):max(b[0])+1, min(b[1]):max(b[1])+1 ] #label all -1024 as 0, because resampling messes that up if (output_maskfilename != None): sim_mask = np.zeros_like(cropped_data) sim_mask[cropped_data>(-1024)] = 1 nrrd.write(output_maskfilename,np.squeeze(sim_mask)) cropped_data[cropped_data<(-1023)] = 0 nrrd.write(output_ct_filename,np.squeeze(cropped_data))
def loadAllDataFromPath(path, casesToExclude, tips=1): # path in directorty # cubeTipsPath = glob.glob(path + "/*/*.nrrd") cubeTipsPath = getTrainingPaths(path, casesToExclude) # number of samples N = len(cubeTipsPath) cubeTips = [] data = [] for path_i in cubeTipsPath: cubeTips.append(nrrd.read(path_i)) for i in range(N): c = np.array(cubeTips[i][0][:,:,:]) d = c[::-1,:,:] e = c[:,::-1,:] f = c[::-1,::-1,:] if c.shape==tuple(patchsize): data.append(np.array(c)) data.append(np.array(d)) data.append(np.array(e)) data.append(np.array(f)) output = np.array(data, dtype='float32') print('number of sample %d' %len(output)) return output
def get_projection_density(self, experiment_id, file_name=None): """ Read a projection density volume for a single experiment. Download it first if it doesn't exist. Projection density is the proportion of of projecting pixels in a grid voxel in [0,1]. Parameters ---------- experiment_id: int ID of the experiment to download/read. This corresponds to section_data_set_id in the API. file_name: string File name to store the template volume. If it already exists, it will be read from this file. If file_name is None, the file_name will be pulled out of the manifest. Default is None. """ file_name = self.get_cache_path(file_name, self.PROJECTION_DENSITY_KEY, experiment_id, self.resolution) if file_name is None: raise Exception("No file name to save volume.") if not os.path.exists(file_name): self.safe_mkdir(os.path.dirname(file_name)) self.api.download_projection_density(file_name, experiment_id, self.resolution) return nrrd.read(file_name)
def get_template_volume(self, file_name=None): """ Read the template volume. Download it first if it doesn't exist. Parameters ---------- file_name: string File name to store the template volume. If it already exists, it will be read from this file. If file_name is None, the file_name will be pulled out of the manifest. Default is None. """ file_name = self.get_cache_path(file_name, self.TEMPLATE_KEY, self.resolution) if file_name is None: raise Exception("No save file provided for annotation volume.") if os.path.exists(file_name): annotation, info = nrrd.read(file_name) else: Manifest.safe_make_parent_dirs(file_name) annotation, info = self.api.download_template_volume(self.resolution, file_name) return annotation, info
def readDataset(niifilename, niiBrainMaskFilename, btablefilename, parcellationfilename = None): # load the masked diffusion dataset diffusionData = nib.load(niifilename).get_data() affine = nib.load(niifilename).get_affine() # load the brain mask mask = nib.load(niiBrainMaskFilename).get_data() rows, cols, nSlices, nDirections = diffusionData.shape bvals, bvecs = readbtable(btablefilename) gtable = gradient_table(bvals, bvecs) if parcellationfilename != None: #parcellation = nib.load(parcellationfilename).get_data() parcellation,_ = nrrd.read(parcellationfilename) if parcellation.shape[2] != nSlices: # for the second phantom (unc_res) parcellation = parcellation[:,:,parcellation.shape[2]-nSlices:] parcellation = np.squeeze(parcellation) else: parcellation = None return diffusionData, mask, affine, gtable, parcellation
def get_injection_fraction(self, experiment_id, file_name=None): """ Read an injection fraction volume for a single experiment. Download it first if it doesn't exist. Injection fraction is the proportion of pixels in the injection site in a grid voxel in [0,1]. Parameters ---------- experiment_id: int ID of the experiment to download/read. This corresponds to section_data_set_id in the API. file_name: string File name to store the template volume. If it already exists, it will be read from this file. If file_name is None, the file_name will be pulled out of the manifest. Default is None. """ file_name = self.get_cache_path(file_name, self.INJECTION_FRACTION_KEY, experiment_id, self.resolution) if file_name is None: raise Exception("No file name to save volume.") if not os.path.exists(file_name): Manifest.safe_make_parent_dirs(file_name) self.api.download_injection_fraction(file_name, experiment_id, self.resolution) return nrrd.read(file_name)
def get_data_mask(self, experiment_id, file_name=None): """ Read a data mask volume for a single experiment. Download it first if it doesn't exist. Data mask is a binary mask of voxels that have valid data. Only use valid data in analysis! Parameters ---------- experiment_id: int ID of the experiment to download/read. This corresponds to section_data_set_id in the API. file_name: string File name to store the template volume. If it already exists, it will be read from this file. If file_name is None, the file_name will be pulled out of the manifest. Default is None. """ file_name = self.get_cache_path(file_name, self.DATA_MASK_KEY, experiment_id, self.resolution) if file_name is None: raise Exception("No file name to save volume.") if not os.path.exists(file_name): Manifest.safe_make_parent_dirs(file_name) self.api.download_data_mask(file_name, experiment_id, self.resolution) return nrrd.read(file_name)
def write_and_read_back_with_encoding(self, encoding): output_filename = os.path.join(self.temp_write_dir, "testfile_%s.nrrd" % encoding) nrrd.write(output_filename, self.data_input, {u'encoding':encoding}) # Read back the same file data, header = nrrd.read(output_filename) self.assertEqual(self.expected_data, data.tostring(order='F')) self.assertEqual(header['encoding'], encoding)
def extractAndSaveBaselineToNRRD(nrrdfilename, baselinenrrdfilename): nrrdData, bvalue, gradientDirections, baselineIndex,_ = readHARDI(nrrdfilename) baseline = nrrdData[:,:,:,baselineIndex] # save to nrrd _, options = nrrd.read(nrrdfilename) options['keyvaluepairs'] = [] if options.has_key('centerings'): options['centerings'] = [options['centerings'][0], options['centerings'][1], options['centerings'][2]] options['dimension'] = 3 options['kinds'] = ['space', 'space', 'space'] if options.has_key('thicknesses'): options['thicknesses'] = [] if options.has_key('space directions'): options['space directions'] = [options['space directions'][0], options['space directions'][1], options['space directions'][2]] options['thicknesses'] = [abs(options['space directions'][0][0]), abs(options['space directions'][1][1]), abs(options['space directions'][2][2])] #if options.has_key('thicknesses'): # options['thicknesses'] = [options['thicknesses'][0], options['thicknesses'][1], options['thicknesses'][2]] options['sizes'] = list(baseline.shape) nrrd.write( baselinenrrdfilename, baseline, options)
def test_read_header_and_data_filename(self): data, header = nrrd.read(RAW_NRRD_FILE_PATH, index_order=self.index_order) np.testing.assert_equal(self.expected_header, header) np.testing.assert_equal(data, self.expected_data) # Test that the data read is able to be edited self.assertTrue(data.flags['WRITEABLE'])
def compute_simple_mask(input_mask_name, output_mask_name): """ generates a mask with some dilation of the input mask """ mask_data, options = nrrd.read(input_mask_name) print(" reading mask name "+input_mask_name) #mask_data = np.array(ndimage.binary_dilation(mask_data, iterations = 10)).astype(np.int) nrrd.write(output_mask_name,mask_data)
# NOTE: this does not handle the situation if a voxel is in more than one consensus. # this should not typically happen, so just overwrite in this case. sel = (consensus > 0) consensus_labels[sel] = (consensus.astype(np.uint32) * consensus_label_values[j])[sel] # calculate a score for each user for i in range(nlabelers): fn = os.path.join(dn, wkw_labels[i]) dataset = wkw.Dataset.open(fn) # xxx - what is the first singleton dimension? data = np.squeeze(dataset.read(labeled_coordinate, labeled_size)) # if each label is stored sepearately binarized_data = (data == consensus_label_values[j]) fScores[j, i], tpr_recall, precision, tp, fp, fn = pix_fscore_metric( consensus, binarized_data) print('User fScores:') print(fScores) # read in the raw data format to keep the same nrrd header rawdata, header = nrrd.read(raw_fn) #print(rawdata.shape, rawdata.dtype, data.shape, data.dtype) #print(header) # write out the consensus labels header['type'] = 'uint32' nrrd.write(out_labels, consensus_labels, header)
def eval(net, dataset, save_dir=None, do_postprocess=False, anchor_params=None): net.set_mode('eval') net.use_mask = True net.use_rcnn = True avg_dice = [] avg_HD95 = [] raw_dir = config['raw_dir'] data_dir = config['data_dir'] preprocessed_dir = config['preprocessed_data_dir'] print('Total # of eval data %d' % (len(dataset))) for i, (input, truth_bboxes, truth_labels, truth_masks, mask, image) in enumerate(dataset): try: D, H, W = image.shape pid = dataset.filenames[i] print('[%d] Predicting %s' % (i, pid), image.shape) gt_mask = mask.astype(np.uint8) with torch.no_grad(): input = input.cuda().unsqueeze(0) net.forward(input, truth_bboxes, truth_labels, truth_masks, mask) crop_boxes = net.crop_boxes segments = [F.sigmoid(m).cpu().numpy() > 0.5 for m in net.mask_probs] pred_mask = crop_boxes2mask(crop_boxes[:, 1:], segments, input.shape[2:]) pred_mask = pred_mask[:, :D, :H, :W] pred_mask = pred_mask.astype(np.uint8) np.save(os.path.join(save_dir, '%s.npy' % (pid)), pred_mask) if do_postprocess: raise NotImplementedError # Read raw image, the one before being preprocessed and crop start and end # coordiantes # spacing = miccai_spacings[dataset.filenames[i]] dicom_img, header = nrrd.read(os.path.join(data_dir, pid, 'img.nrrd')) # from [x, y, z] to [z, y, x] spacing = header['spacings'][::-1] print('spacing ', spacing) # raw_img, _ = nrrd.read(os.path.join(data_dir, pid, 'img.nrrd')) # raw_img = np.swapaxes(raw_img, 0, -1) # normalized_img = (normalize(raw_img) + 1) / 2 # D_raw, H_raw, W_raw = raw_img.shape # start, end = np.load(os.path.join(preprocessed_dir, '%s_bbox.npy' % (pid))) # raw_gt_mask = np.zeros((len(config['roi_names']), D_raw, H_raw, W_raw), dtype=np.uint8) # raw_gt_mask[:, start[0]:end[0], start[1]:end[1], start[2]:end[2]] = gt_mask # raw_pred_mask = np.zeros((len(config['roi_names']), D_raw, H_raw, W_raw), dtype=np.uint8) # raw_pred_mask[:, start[0]:end[0], start[1]:end[1], start[2]:end[2]] = pred_mask # # Generate comparison image # gt_contours = merge_contours(get_contours_from_masks(raw_gt_mask)) # gt_img = draw_gt(normalized_img, gt_contours) # pred_contours = merge_contours(get_contours_from_masks(raw_pred_mask)) # pred_img = draw_pred(normalized_img, pred_contours) # full = np.concatenate((gt_img, pred_img), 2) # np.save(os.path.join(save_dir, '%s_raw_mask.npy' % (pid)), raw_pred_mask) # np.save(os.path.join(save_dir, '%s_raw_contour.npy' % (pid)), get_contours_from_masks(raw_pred_mask)) # generate_image_anim(full, save_path=os.path.join(save_dir, 'videos', '%s.mp4' % (pid))) # Generate and print dice score for each class HD95 = hausdorff_distance(get_contours_from_masks(pred_mask), get_contours_from_masks(gt_mask), spacing=spacing, percent=0.95) # HD95 = [None] * len(config['roi_names']) score = dice_score_seperate(pred_mask, gt_mask) avg_HD95.append(np.array(HD95)) avg_dice.append(np.array(score)) for i in range(0, len(score)): print(config['roi_names'][i], ' DC', score[i], ', 95% HD', HD95[i]) print() # Clear gpu memory del input, truth_bboxes, truth_labels, truth_masks, mask, image, pred_mask#, gt_mask, gt_img, pred_img, full, score torch.cuda.empty_cache() except Exception as e: del input, truth_bboxes, truth_labels, truth_masks, mask, image, torch.cuda.empty_cache() traceback.print_exc() print() return print('====================') print('Average dice:') avg_dice = np.array(avg_dice) avg_HD95 = np.array(avg_HD95) for i in range(0, len(avg_dice[0])): s_region = avg_dice[:, i] s_region = s_region[s_region != None] HD_region = avg_HD95[:, i] HD_region = HD_region[HD_region != None] if len(s_region) and len(HD_region): print(config['paper_roi_names'][i], ',', round(np.mean(s_region), 4) * 100, ',', round(np.std(s_region), 4) * 100, ',', \ round(np.mean(HD_region), 4), ',', round(np.std(HD_region), 4)) else: print(config['paper_roi_names'][i], ', None, None, None, None') # print('avg, DC ', round(np.mean(avg_dice[avg_dice != None]), 4) * 100) print()
def full_model_pred_DCRF(expr, model, sess, img_path, mask_path, slice_inds, save_dir=None): """Generating predictions of a model that is post-processed by Dense-CRF, over particular slices of an image If `method_name` is given, the last model that is saved for that method will be used (in the given experiment's run) If `method_name` is an empty list, then this function loads the weights to which the parameter `expr.pars['init_weights_path]` is referring. """ img, _ = nrrd.read(img_path) mask, _ = nrrd.read(mask_path) DCRF_preds = np.zeros(img.shape) if save_dir: if not (os.path.exists(save_dir)): os.mkdir(save_dir) for i, ind in enumerate(slice_inds): # get the posteriors slice_posts = full_slice_eval(model, img_path, [ind], 'axial', expr.pars['patch_shape'], expr.pars['ntb'], expr.pars['stats'], sess, 'posteriors') slice_dcrf = DCRF_postprocess_2D(slice_posts[0], img[:, :, ind]) DCRF_preds[:, :, ind] = slice_dcrf #print('%d / %d'% # (i, len(slice_inds)-1)) if False: # save the results, showing # predictions on mask boundaries mask_bound = find_boundaries(mask[:, :, ind]) rgb_result = patch_utils.\ generate_rgb_mask( img[:,:,ind], slice_dcrf, mask_bound) fig = plt.figure(figsize=(7, 7)) plt.imshow(rgb_result, cmap='gray') plt.axis('off') plt.savefig(os.path.join(save_dir, '%d.png' % (slice_inds[i])), bbox_inches='tight') plt.close(fig) # computing F-measure P, N, TP, FP, TN, FN = get_preds_stats(DCRF_preds[:, :, slice_inds], mask[:, :, slice_inds]) Pr = TP / (TP + FP) Rc = TP / P F1 = 2. / (1 / Pr + 1 / Rc) if save_dir: nrrd.write(os.path.join(save_dir, 'dcrf_segs.nrrd'), DCRF_preds) np.savetxt(os.path.join(save_dir, 'F1_score_dcrf.txt'), [F1]) return DCRF_preds, F1
for subdir1 in subdirs1: if '24h' in subdir1 or '48h' in subdir1 or '72h' in subdir1: # Cultivation period level data_dir = os.path.join(path_to_data, subdir1) subdirs2 = get_immediate_subdirectories(data_dir) for subdir2 in subdirs2: # Spheroid data level untreated_dir = os.path.join(data_dir, subdir2) subdirs3 = get_immediate_subdirectories(untreated_dir) for subdir3 in subdirs3: # OpensegSPIM results level result_dir = os.path.join(untreated_dir, subdir3) centroid_name = result_dir.split( os.path.sep)[4] + '->' + result_dir.split(os.path.sep)[6] if centroid_name.endswith('_OpenSegSPIMResults_'): centroid_name = centroid_name[:-20] binary_segmentation_file = os.path.join( result_dir, 'Nucleisegmentedfill2r.nrrd') binary_segmentation, header = nrrd.read( binary_segmentation_file) #XYZ binary_segmentation = np.transpose(binary_segmentation, axes=(2, 1, 0)) #ZYX labelled_segmentation = cc3d.connected_components( binary_segmentation, connectivity=6) labelled_segmentation = np.transpose(labelled_segmentation, axes=(2, 1, 0)) #XYZ export_file = os.path.join( result_dir, 'Nucleisegmentedfill2r_labelled.nrrd') nrrd.write(filename=export_file, data=labelled_segmentation, header=header)
else: raise ValueError("Unexpected value for overwrite, Use 1 or 0") """ PIOTR EDIT""" tmp_nifti_folder = '/home/deepcyst/data' nifti_filename = 'seg_0000.nii.gz' os.makedirs(tmp_nifti_folder, exist_ok=True) if input_folder.endswith('.nii'): nifti = nib.load(input_folder) nib.save(nifti, join(tmp_nifti_folder, nifti_filename)) elif input_folder.endswith('.nii.gz'): copyfile(input_folder, join(tmp_nifti_folder, nifti_filename)) elif input_folder.endswith('.nrrd'): _nrrd = nrrd.read(input_folder) data = _nrrd[0] header = _nrrd[1] print(input_folder) print('DATA:', data) print("HEADER:", header) #nrrd = sitk.ReadImage(input_folder, sitk.sitkFloat64) #data = sitk.GetArrayFromImage(nrrd) x = list(map(float, header['srow_x'].split(' '))) y = list(map(float, header['srow_y'].split(' '))) z = list(map(float, header['srow_z'].split(' '))) affine = np.vstack([x, y, z]) affine = np.concatenate([affine, np.expand_dims(np.array([0, 0, 0, 1]), axis=0)], axis=0)
def load_data(split, img_res, img_path, seg_path, img_type, ignore): HU_min = -500 HU_max = 2500 unique_ids = [] train_test_split = 10 for img_id in os.listdir(img_path): if img_id[0:4] not in unique_ids and img_id[0:6] != ignore: unique_ids.append(img_id[0:4]) np.random.seed(5) np.random.shuffle(unique_ids) if split == "train": fold_ids = unique_ids[0:train_test_split] elif split == "test": fold_ids = unique_ids[train_test_split:] else: raise ValueError img_list = [ img for img in os.listdir(img_path) if img_type in img and img[0:4] in fold_ids ] seg_list = [] for img in img_list: if img_type in ["AC", "VC"]: seg_list.append(f"{img.split('.')[0]}-label.nrrd") else: seg_list.append(f"{img[0:6]}HQ{img[8:11]}-label.nrrd") imgs = [] segs = [] for img, seg in zip(img_list, seg_list): img_arr = nrrd.read(f"{img_path}/{img}")[0].astype("float32") seg_arr = nrrd.read(f"{seg_path}/{seg}")[0].astype("float32") img_dims = img_arr.shape img_arr[img_arr < HU_min] = HU_min img_arr[img_arr > HU_max] = HU_max img_arr = (img_arr - HU_min) / (HU_max - HU_min) idx = np.argwhere(seg_arr == 1) x = (np.unique(idx[:, 0])[0], np.unique(idx[:, 0])[-1]) y = (np.unique(idx[:, 1])[0], np.unique(idx[:, 1])[-1]) z = (np.unique(idx[:, 2])[0], np.unique(idx[:, 2])[-1]) padding_x = img_res - (x[1] - x[0] + 1) padding_y = img_res - (y[1] - y[0] + 1) x_padded = [x[0] - padding_x // 2, x[1] + padding_x // 2] y_padded = [y[0] - padding_y // 2, y[1] + padding_y // 2] if padding_x % 2 != 0: x_padded[1] += 1 if padding_y % 2 != 0: y_padded[1] += 1 if x_padded[1] > img_dims[0] - 1: x_padded[0] -= (x_padded[1] - img_dims[0] + 1) x_padded[1] -= (x_padded[1] - img_dims[0] + 1) elif x_padded[0] < 0: x_padded[1] -= x_padded[0] x_padded[0] -= x_padded[0] else: pass if y_padded[1] > img_dims[1] - 1: y_padded[0] -= (y_padded[1] - img_dims[1] + 1) y_padded[1] -= (y_padded[1] - img_dims[1] + 1) elif y_padded[0] < 0: y_padded[1] += y_padded[0] y_padded[0] += y_padded[0] else: pass for i in range(*z): imgs.append(img_arr[x_padded[0]:(x_padded[1] + 1), y_padded[0]:(y_padded[1] + 1), i][:, :, np.newaxis]) segs.append(seg_arr[x_padded[0]:(x_padded[1] + 1), y_padded[0]:(y_padded[1] + 1), i][:, :, np.newaxis]) return imgs, segs
import numpy as np import SimpleITK as sitk import matplotlib.pyplot as plt import nrrd def resample(image, reference_image, transform): interpolator = sitk.sitkBSpline default_value = 100.0 return sitk.Resample(image, reference_image, transform, interpolator, default_value) # Load the data data, header = nrrd.read( 'test_data/test_image_stack.nrrd') #XYZ -> Nothing to transpose #data = data[0,] plt.imshow(data[:, :, 2]) #Transpose the numpy array from XYZ to ZYX for the use with SimpleITK data_t = np.transpose(data, axes=(2, 1, 0)) # Make a SimpleITK out of the numpy array image = sitk.GetImageFromArray(data_t, isVector=False) # width = image.GetWidth() height = image.GetHeight() depth = image.GetDepth() print('Width: ', width) print('Height: ', height) print('Depth: ', depth)
print( "This script fixes the 'space direction' by adding a 'none' at the begining (component) or at the end (time). It also fixes the 'domains' accordingly." ) print("args should be:") print("<input.nrrd> <output.nrrd> <component|time>") exit() input = sys.argv[-3] output = sys.argv[-2] dim_type = sys.argv[-1] if dim_type != "component" and dim_type != "time": print("The last argument must be 'component' or 'time'") exit() data, header = nrrd.read(input) if len(header["space directions"]) == header["dimension"]: print( "No need to add none as a space direction, it already contains the right number of items." ) exit() modified = False # so that we can add a None inside (not possible with np.ndarray) header["space directions"] = header["space directions"].tolist() if dim_type == "component": modified = True header["space directions"].insert(0, None)
tmp_command = unu + " resample -i %(in)s -s x1 x1 = -k dgauss:%(sigma)f,3 | "+ unu + " resample -s x%(r)f x%(r)f x%(r)f -k tent -o %(out)s" tmp_command = tmp_command % {'in':im, 'out':out,'sigma':sigma,'r':rate} print tmp_command subprocess.call( tmp_command, shell=True) #Define region labels and variables regions=[(5,7),(5,5),(6,6),(7,7)] regions_labels=['Global','Upper','Middle','Lower'] #Define phenotypes labels phenos_labels=['fSAD','RMperc','Jac'] phenos=dict() # Compute Gas Trapping metrics based on Galban's paper insp_im , insp_header = nrrd.read(insp_tmp) reg_exp_im , reg_exp_header = nrrd.read(exp_to_insp_tmp) insp_mask_im , insp_mask_header = nrrd.read(insp_mask) fSAD=list() gastrapping_mask= np.logical_and(insp_im > -950, reg_exp_im < -856) for ii,rr in enumerate(regions): region_mask = np.logical_and(insp_mask_im>=rr[0],insp_mask_im<=rr[1]) #print ii,gastrapping_mask[region_mask].sum(),region_mask.sum() fSAD.append(100*gastrapping_mask[region_mask].sum()/float(region_mask.sum())); print "Gas trapping region "+ str(rr[0]) + " is "+ str(fSAD[ii]) phenos[phenos_labels[0]]=fSAD # Compute Gas Trapping metric using Jacobian tmp_command = "ANTSJacobian 3 %(warp)s %(out)s"
edsr.load_state_dict(torch.load(os.path.join(root,'model','hcp_edsr_2_params.pkl'))) print('load weight success') optimizer = torch.optim.Adam(edsr.parameters(),lr=0.0001, betas=(0.9, 0.999), eps=1e-08) loss_function = nn.L1Loss() ##################################################################### loss_list = [] for step in range(10000000): paths = glob.glob(os.path.join(root,'target_dataset','*.nrrd'))#获取训练集路径 random.shuffle(paths)#打乱 avg_loss = 0 for datas_path in paths: original_datas,original_options = nrrd.read(datas_path)#读取nrrd数据 original_datas = PaddingData(original_datas).astype(np.float32)#padding数据 datas = ThreeDDown(original_datas)#下采样数据 result = np.zeros_like(original_datas,dtype=original_datas.dtype)#构造结果矩阵 #考虑到硬件处理能力的问题,对数据进行移动窗口处理 cha_step = 20#第2维移动步数 row_step = 20#第0维移动步数 col_step = 20#第1维移动步数 for cha in range(0,datas.shape[-1],cha_step): for row in range(0,datas.shape[0],row_step): for col in range(0,datas.shape[1],col_step): _datas = datas[row:row+row_step,col:col+col_step,cha:cha+cha_step]#获取下采样数据的窗口数据 _original_datas = original_datas[row*4:(row+row_step)*4,col*4:(col+col_step)*4,cha*4:(cha+cha_step)*4]#获取下采样数据对应的真值数据,维度扩大四倍
import os import time import datetime import tensorflow as tf import nrrd from keras.callbacks import TensorBoard, ModelCheckpoint, EarlyStopping from tools.cnn import CNN from tools import datagen from tools import image_processing as impro from tools import datatools import SimpleITK as sitk path_to_nuclei = os.path.join('..', '..', '..', 'Daten', '48h', 'untreated', 'C1-untreated_4.1_8_bit.nrrd') path_to_seg = os.path.join('..', '..', '..', 'Daten', '48h', 'untreated', 'C1-untreated_4.1_OpenSegSPIMResults_', 'Nucleisegmentedfill2r.nrrd') nuclei, nuclei_header = nrrd.read(path_to_nuclei) seg, seg_header = nrrd.read(path_to_seg) spacings = seg_header.get('space directions') spacings = [spacings[0,0], spacings[1,1], spacings[2,2]] spacings = np.array(spacings) #centroids, statistics = impro.get_centroids(seg, spacings, 1.) #nuclei = np.zeros(shape=(300, 200, 500)) #XYZ #seg = np.zeros_like(nuclei) #seg[150:170, 90:105, 200:230] = 1 raw_data = np.copy(seg) excluded_volume_size = 1. spacings = np.array([0.5681, 0.5681, 0.5681])
def Generator_3D_patches(path_patients_X, path_patients_y, batchsize): # Generator for iterable that yields patches of (X[i], y[i]), where X is # original image (training data), y segmented image (segmented image), and # patch i is obtained by # Input: path to folder with CT nrrd files of shape: (H,W,D), 1 channel # Output: generator for N patches at a time, in output matrix: (N,H,W,D). # The output matrix will eventually need to be reshaped to (N,C=1,D,H,W) for input to Unet3D for # conv3D and ConvTranspose3d input shape print('Original CT folder:', path_patients_X) print('Labeled CT folder:', path_patients_y) print('Batch size:', batchsize) patients = os.listdir( path_patients_X) # directory of original CT images for all patients patients2 = os.listdir( path_patients_y) # directory of CT masks for all patients print('Patients: ', patients) while True: # shuffle(patients) for idx, namepatient in enumerate(patients): print(namepatient) filename = os.path.join(path_patients_X, namepatient) f, header = nrrd.read(filename) dataMR = f # training data, assume shape is (H,W,D) filename2 = os.path.join(path_patients_y, patients2[idx]) g, header2 = nrrd.read(filename2) dataCT = g # ground truth label dataMR = np.squeeze(dataMR) dataCT = np.squeeze(dataCT) print('Original CT shape: ', dataMR.shape) shapedata = dataMR.shape # #Shuffle data # idx_rnd=np.random.choice(shapedata[0], shapedata[0], replace=False) # dataMR=dataMR[idx_rnd,...] # dataCT=dataCT[idx_rnd,...] # if inputKey2!=None: # dataT1 = dataT1[idx_rnd,...] # # if inputKey3!=None: # dataT2 = dataT2[idx_rnd,...] modulo = np.mod(shapedata[0], batchsize) ################## always the number of samples will be a multiple of batchsz##########################3 if modulo != 0: to_add = batchsize - modulo inds_toadd = np.random.randint(0, dataMR.shape[0], to_add) X = np.zeros((dataMR.shape[0] + to_add, dataMR.shape[1], dataMR.shape[2], dataMR.shape[3])) #dataMR X[:dataMR.shape[0], ...] = dataMR X[dataMR.shape[0]:, ...] = dataMR[inds_toadd] # if inputKey2!=None: # X1=np.zeros((dataT1.shape[0]+to_add, dataT1.shape[1], dataT1.shape[2], dataT1.shape[3]))#dataMR # X1[:dataT1.shape[0],...]=dataT1 # X1[dataT1.shape[0]:,...]=dataT1[inds_toadd] # # if inputKey3!=None: # X2=np.zeros((dataT2.shape[0]+to_add, dataT2.shape[1], dataT2.shape[2], dataT2.shape[3]))#dataMR # X2[:dataT2.shape[0],...]=dataT2 # X2[dataT2.shape[0]:,...]=dataT2[inds_toadd] y = np.zeros((dataCT.shape[0] + to_add, dataCT.shape[1], dataCT.shape[2], dataCT.shape[3])) #dataCT y[:dataCT.shape[0], ...] = dataCT y[dataCT.shape[0]:, ...] = dataCT[inds_toadd] else: X = np.copy(dataMR) # if inputKey2!=None: # X1 = np.copy(dataT1) # if inputKey3!=None: # X2 = np.copy(dataT2) y = np.copy(dataCT) X = np.expand_dims(X, axis=4) X = X.astype(np.float32) # if inputKey2!=None: # X1 = np.expand_dims(X1, axis=4) # X1 = X1.astype(np.float32) # # if inputKey3!=None: # X2 = np.expand_dims(X2, axis=4) # X2 = X2.astype(np.float32) y = np.expand_dims(y, axis=4) y = y.astype(np.float32) print('y shape ', y.shape) print('X shape ', X.shape) for i_batch in range(int(X.shape[0] / batchsize)): yield (X[i_batch * batchsize:(i_batch + 1) * batchsize, ...], y[i_batch * batchsize:(i_batch + 1) * batchsize, ...])
import nrrd import os import numpy as np from skimage import measure path = '/home/drs/Desktop/mvi_data/MVI/2081487/4832691' idx = -1 for item in sorted(os.listdir(path)): idx += 1 if item.endswith('ART.nrrd'): break mask_data, mask_head = nrrd.read( os.path.join(path, sorted(os.listdir(path))[idx])) mask_slice = list(set(np.nonzero(mask_data)[-1])) largest_area = 0 largest_slice = 0 for i in mask_slice: img_labeled = measure.label(mask_data[..., i], connectivity=2) prop = measure.regionprops(img_labeled) area = prop[0].area if area > largest_area: largest_slice = i largest_area = area ratio = mask_head['space directions'][0][0] mm = 10 # the mm you want to expand
def full_model_eval(expr, model, sess, img_path, mask_path, slice_inds, save_dir=None): """Evaluating the last model of a querying method in an experiment's run """ if save_dir: if not (os.path.exists(save_dir)): os.mkdir(save_dir) mask, _ = nrrd.read(mask_path) img, _ = nrrd.read(img_path) # slice-by-slice prediction preds = np.zeros(mask.shape) for i, ind in enumerate(slice_inds): # get the predictins slice_evals = full_slice_eval(model, img_path, [ind], 'axial', expr.pars['patch_shape'], expr.pars['ntb'], expr.pars['stats'], sess) preds[:, :, ind] = slice_evals[0] print('%d / %d' % (i, len(slice_inds)), end=',') # save the results, with showing both # model evaluations and mask boundaries if False: mask_bound = find_boundaries(mask[:, :, ind]) rgb_result = patch_utils.generate_rgb_mask(img[:, :, ind], slice_evals[0], mask_bound) fig = plt.figure(figsize=(7, 7)) plt.imshow(rgb_result, cmap='gray') plt.axis('off') plt.savefig(os.path.join(save_dir, '%d.png' % (ind)), bbox_inches='tight') plt.close(fig) # computing F-measure P, N, TP, FP, TN, FN = get_preds_stats(preds[:, :, slice_inds], mask[:, :, slice_inds]) Pr = TP / (TP + FP) Rc = TP / P F1 = 2. / (1 / Pr + 1 / Rc) print('\n F1: %.4f' % F1) # save the results if necessary # this save_path will be created inside the # method's directory if save_dir: # save the results itself nrrd.write(os.path.join(save_dir, 'segs.nrrd'), np.uint8(preds)) np.savetxt(os.path.join(save_dir, 'F1_socre.txt'), [F1]) return preds, F1
def test_read_detached_header_and_data_with_byteskip_minus5(self): with self.assertRaisesRegex( nrrd.NRRDError, 'Invalid byteskip, allowed values ' + 'are greater than or equal to -1'): nrrd.read(RAW_INVALID_BYTESKIP_NHDR_FILE_PATH, index_order=self.index_order)
from PIL import Image import numpy as np import nrrd # nrrd图片读取 # nrrd图片使用nrrd包gitHub中的data数据 nrrd_filename = '../lung/Nodule_0_84.nrrd' nrrd_data, nrrd_options = nrrd.read(nrrd_filename) print(nrrd_options) nrrd_image = Image.fromarray(nrrd_data[:, :, 100] * 100) nrrd_image.show() # 显示这图片
def test_read_detached_header_and_nifti_data(self): with self.assertRaisesRegex( nrrd.NRRDError, 'Size of the data does not equal ' + 'the product of all the dimensions: 27000-27176=-176'): nrrd.read(GZ_NIFTI_NHDR_FILE_PATH, index_order=self.index_order)
def test_invalid_index_order(self): with self.assertRaisesRegex(nrrd.NRRDError, 'Invalid index order'): nrrd.read(RAW_NRRD_FILE_PATH, index_order=None)
import os.path import pandas as pd import nrrd from cip_python.ChestConventions import ChestConventions from cip_python.phenotypes.body_composition_phenotypes import * import pdb np.set_printoptions(precision = 3, suppress = True, threshold=1e6, linewidth=200) this_dir = os.path.dirname(os.path.realpath(__file__)) lm_name = this_dir + '/../../../Testing/Data/Input/simple_lm.nrrd' lm, lm_header = nrrd.read(lm_name) ct_name = this_dir + '/../../../Testing/Data/Input/simple_ct.nrrd' ct, ct_header = nrrd.read(ct_name) def test_execute(): c = ChestConventions() wc = c.GetChestWildCardName() spacing = np.array([0.5, 0.4, 0.3]) bc_pheno = BodyCompositionPhenotypes() df = bc_pheno.execute(ct, lm, 'simple', spacing) for i in xrange(0, 14): r = df['Region'].iloc[i] t = df['Type'].iloc[i] if (r == 'LeftLung' and t == wc): assert np.isclose(df['HUMean'].iloc[i], -773.333333), \ 'Phenotype not as expected'
def obtain_png_images(self, output_folder, imageName, imageBytes): path = self.save_image(imageBytes, output_folder + imageName, 37) readdata, header = nrrd.read(path, index_order='C') return self.save_png_images(output_folder, readdata)
def process_structure_graph(structureGraph): for structure in structureGraph: #Pop the children out to deal with them recursively children = structure.pop('children', None) # Add the structure info to the dataframe structureList.append(pandas.Series(structure)) #Deal with the children process_structure_graph(children) process_structure_graph(structureGraph) df = pandas.DataFrame(structureList) annotation = nrrd.read( '/home/nick/Dropbox/data/allenbrain/ccf/coronal_annotation_25.nrrd') annotationVol = annotation[0] annotationMetadata = annotation[1] #X, Y, Z coords = np.array([8625, 1400, 6400]) coordsVoxel = coords / 25 #Gives you the id # structid = int(annotationVol[345, 56, 256]) structid = int(annotationVol[329, 149, 180]) name = df.query('id == @structid')['name'] def trace_parents(df, structID):
def readnrrd(nrrd_path, position): data, header = nrrd.read(nrrd_path, index_order='F') return data[int(position[0]), int(position[1]), int(position[2])]
import numpy as np import cv2 import nrrd import random import os import glob root = os.getcwd() for nrrd_data_path in glob.glob( os.path.join(root, 'original_dataset', '*.nrrd')): original_datas, original_options = nrrd.read(nrrd_data_path) nrrd_name = nrrd_data_path.split('\\')[-1][:-5] count = 0 save_count = 0 for cha in range(0, original_datas.shape[-1] - 80, 40): for row in range(0, original_datas.shape[0] - 80, 40): for col in range(0, original_datas.shape[1] - 80, 40): _original_datas = original_datas[row:row + 80, col:col + 80, cha:cha + 80] _original_datas = (65536 * ( (_original_datas - _original_datas.min()) / (_original_datas.max() - _original_datas.min()))).astype( np.uint16) print('{} process shape {}'.format(nrrd_name, _original_datas.shape))
parser = argparse.ArgumentParser() parser.add_argument('-i', action='store', dest='inputImage', help='image .nrrd') parser.add_argument('-o', action='store', dest='path', help='output path') options = parser.parse_args() # get name output_path = options.path input_image = options.inputImage _, name_save = os.path.split(input_image) name_save = str.split(name_save, '.')[0] # read image input_image, input_header = nrrd.read(input_image) #input_image = (input_image.astype(float) - 255.5 / 2) / 255.0 #input_image = (input_image.astype(float)) / 65536.0 input_image = input_image.reshape((1,image_size, image_size)).astype(np.float32) # save as npy print output_path np.save(output_path + name_save + '.npy', input_image) pickle_file = output_path + name_save + '_header.pickle' save = {name_save: input_header} try: f = open(pickle_file, 'wb')
def create_tensors(self, patch_size=(96, 96), save2npy=True): "Function to create the 2D tensor from the 3D images" image_tensor = [] LOGGER.info('Creating the patches to fed then into the network.') LOGGER.info('Chosen path size is: {0}x{1}.'.format( patch_size[0], patch_size[1])) for i, image in enumerate(self.preprocessed_images): patch = 0 im_base, im_name, ext = split_filename(image) im_path = os.path.join(im_base, im_name) if not glob.glob(im_path + '*.npy'): image, _ = nrrd.read(image) im_size = image.shape[:2] if self.preprocessed_masks and not self.testing: mask = self.preprocessed_masks[i] mask_base, mask_name, _ = split_filename(mask) mask_path = os.path.join(mask_base, mask_name) mask, _ = nrrd.read(mask) for n_slice in range(image.shape[2]): im_array, info_dict = load_data_2D('', '', array=image[:, :, n_slice], img_size=im_size, patch_size=patch_size, binarize=False, normalization=True, prediction=self.testing, mb=[]) if self.preprocessed_masks and not self.testing: mask_array, _ = load_data_2D('', '', array=mask[:, :, n_slice], img_size=im_size, patch_size=patch_size, binarize=True, normalization=False) if save2npy: for j in range(im_array.shape[0]): np.save( im_path + '_patch{}.npy'.format(str(patch).zfill(5)), im_array[j, :]) if self.preprocessed_masks: np.save( mask_path + '_patch{}.npy'.format(str(patch).zfill(5)), mask_array[j, :]) patch = patch + 1 else: for j in range(im_array.shape[0]): image_tensor.append(im_array[j, :]) if not save2npy: if info_dict is not None: im_name = im_path + ext self.image_info[im_name]['slices'] = n_slice + 1 for k in info_dict[0].keys(): self.image_info[im_name][k] = info_dict[0][k] if image_tensor: self.image_tensor = (np.asarray(image_tensor).reshape( -1, im_array.shape[1], im_array.shape[2], 1))
# Set up the handler to write out all log entries to a file handler = logging.FileHandler(filename='testLog.txt', mode='w') formatter = logging.Formatter("%(levelname)s:%(name)s: %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # Initialize feature extractor extractor = featureextractor.RadiomicsFeatureExtractor(additionalInfo=True) extractor.enableAllFeatures() extractor.enableAllImageTypes() mask = r"C:\Users\logiusti\Lorenzo\PyWorkspace\Camel-Blue\mask.nrrd" for imageName in os.listdir(nrrd_train_tumor_path): img = nrrd_train_tumor_path + "\\" + imageName mask_shape = tuple(nrrd.read(img)[1]['sizes']) mask = np.ones(mask_shape) nrrd.write("mask.nrrd", mask) print("Calculating features") featureVector = extractor.execute(img, "mask.nrrd") another_dict = dict() for k, v in featureVector.items(): try: another_dict[k] = v.tolist() except: another_dict[k] = v with open(radomics_train_tumor_path + "\\" + imageName + ".json", 'w') as f: json.dump(another_dict, f, indent=2) for imageName in os.listdir(nrrd_train_no_tumor_path):
##print(image_tuple[0].size - vwi_mask_array.shape[0]) #print(case_id, "mean: {0}".format(np.mean(vwi_mask_array)), #" std: {0}".format(np.std(vwi_mask_array)), #" sample size {0}".format(vwi_mask_array.shape[0])) ##print(np.std(vwi_mask_array), np.std(pre2post_mask_array)) if ((not os.path.exists(pickle_file)) or overwrite == 1): for case_id, images in data.items(): image_dict[case_id] = {} print(case_id) for post_label, pre_label in groups: #print(pre_label, post_label) pre_path = images[pre_label] post_path = images[post_label] #vwi_mask, vwi_mask_header = nrrd.read(vwi_mask_path) image_tuple_pre = nrrd.read(pre_path) image_tuple_post = nrrd.read(post_path) image_dict[case_id][pre_label] = image_tuple_pre image_dict[case_id][post_label] = image_tuple_post #vwi_pre = image_tuple_pre[0][image_tuple_pre[0] >= 0.0] #mean_pre = np.mean(vwi_pre) #std_pre = np.std(vwi_pre) #vwi_post = image_tuple_post[0][image_tuple_post[0] >= 0.0] #mean_post = np.mean(vwi_post) #std_post = np.std(vwi_post) #U_pre = conf * std_pre #U_post = conf * std_post
import numpy as np a = np.arange(-8192, 24576, 1) a = np.reshape(a, (32, 32, 32)) print('Min: ', np.min(a)) print('Max: ', np.max(a)) a = np.clip(a, 0, np.max(a)) print('Min: ', np.min(a)) print('Max: ', np.max(a)) import nrrd import os path_to_nuclei = os.path.join('..', '..', '..', 'Daten', '24h', 'untreated', 'C1-untreated_1.1_OpenSegSPIMResults_', 'gauss_centroids.nrrd') data, header = nrrd.read(path_to_nuclei) print(np.sum(data)) print(np.min(data)) print(np.max(data))
# get three phase image and mask path art_image_path = case_path + '/' + item_list[art_mask_slice - 1] art_mask_path = case_path + '/' + item_list[art_mask_slice] nc_image_path = case_path + '/' + item_list[nc_mask_slice - 1] nc_mask_path = case_path + '/' + item_list[nc_mask_slice] pv_image_path = case_path + '/' + item_list[pv_mask_slice - 1] pv_mask_path = case_path + '/' + item_list[pv_mask_slice] # get art image array art_array = nib.load(art_image_path).get_data() # channel last nc_array = nib.load(nc_image_path).get_data() # channel last pv_array = nib.load(pv_image_path).get_data() # channel last # get art mask array art_mask_array, _ = nrrd.read(art_mask_path) art_mask_slice = list(set(np.nonzero(art_mask_array)[-1])) # use the maximum connectivity method to find the largest slice largest_area = 0 largest_slice = 0 for idx in art_mask_slice: img_labeled = measure.label(art_mask_array[:, :, idx], connectivity=2) prop = measure.regionprops(img_labeled) area = prop[0].area if area > largest_area: largest_area = area largest_slice = idx # get the bounding box bbox = prop[0].bbox
import nrrd import tensorflow as tf import os import numpy as np import matplotlib.pyplot as plt import sys sys.path.append("..") from tools import image_processing as impro path_to_data = os.path.join('test_data', 'test_patch.nnrd') data, header = nrrd.read(path_to_data) X = data[0, ] y = data[1, ] plt.imshow(X[:, :, 25]) ############################################################################### # Data normalization ############################################################################### X_norm = tf.keras.utils.normalize(X, axis=-1, order=2) # Check min and max print(np.min(X_norm)) print(np.max(X_norm)) plt.imshow(X_norm[:, :, 25]) ############################################################################### # Data standardization ###############################################################################