Example #1
0
def renderScene(drug_grp, imagefn, options): 
  imagefn, ext = splitext(imagefn)
  ext = '.' + options.format
  
  options.imageFileName = imagefn+'_iff_drug'+ext
  
  max_conc = getMaxConcentration(drug_grp.file)    
  dataman = myutils.DataManager(2, [DataBasicVessel()])
  
  timepoint = drug_grp.attrs['time'] #comes in seconds
  timepoint = timepoint/3600.
  #gvessels = drug_grp.parent['iff/vessels']
  gvessels = drug_grp.parent['vessels']
  iff_pressure_field = drug_grp.parent['iff/iff_pressure']
  drug_conc_field = drug_grp['conc']
  cell_drug_conc_field = drug_grp['conc_cell']
  ex_drug_conc_field = drug_grp['conc_ex']
  
  #ex_drug_auc_field = drug_grp.parent['measurements/drug_local_integral']['auc_ex']
  #in_drug_auc_field = drug_grp.parent['measurements/drug_local_integral']['auc_in']
  
  #iff_ld  = krebsutils.read_lattice_data_from_hdf_by_filename(drug_grp.parent['field_ld'])
  iff_ld  = krebsutils.read_lattice_data_from_hdf_by_filename(str(drug_grp.file.filename),str(drug_grp.parent.name)+'field_ld')
  
  #ld = iffgroup['lattice']

  #po2vessels, po2field_ld, po2field, parameters = dataman('detailedPO2', po2group)
  #po2vessels = np.average(po2vessels, axis=0)
  #print 'po2vessels:', po2vessels.min(), po2vessels.max()
  print 'ifpfield:', np.amin(iff_pressure_field), np.amax(iff_pressure_field)
  print 'drug_conc_field:', np.amin(drug_conc_field), np.amax(drug_conc_field)

  #vessel_ld = krebsutils.read_lattice_data_from_hdf(gvessels['lattice'])
  vessel_ld = krebsutils.read_lattice_data_from_hdf_by_filename(str(gvessels.file.filename),str(gvessels.name)+'/lattice')
  vessel_graph = dataman('vessel_graph', gvessels, ['position', 'flags', 'radius'])  
    
  #vessel_graph.edges['po2vessels'] = po2vessels
  #vessel_graph.edges['saturation'] = PO2ToSaturation(po2vessels, parameters)
  #vessel_graph.edges['hboconc'] = vessel_graph.edges['saturation']*vessel_graph.edges['hematocrit']*chb_of_rbcs*1.0e3
  vessel_graph = vessel_graph.get_filtered(edge_indices = myutils.bbitwise_and(vessel_graph['flags'], krebsutils.CIRCULATED))
  if options.filterradiuslowpass>0.0:
    print("lowpass filter activated:")
    vessel_graph = vessel_graph.get_filtered(edge_indices = vessel_graph['radius']< kwargs['filterradiuslowpass'])
  imagefn, ext = splitext(imagefn)
  ext = '.' + options.format
  
  if 1:
    if timepoint==0:
      renderSliceWithDistribution((vessel_ld, vessel_graph, 'iff_pressure'), (iff_ld, iff_pressure_field), (imagefn+'_iff_pressure_t%0.1fh'%timepoint )+ext, 'IF pressure t=%.1f h'%timepoint, options, max_conc=max_conc)
    renderSliceWithDistribution((vessel_ld, vessel_graph, 'drug_conc'), (iff_ld, drug_conc_field), (imagefn+'_iff_drug_t%0.1fh'%timepoint )+ext, '%s t=%.1f h'%(drug_grp.file.attrs.get('MESSAGE'),timepoint), options, max_conc=max_conc)
    #renderSliceWithDistribution((vessel_ld, vessel_graph, 'drug_conc'), (iff_ld, cell_drug_conc_field), (imagefn+'_iff_drug_incell_t%0.1fh'%timepoint)+ext, 'Tr. intr. t=%.1f h'%timepoint, kwargs)
    #renderSliceWithDistribution((vessel_ld, vessel_graph, 'drug_conc'), (iff_ld, ex_drug_conc_field), (imagefn+'_iff_drug_excell_t%0.1fh'%timepoint)+ext, 'Tr. extr. t=%.1f h'%timepoint, kwargs)
    if options.plot_auc:
      renderSliceWithDistribution((vessel_ld, vessel_graph, 'auc'), (iff_ld, ex_drug_auc_field), imagefn+'_iff_ex_drug_auc'+ext, 'Tr. extr. t=%.1f h'%timepoint, options, max_conc=max_conc)
    #renderSliceWithDistribution((vessel_ld, vessel_graph, 'auc'), (iff_ld, in_drug_auc_field), imagefn+'_iff_in_drug_auc'+ext, 'Tr. intr. t=%.1f h'%timepoint, kwargs)
  if 0:
    renderSlice((vessel_ld, vessel_graph, 'radius'), (iff_ld, iff_pressure_field), imagefn+'_iff_pressure'+ext, '', options)
    renderSlice((vessel_ld, vessel_graph, 'radius'), (iff_ld, drug_conc_field), imagefn+'_iff_drug'+ext, '', options)
    renderSlice((vessel_ld, vessel_graph, 'radius'), (iff_ld, cell_drug_conc_field), imagefn+'_iff_drug_incell'+ext, '', options)
    renderSlice((vessel_ld, vessel_graph, 'radius'), (iff_ld, ex_drug_conc_field), imagefn+'_iff_drug_excell'+ext, '', options)
