def plot_curv(ax):
     data = imslice(
         resultfile.obtain_data('field_curvatureradius', groupname))
     delta = imslice(
         resultfile.obtain_data('field_interfacedelta',
                                groupname)) * ld.scale
     ax.set(title='$\kappa$')
     imshow_cb(ax, data * delta, ld, matplotlib.cm.coolwarm,
               'zero-centered', 1.)
def obtain_volumes(f, group, dataman):
    ld = dataman('ld', f)
    dist_viabletumor = dataman('fieldvariable', f, 'dist_viabletumor', group)
    dist_tumor = dataman('fieldvariable', f, 'dist_tumor', group)
    pyplot.imshow(imslice(dist_viabletumor))
    pyplot.colorbar()
    pyplot.show()
    nsites_total = np.prod(ld.shape)
    nsites_tumor = np.sum(np.asarray(dist_tumor < 0., dtype=np.uint))
    nsites_viabletumor = np.sum(
        np.asarray(dist_viabletumor < 0., dtype=np.uint))
    return np.prod(ld.GetWorldSize()), float(
        nsites_tumor) / nsites_total, float(nsites_viabletumor) / nsites_total
Exemple #3
0
  def obtain_data(self, dataman, dataname, *args):
    if dataname == 'ld':
      f, = args
      ld = krebsutils.read_lattice_data_from_hdf(f['field_ld'])
      return ld

    ####
    if dataname == 'time':
      if len (args) == 1:
        group, = args
      else:
        group = args[0][args[1]] # f, group = args
      return group.attrs['time'] # in hours

    #####
    if dataname == 'fieldvariable':
      f, fieldname, group = args[:3]
      g = f[group] # group name to actual group
      tumor_path = tumor_path_(f, group)

      ld = dataman.obtain_data('ld', f)

      def get(name):
        return self.obtain_data(dataman, 'fieldvariable', f, name, group)

      if fieldname == 'phi_cells':
        data = f[tumor_path]['conc']
      elif fieldname == 'dist_tumor_':
        data = f[tumor_path]['ls']
      elif fieldname == 'theta_tumor':
        gtumor = f[tumor_path]
        if gtumor.attrs['TYPE'] == 'faketumor':
          data = gtumor['tc_density']
        else:
          data = gtumor['ptc']
      elif fieldname == 'phi_necro':
        data = f[tumor_path]['necro']
      elif fieldname in ('oxy'):
        try:
          data = g['oxy']
        except KeyError:
          data = g['fieldOxy']
      elif fieldname in ('gf'):
        data = g['fieldGf']
      elif fieldname in ('press', 'sources', 'vel'):
        data = f[tumor_path][fieldname]
      elif fieldname == 'phi_viabletumor':
        theta_tumor = get('theta_tumor')
        phi_cells = get('phi_cells')
        phi_necro = get('phi_necro')
        data = theta_tumor * (phi_cells - phi_necro)
      elif fieldname == 'phi_tumor':
        theta_tumor = get('theta_tumor')
        phi_cells = get('phi_cells')
        data = theta_tumor * phi_cells
      elif fieldname == 'dist_necro':
        phi_necro = get('phi_necro')
        phi_cells = get('phi_cells')
        tumor_contour_level = 0.5*np.average(phi_cells)
        data = calc_distmap(phi_necro, ld, tumor_contour_level)
      elif fieldname == 'dist_viabletumor':
        def read(gmeasure, dsname):
          return np.asarray(gmeasure[dsname])
        def write(gmeasure, dsname):
          dist_tumor = get('dist_tumor')
          dist_necro = get('dist_necro')
          data = np.maximum(dist_tumor, -dist_necro)
          gmeasure.create_dataset(dsname, data = data, compression = 9)
        fm = myutils.MeasurementFile(f, h5files)
        data = myutils.hdf_data_caching(read, write, fm, (tumor_path, 'dist_viabletumor'), (0,1))
      elif fieldname == 'phi_vessels':
        def read(gmeasure, name):
          return np.asarray(gmeasure[name])
        def write(gmeasure, name):
          phi_vessels = CalcPhiVessels(dataman, f[group]['vessels'], ld, scaling = 1.)
          gmeasure.create_dataset(name, data = phi_vessels, compression = 9)
        fm = myutils.MeasurementFile(f, h5files)
        data = myutils.hdf_data_caching(read, write, fm, (group, 'phi_vessels'), (0,1))
      elif fieldname == 'dist_tumor':
        def read(gmeasure, name):
          return np.asarray(gmeasure[name])
        def write(gmeasure, name):
          ls = get('dist_tumor_')
          ls = -ls
          dist = calc_distmap(np.asarray(ls < 0., dtype=np.float32), ld, 0.5)
          gmeasure.create_dataset(name, data = dist, compression = 9)
        fm = myutils.MeasurementFile(f, h5files)
        data = myutils.hdf_data_caching(read, write, fm, (tumor_path, 'dist_tumor_full'), (0,1))
      else:
        raise RuntimeError('unkown field %s' % fieldname)

      if len(args)>3 and args[3] == 'imslice':
        import plotBulkTissue
        return plotBulkTissue.imslice(data)
      else:
        return np.asarray(data)

    if dataname == 'fieldvariable_radial':
      property_name, tumorgroup, bins_spec, distance_distribution_name, cachelocation = args

      def write(gmeasure, groupname):
        distmap, ld = obtain_distmap_(dataman, tumorgroup, distance_distribution_name)
        data    = dataman.obtain_data('fieldvariable', tumorgroup.file, property_name, tumorgroup.name)
        bins    = bins_spec.arange()
        a = myutils.MeanValueArray.fromHistogram1d(bins, np.ravel(distmap), np.ravel(data))
        ds = a.write(gmeasure, groupname)
        ds.attrs['BINS_SPEC'] = str(bins_spec)

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

      return HdfCacheRadialDistribution((read, write), property_name, bins_spec, distance_distribution_name, cachelocation, 1)

    if dataname == 'approximate_tumor_radius':
      tumorgroup, = args
      def write(gmeasure, groupname):
        rad = ApproximateTumorRadius(dataman, tumorgroup)
        ds = gmeasure.create_dataset(groupname, data = rad)
      def read(gmeasure, groupname):
        return np.asscalar(gmeasure[groupname][()])
      return myutils.hdf_data_caching(read, write, tumorgroup, ('approximate_tumor_radius',), (1,))
