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
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
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