Example #2
0
def totalLdVolume(vesselgrp):
  if vesselgrp.attrs['CLASS'] == 'GRAPH':
    #ld = krebsutils.read_lattice_data_from_hdf(vesselgrp['lattice'])
    print(str(vesselgrp.file.filename))
    print(vesselgrp.name)
    ld = krebsutils.read_lattice_data_from_hdf_by_filename(str(vesselgrp.file.filename), str(vesselgrp.name)+'/lattice')
    #if we have 2d config one entry of sizeOfDim is zero!  
    sizeOfDim = ld.GetWorldSize()
    sizeOfDim = np.asarray(sizeOfDim, dtype='float64')
    vol = np.prod(sizeOfDim[sizeOfDim.nonzero()])
    if vol>0:
      return vol
    else:
      print("to be implemented")
  if vesselgrp.attrs['CLASS'] == 'REALWORLD':
    pos=vesselgrp['nodes/world_pos']
    x_min = np.min(pos[:,0])
    x_max = np.max(pos[:,0])
    delta_x = np.fabs(x_max-x_min)
    #failsave for 2d
    if delta_x == 0:
        delta_x =1
    y_min = np.min(pos[:,1])
    y_max = np.max(pos[:,1])
    delta_y = np.fabs(y_max-y_min)
    if delta_y == 0:
        delta_y =1
    z_min = np.min(pos[:,2])
    z_max = np.max(pos[:,2])
    delta_z = np.fabs(z_max-z_min)
    if delta_z == 0:
        delta_z =1
    vol = delta_x*delta_y*delta_z
    return vol
def ComputeBoundingBox(vesselgroup, vesselgraph):
    if 'lattice' in vesselgroup:
        vess_ldgroup = vesselgroup['lattice']
        #wbbox = krebsutils.read_lattice_data_from_hdf(vess_ldgroup).worldBox
        fn = str(vess_ldgroup.file.filename)
        path = str(vess_ldgroup.name)
        Pyld = krebsutils.read_lattice_data_from_hdf_by_filename(fn, path)
        wbbox = Pyld.worldBox
    else:
        pos = vesselgraph['position']
        minval = np.amin(pos, axis=0)
        maxval = np.amax(pos, axis=0)
        wbbox = np.vstack(
            (minval, maxval
             )).transpose().ravel()  # xmin ,xmax, ymin, ymax, zmin ,zmax ...
        print 'WBBOX = ', wbbox
    return wbbox
