def AddStatsPage(self, pdfwriter):
     text = GenerateGlobalMeasurementOutputStrings(self.dataman,
                                                   self.po2group,
                                                   self.cachelocation)
     text += ['-----------------------']
     text += GenerateParameterOutputStrings(self.dataman, self.group,
                                            self.cachelocation)
     fig, _ = mpl_utils.MakeTextPage(text,
                                     figsize=(mpl_utils.a4size[0] * 0.5,
                                              mpl_utils.a4size[1] * 0.75))
     pdfwriter.savefig(fig, postfix='_data')
Beispiel #2
0
def DoIt(filenames, pattern, with_o2):
    fn_measure = basename(commonprefix(filenames))
    fn_measure = myutils.strip_from_end(fn_measure, '.h5')
    fn_measure = myutils.strip_from_end(fn_measure, '-type')

    def cachelocation(g):
        path = posixpath.join(
            'FileCS_' + myutils.checksum(basename(g.file.filename)),
            g.name.strip(posixpath.sep))
        return (f_measure, path)

    if with_o2:
        fn_measure = myutils.strip_from_end(fn_measure, '_detailedpo2')

    files = [h5files.open(fn, 'a') for fn in filenames]
    f_measure = h5files.open('plotVessels_chache.h5', 'a', search=False)
    groups = list(
        itertools.chain.from_iterable(
            myutils.walkh5(f, pattern, return_h5objects=True) for f in files))
    if len(groups) <= 0:
        print 'no matching groups in hdf file(s)'
        sys.exit(0)

    if with_o2:
        name = posixpath.commonprefix(map(lambda g: g.name, groups))
        name = myutils.strip_from_start(name, '/po2/vessels').replace('/', '-')
        fn_measure += name

    with mpl_utils.PdfWriter(fn_measure + '.pdf') as pdfpages:
        rc = matplotlib.rc
        rc('font', size=8.)
        rc('axes', titlesize=10., labelsize=8.)

        if with_o2:
            import detailedo2Analysis as o2analysis
            import detailedo2Analysis.plotsForPaper
            import detailedo2
            dataman = myutils.DataManager(20, [
                o2analysis.DataDetailedPO2(),
                analyzeGeneral.DataTumorTissueSingle(),
                analyzeGeneral.DataDistanceFromCenter(),
                analyzeGeneral.DataBasicVessel(),
                analyzeGeneral.DataVesselSamples(),
                analyzeBloodFlow.DataTumorBloodFlow(),
                analyzeGeneral.DataVesselRadial(),
                analyzeGeneral.DataVesselGlobal()
            ])

            vesselgroups = list(
                detailedo2.OpenVesselAndTumorGroups(g)[0] for g in groups)
            #original_vesselgroups = list(h5files.openLink(g, 'SOURCE') for g in vesselgroups)
            if 1:
                PrintGlobalDataWithOxygen(pdfpages, groups, vesselgroups,
                                          f_measure, dataman)
                '''FormatParameters makes the network creation parameters
            that does not work, if we have an o2 file'''
                #text = FormatParameters(original_vesselgroups[0].file)
                text = [' ']
                text += detailedo2Analysis.plotsForPaper.FormatParameters(
                    groups[0])
                fig, _ = mpl_utils.MakeTextPage(text,
                                                figsize=(mpl_utils.a4size[0],
                                                         mpl_utils.a4size[0]))
                pdfpages.savefig(fig, postfix='_vesselsparams')
            if 1:
                res = getMultiScatter(300. * len(filenames), vesselgroups)
                plotMultiScatterBeauty(res, pdfpages)

        else:
            dataman = myutils.DataManager(20, [
                analyzeGeneral.DataTumorTissueSingle(),
                analyzeGeneral.DataVesselRadial(),
                analyzeGeneral.DataDistanceFromCenter(),
                analyzeBloodFlow.DataTumorBloodFlow(),
                analyzeGeneral.DataBasicVessel(),
                analyzeGeneral.DataVesselSamples(),
                analyzeGeneral.DataVesselGlobal()
            ])
            #dataman = myutils.DataManager(20, [ analyzeGeneral.DataBasicVessel(), analyzeGeneral.DataVesselSamples(), analyzeGeneral.DataVesselGlobal()])
            vesselgroups = groups

            if 0:
                res = getMultiScatter(300. * len(filenames), vesselgroups)
                plotMultiScatterBeauty(res, pdfpages)
            if 0:
                PlotRadiusHistogram2(dataman, vesselgroups, pdfpages)

            if 0 and all(map(lambda g: 'data' in g.parent, vesselgroups)):
                data = VesselData()
                for g in vesselgroups:
                    data.add(g.parent['data'])
                plot_topological_stats_avg(data, pdfpages)
            if 0:  #reproduce swine
                plot_geometric_stuff_on_RC(dataman, f_measure, filenames,
                                           options, pdfpages)
            if 1:
                PrintGlobalData(pdfpages, vesselgroups, f_measure, dataman)
