def surface2Volume(vesselgroup):
  vessels = krebsutils.read_vesselgraph(vesselgroup, ['flags', 'radius', 'length'])
  flags   = RemoveArteriovenousFlagsFromCapillaries(vessels['flags'])
  flags = flags[:,0]
  mask1 = myutils.bbitwise_and(flags, krebsutils.CIRCULATED)
  totalvol = totalLdVolume(vesselgroup)
  def compute(flagMask):
    if flagMask:
      mask = mask1 & myutils.bbitwise_and(flags, flagMask)
    else:
      mask = mask1
    length = np.asarray(vessels['length'][mask], dtype = np.float64)
    radius = np.asarray(vessels['radius'][mask], dtype = np.float64)
    radius = radius[:,0]
    vol = math.pi * np.power(radius, 2.) * length
    surface = 2* math.pi* radius* length
    #surface = np.sum(surface)
    #vol = np.sum(vol)
    s2v = surface/vol
    mystd = np.std(s2v)
    myavg = np.average(s2v)
    myrel = mystd/myavg
    print("spread: %f" % myrel)
    return np.average(surface/vol)
  return compute(0), compute(krebsutils.ARTERY), compute(krebsutils.VEIN), compute(krebsutils.CAPILLARY)
def cylinderCollectionLineDensity(vesselgroup):
  vessels = krebsutils.read_vesselgraph(vesselgroup, ['flags', 'length'])
  flags   = RemoveArteriovenousFlagsFromCapillaries(vessels['flags'])
  flags = flags[:,0]
  mask1 = myutils.bbitwise_and(flags, krebsutils.CIRCULATED)
  totalvol = totalLdVolume(vesselgroup)
  def compute(flagMask):
    if flagMask:
      mask = mask1 & myutils.bbitwise_and(flags, flagMask)
    else:
      mask = mask1
    length = np.asarray(vessels['length'][mask], dtype = np.float64)
    total = np.sum(length)
    return total/totalvol
  return compute(0), compute(krebsutils.ARTERY), compute(krebsutils.VEIN), compute(krebsutils.CAPILLARY)
Beispiel #3
0
def calcBoxCounts(data, vesselgroup, dataId, opts):
    """
      boxcounting for various configurations
    """
    if dataId.startswith('tumor'):
        raise RuntimeError('not implemented')


#        ld = krebs.hdf.get_ld(file['lattice'])
#        ds = root['tumor/tc_density']
#        binimage, filter, scale = makeInnerTumorFilter(ds, innerOffset, 0.5)
#        data = boxcountImage(binimage,pixelsize=scale)
    else:
        graph = krebsutils.read_vesselgraph(vesselgroup,
                                            ['position', 'flags', 'radius'])
        flags = graph.edges['flags']
        rad = graph.edges['radius']
        # compute filter
        if dataId == 'arteries':
            mask = np.logical_and((flags & krebsutils.ARTERY) > 0, rad > 10.0)
        elif dataId == 'veins':
            mask = np.logical_and((flags & krebsutils.VEIN) > 0, rad > 10.0)
        elif dataId == 'arteriovenous':
            mask = np.logical_and(
                (flags & krebsutils.VEIN) | (flags & krebsutils.ARTERY),
                rad > 10.0)
        elif dataId == 'capillaries':
            mask = rad <= 6.
        elif dataId == 'complete':
            mask = None
        elif dataId == 'withintumor':
            mask = flags & krebsutils.WITHIN_TUMOR
        if mask is not None:
            subgraph = graph.get_filtered(edge_indices=np.nonzero(mask))
        else:
            subgraph = graph
        if len(subgraph.edgelist):  # if graph is not empty
            print 'computing %s' % dataId
            krebsutils.set_num_threads(opts.num_threads)
            #do boxcounting
            spacing = opts.spacing
            max_spacing = max(
                krebsutils.LatticeDataGetWorldSize(makeLd(graph, spacing, 0.)))
            bs, bc = calcVesselBoxCounts(subgraph, spacing, max_spacing)
            data[dataId] = dict(bs=bs, bc=bc)
Beispiel #4
0
if SHOW_BLURRED_IMAGES:
    import scipy.signal as sig
