Exemple #1
0
    def obtain_data(self, dataman, dataname, *args):
      if dataname == 'basic_vessel_radial':
        property_name, vesselgroup, tumorgroup, sample_length, bins_spec, distance_distribution_name, ld, cachelocation = args

        def write(gmeasure, groupname):
          if property_name == 'mvd':
            smpl = dataman.obtain_data('basic_vessel_samples', 'flags', vesselgroup, sample_length)
            smpl  = np.ones_like(smpl) # only need number of samples
            avg_mode = radialAvgPerVolume
          elif property_name == 'phi_vessels':
            smpl = dataman.obtain_data('basic_vessel_samples', 'radius', vesselgroup, sample_length)
            smpl = math.pi*np.power(smpl, 2.) # pi r^2 * length, where length comes in through GenerateRadialDistributions
            avg_mode = radialAvgPerVolume
          elif property_name in ['S_over_V', 'S_rho']: # alternative names for now ...
            smpl = dataman.obtain_data('basic_vessel_samples', 'radius', vesselgroup, sample_length)
            smpl = (math.pi*2.)*smpl # 2 pi r = surface area
            avg_mode = radialAvgPerVolume
          elif property_name == 'radial_distance':
            smpl, _, _ , _  = dataman.obtain_data('distancemap_samples', vesselgroup, tumorgroup, sample_length, distance_distribution_name, ld)
            avg_mode = radialAvgPerVessels
          else:
            smpl = dataman.obtain_data('basic_vessel_samples', property_name, vesselgroup, sample_length)
            avg_mode = radialAvgPerVessels
          a, = GenerateRadialDistributions(dataman,  vesselgroup, tumorgroup, sample_length, bins_spec, distance_distribution_name, ld, [(smpl, avg_mode)])
          ds = a.write(gmeasure, groupname)
          ds.attrs['BINS_SPEC'] = str(bins_spec)

        def read(gmeasure, groupname):
          assert groupname == property_name
          return FixUnits_(groupname, myutils.MeanValueArray.read(gmeasure, groupname))

        version = myutils.checksum(MakeVersionId(vesselgroup), MakeVersionId(tumorgroup))
        return HdfCacheRadialDistribution((read, write), property_name, bins_spec, distance_distribution_name, cachelocation, version)
Exemple #2
0
 def makeCacheLocation(self, dataname, args, name, ver=1):
     vesselgroup, tumorgroup, fieldLd, fieldLdFine = args
     myfile, mycachelocation = self.cachelocationFactory(
         dataname, vesselgroup, tumorgroup,
         myutils.checksum(fieldLd.box, fieldLd.scale,
                          self.fine_bin_subdivision))
     version = (None, ) * len(mycachelocation) + (ver, )
     mycachelocation = mycachelocation + (name, )
     return myfile, mycachelocation, version
def obtain_average_radial_distance(dataman, fmeasure, groups, bins_spec,
                                   distancemap_spec):
    l = []
    for group in groups:
        cachelocation = (fmeasure, '%s_PARAMID%s_FILEID%s' %
                         (group.name, str(hash(bins_spec)),
                          myutils.checksum(group.file.filename)))
        data = dataman.obtain_data('basic_vessel_radial', 'radial_distance',
                                   group['vessels'], group['tumor'], 30.,
                                   bins_spec, distancemap_spec, None,
                                   cachelocation)
        l.append(data)
    return myutils.MeanValueArray.fromSummation(x.avg for x in l)