Example #4
0
def addBulkTissueTumor(epv, tumorgroup, trafo, options):
    ld = krebsutils.read_lattice_data_from_hdf_by_filename(
        str(tumorgroup.file.filename),
        str(tumorgroup['conc'].attrs['LATTICE_PATH']))
    ld = transform_ld(trafo, ld)

    ds_necro = np.asarray(tumorgroup['necro'])
    data = np.clip(np.asarray(tumorgroup['conc']), 0.,
                   1.)  # - np.asanyarray(tumorgroup['necro']), 0., 1.)

    ds_levelset = -np.minimum(np.asarray(tumorgroup['ls']), 0.4 - ds_necro)
    ds_levelset = krebsutils.distancemap(ds_levelset) * ld.scale

    #    import matplotlib
    #    matplotlib.use('Qt4Agg')
    #    import matplotlib.pyplot as pyplot
    #    pyplot.imshow(ds_levelset[:,:,8])
    #    pyplot.contour(ds_levelset[:,:,8],[0.])
    #    pyplot.show()
    if 'tumor_clip' in options:
        clip = clipFactory(options.tumor_clip)
    else:
        clip = clipFactory('None')

    voldata_ls = epv.declareVolumeData(ds_levelset, ld.GetWorldBox())
    voldata_cells = epv.declareVolumeData(data, ld.GetWorldBox())

    value_bounds = voldata_cells.value_bounds
    style = """
      texture {
        pigment {
          function { %f + %f*%s(x,y,z) }
          color_map {
            [0.0  color <0.3,0,0>]
            [0.5  color <1,0.8, 0.3>]
            [0.8  color <1,1,0.1>]
          }
        }
        finish { 
          specular 0.3
        }
      }""" % (value_bounds[0],
              (value_bounds[1] - value_bounds[0]), voldata_cells.name)
    #style = " texture { pigment { color rgb<1,0.8,0.3> }  finish { specular 0.3 }}"
    epv.addIsosurface(voldata_ls, 0., lambda: style, clip, style)
Example #5
0
def from_vessel_file(filenames,grp_pattern):
  dirs = set()
  dataman = myutils.DataManager(20, [krebs.plotIff.DataTissue(), krebs.plotIff.DataGlobalIff(), krebs.plotIff.DataRadialIff(), krebs.analyzeGeneral.DataDistanceFromCenter(), krebs.analyzeGeneral.DataVesselSamples(),krebs.analyzeGeneral.DataBasicVessel(), o2analysis.DataDetailedPO2()])
  with h5py.File('chache.h5', 'a') as f_measure:
  #f_measure = h5files.open('chache.h5', 'a', search = False)  
    def cachelocation(g):
      path = posixpath.join('FileCS_'+myutils.checksum(basename(g.file.filename)), g.name.strip(posixpath.sep))
      return (f_measure, path)
    #run with grp_pattern: iff/vessels
    for fn in filenames:
      with h5py.File(fn, 'r') as f:
        d = myutils.walkh5(f, grp_pattern)
        assert len(d), 'your refered pattern "%s" not found in "%s"!' % (grp_pattern, fn)
        dirs =set.union(dirs, d)
        for group_path in dirs:
          if 'vessel' in grp_pattern and not 'o2' in grp_pattern:
            vesselgroup = f[group_path]
            print("try to read vessel lattice data")
            ldvessels = ku.read_lattice_data_from_hdf_by_filename(str(vesselgroup.file.filename), str(vesselgroup.name)+'/lattice') #['lattice']
            print('worldbox:')
            print(ldvessels.worldBox)
            fieldld = ku.SetupFieldLattice(ldvessels.worldBox, 3, 10, 0.)
            print("extract vessel fraction")
            phi_vessels = krebs.analyzeGeneral.CalcPhiVessels(dataman, vesselgroup, fieldld, scaling = 1., samples_per_cell = 5)
            import nibabel as nib
            new_image=nib.Nifti1Image(phi_vessels,affine=np.eye(4))
            common_filename = os.path.splitext(os.path.basename(fn))[0]
            new_image.to_filename(common_filename + '_vessels'+ '.nii')
          if 'o2' in grp_pattern:
            po2group = f[group_path]
            #sample_length = 500.
            #data = dataman.obtain_data('detailedPO2_global', 'po2_tissue', po2group, sample_length, cachelocation(po2group))        
            data = np.asarray(po2group['po2field'])
            print('bla')
            import nibabel as nib
            new_image=nib.Nifti1Image(data,affine=np.eye(4))
            common_filename = os.path.splitext(os.path.basename(fn))[0]
            new_image.to_filename(common_filename + '_po2'+ '.nii')