import scipy.ndimage as ndimage

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')
import krebsutils
import myutils
import mpl_utils

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', '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
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)
Beispiel #7
0
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)
Beispiel #8
0
 def write(gmeasure, groupname):
   ld = krebsutils.read_lattice_data_from_hdf(vesselgroup['lattice'])
   volume = np.prod(ld.GetWorldSize())
   if property_name == 'mvd_linedensity':
     graph = dataman.obtain_data('vessel_graph', vesselgroup, ['length'])
     l = np.asarray(graph['length'], dtype=np.float64)
     data  = (np.sum(l) / volume, 0.)
     data  = [d*1e6 for d in data] #from 1/mum^2 to 1/mm^2
   elif property_name in 'phi_vessels phi_a phi_v phi_c'.split():
     from analyzeBloodVolumeSimple import cylinderCollectionVolumeDensity
     phi_vessels, phi_a, phi_v, phi_c = cylinderCollectionVolumeDensity(vesselgroup)
     if property_name == 'phi_vessels':
       data = [phi_vessels, 0.]
     if property_name == 'phi_a':
       data = [phi_a, 0.]
     if property_name == 'phi_v':
       data = [phi_v, 0.]
     if property_name == 'phi_c':
       data = [phi_c, 0.]
   elif property_name in 'mvd mvd_a mvd_v mvd_c'.split():
     from analyzeBloodVolumeSimple import cylinderCollectionLineDensity
     mvd, mvd_a, mvd_v, mvd_c = cylinderCollectionLineDensity(vesselgroup)
     if property_name == 'mvd':
       data = [mvd, 0.]
     if property_name == 'mvd_a':
       data = [mvd_a, 0.]
     if property_name == 'mvd_v':
       data = [mvd_v, 0.]
     if property_name == 'mvd_c':
       data = [mvd_c, 0.]
   elif property_name == 'mvd_sphere_sampling':
     ld = krebsutils.read_lattice_data_from_hdf(vesselgroup.parent['field_ld'])
     mvd_sampling_results, mvd_bins = dataman.obtain_data('sphere_vessel_density',  vesselgroup, None, suggest_bins_from_world(ld), 'radial', ld, cachelocation )
       #print(mvd_sampling_results)    
     data = [ np.mean(np.asarray(mvd_sampling_results)*1e6),
             np.std(np.asarray(mvd_sampling_results)*1e6)]
   elif property_name == 'avg_cap_dist':
     vessels = krebsutils.read_vesselgraph(vesselgroup, ['flags', 'length','radius'])
     flags   = RemoveArteriovenousFlagsFromCapillaries(vessels['flags'])
     mask1 = myutils.bbitwise_and(flags, krebsutils.CIRCULATED)
     #totalvol = totalLdVolume(vesselgroup)          
     def compute(flagMask):
       if flagMask:
         mask = mask1 & myutils.bbitwise_and(flags, flagMask)
       else:
         mask = mask1
       length = np.asarray(vessels['length'][mask], dtype = np.float64)
       total = np.sum(length)
       return total/volume
     '''luedemann et. al. assume capillaries are vessels
     smaller than this:
       see manuscript draft
     '''
     mvd_cap = compute((vessels['radius']<=4.0).all())
     data = (np.sqrt(1/mvd_cap), 0.)
   elif property_name == 'phi_vessels':
     graph = dataman.obtain_data('vessel_graph', vesselgroup, ['length','radius'])
     l = np.asarray(graph['length'], dtype=np.float64)
     r = np.asarray(graph['radius'], dtype=np.float64)
     data = l*np.square(r)*math.pi
     data = (np.sum(data) / volume, 0.)
   elif property_name == 'total_perfusion':
     data = getTotalPerfusion([vesselgroup])*60 #to minutes
   else:
     data   = dataman.obtain_data('basic_vessel_samples', property_name, vesselgroup, 30.)
     weight = dataman.obtain_data('basic_vessel_samples', 'weight', vesselgroup, 30.)
     data = myutils.WeightedAverageStd(data, weights=weight)
   gmeasure.create_dataset(groupname, data = data)