def create_nice_file_containing_all_the_data():
    filenames = sys.argv[1:]
    files = [h5files.open(fn, 'r') for fn in filenames]
    fmeasure = h5files.open('analyzeVesselTumor.h5', 'a')
    dataman = myutils.DataManager(10, [DataVesselLength(), DataVesselTumor()])

    allgroups = defaultdict(list)
    for f in files:
        keys = filter(lambda k: k.startswith('out'), f.keys())
        for k in keys:
            allgroups[k].append(f[k])
    allgroups = [(k, allgroups[k]) for k in sorted(allgroups.keys())]
    groupslist = [allgroups[-1]]
    outfn = 'Length-%s.pdf' % splitext(basename(filenames[0]))[0]
    pprint(groupslist)
    print '-> %s' % (outfn)

    for key, groups in groupslist:
        for group in groups:
            cachelocation = (fmeasure, '%s_file_%s_FILEID%s' %
                             (group.name, group.file.filename,
                              myutils.checksum(group.file.filename)))
            data = dataman.obtain_data('length_dist', 'length_dist',
                                       group['vessels'], group['tumor'],
                                       cachelocation)
            data = dataman.obtain_data('within_fake_tumor',
                                       'radii_within_tumor', group['vessels'],
                                       group['tumor'], cachelocation)
            data = dataman.obtain_data('within_fake_tumor',
                                       'pressures_within_tumor',
                                       group['vessels'], group['tumor'],
                                       cachelocation)
            data = dataman.obtain_data('within_fake_tumor',
                                       'flows_within_tumor', group['vessels'],
                                       group['tumor'], cachelocation)
    print('you just created a big nice file')
Exemple #5
0
  orig = ld.GetOriginPosition()
  orig += offset
  ld.SetOriginPosition(orig)
  parent = g.parent
  name   = str(g.name)
  del g
  del parent[name]
  #krebsutils.write_lattice_data_to_hdf(parent, name, ld)
  fn=str(parent.file.filename)
  #path=str(vesselgroup.name)
  krebsutils.write_lattice_data_to_hdf_by_filename(fn, name, ld)
  return offset


def GenerateSingleCapillaryWPo2(dataman, f, config_name, config_version, (bfparams, po2params, params)):
  version = myutils.checksum(1,config_version, params, bfparams, po2params)

  def write(gmeasure, name):
    gmeasure = gmeasure.create_group(name)
    # --- create single capillary vessel system ---
    p = params['pgrad']*params['size'][0]*params['scale']
    krebsutils.vesselgen_generate_single(gmeasure, params['size'], params['direction_mode'], params['scale'], params['ld_type'], params['r'], 0.1, p, params['size'][0]/10, bfparams)
    vesselgroup = gmeasure['vessels']
    if params['direction_mode'] == 0:
      CenterLattice(vesselgroup['lattice'])
    vesselgroup.attrs['VERSION'] = version
    # --- setup po2 stuff ---
    po2group = gmeasure.create_group('po2')
    po2group.attrs['SOURCE_FILE'] = f.filename
    po2group.attrs['SOURCE_PATH'] = str(vesselgroup.name)
    po2group['SOURCE']            = h5py.SoftLink(vesselgroup)
Exemple #6
0
 def cachelocation(g):
   path = posixpath.join('FileCS_'+myutils.checksum(basename(g.file.filename)), g.name.strip(posixpath.sep))
   return (f_measure, path)
Exemple #7
0
 def cachelocationEnsemble(dataname, groups):
     groupchecksum = myutils.checksum(*map(
         lambda g: str(g.file.filename + '/' + g.name), sum(groups, [])))
     path = ('%s_%s' % (dataname, groupchecksum), )
     return (f_measure, path)