Exemple #4
0
def analyzeO2_with_tumor(f):

    dataman = myutils.DataManager(100,
                                  [plotBulkTissue2d.DataTumorTissueSingle()])

    resultfile = plotBulkTissue2d.ResultFile(f, dataman)
    group = resultfile.groupnames[-1]
    ld = resultfile.obtain_data('ld')

    print group

    arterial_reference = 12.

    theta_tumor = resultfile.obtain_data('fieldvariable', 'theta_tumor', group)
    o2 = arterial_reference * resultfile.obtain_data('fieldvariable', 'oxy',
                                                     group)
    dist_tumor = resultfile.obtain_data('fieldvariable', 'dist_tumor', group)

    def tumorAndTissueAverages(field):
        tumor = field[theta_tumor > 0.5]
        normal = field[theta_tumor < 0.5]
        tissueavg = (np.average(normal), np.std(normal))
        tumoravg = (np.average(tumor), np.std(tumor))
        return tissueavg, tumoravg

    cons_rate_normal = float(f['parameters'].attrs['o2_cons_coeff_normal'])
    cons_rate_tumor = float(f['parameters'].attrs['o2_cons_coeff_tumor'])
    consumption = (theta_tumor * cons_rate_tumor +
                   (1. - theta_tumor) * cons_rate_normal) * o2
    solubility = 0.00021  # ml O2 / ml Tissue / kPa
    consfactors = 2000. * 60 * solubility * 1.e2  # 2000. from diff koff, 60 from s to min conversion.
    consumption *= consfactors

    avgStr = lambda t: r'$%s \pm %s$' % tuple(f2l(q) for q in t)

    outfn = 'oxycheck-' + splitext(basename(str(f.filename)))[0] + '.pdf'
    with mpl_utils.PdfWriter(outfn) as pdfwriter:
        fig = pyplot.figure(figsize=(5, 5))

        ax = fig.add_axes([0.05, 0.05, 4.2 / 5, 4.2 / 5])
        img = plotBulkTissue.imshow(ax,
                                    plotBulkTissue.imslice(o2),
                                    ld,
                                    vmin=0.,
                                    vmax=9.,
                                    cmap=matplotlib.cm.Reds,
                                    worldscaling=1.e-3)
        plotBulkTissue.colorbar(ax.get_figure(), ax, img)
        plotBulkTissue.contour(ax,
                               plotBulkTissue.imslice(dist_tumor),
                               ld,
                               levels=[0.],
                               colors=['k'],
                               worldscaling=1.e-3)
        tissueavg, tumoravg = tumorAndTissueAverages(o2)
        print 'normal po2: %f +/- %f' % tissueavg
        print 'tumor po2:  %f +/- %f' % tumoravg

        fig.suptitle(r'O2 [kPa] (arterial $P_{O_2} = 12 kPa$)')
        fig.text(
            0.1, 0.90, '<tissue> ' + avgStr(tissueavg) + '\n' + '<tumor> ' +
            avgStr(tumoravg))

        pdfwriter.savefig(fig)

        fig = pyplot.figure(figsize=(5, 5))

        ax = fig.add_axes([0.05, 0.05, 4.2 / 5, 4.2 / 5])
        img = plotBulkTissue.imshow(ax,
                                    plotBulkTissue.imslice(consumption),
                                    ld,
                                    vmin=consumption.min(),
                                    vmax=consumption.max(),
                                    cmap=matplotlib.cm.Reds,
                                    worldscaling=1.e-3)
        plotBulkTissue.colorbar(ax.get_figure(), ax, img)
        plotBulkTissue.contour(ax,
                               plotBulkTissue.imslice(dist_tumor),
                               ld,
                               levels=[0.],
                               colors=['k'],
                               worldscaling=1.e-3)

        tissueavg, tumoravg = tumorAndTissueAverages(consumption)
        print 'mro2 normal o2: %f +/- %f' % tissueavg
        print 'mro2 tumor o2:  %f +/- %f' % tumoravg

        fig.suptitle(r'$MRO_2$ [ml $O_2$ / 100 ml Tissue min]')
        fig.text(
            0.1, 0.90, '<tissue> ' + avgStr(tissueavg) + '\n' + '<tumor> ' +
            avgStr(tumoravg))

        pdfwriter.savefig(fig)