def testdistancemap(): c = np.asarray((25, 25, 25)) a = make_test_array(c) x = krebs.distancemap(a) print "range: %f %f" % (x.min(), x.max()) grad = krebs.field_gradient(x, spacing=1.) for i, g in enumerate(grad): print "grad range %i: %f %f" % (i, g.min(), g.max()) gnorm = np.sum(g * g for g in grad) for i in [c[2]]: #xrange(x.shape[2]): plt.subplot(221) p = plt.imshow(x[:, :, i], vmin=x.min(), vmax=6, interpolation='nearest') plt.colorbar(p) plt.subplot(222) p = plt.imshow(a[:, :, i], interpolation='nearest') plt.colorbar(p) plt.subplot(223) p = plt.imshow(grad[0][:, :, i], interpolation='nearest') plt.colorbar(p) plt.subplot(224) p = plt.imshow(gnorm[:, :, i], interpolation='nearest') plt.colorbar(p) plt.show()
def max_grad(self): if self.max_grad_ is not None: return self.max_grad_ import krebsutils dmin, dmax = self.value_bounds grad = krebsutils.field_gradient(self.data, spacing = 1.) g = np.sqrt(np.sum(tuple(g*g for g in grad), axis=0)) g = g.max() / (dmax-dmin + 1.e-24) # divide by data range because data range is mapped to 0, 1 g *= 1./self.spacing self.max_grad_ = g return g
def write(gmeasure, groupname): graph = dataman.obtain_data( 'vessel_graph', vesselgroup, ['position', 'radius', 'flags', 'pressure']) graph = graph.get_filtered( myutils.bbitwise_and(graph['flags'], krebsutils.CIRCULATED)) edgevalues = graph['pressure'] edgevalues = edgevalues[graph.edgelist] # main calculation thefield = krebsutils.CalcIntervascularInterpolationField( graph.edgelist, graph['radius'], graph['position'], edgevalues, fieldLdFine, 1.) del edgevalues # gradient of the interpolated blood pressure thegrad = scipy.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_, thefield # now we scale down the highres version, TODO: make it work with other than 3 dimensions # first local average m = self.fine_bin_subdivision kernel = np.ones((m, m, m), dtype=np.float32) thegrad = scipy.signal.fftconvolve(thegrad, kernel, mode='valid') # then pick every m'th which contains the average of m finer boxes combined thegrad = np.ascontiguousarray(thegrad[::m, ::m, ::m]) assert all(thegrad.shape == fieldLd.shape) gmeasure.create_dataset(groupname, data=thegrad, compression=9, dtype=np.float32)
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_ if SHOW_BLURRED_IMAGES: kernel = (1.0 / 64.0) * np.ones((4, 4, 4), dtype=np.float32) thefield = sig.fftconvolve(thefield, kernel) volfraction = sig.fftconvolve(volfraction, kernel) thegrad = sig.fftconvolve(thegrad, kernel) z = fieldld.shape[2] / 2 fig, axes = pyplot.subplots(2, 2) ax = axes[0, 0] plt = ax.imshow(thefield[:, :, z])
def generate_data(statedata, dstgroup): """ main routine that does all the measurement. Enable Individual parts. Computed date overwrites old data. """ print statedata.file.filename, statedata.name time = statedata.attrs['time'] tum_grp = statedata['tumor'] tum_ld = get_tumld(tum_grp) cellvol = tum_ld.scale**3 distmap = calc_distmap(tum_grp) radialmap = krebsutils.make_radial_field(tum_ld) axial_max_rad = np.max(np.abs(tum_ld.worldBox.ravel())) ptc = np.asarray(tum_grp['ptc']) if 1: ## shape data: rim surface area, volume vtkds, = extractVtkFields.Extractor(statedata['tumor'], ['ls']).asVtkDataSets() area = integrate_surface(vtkds) del vtkds vol = np.sum(ptc) * cellvol radius = np.average(radialmap[np.nonzero( np.logical_and(distmap > -2 * tum_ld.scale, distmap < 2 * tum_ld.scale))]) sphere_equiv_radius = math.pow(vol * 3. / (4. * math.pi), 1. / 3.) sphere_equiv_area = 4. * math.pi * (sphere_equiv_radius**2) sphericity = sphere_equiv_area / area #cylinder vol = pi r^2 h cylinder_equiv_radius = math.sqrt(vol / tum_ld.GetWorldSize()[2] / math.pi) cylinder_equiv_area = 2. * math.pi * cylinder_equiv_radius * tum_ld.GetWorldSize( )[2] cylindericity = cylinder_equiv_area / area d = dict(time=time, area=area, volume=vol, radius=radius, sphericity=sphericity, cylindericity=cylindericity, sphere_equiv_radius=sphere_equiv_radius, sphere_equiv_area=sphere_equiv_area, cylinder_equiv_radius=cylinder_equiv_radius, cylinder_equiv_area=cylinder_equiv_area) pprint(d) print 'geometry data for %s/%s' % (statedata.file.filename, statedata.name) g = dstgroup.recreate_group('geometry') myutils.hdf_write_dict_hierarchy(g, '.', d) if 1: print 'regenerating radial for %s/%s' % (statedata.file.filename, statedata.name) dstdata = dstgroup.recreate_group('radial') ## radial data; vessels vessels = krebsutils.read_vesselgraph(statedata['vessels'], [ 'position', 'flags', 'shearforce', 'radius', 'flow', 'maturation' ]) vessels = vessels.get_filtered( edge_indices=(vessels.edges['flags'] & krebsutils.CIRCULATED != 0)) sample_length = 50. far = 1.e10 s = plotVessels.generate_samples(vessels, 'position', 'nodes', sample_length) dist = krebsutils.sample_field(s, distmap, tum_ld, linear_interpolation=True, extrapolation_value=far) rad = krebsutils.sample_field(s, radialmap, tum_ld, linear_interpolation=True, extrapolation_value=far) del s dstdata.recreate_group('vs_r').create_dataset( 'bins', data=np.average((bins_rad[:-1], bins_rad[1:]), axis=0)) dstdata.recreate_group('vs_dr').create_dataset( 'bins', data=np.average((bins_dist[:-1], bins_dist[1:]), axis=0)) # dstdata['vs_r'] = dict(bins = np.average((bins_rad[:-1],bins_rad[1:]), axis=0)) # dstdata['vs_dr'] = dict(bins = np.average((bins_dist[:-1],bins_dist[1:]), axis=0)) # def add_histogram_data(dst, name, (s_avg, s_std, s_sqr)): # dst[name] = dict(avg = s_avg, std = s_std, sqr = s_sqr) for name in ['shearforce', 'radius', 'flow', 'maturation']: s = plotVessels.generate_samples(vessels, name, 'edges', sample_length) myutils.MeanValueArray.fromHistogram1d(bins_rad, rad, s).write( dstdata['vs_r'], name) myutils.MeanValueArray.fromHistogram1d(bins_dist, dist, s).write( dstdata['vs_dr'], name) # d = myutils.scatter_histogram(rad, s, bins_rad, 1.) # add_histogram_data(dstdata['vs_r'], name, d) # d = myutils.scatter_histogram(dist, s, bins_dist, 1.) # add_histogram_data(dstdata['vs_dr'], name, d) def make_mvd(flavor, s, bins, smap, mask_bound): a = myutils.MeanValueArray.fromHistogram1d(bins, s, np.ones_like(s)) b = myutils.MeanValueArray.fromHistogram1d( bins, smap.ravel(), np.ones_like(smap.ravel())) a.cnt = b.cnt.copy() a.sum *= sample_length / cellvol a.sqr *= a.sum**2 a.write(dstdata[flavor], 'mvd') # d = myutils.scatter_histogram(xdata, np.ones_like(xdata), bins=bins, xdata2 = np.ravel(xdata2)) # m = dstdata[flavor]['bins'] > mask_bound # for x in d[:2]: # x *= sample_length/cellvol # x.mask |= m # for x in d[2:]: # x *= (sample_length/cellvol)**2 # x.mask |= m # add_histogram_data(dstdata[flavor], 'mvd', d) make_mvd('vs_r', rad, bins_rad, radialmap, axial_max_rad) make_mvd('vs_dr', dist, bins_dist, distmap, 1000.) # pyplot.errorbar(dstdata['vs_dr']['bins'], dstdata['vs_dr']['mvd_avg'], yerr=dstdata['vs_dr']['mvd_std']) # pyplot.show() del rad, dist for name in ['conc', 'sources', 'ptc', 'press']: a = np.ravel(np.asarray(tum_grp[name])) myutils.MeanValueArray.fromHistogram1d( bins_rad, np.ravel(radialmap), a).write(dstdata['vs_r'], name) myutils.MeanValueArray.fromHistogram1d(bins_dist, np.ravel(distmap), a).write( dstdata['vs_dr'], name) # d = myutils.scatter_histogram(np.ravel(distmap), np.ravel(np.asarray(tum_grp[name])), bins_dist, 1.) # add_histogram_data(dstdata['vs_dr'], name, d) # d = myutils.scatter_histogram(np.ravel(radialmap), np.ravel(np.asarray(tum_grp[name])), bins_rad, 1.) # add_histogram_data(dstdata['vs_r'], name, d) # velocities, projected radially outward vel_field = tuple( np.asarray(tum_grp['vel'][:, :, :, i]) for i in range(3)) vnorm = np.zeros(distmap.shape, dtype=np.float32) dgrad = krebsutils.field_gradient(radialmap, spacing=tum_ld.scale) for vv, g in zip(vel_field, dgrad): vnorm += vv * g del vel_field, dgrad phi_tumor = ptc * np.asarray(tum_grp['conc']) oxy = np.asarray(statedata['fieldOxy']) gf = np.array(statedata['fieldGf']) for name, field in [('phi_tumor', phi_tumor), ('vel', vnorm), ('oxy', oxy), ('gf', gf)]: a = np.ravel(field) myutils.MeanValueArray.fromHistogram1d( bins_rad, np.ravel(radialmap), a).write(dstdata['vs_r'], name) myutils.MeanValueArray.fromHistogram1d(bins_dist, np.ravel(distmap), a).write( dstdata['vs_dr'], name)