Exemple #8
0
    def obtain_data(self, dataman, dataname, *args):
        if dataname == 'detailedPO2Parameters':
            po2group, = args
            return detailedo2.readParameters(po2group)

        if dataname == 'detailedPO2':
            po2group, = args
            a = np.asarray(po2group['po2vessels'])
            if a.shape[0] <> 2: a = np.transpose(a)
            po2field = po2group['po2field']
            ld = krebsutils.read_lattice_data_from_hdf(po2group['field_ld'])
            parameters = dataman.obtain_data('detailedPO2Parameters', po2group)
            return a, ld, po2field, parameters

        if dataname == 'detailedPO2_consumption':
            po2group, tumorgroup = args
            return detailedo2.computeO2Uptake(po2group, tumorgroup)

        if dataname == 'detailedPO2_samples' or dataname == 'detailedPO2_total_fluxes':
            prop, po2group, sample_length, every, cachelocation = args

            def read(gmeasure, name):
                gmeasure = gmeasure[name]
                if dataname == 'detailedPO2_samples':
                    if prop == 'gtv':
                        parameters = dataman.obtain_data(
                            'detailedPO2Parameters', po2group)
                        #po2 = dataman.obtain_data('detailedPO2_samples','po2', *args[1:])
                        #extpo2 = dataman.obtain_data('detailedPO2_samples','extpo2', *args[1:])
                        #return (po2-extpo2)/(parameters['grid_lattice_const']*parameters.get('transvascular_ring_size',0.5))
                        gvessels, gtumor = detailedo2.OpenVesselAndTumorGroups(
                            po2group)
                        jtv = dataman.obtain_data('detailedPO2_samples', 'jtv',
                                                  *args[1:])
                        #radius = dataman.obtain_data('basic_vessel_samples', 'radius', gvessels, sample_length)
                        #radius = radius * (60.e-4*math.pi*2.* parameters['kD_tissue']*parameters['alpha_t'])
                        radius = (
                            60.e-4 *
                            parameters.get('D_tissue',
                                           parameters.get('kD_tissue', None)) *
                            parameters.get('solubility_tissue',
                                           parameters.get('alpha_t', None)))
                        gtv = jtv / radius
                        return gtv
                    elif prop == 'sat':
                        po2 = dataman.obtain_data('detailedPO2_samples', 'po2',
                                                  *args[1:])
                        parameters = dataman.obtain_data(
                            'detailedPO2Parameters', po2group)
                        return detailedo2.PO2ToSaturation(po2, parameters)
                    else:
                        ds = gmeasure['smpl_' + prop]
                        ds = ds[...] if every is None else ds[::every]
                        return ds
                else:
                    keys = filter(lambda k: k.startswith('flux_'),
                                  gmeasure.keys())
                    fluxes = dict(map(lambda k: (k[5:], gmeasure[k][()]),
                                      keys))
                    fluxes['e1'] = abs(
                        100. * (fluxes['Jin_root'] - fluxes['Jout_root'] -
                                fluxes['Jout_tv']) / fluxes['Jin_root'])
                    fluxes['e2'] = abs(
                        100. *
                        (fluxes['Jin_root'] - fluxes['Jout_root'] -
                         fluxes['Jout_cons'] - fluxes.get('tv_cons', 0.)) /
                        fluxes['Jin_root'])
                    fluxes['e3'] = abs(
                        100. * (fluxes['Jout_tv'] - fluxes['Jout_cons'] -
                                fluxes.get('tv_cons', 0.)) / fluxes['Jout_tv'])
                    return fluxes

            def write(gmeasure, name):
                # do the sampling
                gvessels, gtumor = detailedo2.OpenVesselAndTumorGroups(
                    po2group)
                smpl, fluxes = detailedo2.sampleVessels(
                    po2group, gvessels, gtumor, sample_length)
                #cons = dataman.detailedPO2_consumption(po2group, gtumor)
                cons = dataman.obtain_data('detailedPO2_consumption', po2group,
                                           gtumor)
                # get the raw data, just for the consumption flux
                po2vessels, ld, po2field, parameters = dataman.obtain_data(
                    'detailedPO2', po2group)
                avgCons = np.asarray(myutils.largeDatasetAverage(cons),
                                     dtype=np.float64)
                fluxes['Jout_cons'] = avgCons * np.product(
                    cons.shape) * (ld.scale * 1.e-4)**3
                del po2vessels, po2field, ld

                gmeasure = gmeasure.create_group(name)
                for k, v in smpl.iteritems():
                    gmeasure.create_dataset('smpl_' + k,
                                            data=v,
                                            compression=9,
                                            dtype=np.float32)
                for k, v in fluxes.iteritems():
                    gmeasure.create_dataset('flux_' + k,
                                            data=v)  # scalar dataset

            version_id = myutils.checksum(sample_length, 3, getuuid_(po2group))
            ret = myutils.hdf_data_caching(
                read, write, cachelocation[0],
                (cachelocation[1], 'samples_and_fluxes'), (None, version_id))
            return ret

        if dataname == 'detailedPO2_global':
            prop, po2group, sample_length, cachelocation = args
            samplelocation = MakeSampleLocation(po2group)

            def write(gmeasure, measurename):
                assert prop == measurename
                gvessels, gtumor = detailedo2.OpenVesselAndTumorGroups(
                    po2group)
                if prop in ['po2', 'sat', 'gtv', 'jtv']:
                    w = dataman.obtain_data('basic_vessel_samples', 'weight',
                                            gvessels, sample_length)
                    d = dataman.obtain_data('detailedPO2_samples', prop,
                                            po2group, sample_length, 1,
                                            samplelocation)
                    gmeasure.create_dataset(prop,
                                            data=myutils.WeightedAverageStd(
                                                d, weights=w))
                elif prop in ['sat_vein', 'sat_capi', 'sat_art']:
                    w = dataman.obtain_data('basic_vessel_samples', 'weight',
                                            gvessels, sample_length)
                    d = dataman.obtain_data('detailedPO2_samples', 'sat',
                                            po2group, sample_length, 1,
                                            samplelocation)
                    f = dataman.obtain_data('basic_vessel_samples', 'flags',
                                            gvessels, sample_length)
                    mask = ~myutils.bbitwise_and(
                        f, krebsutils.WITHIN_TUMOR) & myutils.bbitwise_and(
                            f, krebsutils.CIRCULATED)
                    m = {
                        'sat_vein': krebsutils.VEIN,
                        'sat_capi': krebsutils.CAPILLARY,
                        'sat_art': krebsutils.ARTERY
                    }
                    mask &= myutils.bbitwise_and(f, m[prop])
                    d, w = d[mask], w[mask]
                    gmeasure.create_dataset(prop,
                                            data=myutils.WeightedAverageStd(
                                                d, weights=w))
                elif prop in [
                        'e1', 'e2', 'e3', 'Jin_root', 'Jout_root', 'Jout_tv',
                        'tv_cons', 'Jout_cons'
                ]:
                    d = dataman.obtain_data('detailedPO2_total_fluxes', prop,
                                            po2group, sample_length, 1,
                                            samplelocation)
                    gmeasure.create_dataset(prop, data=[d[prop], 0])
                elif prop == 'po2_tissue':
                    _, po2ld, po2field, parameters = dataman.obtain_data(
                        'detailedPO2', po2group)
                    d = myutils.largeDatasetAverageAndStd(po2field)
                    gmeasure.create_dataset(prop, data=d)
                elif prop == 'mro2':
                    uptakefield = detailedo2.computeO2Uptake(po2group, gtumor)
                    d = myutils.largeDatasetAverageAndStd(uptakefield)
                    gmeasure.create_dataset(prop, data=d)
                elif prop in ('sat_via_hb_ratio', 'vfhb_oxy', 'vfhb_deoxy',
                              'vfhb'):
                    weight = dataman.obtain_data('basic_vessel_samples',
                                                 'weight', gvessels,
                                                 sample_length)
                    flags = dataman.obtain_data('basic_vessel_samples',
                                                'flags', gvessels,
                                                sample_length)
                    mask = myutils.bbitwise_and(flags, krebsutils.CIRCULATED)
                    hema = dataman.obtain_data('basic_vessel_samples',
                                               'hematocrit', gvessels,
                                               sample_length)[mask]
                    sat = dataman.obtain_data('detailedPO2_samples', 'sat',
                                              po2group, sample_length, None,
                                              samplelocation)[mask]
                    rad = dataman.obtain_data('basic_vessel_samples', 'radius',
                                              gvessels, sample_length)[mask]
                    weight = weight[mask]
                    hbvolume = weight * rad * rad * math.pi * hema
                    ld = krebsutils.read_lattice_data_from_hdf(
                        po2group['field_ld'])
                    volume = np.product(ld.GetWorldSize())
                    if prop == 'sat_via_hb_ratio':
                        result = np.sum(hbvolume * sat) / np.sum(hbvolume)
                    elif prop == 'vfhb_oxy':
                        result = np.sum(hbvolume * sat) / volume
                    elif prop == 'vfhb_deoxy':
                        result = np.sum(hbvolume * (1. - sat)) / volume
                    elif prop == 'vfhb':
                        result = np.sum(hbvolume) / volume
                    gmeasure.create_dataset(prop, data=[result, 0.])
                elif prop in ('chb_oxy', 'chb_deoxy', 'chb'):
                    m = {
                        'chb_oxy': 'vfhb_oxy',
                        'chb_deoxy': 'vfhb_deoxy',
                        'chb': 'vfhb'
                    }
                    result = dataman.obtain_data('detailedPO2_global', m[prop],
                                                 po2group, sample_length,
                                                 cachelocation)
                    result = result * detailedo2.chb_of_rbcs
                    gmeasure.create_dataset(prop, data=[result, 0.])
                elif prop == 'mro2_by_j':
                    fluxes = dataman.obtain_data('detailedPO2_total_fluxes',
                                                 prop, po2group, sample_length,
                                                 1)
                    ld = krebsutils.read_lattice_data_from_hdf(
                        po2group['field_ld'])
                    worldbb = ld.worldBox
                    result = fluxes['Jout_tv'] / np.prod(worldbb[1] -
                                                         worldbb[0]) * 1.e12
                    gmeasure.create_dataset(prop, data=[result, 0.])
                elif prop == 'oef':
                    fluxes = dataman.obtain_data('detailedPO2_total_fluxes',
                                                 prop, po2group, sample_length,
                                                 1, samplelocation)
                    result = (fluxes['Jin_root'] -
                              fluxes['Jout_root']) / fluxes['Jin_root']
                    gmeasure.create_dataset(prop, data=[result, 0.])

                else:
                    assert False

            def read(gmeasure, measurename):
                d = np.asarray(gmeasure[measurename])
                if measurename in ('chb_oxy', 'chb', 'chb_deoxy'):
                    d *= 1.e6
                return d[0]  # its a tuple (avg, std), we want avg now.

            version_num = collections.defaultdict(lambda: 3)
            version_id = myutils.checksum(sample_length, version_num[prop],
                                          getuuid_(po2group))
            #version_id = myutils.checksum(sample_length, (2 if prop in  else 1))
            return myutils.hdf_data_caching(read, write, cachelocation[0],
                                            ('global', cachelocation[1], prop),
                                            (1, 1, version_id))

        if dataname == 'detailedPO2_radial':
            po2group, sample_length, bins_spec, distance_distribution_name, cachelocation = args
            samplelocation = MakeSampleLocation(po2group)

            # we assume that there is a tumor. without this measurement makes little sense

            def read(gmeasure, name):
                d = dict(gmeasure[name].items())
                d = dict(
                    (k, myutils.MeanValueArray.read(v)) for k, v in d.items())
                hbo = d['vfhb_oxy']
                hbd = d['vfhb_deoxy']
                hb = myutils.MeanValueArray(hbo.cnt, hbo.sum + hbd.sum,
                                            hbo.sqr + hbd.sqr)
                d['vfhb'] = hb
                sat = hbo.avg / hb.avg
                d['sat_via_hb_ratio'] = myutils.MeanValueArray(
                    np.ones_like(hb.cnt), sat, sat * sat)
                d['chb_oxy'] = d['vfhb_oxy'] * detailedo2.chb_of_rbcs * 1.e6
                d['chb_deoxy'] = d['vfhb_deoxy'] * detailedo2.chb_of_rbcs * 1.e6
                d['chb'] = d['vfhb'] * detailedo2.chb_of_rbcs * 1.e6
                return d

            def write(gmeasure, name):
                gvessels, gtumor = detailedo2.OpenVesselAndTumorGroups(
                    po2group)
                weight_smpl = dataman.obtain_data('basic_vessel_samples',
                                                  'weight', gvessels,
                                                  sample_length)
                flags = dataman.obtain_data('basic_vessel_samples', 'flags',
                                            gvessels, sample_length)
                # get teh radial distance function (either distance from tumor border or distance from center)
                dist_smpl, distmap, mask, tumor_ld = dataman.obtain_data(
                    'distancemap_samples', gvessels, gtumor, sample_length,
                    distance_distribution_name, None)
                # tumor_ld might actually be a unrelated lattice

                #filter uncirculated
                mask = mask & myutils.bbitwise_and(flags,
                                                   krebsutils.CIRCULATED)
                dist_smpl = dist_smpl[mask]
                weight_smpl = weight_smpl[mask]

                bins = bins_spec.arange()
                gmeasure = gmeasure.create_group(name)

                for name in ['po2', 'extpo2', 'jtv', 'sat', 'gtv', 'dS_dx']:
                    smpl = dataman.obtain_data('detailedPO2_samples', name,
                                               po2group, sample_length, None,
                                               samplelocation)
                    myutils.MeanValueArray.fromHistogram1d(
                        bins, dist_smpl, smpl[mask],
                        w=weight_smpl).write(gmeasure, name)
                del smpl

                _, po2ld, po2field, parameters = dataman.obtain_data(
                    'detailedPO2', po2group)
                po2field = krebsutils.resample_field(np.asarray(po2field),
                                                     po2ld.worldBox,
                                                     tumor_ld.shape,
                                                     tumor_ld.worldBox,
                                                     order=1,
                                                     mode='nearest')
                myutils.MeanValueArray.fromHistogram1d(bins, distmap.ravel(),
                                                       po2field.ravel()).write(
                                                           gmeasure,
                                                           'po2_tissue')
                del po2field

                uptakefield = detailedo2.computeO2Uptake(po2group, gtumor)
                uptakefield = krebsutils.resample_field(uptakefield,
                                                        po2ld.worldBox,
                                                        tumor_ld.shape,
                                                        tumor_ld.worldBox,
                                                        order=1,
                                                        mode='nearest')
                myutils.MeanValueArray.fromHistogram1d(
                    bins, distmap.ravel(),
                    uptakefield.ravel()).write(gmeasure, 'mro2')
                del uptakefield

                hema = dataman.obtain_data('basic_vessel_samples',
                                           'hematocrit', gvessels,
                                           sample_length)[mask]
                sat = dataman.obtain_data('detailedPO2_samples', 'sat',
                                          po2group, sample_length, None,
                                          samplelocation)[mask]
                rad = dataman.obtain_data('basic_vessel_samples', 'radius',
                                          gvessels, sample_length)[mask]
                hbvolume = weight_smpl * rad * rad * math.pi * hema
                vol_per_bin = myutils.MeanValueArray.fromHistogram1d(
                    bins, distmap.ravel(), np.ones_like(
                        distmap.ravel())).cnt * (tumor_ld.scale**3)
                tmp = myutils.MeanValueArray.fromHistogram1d(
                    bins, dist_smpl, hbvolume * sat)
                tmp.cnt = vol_per_bin.copy()
                tmp.write(gmeasure, 'vfhb_oxy')
                tmp = myutils.MeanValueArray.fromHistogram1d(
                    bins, dist_smpl, hbvolume * (1. - sat))
                tmp.cnt = vol_per_bin.copy()
                tmp.write(gmeasure, 'vfhb_deoxy')
                del tmp, hbvolume, vol_per_bin

            version = getuuid_(po2group)
            ret = analyzeGeneral.HdfCacheRadialDistribution(
                (read, write), 'po2', bins_spec, distance_distribution_name,
                cachelocation, version)
            return ret
        assert False