Example #6
0
    def write(self, filename):
        if len(self.data) > 1 or (VtkFiles in self.data
                                  and len(self.data[VtkFiles]) > 1):
            raise RuntimeError('cannot save data on multiple grids')
        k, g = self.data.popitem()
        if k == VtkFiles:
            g = g.pop()
            ext = '.vtk' if g.attrs['TYPE'] == 'VTK_FILE' else '.vtu'
            f = open(os.path.splitext(filename)[0] + ext, 'wb')
            f.write(np.asarray(g).tostring())
            f.close()
        else:
            #ds = vtkcommon.vtkImageDataFromLd(self.file[k].attrs)
            #ld = ku.read_lattice_data_from_hdf(self.file[k])
            fn = str(self.file[k].file.filename)
            path = str(self.file[k].name)
            Pyld = ku.read_lattice_data_from_hdf_by_filename(fn, path)

            ds = vtkcommon.vtkImageDataFromLd(Pyld)
            for q in g:
                # iterate over hdf datasets and add them to the image data
                try:
                    vtkcommon.vtkImageDataAddData(ds, q, 'CellData',
                                                  posixpath.basename(q.name))
                except RuntimeError, e:
                    print 'Warning: cannot add data %s' % q.name
                    print '  Exception reads "%s"' % str(e)
                    pass
            writer = vtk.vtkDataSetWriter()
            if int(vtk.vtkVersion.GetVTKSourceVersion()[12]) > 5:
                writer.SetInputData(ds)
            else:
                writer.SetInput(ds)
            writer.SetFileName(os.path.splitext(filename)[0] + '.vtk')
            writer.Write()
            del ds
Example #7
0
def vess_size_to_tum_size(fn, tum_lattice_const):
    #vesselgroup = h5files.open(fn, 'r')['/vessels']
    #if __debug__:
    #  print("vesselgroup.id.id: %i" % vesselgroup.id.id)
    #ld = krebsutils.read_lattice_data_from_hdf(krebsutils.find_lattice_group_(vesselgroup))
    #ld_vessels = krebsutils.read_lattice_data_from_hdf(vesselgroup['lattice'])
    ld_vessels = krebsutils.read_lattice_data_from_hdf_by_filename(
        fn, "/vessels/lattice")
    bbox = ld_vessels.worldBox
    boxLengths = ((bbox[1] - bbox[0]), (bbox[3] - bbox[2]),
                  (bbox[5] - bbox[4]))
    number_of_numerical_grid_points = np.ceil(
        (np.asarray(boxLengths) + .5) / tum_lattice_const)
    for (i, x) in enumerate(number_of_numerical_grid_points):
        if x == 1:
            number_of_numerical_grid_points[i] = 2

    #del vesselgroup
    l = ld_vessels.shape
    s = int(ld_vessels.scale)
    """
  mapping from: latticesize initial vesselsnetwork 
  to numical lattice of tissue lattice
  first number: size x
  second number: size z
  third number: lattice constant
  """
    vs = {
        (9, 1, 130): (10, 10),  #div runtime test
        (100, 6, 80): (228, 10),  # 100x5.80
        (100, 62, 80): (196, 128),  #100x50.80
        (200, 6, 80): (384, 10),  #200x5.80
        (60, 75, 80): (128, 128),  #60x60.80
        # now hierarchical builds
        (167, 17, 80): (300, 34),
        (103, 121, 80): (150, 150),
        # with large lattice spacing
        (59, 69, 150): (160, 160),
        (63, 65, 150): (160, 160),
        #(103,121,80) : (200, 200), # a special extra large configuration
        # 7x7 * 2^3 hierarchical biulds
        (127, 129, 80): (150, 150),  # actual size could be 270**3
        # twodimensional system
        (160, 1, 80): (350, 1),
        # quasi twodimensional
        (163, 9, 80): (350, 24),
        (59, 9, 150): (250, 32),
        # small system
        (31, 5, 150): (80, 16),
        # 16 cm flat
        (111, 9, 150): (267, 32),
        # 8mm P10 3d
        #(55,61, 130) : (80,90),
        #(55,61, 130) : (155,155),
        (55, 61, 130): (100, 100),
        # 8mm P10 q2d
        (59, 5, 130): (100, 15),
        (47, 9, 130): (50, 20),
        # for testing 3d_mini_mini
        (14, 18, 130): (40, 40),
        # trastuzumab calibration growth
        (53, 65, 80): (100, 100),
        # 5mm P11 q2d, 5mm x 5mm x 0.3mm
        (59, 5, 90):
        (int(4000. / tum_lattice_const), int(300. / tum_lattice_const + 0.5)),
        # swine h1
        (17, 19, 75):
        (int(1200. / tum_lattice_const), int(1200. / tum_lattice_const)),
        # swine h2
        (35, 37, 75): (int(1400. / tum_lattice_const),
                       int(1400. / tum_lattice_const)),
        # swine h2 -big (67, 77, 75
        (67, 77, 75): (int(5000. / tum_lattice_const),
                       int(5000. / tum_lattice_const)),
        # 4mm x 0.3 P? ??
        (51, 57, 88): (int(4000. / tum_lattice_const),
                       int(4000. / tum_lattice_const)),
        #the vessBigTras
        (119, 141, 88): (int(10000. / tum_lattice_const),
                         int(10000. / tum_lattice_const)),
    }  #[(l[0],l[2],s)]
    theKey = (l[0], l[2], s)
    if theKey in vs:
        return (vs[theKey][0], vs[theKey][0], vs[theKey][1])
    else:
        print('Warning: Guessed tum grid size')
        return number_of_numerical_grid_points.astype(int)
