Exemplo n.º 1
0
def ComputeIsosurfaceRadialCurve(dataman, vesselgroup, tumorgroup, bins_spec,
                                 distance_distribution_name, ld, cachelocation,
                                 WorkerFunction):
    print 'compute', WorkerFunction.__name__, vesselgroup.name
    vessels = dataman.obtain_data('vessel_graph', vesselgroup,
                                  ['position', 'flags'])
    pos = vessels['position']
    distancemap, ld = krebs.analyzeGeneral.obtain_distmap_(
        dataman, tumorgroup, distance_distribution_name, ld)
    dist = krebsutils.sample_field(pos,
                                   distancemap,
                                   ld,
                                   linear_interpolation=True)
    bounds = np.amin(distancemap), np.amax(distancemap)
    bins = bins_spec.arange()
    results = []
    for level in bins:
        if level < bounds[0] or level > bounds[1]:
            data = np.nan
        else:
            data = WorkerFunction(dataman, vesselgroup, dist, ld, distancemap,
                                  level)
            print 'level %f, data %f' % (level, data)
        results.append(data)
    return results, bins
Exemplo n.º 2
0
            def write(gmeasure, groupname):
                #vessels = krebsutils.read_vesselgraph(vesselgroup, ['flow', 'pressure', 'position', 'flags'])
                vessels = dataman.obtain_data('vessel_graph', vesselgroup,
                                              ['position', 'flags', 'flow'])
                pos = vessels['position']

                if has_tumor:
                    ldtumor = dataman.obtain_data('ld', tumorgroup.file)
                    dist = dataman.obtain_data('fieldvariable',
                                               tumorgroup.file, 'theta_tumor',
                                               tumorgroup.name)
                    dist = krebsutils.sample_field(pos,
                                                   dist,
                                                   ldtumor,
                                                   linear_interpolation=True)
                    res = ComputeIsosurfaceBloodFlow(dataman, vesselgroup,
                                                     dist, 0.5)
                    res.update(
                        DataTumorBloodFlow.ComputeTotalBloodFlow_(vessels))
                else:
                    res = DataTumorBloodFlow.ComputeTotalBloodFlow_(vessels)

                g = gmeasure.create_group(groupname)
                for k, v in res.iteritems():
                    g.create_dataset(k, data=v)
Exemplo n.º 3
0
 def write(gmeasure, groupname):
     distancemap, ld, volume = ComputeTumorDistanceMapAndVolumeForPerfusion_(
         dataman, tumorGroup)
     vessels = dataman.obtain_data('vessel_graph', vesselgroup,
                                   ['position', 'flags'])
     distSamples = krebsutils.sample_field(vessels['position'],
                                           distancemap,
                                           ld,
                                           linear_interpolation=True)
     result = ComputeIsosurfaceBloodFlow(dataman, vesselgroup, distSamples,
                                         0)
     result = result['flow_in']
     result /= volume
     ds = gmeasure.create_dataset(groupname, data=result)
     ds.attrs['tumorGroup'] = str(tumorGroup)
     ds.attrs['volume'] = volume
     ds.attrs['unit'] = '1 / s'
Exemplo n.º 4
0
    def obtain_data(self, dataman, dataname, *args):
      if dataname == 'distance_from_center_distribution':
        ld, = args
        return krebsutils.make_radial_field(ld)        

      elif dataname == 'distancemap_samples':
        try:
          vesselgroup, tumorgroup, sample_length, distance_distribution_name, ld = args
        except ValueError:
          vesselgroup, tumorgroup, sample_length, distance_distribution_name = args
          ld = None
        pos_smpl   = dataman.obtain_data('basic_vessel_samples', 'position', vesselgroup, sample_length)
        #tumor_ld   = dataman.obtain_data('ld', tumorgroup.file)z
        distmap, ld = obtain_distmap_(dataman, tumorgroup, distance_distribution_name, ld)
        dist_smpl   = krebsutils.sample_field(pos_smpl, distmap, ld, linear_interpolation=True) #, extrapolation_value = 1.e10)
        bmin, bmax  = ld.worldBox.reshape(3,2).transpose()
        mask        = (pos_smpl>bmin) & (pos_smpl<bmax)
        mask        = (mask[:,0] & mask[:,1]) & mask[:,2]
        return dist_smpl, distmap, mask, ld