Exemple #9
0
    def obtain_data(self, dataman, dataname, *args):
        if dataname == 'detailedPO2_peff_samples':
            po2group, sample_length, every, cachelocation = args
            gvessels, gtumor = detailedo2.OpenVesselAndTumorGroups(po2group)

            def read(gmeasure, name):
                ds = gmeasure[name]
                return ds[...] if every is None else ds[::every]

            def write(gmeasure, name):
                samplelocation = MakeSampleLocation(po2group)
                parameters = dataman.obtain_data('detailedPO2Parameters',
                                                 po2group)
                rad = dataman.obtain_data('basic_vessel_samples', 'radius',
                                          gvessels, sample_length)
                hema = dataman.obtain_data('basic_vessel_samples',
                                           'hematocrit', gvessels,
                                           sample_length)
                po2 = dataman.obtain_data('detailedPO2_samples', 'po2',
                                          po2group, sample_length, 1,
                                          samplelocation)
                mtc = detailedo2.computeMassTransferCoefficient(
                    rad, parameters)
                blood_solubility = parameters.get(
                    'solubility_plasma', parameters.get('alpha_p', None))
                c_o2_total = detailedo2.PO2ToConcentration(
                    po2, hema, parameters)
                c_o2_plasma = detailedo2.PO2ToConcentration(
                    po2, np.zeros_like(hema), parameters
                )  # to get the o2 conc. in plasma i just set the amount of RBCs to zero
                c_o2_plasma *= (
                    1.0 - hema
                )  # and reduce the concentration according to the volume fraction of plasma
                beta_factor = c_o2_plasma / c_o2_total
                peff = (1.0 / blood_solubility) * mtc * beta_factor

                #        print 'po2', np.average(po2)
                #        print 'mtc', np.average(mtc)
                #        print 'hema', np.average(hema)
                #        print 'blood_solubility', blood_solubility
                #        print 'c_o2_total', np.average(c_o2_total)
                #        print 'c_o2_plasma', np.average(c_o2_plasma)
                #        print 'peff', np.average(peff)
                #        print 'beta_factor', np.average(beta_factor)

                gmeasure.create_dataset(name, data=peff, compression=9)

            version_id = myutils.checksum(sample_length, 1, getuuid_(po2group))
            ret = myutils.hdf_data_caching(
                read, write, cachelocation[0],
                (cachelocation[1], 'samples_and_fluxes', 'Peff'),
                (None, None, version_id))
            return ret

        elif dataname == 'detailedPO2_peff_radial':
            po2group, sample_length, bins_spec, distance_distribution_name, cachelocation = args

            # we assume that there is a tumor. without this measurement makes little sense

            def read(gmeasure, name):
                return myutils.MeanValueArray.read(gmeasure[name])

            def write(gmeasure, name):
                samplelocation = MakeSampleLocation(po2group)
                gvessels, gtumor = detailedo2.OpenVesselAndTumorGroups(
                    po2group)
                smpl = dataman.obtain_data('detailedPO2_peff_samples',
                                           po2group, sample_length, None,
                                           samplelocation)

                data, = analyzeGeneral.GenerateRadialDistributions(
                    dataman, gvessels, gtumor, sample_length, bins_spec,
                    distance_distribution_name, None,
                    [(smpl, analyzeGeneral.radialAvgPerVessels)])
                data.write(gmeasure, name)

            version = myutils.checksum(2, getuuid_(po2group))
            ret = analyzeGeneral.HdfCacheRadialDistribution(
                (read, write), 'Peff', bins_spec, distance_distribution_name,
                cachelocation, version)
            return ret

        # IDEA: store config stuff like sample length and distance distribution as member of the data handler for less call args
        # or store it in a common config instance.
        # Then allow to obtain a temporary handle on data which knows its GUID for a quick accurate check if data has changed.
        # The handle probably would have to obtain the GUID from disk but this is it.
        elif dataname == 'detailedPO2_peffSrho_radial':
            po2group, sample_length, bins_spec, distance_distribution_name, cachelocation = args

            def read(gmeasure, name):
                return myutils.MeanValueArray.read(gmeasure[name])

            def write(gmeasure, name):
                samplelocation = MakeSampleLocation(po2group)
                gvessels, gtumor = detailedo2.OpenVesselAndTumorGroups(
                    po2group)
                peff = dataman.obtain_data('detailedPO2_peff_samples',
                                           po2group, sample_length, None,
                                           samplelocation)
                rad = dataman.obtain_data('basic_vessel_samples', 'radius',
                                          gvessels, sample_length)
                peff = peff * math.pi * 2. * rad

                data, = analyzeGeneral.GenerateRadialDistributions(
                    dataman, gvessels, gtumor, sample_length, bins_spec,
                    distance_distribution_name, None,
                    [(peff, analyzeGeneral.radialAvgPerVolume)])
                data.write(gmeasure, name)

            version = myutils.checksum(2, getuuid_(
                po2group))  # this should actually depend on the samples
            ret = analyzeGeneral.HdfCacheRadialDistribution(
                (read, write), 'PeffSrho', bins_spec,
                distance_distribution_name, cachelocation, version)
            return ret

        assert False