Example #8
0
def renderScene(vesselgroup, tumorgroup, imagefn, options):
    if vesselgroup is not None:
        vgrp = vesselgroup['lattice']
        wbbox = krebsutils.read_lattice_data_from_hdf_by_filename(
            str(vgrp.file.filename), str(vgrp.name)).worldBox
    else:
        wbbox = krebsutils.read_lattice_data_from_hdf(
            tumorgroup.file['field_ld']).worldBox
    trafo = calc_centering_normalization_trafo(wbbox)
    zsize = (wbbox[5] - wbbox[4])

    vessel_ld = krebsutils.read_lattice_data_from_hdf_by_filename(
        str(vesselgroup.file.filename),
        str(vesselgroup.name) + '/lattice')
    options.wbbox = vessel_ld.GetWorldBox()

    with EasyPovRayRender(options) as epv:
        epv.setBackground(options.background)
        cam = options.cam
        if cam in ('topdown', 'topdown_slice'):
            cam_fov = 60.
            cam_distance_factor = options.cam_distance_multiplier * ComputeCameraDistanceFactor(
                cam_fov, options.res, wbbox)
            epv.addLight(10 * Vec3(1.7, 1.2, 2),
                         1.,
                         area=(4, 4, 3, 3),
                         jitter=True)
            if cam == 'topdown_slice':
                imagefn += '_slice'
                options.vessel_clip = ('zslice', -201 * trafo.w, 201 * trafo.w)
                options.tumor_clip = ('zslice', -100 * trafo.w, 100 * trafo.w)
                epv.setCamera(
                    (0, 0, cam_distance_factor * 0.5 * (200. * trafo.w + 2.)),
                    (0, 0, 0),
                    cam_fov,
                    up='y')
            else:
                imagefn += '_top'
                epv.setCamera(
                    (0, 0, cam_distance_factor * 0.5 * (zsize * trafo.w + 2.)),
                    (0, 0, 0),
                    cam_fov,
                    up='y')
        else:
            imagefn += '_pie'
            cam_fov = 60.
            basepos = options.cam_distance_multiplier * np.asarray(
                (0.6, 0.7, 0.55)) * (1. / 1.4) * math.tan(
                    math.pi * 0.25) / math.tan(math.pi / 180. / 2. * cam_fov)
            epv.setCamera(basepos, (0, 0, 0), cam_fov, up=(0, 0, 1))
            num_samples_large_light = 4
            num_samples_small_light = 2
            epv.addLight(10 * Vec3(0.7, 1., 0.9),
                         0.8,
                         area=(1., 1., num_samples_small_light,
                               num_samples_small_light),
                         jitter=True)
            epv.addLight(10 * Vec3(0.5, 0.5, 0.5),
                         0.6,
                         area=(5., 5., num_samples_large_light,
                               num_samples_large_light),
                         jitter=True)
            options.vessel_clip = ('pie', 0.)
            options.tumor_clip = ('pie', -50 * trafo.w)

        if vesselgroup is not None:
            graph = krebsutils.read_vessels_from_hdf(
                vesselgroup, ['position', 'flags', 'radius', 'pressure'],
                return_graph=True)
            if options.filteruncirculated:
                graph = graph.get_filtered(edge_indices=myutils.bbitwise_and(
                    graph['flags'], krebsutils.CIRCULATED))
            if 'colorfactory' in options:
                print('colorfactory is in options')
                colorfactory = options.colorfactory
            else:
                print('colorfactory not in options')
                colorfactory = make_pressure_color_arrays
            colorfactory(graph)
            #addVesselTree(epv, graph, trafo, vesselgroup = vesselgroup, options)
            epv.addVesselTree2(epv, graph, trafo, options)

        if tumorgroup is not None and 'conc' in tumorgroup:
            addBulkTissueTumor(epv, tumorgroup, trafo, options)

        if (tumorgroup is not None
                and tumorgroup.attrs['TYPE'] == 'faketumor'):
            print('nix')

        if options.noOverlay:
            epv.render(imagefn + '.png')
        else:
            povrayEasy.RenderImageWithOverlay(epv, imagefn + '.png', None,
                                              'tumor', options)