Beispiel #3
0
def PrintGlobalData(pdfpages, vesselgroups, f_measure, dataman):
    from analyzeBloodVolumeSimple import cylinderCollectionVolumeDensity
    import detailedo2Analysis.plotsForPaper
    import detailedo2

    sample_length = detailedo2Analysis.plotsForPaper.sample_length

    def cachelocation(g):
        path = posixpath.join(
            'FileCS_' + myutils.checksum(basename(g.file.filename)),
            g.name.strip(posixpath.sep))
        return (f_measure, path)

    f2l = myutils.f2l

    bbox_vessels = list()
    data_by_name = collections.defaultdict(list)
    #prop_list = ['mvd', 'mvd_a', 'mvd_v', 'mvd_c', 'rBV', 'rBV_a', 'rBV_v', 'rBV_c', 'venous_rBV_fraction', 'rBF', 'meanCapillaryDistance', 'mean_r']
    #prop_list = ['rBV',  'rBF', ]
    #prop_list = 'phi_a phi_v phi_c mvd_a mvd_v mvd_c mean_r'.split()

    prop_list2 = [
        'radius', 'shearforce', 'velocity', 'flow', 'avg_cap_dist',
        'mvd_linedensity', 'mvd_sphere_sampling', 'mvd', 'mvd_a', 'mvd_v',
        'mvd_c', 'phi_vessels', 'phi_a', 'phi_v', 'phi_c', 'total_perfusion'
    ]
    #prop_list2 = ['radius','shearforce','velocity','flow','avg_cap_dist','mvd_linedensity','phi_vessels']
    prop_data_vessel_global = ['mvd']
    try:
        os.remove('initialvesseldata.h5')
    except OSError:
        pass
        with h5py.File('initialvesseldata.h5', 'w-') as f:
            for k, v in data_by_name.iteritems():
                f.create_dataset(k, data=v)

    result_string = []

    for name in prop_list2:
        data = []
        for gvessels in vesselgroups:
            data.append(
                dataman.obtain_data('basic_vessel_global', name, gvessels,
                                    cachelocation(gvessels)))
            ld_vessels = krebsutils.read_lattice_data_from_hdf(
                gvessels['lattice'])
            bbox_vessels.append(ld_vessels.worldBox)
        result_string.append(r'$<%s>$ = $%s$%s' %
                             (Prettyfier.get_sym(name), Format(
                                 name, data), Prettyfier.get_munit(name)))

    ld = krebsutils.read_lattice_data_from_hdf(vesselgroups[0]['lattice'])
    bbox_vessels.append(ld.worldBox)

    bbox_vessels = np.average(bbox_vessels, axis=0).reshape(3, 2).transpose()
    result_string += ['Vessel System Bounding Box'] + list(fmt_(bbox_vessels))

    fig, _ = mpl_utils.MakeTextPage(result_string,
                                    figsize=(mpl_utils.a4size[0],
                                             mpl_utils.a4size[0]))
    pdfpages.savefig(fig, postfix='_vesselsglobal')