Exemplo n.º 5
0
def testfieldsampling():
    c = np.asarray((40, 20, 1))
    bb = np.vstack((-c, c - 1)).transpose().reshape((6, ))
    ld = krebs.LatticeDataQuad3d(bb, 1.)
    ld.SetCellCentering((True, True, True))
    wbb = ld.worldBox
    print(ld)
    print("box: ")
    print(wbb)

    num = (bb[1] - bb[0]) * (bb[3] - bb[2])
    pos = np.random.uniform(wbb[0], wbb[1],
                            num), np.random.uniform(wbb[2], wbb[3],
                                                    num), 0.5 * np.ones(num)
    pos = np.asarray(pos, dtype=np.float32).transpose()

    a = np.asarray(make_test_array(c), dtype=np.float32)

    smpl = krebs.sample_field(pos, a, ld, linear_interpolation=True)

    print 'arange = %f %f' % (a.min(), a.max())
    print 'smpl range = %f %f' % (smpl.min(), smpl.max())

    img = a[:, ::-1, 1]
    img = img.transpose()
    theExtent = ld.worldBox
    #mpl change the interface here
    plt.imshow(img,
               extent=theExtent[0:4],
               interpolation='nearest',
               vmin=0,
               vmax=1)
    #plt.imshow(img, extent = ld.worldBox, interpolation='nearest', vmin=0, vmax=1)

    plt.scatter(pos[:, 0], pos[:, 1], c=smpl, vmin=0., vmax=1.)

    plt.show()
Exemplo n.º 6
0
def generate_data(statedata, dstgroup):
    """
    main routine that does all the measurement. 
    Enable Individual parts. Computed date overwrites old data.
  """
    print statedata.file.filename, statedata.name

    time = statedata.attrs['time']
    tum_grp = statedata['tumor']
    tum_ld = get_tumld(tum_grp)
    cellvol = tum_ld.scale**3
    distmap = calc_distmap(tum_grp)
    radialmap = krebsutils.make_radial_field(tum_ld)
    axial_max_rad = np.max(np.abs(tum_ld.worldBox.ravel()))
    ptc = np.asarray(tum_grp['ptc'])

    if 1:
        ## shape data: rim surface area, volume
        vtkds, = extractVtkFields.Extractor(statedata['tumor'],
                                            ['ls']).asVtkDataSets()
        area = integrate_surface(vtkds)
        del vtkds
        vol = np.sum(ptc) * cellvol
        radius = np.average(radialmap[np.nonzero(
            np.logical_and(distmap > -2 * tum_ld.scale,
                           distmap < 2 * tum_ld.scale))])
        sphere_equiv_radius = math.pow(vol * 3. / (4. * math.pi), 1. / 3.)
        sphere_equiv_area = 4. * math.pi * (sphere_equiv_radius**2)
        sphericity = sphere_equiv_area / area
        #cylinder vol = pi r^2 h
        cylinder_equiv_radius = math.sqrt(vol / tum_ld.GetWorldSize()[2] /
                                          math.pi)
        cylinder_equiv_area = 2. * math.pi * cylinder_equiv_radius * tum_ld.GetWorldSize(
        )[2]
        cylindericity = cylinder_equiv_area / area

        d = dict(time=time,
                 area=area,
                 volume=vol,
                 radius=radius,
                 sphericity=sphericity,
                 cylindericity=cylindericity,
                 sphere_equiv_radius=sphere_equiv_radius,
                 sphere_equiv_area=sphere_equiv_area,
                 cylinder_equiv_radius=cylinder_equiv_radius,
                 cylinder_equiv_area=cylinder_equiv_area)
        pprint(d)

        print 'geometry data for %s/%s' % (statedata.file.filename,
                                           statedata.name)
        g = dstgroup.recreate_group('geometry')
        myutils.hdf_write_dict_hierarchy(g, '.', d)

    if 1:
        print 'regenerating radial for %s/%s' % (statedata.file.filename,
                                                 statedata.name)
        dstdata = dstgroup.recreate_group('radial')

        ## radial data; vessels
        vessels = krebsutils.read_vesselgraph(statedata['vessels'], [
            'position', 'flags', 'shearforce', 'radius', 'flow', 'maturation'
        ])
        vessels = vessels.get_filtered(
            edge_indices=(vessels.edges['flags'] & krebsutils.CIRCULATED != 0))
        sample_length = 50.

        far = 1.e10

        s = plotVessels.generate_samples(vessels, 'position', 'nodes',
                                         sample_length)
        dist = krebsutils.sample_field(s,
                                       distmap,
                                       tum_ld,
                                       linear_interpolation=True,
                                       extrapolation_value=far)
        rad = krebsutils.sample_field(s,
                                      radialmap,
                                      tum_ld,
                                      linear_interpolation=True,
                                      extrapolation_value=far)
        del s

        dstdata.recreate_group('vs_r').create_dataset(
            'bins', data=np.average((bins_rad[:-1], bins_rad[1:]), axis=0))
        dstdata.recreate_group('vs_dr').create_dataset(
            'bins', data=np.average((bins_dist[:-1], bins_dist[1:]), axis=0))
        #    dstdata['vs_r'] = dict(bins = np.average((bins_rad[:-1],bins_rad[1:]), axis=0))
        #    dstdata['vs_dr'] = dict(bins = np.average((bins_dist[:-1],bins_dist[1:]), axis=0))

        #    def add_histogram_data(dst, name, (s_avg, s_std, s_sqr)):
        #      dst[name] = dict(avg = s_avg, std = s_std, sqr = s_sqr)

        for name in ['shearforce', 'radius', 'flow', 'maturation']:
            s = plotVessels.generate_samples(vessels, name, 'edges',
                                             sample_length)
            myutils.MeanValueArray.fromHistogram1d(bins_rad, rad, s).write(
                dstdata['vs_r'], name)
            myutils.MeanValueArray.fromHistogram1d(bins_dist, dist, s).write(
                dstdata['vs_dr'], name)