Example #9
0
def get_o2_field(fn):
  with open('TissueLevels.out','r') as f:
    read_data = f.read()
  f.closed
  print("we read the tissue oxygen levels")
  niceList=[]
  start = False
  for (linenumber,line) in enumerate(read_data.split(os.linesep)):          
      if start:
          print line
          for wert in line.split():
              niceList.append(wert)
      if line =='Solute 1':
          start = True
      if not string.find(line, 'pmean,'):
          start = False
  '''on this stage secombs data is in array nice List
  now we get Michaels data...
  '''
  niceList.pop()
  niceList.pop()
  niceList.pop()
  niceList.pop()
  niceList.pop()
  niceList.pop()
  niceList.pop()
  niceList.pop()
  
  print("we load the tissue data")
  with open('TissueSources.out','r') as f:
    read_data = f.read()
  f.closed
  discretization_points=[]
  X=[]
  Y=[]
  Z=[]

  myindex=0
  for (linenumber,line) in enumerate(read_data.split(os.linesep)):          
      if linenumber==0:
          print line
          for (pos,wert) in enumerate(line.split()):
            if pos<3:
              discretization_points.append(int(wert))
      if linenumber>1 and (not 'Tissue point xyz' in line):
          print line
          for (pos,wert) in enumerate(line.split()):
            if myindex<discretization_points[0]:
              X.append(float(wert))
              myindex=myindex+1
              continue
            if myindex>discretization_points[0]-1 and myindex<(discretization_points[0]+discretization_points[1]):
              Y.append(float(wert))
              myindex=myindex+1
              continue
            if myindex>(discretization_points[0]+discretization_points[1]-1) and myindex<(discretization_points[0]+discretization_points[1]+discretization_points[2]):
              Z.append(float(wert))
              myindex=myindex+1
              continue
  
  
  discretization_points=np.asarray(discretization_points)

  niceList2 = []
  start = False
  for (linenumber,line) in enumerate(read_data.split(os.linesep)):
        if 'source strengths for solute' in line:
          start = False
            
        if start:
            print line
            for wert in line.split():
                niceList2.append(int(wert))
        if line =='Tissue point xyz indices':
            start = True
  print('Found %i datapoint, got %i discretization points' % (len(niceList),discretization_points[0]*discretization_points[1]*discretization_points[2]))  
  
  niceList2=np.asarray(niceList2)
  dim=0
  number=0
  coords=[]
  coords2=np.zeros([3,len(niceList2)])
  
  for aint in niceList2:
    if dim==0:
      x=X[aint-1]
      coords2[dim,number]=x
      dim=dim+1
      continue
    if dim==1:
      y=Y[aint-1]
      coords2[dim,number]=y
      dim=dim+1
      continue
    if dim==2:
      z=Z[aint-1]
      coords2[dim,number]=z
      dim=dim+1
    if dim==3:
      coords.append([x,y,z])
      number=number+1
      dim=0
      
    
  with h5py.File(fn, 'r') as f:
    #need that data from hdf
    po2_field_mw = np.asarray(f['/po2/vessels/po2field'])
    ld = krebsutils.read_lattice_data_from_hdf_by_filename(fn,'/po2/vessels/field_ld')
    po2_field_mw_at_secomb_positons = krebsutils.sample_field(np.asarray(coords2,dtype=np.float32).transpose(),po2_field_mw,ld)
  
  return coords2, niceList2, po2_field_mw_at_secomb_positons
Example #10
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'])
            ld = krebsutils.read_lattice_data_from_hdf_by_filename(
                str(po2group.file.filename),
                str(po2group.name) + '/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