Beispiel #4
0
def PrintGlobalDataWithOxygen(pdfpages, po2groups, vesselgroups, f_measure,
                              dataman):
    from analyzeBloodVolumeSimple import cylinderCollectionVolumeDensity
    import detailedo2Analysis.plotsForPaper
    import detailedo2

    sample_length = detailedo2Analysis.plotsForPaper.sample_length

    def cachelocation(g):
        path = posixpath.join(
            'FileCS_' + myutils.checksum(basename(g.file.filename)),
            g.name.strip(posixpath.sep))
        return (f_measure, path)

    f2l = myutils.f2l

    bbox_vessels = list()
    bbox_field = list()
    data_by_name = collections.defaultdict(list)
    O2_prop_list = [
        'po2', 'sat', 'sat_via_hb_ratio', 'gtv', 'jtv', 'mro2', 'po2_tissue',
        'chb', 'chb_oxy', 'chb_deoxy', 'oef', 'e1', 'e3', 'Jin_root',
        'Jout_root', 'Jout_tv', 'Jout_cons', 'sat_vein', 'sat_art', 'sat_capi'
    ]
    prop_list = [
        'mvd', 'mvd_a', 'mvd_v', 'mvd_c', 'rBV', 'rBV_a', 'rBV_v', 'rBV_c',
        'venous_rBV_fraction', 'rBF', 'meanCapillaryDistance', 'mean_r'
    ]
    #prop_list2 = ['shearforce', 'velocity']
    #prop_list2 = ['velocity']

    for po2group in po2groups:
        for prop in O2_prop_list:
            data = dataman.obtain_data('detailedPO2_global', prop, po2group,
                                       sample_length, cachelocation(po2group))
            data_by_name[prop].append(data)
        ld = krebsutils.read_lattice_data_from_hdf(po2group['field_ld'])
        bbox_field.append(ld.worldBox)
        gvessels, _ = detailedo2.OpenVesselAndTumorGroups(po2group)
        #    for prop in prop_list2:
        #      data = dataman.obtain_data('basic_vessel_global', prop, gvessels, cachelocation(gvessels))
        #      data_by_name[prop].append(data)
        ld = krebsutils.read_lattice_data_from_hdf(gvessels['lattice'])
        bbox_vessels.append(ld.worldBox)
        rbv, a, v, c = cylinderCollectionVolumeDensity(gvessels)
        sa, sv, sc = data_by_name['sat_art'][-1], data_by_name['sat_vein'][
            -1], data_by_name['sat_capi'][-1]
        data_by_name['sat_estimated_by_acv'].append(
            (sa * a + sv * v + sc * c) / rbv)
    O2_prop_list.append('sat_estimated_by_acv')

    try:
        os.remove('initialvesseldata.h5')
    except OSError:
        pass
    with h5py.File('initialvesseldata.h5', 'w-') as f:
        for k, v in data_by_name.iteritems():
            f.create_dataset(k, data=v)

#  def fmt_(v, exponent=None, multi=1.):
#    avg, std = np.average(v, axis=0), np.std(v, axis=0)
#    if exponent is not None:
#      exponent_str = ('\,10^{%i}' % exponent) if exponent<>0 else ''
#      f = math.pow(10., exponent)
#      s = r'%s \pm %s%s' % (f2l(avg/f*multi, exponential=False), f2l(std/f*multi, exponential=False), exponent_str)
#    else:
#      s = r'%s \pm %s\,' % (f2l(avg*multi), f2l(std*multi))
#    return s

    result_string = []
    for name, v in myutils.iterate_items(data_by_name, O2_prop_list):
        result_string.append(r'$<%s>$ = $%s$%s' %
                             (Prettyfier.get_sym(name), Format(
                                 name, v), Prettyfier.get_munit(name)))

#  text = FormatGeometricAndPerfusionData()

    prop_list2 = ['avg_cap_dist', 'mvd_linedensity']
    #bbox_vessels = list()
    #bbox_field   = list()

    for name in prop_list2:
        data = []
        for gvessels in vesselgroups:
            data.append(
                dataman.obtain_data('basic_vessel_global', name, gvessels,
                                    cachelocation(gvessels)))
            #ld = krebsutils.read_lattice_data_from_hdf(vesselgroups[0]['lattice'])
            #bbox_vessels.append(ld.worldBox)
        result_string.append(r'$<%s>$ = $%s$%s' %
                             (Prettyfier.get_sym(name), Format(
                                 name, data), Prettyfier.get_munit(name)))