Exemple #10
0
      b = myutils.MeanValueArray.fromHistogram1d(bins, distmap.ravel(), np.ones_like(distmap.ravel())) # how much tissue volume in the bins
      a.cnt = b.cnt.copy()
      a.sum *= 1./(tumor_ld.scale**3)
      a.sqr *= a.sum**2  # length integral per tissue volume
      #a *= 1.e6
    elif avg_mode is radialAvgPerVessels:
      a = myutils.MeanValueArray.fromHistogram1d(bins, dist_smpl, smpl[mask], weight_smpl)
    else:
      assert (avg_mode is radialAvgPerVolume or avg_mode is radialAvgPerVessels)
    res.append(a)
  return res


def HdfCacheRadialDistribution((read, write), property_name, bins_spec, distance_distribution_name, cachelocation, version):
  path1 = 'radial_vs_'+distance_distribution_name+'_bins'+str(hash(bins_spec))
  version_args = myutils.checksum(30., 13)
  return myutils.hdf_data_caching(read, write, cachelocation[0], (path1,cachelocation[1],property_name), (version_args,0,version))


#def GetMaskUncirculated(dataman, vesselgroup):
#  flags       = dataman.obtain_data('basic_vessel_samples', 'flags', vesselgroup, sample_length)
#  return myutils.bbitwise_and(flags, krebsutils.CIRCULATED)

def GetMaskArea(dataman, vesselgroup, tumorgroup, distance_distribution_name, ld, distmin, distmax, sample_length = 30.):
  dist_smpl, distmap, mask, _   = dataman.obtain_data('distancemap_samples', vesselgroup, tumorgroup, sample_length, distance_distribution_name, ld)
  mask2 = (dist_smpl>distmin) & (dist_smpl<distmax)
  return mask & mask2


def ComputeSampleHistogram(dataman, vesselgroup, binspec, samples, mask = None, sample_length = 30.):
  weight      = dataman.obtain_data('basic_vessel_samples', 'weight', vesselgroup, sample_length)