def makeLD(self, vesselgroup): ldvessels = krebsutils.read_lattice_data_from_hdf( vesselgroup['lattice']) fieldld = krebsutils.SetupFieldLattice(ldvessels.worldBox, 3, self.bin_size, 0.) fieldldFine = krebsutils.SetupFieldLattice( fieldld.worldBox, 3, self.bin_size / self.fine_bin_subdivision, 0.) return fieldld, fieldldFine
def from_vessel_file(filenames, grp_pattern): dirs = set() dataman = myutils.DataManager(20, [ krebs.plotIff.DataTissue(), krebs.plotIff.DataGlobalIff(), krebs.plotIff.DataRadialIff(), krebs.analyzeGeneral.DataDistanceFromCenter(), krebs.analyzeGeneral.DataVesselSamples(), krebs.analyzeGeneral.DataBasicVessel(), o2analysis.DataDetailedPO2() ]) f_measure = h5files.open('chache.h5', 'a', search=False) def cachelocation(g): path = posixpath.join( 'FileCS_' + myutils.checksum(basename(g.file.filename)), g.name.strip(posixpath.sep)) return (f_measure, path) #run with grp_pattern: iff/vessels for fn in filenames: with h5py.File(fn, 'r+') as f: d = myutils.walkh5(f, grp_pattern) assert len(d), 'you f****d up, pattern "%s" not found in "%s"!' % ( grp_pattern, fn) dirs = set.union(dirs, d) for group_path in dirs: if 'vessel' in grp_pattern and not 'o2' in grp_pattern: vesselgroup = f[group_path] ldvessels = ku.read_lattice_data_from_hdf( vesselgroup['lattice']) fieldld = ku.SetupFieldLattice(ldvessels.worldBox, 3, 10, 0.) phi_vessels = krebs.analyzeGeneral.CalcPhiVessels( dataman, vesselgroup, fieldld, scaling=1., samples_per_cell=5) print('bla') import nibabel as nib new_image = nib.Nifti1Image(phi_vessels, affine=np.eye(4)) common_filename = os.path.splitext(os.path.basename(fn))[0] new_image.to_filename(common_filename + '_vessels' + '.nii') if 'o2' in grp_pattern: po2group = f[group_path] #sample_length = 500. #data = dataman.obtain_data('detailedPO2_global', 'po2_tissue', po2group, sample_length, cachelocation(po2group)) data = np.asarray(po2group['po2field']) print('bla') import nibabel as nib new_image = nib.Nifti1Image(data, affine=np.eye(4)) common_filename = os.path.splitext(os.path.basename(fn))[0] new_image.to_filename(common_filename + '_po2' + '.nii')
def from_vessel_file(filenames, grp_pattern): dirs = set() dataman = myutils.DataManager(20, [ krebs.plotIff.DataTissue(), krebs.plotIff.DataGlobalIff(), krebs.plotIff.DataRadialIff(), krebs.analyzeGeneral.DataDistanceFromCenter(), krebs.analyzeGeneral.DataBasicVessel() ]) #run with grp_pattern: iff/vessels for fn in filenames: with h5py.File(fn, 'r') as f: d = myutils.walkh5(f, grp_pattern) assert len(d), 'you f****d up, pattern "%s" not found in "%s"!' % ( grp_pattern, fn) dirs = set.union(dirs, d) for group_path in dirs: vesselgroup = f[group_path] ldvessels = ku.read_lattice_data_from_hdf( vesselgroup['lattice']) fieldld = ku.SetupFieldLattice(ldvessels.worldBox, 3, 10, 0.) #fieldldFine = ku.SetupFieldLattice(fieldld.worldBox, 3, 50 / 10, 0.) phi_vessels = krebs.analyzeGeneral.CalcPhiVessels( dataman, f['iff/vessels'], fieldld, scaling=1., samples_per_cell=5) #a_abs = np.fabs(phi_vessels) #a_max = np.amax(a_abs) #n_phi = phi_vessels/a_max #visual = (phi_vessels-phi_vessels.min()) #inner1 = matplotlib.cm.gist_earth(n_phi) #inner2 = np.uint8(inner1*255) #result = Image.fromarray(inner2[:,:,:,0:2],mode='RGB') #result.save('out.tiff') print('bla') import nibabel as nib new_image = nib.Nifti1Image(phi_vessels, affine=np.eye(4)) new_image.to_filename('myni') #for i in np.arange(0,phi_vessels.shape[2]): # plt.imsave('img/%03i.png' % i,phi_vessels[:,:,i]) #scipy.io.savemat('test.mat', phi_vessels) #np.save('mydata.tiff',phi_vessels) #pydicom. #pydicom.write_file('mydata.dcm', phi_vessels) plt.imshow(phi_vessels[:, :, 20]) plt.show()
if __name__ == '__main__': filename = sys.argv[1] grouppath = sys.argv[2] with h5py.File(filename, 'r') as file: vesselgroup = file[grouppath] ldvessels = krebsutils.read_lattice_data_from_hdf( vesselgroup['lattice']) wbbox = ldvessels.worldBox graph = krebsutils.read_vesselgraph( vesselgroup, ['position', 'radius', 'flags', 'pressure']) graph = graph.get_filtered( myutils.bbitwise_and(graph['flags'], krebsutils.CIRCULATED)) fieldld = krebsutils.SetupFieldLattice(wbbox, 3, 50., 0.) print fieldld edgevalues = graph['pressure'] edgevalues = edgevalues[graph.edgelist] thefield = krebsutils.CalcIntervascularInterpolationField( graph.edgelist, graph['radius'], graph['position'], edgevalues, fieldld, 1.) volfraction = krebsutils.make_vessel_volume_fraction_field( graph['position'], graph.edgelist, graph['radius'], fieldld, 5) thegrad = ndimage.gaussian_filter(thefield, 1.0, 0, mode='nearest') gradfield_ = krebsutils.field_gradient(thegrad) thegrad = np.sum([np.square(g) for g in gradfield_], axis=0) thegrad = np.sqrt(thegrad) del gradfield_
ldvessels = krebsutils.read_lattice_data_from_hdf( vesselgroup['lattice']) wbbox = ldvessels.worldBox graph = krebsutils.read_vesselgraph(vesselgroup, ['position', 'flags']) graph = graph.get_filtered( myutils.bbitwise_and(graph['flags'], krebsutils.CIRCULATED)) print 'vessel ld:' print ldvessels ''' this splits splits space in lattices of 300. the second 300 adds a 'safety layer of 100. mum so we do not consider the outermost data for calculating the actual mvd ''' fieldld = krebsutils.SetupFieldLattice(wbbox, 3, 20., 100.) wbbox = fieldld.worldBox print 'field ld:' print fieldld z = fieldld.shape[2] / 2 weights = krebsutils.sample_edges_weights(graph.nodes['position'], graph.edgelist, 30.) positions = krebsutils.sample_edges( graph.nodes['position'], graph.edgelist, graph.nodes['position'], 30., krebsutils.VesselSamplingFlags.DATA_PER_NODE | krebsutils.VesselSamplingFlags.DATA_LINEAR) eps = 1.0 - 1.e-15 x0, x1, y0, y1, z0, z1 = wbbox ranges = [
def sample_vessel_system(goodArguments): filename = goodArguments.vesselFileNames grouppath = goodArguments.grp_pattern with h5py.File(filename, 'r') as file: if ('vessels' in grouppath): print('found vessels!') vesselgroup = file[grouppath] else: if ('out' in grouppath): outgroup = file[grouppath] print('found tumor of type: %s' % str(outgroup['tumor'].attrs.get('TYPE'))) vesselgroup = file[grouppath + '/vessels'] else: print("unknown data structure!") ldvessels = krebsutils.read_lattice_data_from_hdf( vesselgroup['lattice']) wbbox = ldvessels.worldBox graph = krebsutils.read_vesselgraph(vesselgroup, ['position', 'flags']) graph = graph.get_filtered( myutils.bbitwise_and(graph['flags'], krebsutils.CIRCULATED)) print 'vessel ld:' print ldvessels ''' this splits splits space in lattices of 300. the second 300 adds a 'safety layer of 100. mum so we do not consider the outermost data for calculating the actual mvd ''' sampling_lattice_spacing = goodArguments.sampling_lattice_spacing fieldld = krebsutils.SetupFieldLattice(wbbox, 3, sampling_lattice_spacing, 100.) wbbox = fieldld.worldBox print 'field ld:' print fieldld z = fieldld.shape[2] / 2 longitudinal_sampling_distance = goodArguments.longitudinal weights = krebsutils.sample_edges_weights(graph.nodes['position'], graph.edgelist, longitudinal_sampling_distance) positions = krebsutils.sample_edges( graph.nodes['position'], graph.edgelist, graph.nodes['position'], longitudinal_sampling_distance, krebsutils.VesselSamplingFlags.DATA_PER_NODE | krebsutils.VesselSamplingFlags.DATA_LINEAR) eps = 1.0 - 1.e-15 x0, x1, y0, y1, z0, z1 = wbbox ranges = [ np.arange(x0, x1, fieldld.scale * eps), np.arange(y0, y1, fieldld.scale * eps), np.arange(z0, z1, fieldld.scale * eps), ] print 'histogram bin ends:', map(lambda r: (r.min(), r.max()), ranges) mvd, _ = np.histogramdd(positions, bins=ranges, weights=weights) mvd *= 1.e6 / (fieldld.scale**3) print 'result shape:', mvd.shape print('average mvd') print(np.mean(mvd[1:-1, 1:-1, 1:-1])) ''' new stuff ''' from scipy import ndimage bool_field = mvd > 0 bool_field = np.logical_not(bool_field) distance_map = ndimage.morphology.distance_transform_edt(bool_field) distance_map = distance_map * sampling_lattice_spacing # fig, ax = pyplot.subplots(1) # plt = ax.imshow(mvd[:,:,z], interpolation = 'none') # ax.set(title = 'MVD') # divider = mpl_utils.make_axes_locatable(ax) # cax = divider.append_axes("right", size = "5%", pad = 0.05) # fig.colorbar(plt, cax = cax) fig, ax = pyplot.subplots(1) plt = ax.imshow(distance_map[:, :, z], interpolation='none') #ax.set(title = 'Distance Map \n group: %s, file: %s' %(grouppath, filename)) ax.set(title='Distance Map \n smp_logitudinal: %s, lattice_const: %s' % (longitudinal_sampling_distance, sampling_lattice_spacing)) divider = mpl_utils.make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(plt, cax=cax) basename(filename) with PdfPages('distmap_' + basename(filename) + '_' + grouppath + '.pdf') as pdf: pdf.savefig(fig)