#""" some problems still exists here
#need field_ld which in not consistently stored"""
#  mvd_exp=[]
#  for (gvessels,po2group) in zip(vesselgroups,po2groups):
#    ld = krebsutils.read_lattice_data_from_hdf(po2group['field_ld'])
#    mvd_sampling_results, mvd_bins = dataman.obtain_data('sphere_vessel_density',  gvessels, None, suggest_bins_from_world(ld), 'radial', ld, cachelocation(gvessels))
#    mvd_exp.append(np.mean(np.asarray(mvd_sampling_results)*1e6))
#
#  result_string.append(r'$<%s>$ = $%s$%s' %
#    (Prettyfier.get_sym('mvd_exp'), Format('mvd_exp', mvd_exp), Prettyfier.get_munit('mvd_exp')))

    bbox_vessels = np.average(bbox_vessels, axis=0).reshape(3, 2).transpose()
    bbox_field = np.average(bbox_field, axis=0).reshape(3, 2).transpose()
    result_string += ['Vessel System Bounding Box'] + list(
        fmt_(bbox_vessels)) + ['FD-Grid Bounding Box'] + list(fmt_(bbox_field))

    fig, _ = mpl_utils.MakeTextPage(result_string,
                                    figsize=(mpl_utils.a4size[0],
                                             mpl_utils.a4size[0]))
    pdfpages.savefig(fig, postfix='_vesselsglobal')

    fig = matplotlib.figure.Figure(figsize=(mpl_utils.a4size[0] * 0.5,
                                            mpl_utils.a4size[0] * 0.5))
    ax = fig.add_axes([0.1, 0.2, 0.8, 0.75])
    for po2group in po2groups:
        iterations = detailedo2Analysis.plotsForPaper.GetConvergenceData(
            po2group)
        x = iterations['iteration']
        y = iterations['delta_vessM'] + iterations['delta_fieldM']
        ax.plot(x, y)
    ax.set(yscale='log',
           xlabel='iterations',
           ylabel=r'$|p(new) - p(old)|_\infty$')
    pdfpages.savefig(fig, postfix='_convergence')
Beispiel #5
0
def DoIt(filenames, options):
  fn_measure = basename(commonprefix(filenames))
  fn_measure = myutils.strip_from_end(fn_measure, '.h5')
  fn_measure = myutils.strip_from_end(fn_measure, '-type')

  f_measure = h5files.open('adaption_common.h5', 'a', search = False)
  
  files = [h5files.open(fn, 'r') for fn in filenames]
  
  groups_with_adaption = [f['/vessels_after_adaption'] for f in files]
  #this data is stored with the adaption
  files_without_adaption = []
  for afile in files:
    completeFilename=str(afile['/vessels_after_adaption/parameters'].attrs['cwd'])+'/'+str(afile['/vessels_after_adaption/parameters'].attrs['vesselFileName'])
    files_without_adaption.append(
        h5files.open(completeFilename))
    
  groups_without_adaption = [f['vessels'] for f in files_without_adaption]

  with mpl_utils.PdfWriter('adaption_' + fn_measure+'.pdf') as pdfpages:
    import analyzeGeneral
    dataman = myutils.DataManager(20, [ analyzeGeneral.DataBasicVessel(), analyzeGeneral.DataVesselSamples(), analyzeGeneral.DataVesselGlobal()])
#    vesselgroups_without = groups_without_adaption
#    vesselgroups_with = groups_with_adaption
    
    geometric_data_before = getGeometricData(groups_without_adaption)
    perfusion_data_before = getTotalPerfusion(groups_without_adaption)*60
    
    geometric_data_after = getGeometricData(groups_with_adaption)
    perfusion_data_after = getTotalPerfusion(groups_with_adaption)*60
    
    if 1:
      res_without = getMultiScatter(300. * len(filenames), groups_without_adaption)
      plotMultiScatterBeauty(res_without, pdfpages)
      res_with = getMultiScatter(300. * len(filenames), groups_with_adaption)
      plotMultiScatterBeauty(res_with, pdfpages, withRadiusPressureRelation=False)
    
