def generate_samples(graph, data, association, scale): '''A convenence function to call the c++ sampling routine for some data stored in my graph structure. Additionaly it converts nodal to edge properties because the sampling needs data associated with edges, but it can still interpolate between data for the endpoints for each vessel. Returns an array of the sampled data. data = the name of the data, or the data itself, association = 'edges' or 'nodes' or 'avg_edges' scale = length which each sample represents approximately. Larger value = less samples. ''' DATA_LINEAR = krebsutils.VesselSamplingFlags.DATA_LINEAR DATA_CONST = krebsutils.VesselSamplingFlags.DATA_CONST DATA_PER_NODE = krebsutils.VesselSamplingFlags.DATA_PER_NODE if isinstance(data, str): data = getattr(graph, association)[data] if association == 'edges': #data = krebsutils.edge_to_node_property(int(np.amax(graph.edgelist)+1), graph.edgelist, graph.edges[name], 'avg') return krebsutils.sample_edges(graph.nodes['position'], graph.edgelist, data, scale, DATA_CONST) else: return krebsutils.sample_edges(graph.nodes['position'], graph.edgelist, data, scale, DATA_LINEAR | DATA_PER_NODE)
def generate_samples(graph, name, association, scale): DATA_LINEAR = krebsutils.VesselSamplingFlags.DATA_LINEAR DATA_CONST = krebsutils.VesselSamplingFlags.DATA_CONST DATA_PER_NODE = krebsutils.VesselSamplingFlags.DATA_PER_NODE if not len(graph.edgelist): return np.asarray([], dtype=float) if association == 'edges': data = krebsutils.edge_to_node_property( int(np.amax(graph.edgelist) + 1), graph.edgelist, graph.edges[name], 'avg') else: data = graph.nodes[name] # return data return krebsutils.sample_edges(graph.nodes['position'], graph.edgelist, data, scale, DATA_LINEAR | DATA_PER_NODE)
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 = [ 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]))
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)