#      d = myutils.scatter_histogram(rad, s, bins_rad, 1.)
#      add_histogram_data(dstdata['vs_r'], name, d)
#      d = myutils.scatter_histogram(dist, s, bins_dist, 1.)
#      add_histogram_data(dstdata['vs_dr'], name, d)

        def make_mvd(flavor, s, bins, smap, mask_bound):
            a = myutils.MeanValueArray.fromHistogram1d(bins, s,
                                                       np.ones_like(s))
            b = myutils.MeanValueArray.fromHistogram1d(
                bins, smap.ravel(), np.ones_like(smap.ravel()))
            a.cnt = b.cnt.copy()
            a.sum *= sample_length / cellvol
            a.sqr *= a.sum**2
            a.write(dstdata[flavor], 'mvd')
#      d = myutils.scatter_histogram(xdata, np.ones_like(xdata), bins=bins, xdata2 = np.ravel(xdata2))
#      m = dstdata[flavor]['bins'] >  mask_bound
#      for x in d[:2]:
#        x *= sample_length/cellvol
#        x.mask |= m
#      for x in d[2:]:
#        x *= (sample_length/cellvol)**2
#        x.mask |= m
#      add_histogram_data(dstdata[flavor], 'mvd', d)

        make_mvd('vs_r', rad, bins_rad, radialmap, axial_max_rad)
        make_mvd('vs_dr', dist, bins_dist, distmap, 1000.)
        #    pyplot.errorbar(dstdata['vs_dr']['bins'], dstdata['vs_dr']['mvd_avg'], yerr=dstdata['vs_dr']['mvd_std'])
        #    pyplot.show()

        del rad, dist

        for name in ['conc', 'sources', 'ptc', 'press']:
            a = np.ravel(np.asarray(tum_grp[name]))
            myutils.MeanValueArray.fromHistogram1d(
                bins_rad, np.ravel(radialmap), a).write(dstdata['vs_r'], name)
            myutils.MeanValueArray.fromHistogram1d(bins_dist,
                                                   np.ravel(distmap), a).write(
                                                       dstdata['vs_dr'], name)
#      d = myutils.scatter_histogram(np.ravel(distmap), np.ravel(np.asarray(tum_grp[name])), bins_dist, 1.)
#      add_histogram_data(dstdata['vs_dr'], name, d)
#      d = myutils.scatter_histogram(np.ravel(radialmap), np.ravel(np.asarray(tum_grp[name])), bins_rad, 1.)
#      add_histogram_data(dstdata['vs_r'], name, d)

# velocities, projected radially outward
        vel_field = tuple(
            np.asarray(tum_grp['vel'][:, :, :, i]) for i in range(3))
        vnorm = np.zeros(distmap.shape, dtype=np.float32)
        dgrad = krebsutils.field_gradient(radialmap, spacing=tum_ld.scale)
        for vv, g in zip(vel_field, dgrad):
            vnorm += vv * g
        del vel_field, dgrad

        phi_tumor = ptc * np.asarray(tum_grp['conc'])

        oxy = np.asarray(statedata['fieldOxy'])

        gf = np.array(statedata['fieldGf'])

        for name, field in [('phi_tumor', phi_tumor), ('vel', vnorm),
                            ('oxy', oxy), ('gf', gf)]:
            a = np.ravel(field)
            myutils.MeanValueArray.fromHistogram1d(
                bins_rad, np.ravel(radialmap), a).write(dstdata['vs_r'], name)
            myutils.MeanValueArray.fromHistogram1d(bins_dist,
                                                   np.ravel(distmap), a).write(
                                                       dstdata['vs_dr'], name)