#    if 0:
#      PlotRadiusHistogram2(dataman, groups_without_adaption, pdfpages)
#      PlotRadiusHistogram2(dataman, groups_with_adaption, pdfpages)
#  
    if 0:
      #printBarPlot_rBV(filenames, pdfpages) -->alt
      printBarPlot_rBV(dataman, f_measure, filenames, options, pdfpages) #-->mw enginered
      
    if 0:  
      printBarPlot_rBF(dataman, f_measure, filenames, options, pdfpages)
    
    if 0:
      PlotRadiusHistogram_with_cache_by_RC(dataman, f_measure, filenames, options, pdfpages)
      #PlotRadiusHistogram_with_cache(dataman, f_measure, filenames, options, pdfpages)
    
    if 0:
      printBarPlot_vesseltype_on_root_node_configuration(filenames, pdfpages)
    
    ## is it worth to update this ???
    if 1:
      #importing the murray thing
      #DoGetMurray(filenames, pdfpages)
      printMurray(dataman, f_measure, filenames, options, pdfpages)
    if 1:      
      printMurray_alphas_effective(dataman, f_measure, filenames, options, pdfpages)
    if 1:
      text = ["Before adaption"]
      text2 = FormatGeometricAndPerfusionData(geometric_data_before, perfusion_data_before)
      text = text+text2
      def cachelocation(g):
        path = posixpath.join('FileCS_'+myutils.checksum(basename(g.file.filename)), g.name.strip(posixpath.sep))
        return (f_measure, path)

      prop_list2 = ['shearforce', 'velocity']        
      for name in prop_list2:
        data = []
        for gvessels in groups_without_adaption:
          data.append(dataman.obtain_data('basic_vessel_global', name, gvessels, cachelocation(gvessels)))
        text.append(r'$<%s>$ = $%s$%s' %
          (Prettyfier.get_sym(name), Format(name, data), Prettyfier.get_munit(name)))
    
      fig, _ = mpl_utils.MakeTextPage(text,figsize = (mpl_utils.a4size[0]*0.8, mpl_utils.a4size[0]*0.8))
      pdfpages.savefig(fig, postfix='_vesselsglobal')

      text = FormatParameters(groups_without_adaption[0].file)
      fig, _ = mpl_utils.MakeTextPage(text,figsize = (mpl_utils.a4size[0]*0.8, mpl_utils.a4size[0]*0.8))
      pdfpages.savefig(fig, postfix='_vesselsparams')
    

      text = ["After adaption"]
      text2 = FormatGeometricAndPerfusionData(geometric_data_after, perfusion_data_after)
      text = text+text2
      def cachelocation(g):
        path = posixpath.join('FileCS_'+myutils.checksum(basename(g.file.filename)), g.name.strip(posixpath.sep))
        return (f_measure, path)

      prop_list2 = ['shearforce', 'velocity']        
      for name in prop_list2:
        data = []
        for gvessels in groups_with_adaption:
          data.append(dataman.obtain_data('basic_vessel_global', name, gvessels, cachelocation(gvessels)))
        text.append(r'$<%s>$ = $%s$%s' %
          (Prettyfier.get_sym(name), Format(name, data), Prettyfier.get_munit(name)))
    
      fig, _ = mpl_utils.MakeTextPage(text,figsize = (mpl_utils.a4size[0]*0.8, mpl_utils.a4size[0]*0.8))
      pdfpages.savefig(fig, postfix='_vesselsglobal')

      text = FormatParameters(groups_without_adaption[0].file)
      fig, _ = mpl_utils.MakeTextPage(text,figsize = (mpl_utils.a4size[0]*0.8, mpl_utils.a4size[0]*0.8))
      pdfpages.savefig(fig, postfix='_vesselsparams')
    
        
    if 0 and all(map(lambda g: 'data' in g.parent, groups_without_adaption)):
      data = VesselData()
      for g in groups_without_adaption:
        data.add(g.parent['data'])
      plot_topological_stats_avg(data, pdfpages)
      
    if 1:
      #PlotQdevs_unnormalized(filenames, pdfpages)
      PlotQdevs_normalized(filenames, options, pdfpages)