Exemplo n.º 7
0
    def write(gmeasure, groupname):
        gvessels, gtumor = detailedo2.OpenVesselAndTumorGroups(po2group)
        parameters = dataman.obtain_data('detailedPO2Parameters', po2group)
        vessels = dataman.obtain_data(
            'vessel_graph', gvessels,
            ['flow', 'pressure', 'position', 'flags', 'hematocrit'])
        pos = vessels['position']
        press = vessels['pressure']
        flow = vessels['flow']
        flags = vessels['flags']
        hema = vessels['hematocrit']
        po2vessels = np.asarray(po2group['po2vessels'])
        hema = np.column_stack((hema, hema))
        conc = detailedo2.PO2ToConcentration(po2vessels, hema, parameters)
        #sat  = detailedo2.PO2ToSaturation(po2vessels, parameters)
        conc_diff = np.abs(conc[:, 1] - conc[:, 0])
        conc = np.average(conc, axis=1)
        flow_diff = 0.5 * flow * conc_diff
        flow = flow * conc
        roots = set(gvessels['nodes/roots'][...])
        del hema
        del conc
        del conc_diff
        del po2vessels

        if gtumor:
            ldtumor = dataman.obtain_data('ld', gtumor.file)
            dist = dataman.obtain_data('fieldvariable', gtumor.file,
                                       'theta_tumor', gtumor.name)
            dist = krebsutils.sample_field(pos,
                                           dist,
                                           ldtumor,
                                           linear_interpolation=True)
            dist = dist - 0.5  # is greater 0 inside the tumor?!

        total_flow_in, total_flow_out = 0., 0.
        flow_in, flow_out = 0., 0.
        flow_in_err, flow_out_err, total_flow_in_err, total_flow_out_err = 0., 0., 0., 0.
        for i, (a, b) in enumerate(vessels.edgelist):
            if not (flags[i] & krebsutils.CIRCULATED): continue
            if gtumor and dist[a] < 0 and dist[b] > 0:  # b is in the tumor
                if press[a] < press[b]:
                    flow_out += flow[i]
                    flow_out_err += flow_diff[i]
                else:
                    flow_in += flow[i]
                    #print 'tumor inflow of sat', sat[i]
                    flow_in_err += flow_diff[i]
            elif gtumor and dist[a] > 0 and dist[b] < 0:  # a is in the tumor
                if press[a] > press[b]:
                    flow_out += flow[i]
                    flow_out_err += flow_diff[i]
                else:
                    flow_in += flow[i]
                    #print 'tumor inflow of sat', sat[i]
                    flow_in_err += flow_diff[i]
            if (a in roots or b in roots):
                if flags[i] & krebsutils.ARTERY:
                    #print 'total inflow of sat', sat[i]
                    total_flow_in += flow[i]
                    total_flow_in_err += flow_diff[i]
                elif flags[i] & krebsutils.VEIN:
                    #print 'total inflow of sat', sat[i]
                    total_flow_out += flow[i]
                    total_flow_out_err += flow_diff[i]

        Err = lambda a, b, da, db: abs(1.0 / a - (a - b) / a / a) * da + abs(
            1.0 / a) * db

        res = dict(tumor_o2_in=flow_in,
                   tumor_o2_out=flow_out,
                   total_o2_out=total_flow_out,
                   total_o2_in=total_flow_in,
                   oef_total=(total_flow_in - total_flow_out) / total_flow_in,
                   oef_tumor=(flow_in - flow_out) / flow_in,
                   flow_in_err=flow_in_err,
                   flow_out_err=flow_out_err,
                   total_flow_in_err=total_flow_in_err,
                   total_flow_out_err=total_flow_out_err,
                   oef_total_err=Err(total_flow_in, total_flow_out,
                                     total_flow_in_err, total_flow_out_err),
                   oef_err=Err(flow_in, flow_out, flow_in_err, flow_out_err))
        #print 'computed oef: '
        #pprint.pprint(res)
        g = gmeasure.create_group(groupname)
        for k, v in res.iteritems():
            g.create_dataset(k, data=v)
Exemplo n.º 8
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