def run(goodArguments):
  print('starting with arguments: %s' % goodArguments)
  no_files = len(goodArguments.oxygenFiles)
  
  hypoxicVolumes=[]
  tumorVolumes=[]
  threshold = 15
  test = myutils.MeanValueArray.empty()
  for aFile in goodArguments.oxygenFiles:
    with h5files.open(aFile.name) as f:
      try:
        if not 'po2' in f:
          raise AssertionError('no proper oxygen file: %s!'%f)
      except Exception, e:
        print e.message
        sys.exit(-1)
      paths = myutils.walkh5(f, 'po2/out*')
      print('found paths: %s' % paths)
      hypoxicVolumes_per_time=[]
      tumorVolumes_per_time=[]
      timepoints=[]
      for path in paths:
        hypoxicFraction,hypoxicTissueVolume = estimate_ratio_hypoxic(f[path], threshold)
        hypoxicVolumes_per_time.append(hypoxicTissueVolume)      
        t=f[path]['SOURCE'].attrs['time']
        r=f[path]['SOURCE/tumor'].attrs['TUMOR_RADIUS']
        volume=4/3.*3.1414*r*r*r/1e9
        tumorVolumes_per_time.append(volume)
        timepoints.append(t)
      hypoxicVolumes.append(hypoxicVolumes_per_time)
      tumorVolumes.append(tumorVolumes_per_time)
 def __init__(self, dataman, filenames, pattern):
     files = [h5files.open(fn, 'r+') for fn in filenames]
     items = []
     has_tumor = True
     for f in files:
         paths = myutils.walkh5(f['.'], pattern)
         for path in paths:
             g = f[path]
             if g.attrs.get('CLASS', None) == 'GRAPH':
                 gvessels = g
                 gtumor = None
                 try:
                     source = h5files.openLink(g, 'SOURCE')
                     gtumor = source.parent['tumor']
                     g = source.parent
                 except Exception, e:
                     raise RuntimeError(
                         'tried to get tumor data but failed:' + str(e))
             else:
                 gvessels, gtumor = g['vessels'], (g['tumor'] if 'tumor'
                                                   in g else None)
             e = EnsembleItem(path=path,
                              gvessels=gvessels,
                              gtumor=gtumor,
                              group=g)
             e.time = g.attrs['time']
             has_tumor = has_tumor and gtumor is not None
             e.vessel_system_length = dataman.obtain_data(
                 'vessel_system_length', gvessels)
             items.append(e)
Exemple #3
0
def DoIt(inputFileNames, pattern, options):
    for this_enlargement_factor in options.enlarge_factor:
        inFiles = [h5files.open(fn, 'r') for fn in inputFileNames]

        inGroups = list(
            itertools.chain.from_iterable(
                myutils.walkh5(f, pattern, return_h5objects=True)
                for f in inFiles))
        if len(inGroups) <= 0:
            print 'no matching groups in hdf file(s)'
            sys.exit(0)
        for in_file in inputFileNames:
            bloodflowparams = krebsutils.pyDictFromParamGroup(
                h5files.open(in_file, 'r')['parameters/calcflow'])
            #enlarge_vessels(float(this_enlargement_factor),in_file, bloodflowparams)

            qsub.submit(
                qsub.func(enlarge_vessels, float(this_enlargement_factor),
                          in_file, bloodflowparams),
                name='job_modify_enlarge_' + str(this_enlargement_factor) +
                '_',
                num_cpus=6,
                days=
                5,  # about one day per thread, the idea being that number of threads is proportional to systems size and runtime is 
                mem='%iMB' % (1000),
                change_cwd=True)
Exemple #4
0
def PlotQdevs_unnormalized(filenames, pdfpages):
  #this is from
  # http://matplotlib.org/examples/axes_grid/inset_locator_demo.html
  from mpl_toolkits.axes_grid1.inset_locator import inset_axes, zoomed_inset_axes
  from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
  qdevVector=[]
  plt = pyplot
  
  if(options.two):
    typelist = 'typeD- typeE- typeG- typeH-'
  else:
    typelist = 'typeA- typeB- typeC- typeF- typeI-'
  if(options.all_types):
    typelist = 'typeA- typeB- typeC- typeD- typeE- typeF- typeG- typeH- typeI-'
  if(options.single):
    typelist = 'typeF- '
    
  for t in typelist.split():
    fig= plt.figure()
    ax1 = plt.axes()
    ax1.set_title(t)
    fig.add_subplot(ax1)
    print('Qdev for type: %s' % t)
     
    filteredFiles = filter( lambda fn: t in fn,filenames)
    files = [h5files.open(fn, 'r+') for fn in filteredFiles]
    groups_with_adaption = list(itertools.chain.from_iterable(myutils.walkh5(f, 'adaption/vessels_after_adaption', return_h5objects=True) for f in files))
    
    for agroup in groups_with_adaption:
      print("filename: %s" % agroup.file.filename)
      qdev_vec = np.zeros(2999)
      qdev_vec_read = np.asarray(agroup['qdev'])
      qdev_vec_read = np.sqrt(qdev_vec_read)
      qdev_vec[0:len(qdev_vec_read)]= qdev_vec_read
      
      if 'qdev_mat' not in locals():
        qdev_mat = qdev_vec
      else:
        qdev_mat = np.vstack((qdev_mat, qdev_vec))
    average_for_type = np.average(qdev_mat,0)
    std_for_type = np.std(qdev_mat,0)
    print("len avg: %i, len std: %i" % (len(average_for_type),len(std_for_type)))
    ax1.errorbar(np.arange(len(average_for_type)),average_for_type, yerr= std_for_type, errorevery=20)
    #ax1.set_yscale('log')    
    ax1.set_ylabel(r'$\sum_{i=1}^{no. vessels} (r_i-r_{i-1})^2$')    
    ax1.set_xlabel('Iteration')

    axins = inset_axes(ax1,
                   width="70%",  # width = 30% of parent_bbox
                   height="30%",  # height : 1 inch
                   loc=1)
    axins.errorbar(np.arange(len(average_for_type))[-1500:],average_for_type[-1500:], yerr= std_for_type[-1500:], errorevery=20) 
    #axins.set_yscale('log')    
    ax1.grid()    
        
    del qdev_mat
    del average_for_type
    del std_for_type
    plt.tight_layout()
    pdfpages.savefig(fig)
Exemple #5
0
def run(parameter_set_name, filenames, grp_pattern, systemsize):
    print 'submitting ...', parameter_set_name
    print dicttoinfo.dicttoinfo(getattr(parameterSetsO2, parameter_set_name))
    print 'for files', filenames

    dirs = set()
    for fn in filenames:
        with h5py.File(fn, 'r') as f:
            d = myutils.walkh5(f, grp_pattern)
            assert len(d), 'you f****d up, pattern "%s" not found in "%s"!' % (
                grp_pattern, fn)
            dirs = set.union(dirs, d)
    print 'and resolved groups therein: %s' % ','.join(dirs)

    o2params = getattr(parameterSetsO2, parameter_set_name)
    o2params['name'] = parameter_set_name
    if callable(o2params):
        o2paramsList = o2params(len(filenames))
    else:
        o2paramsList = itertools.repeat(o2params)

    for (o2params, fn) in zip(o2paramsList, filenames):
        o2params, num_threads = prepareParametersWithNumThreads(
            copy.deepcopy(o2params), systemsize)
        qsub.submit(
            qsub.func(worker_on_client, fn, grp_pattern, o2params),
            name='job_o2_' + parameter_set_name + '_' + basename(fn),
            num_cpus=num_threads,
            days=
            5,  # about one day per thread, the idea being that number of threads is proportional to systems size and runtime is 
            mem='%iMB' % (2000 * num_threads),
            change_cwd=True)
Exemple #6
0
def delete_items(fn, pattern, opts):
    with h5py.File(fn, 'r+') as f:
        items = myutils.walkh5(f, pattern)
        for item in items:
            print '%s:%s' % (fn, item),
            if not opts.dry and check_allow_delete(item, opts):
                del f[item]
            print '\n',
Exemple #7
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()
    ])
    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), 'you f****d up, 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]
                    ldvessels = ku.read_lattice_data_from_hdf(
                        vesselgroup['lattice'])
                    fieldld = ku.SetupFieldLattice(ldvessels.worldBox, 3, 10,
                                                   0.)
                    phi_vessels = krebs.analyzeGeneral.CalcPhiVessels(
                        dataman,
                        vesselgroup,
                        fieldld,
                        scaling=1.,
                        samples_per_cell=5)
                    print('bla')
                    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')
Exemple #8
0
 def __init__(self, dataman, filenames, pattern):
     files = [h5files.open(fn, 'r+') for fn in filenames]
     items = []
     has_tumor = True
     for f in files:
         paths = myutils.walkh5(f['.'], pattern)
         for path in paths:
             po2group_w_a = f[path + '/vessels_after_adaption']
             gvessels_w_a, gtumor = detailedo2.OpenVesselAndTumorGroups(
                 po2group_w_a)
             po2group_no_a = f[path + '/recomputed']
             gvessels_no_a, gtumor = detailedo2.OpenVesselAndTumorGroups(
                 po2group_no_a)
             e = EnsembleItem(path=path,
                              po2group_w_a=po2group_w_a,
                              gvessels_w_a=gvessels_w_a,
                              po2group_no_a=po2group_no_a,
                              gvessels_no_a=gvessels_no_a,
                              gtumor=gtumor)
             #        if 'SOURCE' in po2group:
             #          source = h5files.openLink(po2group, 'SOURCE')
             #          if 'time' in source.attrs.keys():
             #            t = source.attrs['time']
             #            e.time = t
             has_tumor = has_tumor and gtumor is not None
             e.vessel_system_length = dataman.obtain_data(
                 'vessel_system_length', gvessels_no_a)
             e.initialVesselType = GetVesselTypeLabel(po2group_no_a)
             items.append(e)
     if has_tumor:
         d = collections.defaultdict(list)  # path -> list of EnsembleItem
         for e in items:
             d[e.path].append(e)
         tumor_snapshot_times = dict(
             (k, np.average(map(lambda e: e.time, v)))
             for k, v in d.items())
         tumor_snapshot_order = sorted(
             tumor_snapshot_times.keys(),
             key=(lambda path: tumor_snapshot_times[path]))
         tumor_snapshots = [(d[path], path, tumor_snapshot_times[path])
                            for path in tumor_snapshot_order]
     self.files = files
     self.items = items
     if has_tumor:
         self.tumor_snapshots = tumor_snapshots  # list of tuple(items, path, time)
         self.has_tumor = has_tumor
     self.o2ConfigName = set(
         item.po2group_no_a.attrs.get('O2_CONFIG_NAME', None)
         for item in items)
     if len(self.o2ConfigName) != 1:
         raise RuntimeError(
             "Detected different O2_CONFIG_NAMES %s. You don't want to mix configurations, do you?"
             % self.o2ConfigName)
     self.o2ConfigName = self.o2ConfigName.pop()
Exemple #9
0
def PlotQdevs_normalized(filenames, options, pdfpages):
  #this is from
  # http://matplotlib.org/examples/axes_grid/inset_locator_demo.html
  from mpl_toolkits.axes_grid1.inset_locator import inset_axes, zoomed_inset_axes
  from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
  qdevVector=[]
  plt = pyplot
  
  if(options.two):
    typelist = 'typeD- typeE- typeG- typeH-'
  else:
    typelist = 'typeA- typeB- typeC- typeF- typeI-'
  if(options.all_types):
    typelist = 'typeA- typeB- typeC- typeD- typeE- typeF- typeG- typeH- typeI-'
  if(options.single):
    typelist = 'typeF- '
    
  for t in typelist.split():
    fig= plt.figure()
    ax1 = plt.axes()
    ax1.set_title(t)
    fig.add_subplot(ax1)
    print('Qdev for type: %s' % t)
     
    filteredFiles = filter( lambda fn: t in fn,filenames)
    files = [h5files.open(fn, 'r+') for fn in filteredFiles]
    groups_with_adaption = list(itertools.chain.from_iterable(myutils.walkh5(f, 'adaption/vessels_after_adaption', return_h5objects=True) for f in files))

    if(len(groups_with_adaption)==0):
      continue
    
    for agroup in groups_with_adaption:
      print("filename: %s" % agroup.file.filename)
      vessel_count = agroup['edges'].attrs['COUNT']
      nqdev_vec = np.zeros(300)
      nqdev_vec_read = np.asarray(agroup['nqdev'])
      if(len(nqdev_vec_read)<300):
        nqdev_vec[0:len(nqdev_vec_read)] = nqdev_vec_read
      else:
        continue

      if 'nqdev_mat' not in locals():
        nqdev_mat = nqdev_vec
      else:
        nqdev_mat = np.vstack((nqdev_mat, nqdev_vec))
  
    ax1.plot(nqdev_mat.transpose())
    ax1.set_ylabel(r'$\frac{1}{N}\sqrt{\sum_{i=1}^{no. vessels} (r_i-r_{i-1})^2}$')
    ax1.set_yscale('log')
    ax1.set_xlabel('Iteration')
  
    ax1.grid()    
    #plt.tight_layout()
    pdfpages.savefig(fig)
Exemple #10
0
def getMaxConcentration(f):
  if 'max_conc' not in f.attrs.keys():
    max_conc=0.0
    paths = myutils.walkh5(f['.'], 'out*')
    for path in paths:
      if 'conc' in f[path]:
        current_max = np.amax(f[path+'/conc'])
        if current_max > max_conc:
          max_conc=current_max
    print('found max conc over time: %f'%max_conc)
    #f.attrs.create('max_conc',max_conc)
  return max_conc
 def __init__(self, f, dataman, pattern=None):
     self.f = f
     self.dataman = dataman
     self.obtain_data = lambda *args: self.dataman.obtain_data(
         args[0], f, *args[1:])
     self.get_group_time = lambda q: self.obtain_data('time', q)
     groups = myutils.getTimeSortedGroups(f['/'], 'out', key='time')
     groups = [posixpath.basename(group.name) for group in groups]
     if pattern:
         s = set(myutils.walkh5(f, pattern))
         groups = [g for g in groups if g in s]
     if len(groups) == 0:
         raise ValueError('There is no tumor group found in this file!')
     self.groupnames = groups
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.DataBasicVessel()
    ])

    #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), 'you f****d up, pattern "%s" not found in "%s"!' % (
                grp_pattern, fn)
            dirs = set.union(dirs, d)
            for group_path in dirs:
                vesselgroup = f[group_path]
                ldvessels = ku.read_lattice_data_from_hdf(
                    vesselgroup['lattice'])
                fieldld = ku.SetupFieldLattice(ldvessels.worldBox, 3, 10, 0.)
                #fieldldFine = ku.SetupFieldLattice(fieldld.worldBox, 3, 50 / 10, 0.)
                phi_vessels = krebs.analyzeGeneral.CalcPhiVessels(
                    dataman,
                    f['iff/vessels'],
                    fieldld,
                    scaling=1.,
                    samples_per_cell=5)
                #a_abs = np.fabs(phi_vessels)
                #a_max = np.amax(a_abs)
                #n_phi = phi_vessels/a_max
                #visual =  (phi_vessels-phi_vessels.min())
                #inner1 = matplotlib.cm.gist_earth(n_phi)
                #inner2 = np.uint8(inner1*255)
                #result = Image.fromarray(inner2[:,:,:,0:2],mode='RGB')
                #result.save('out.tiff')
                print('bla')
                import nibabel as nib
                new_image = nib.Nifti1Image(phi_vessels, affine=np.eye(4))
                new_image.to_filename('myni')
                #for i in np.arange(0,phi_vessels.shape[2]):
                #  plt.imsave('img/%03i.png' % i,phi_vessels[:,:,i])
                #scipy.io.savemat('test.mat', phi_vessels)
                #np.save('mydata.tiff',phi_vessels)
                #pydicom.
                #pydicom.write_file('mydata.dcm', phi_vessels)
                plt.imshow(phi_vessels[:, :, 20])
                plt.show()
def run_out_in_single_file(goodArguments):
    print('starting with arguments: %s' % goodArguments)
    no_files = len(goodArguments.oxygenFiles)

    annoxicVolumes = []
    hypoxicVolumes = []
    normoxicVolumes = []
    tumorVolumes = []
    #threshold = 15
    threshold1 = 0.1
    threshold2 = 2.5
    test = myutils.MeanValueArray.empty()

    annoxicVolumes_per_time = []
    hypoxicVolumes_per_time = []
    normoxicVolumes_per_time = []
    tumorVolumes_per_time = []
    timepoints = []

    for aFile in goodArguments.oxygenFiles:
        with h5files.open(aFile.name) as f:
            try:
                if not 'po2' in f:
                    raise AssertionError('no proper oxygen file: %s!' % f)
            except Exception, e:
                print e.message
                sys.exit(-1)
            paths = myutils.walkh5(f, 'po2/out*')
            print('found paths: %s' % paths)

            for path in paths:
                #hypoxicFraction,hypoxicTissueVolume = estimate_ratio_hypoxic(f[path], threshold)
                t = f[path]['SOURCE'].attrs['time']
                r = f[path]['SOURCE/tumor'].attrs['TUMOR_RADIUS']
                volume = 4 / 3. * 3.1414 * r * r * r / 1e9
                #volume=(2*r)*(2*r)*(2*r)/1e9
                tumorVolumes_per_time.append(volume)
                t = t / (3600.0 * 24)  #days
                timepoints.append(t)
                annoxicTissueVolume, hypoxicTissueVolume, normoxicTissueVolume = estimate_annoxic_hypoxic_normoxic(
                    f[path], r, threshold1, threshold2)
                annoxicVolumes_per_time.append(annoxicTissueVolume)
                hypoxicVolumes_per_time.append(hypoxicTissueVolume)
                normoxicVolumes_per_time.append(normoxicTissueVolume)
            annoxicVolumes.append(annoxicVolumes_per_time)
            hypoxicVolumes.append(hypoxicVolumes_per_time)
            normoxicVolumes.append(normoxicVolumes_per_time)
            tumorVolumes.append(tumorVolumes_per_time)
Exemple #14
0
def run(outdir,
        name,
        tumorfn,
        config,
        p=dict(),
        piff=dict(),
        pdrug=dict(),
        grp_pattern=''):
    group = grp_pattern
    dirs = set()
    for fn in filenames:
        with h5py.File(fn, 'r') as f:
            d = myutils.walkh5(f, grp_pattern)
            assert len(d), ' pattern "%s" not found in "%s"!' % (grp_pattern,
                                                                 fn)
            dirs = set.union(dirs, d)
    print 'and resolved groups therein: %s' % ','.join(dirs)

    c = deepcopy(config)
    #c.update(p)
    #c['iff'].update(piff)
    #c['ift'].update(pdrug)
    if 'adaption' in group:
        setvesselfile(c, tumorfn, group, '', None)
    else:
        setvesselfile(c, tumorfn, group, 'vessels', 'tumor')

    stripped_name = splitext(basename(tumorfn))[0]
    stripped_name = stripped_name[len('tum-'):]

    c['fn_out'] = join(outdir, name + '_' + stripped_name + '.h5')
    try:
        moviename = c['ift_measure']['moviefilename']
    except:
        pass
    else:
        c['ift_measure']['moviefilename'] = join(
            outdir, name + '_' + moviename + '_' + stripped_name + '.h5')

    print '----------------------------------------'
    print 'submitting %s with params' % c['fn_out']
    myutils.pprint(c)
    print '----------------------------------------'

    run_simple(c['fn_out'], c)
    print('---- run_simple returned ----')
Exemple #15
0
                      type="int")
    parser.add_option("--picz",
                      dest="picz",
                      help="make picz",
                      default=False,
                      action='store_true')
    options, args = parser.parse_args()
    filenames = args[:-2]
    pattern_before = args[-2]
    pattern_after = args[-1]
    print '-----------  looking for files  ----------'
    thegroups = []
    for filename in filenames:
        f = h5files.open(filename)
        print 'opened -- ', filename, '/',
        paths_before = myutils.walkh5(f['.'], pattern_before)
        paths_after = myutils.walkh5(f['.'], pattern_after)
        print paths_before, paths_after
        for path_before, path_after in zip(paths_before, paths_after):
            vesselgroup_before = f[path_before]
            vesselgroup_after = f[path_after]
            tumorgroup = analyzeGeneral.try_find_tumor_group_from_vesselgroup(
                vesselgroup_after)
            assert tumorgroup
            thegroups.append(
                [vesselgroup_before, vesselgroup_after, tumorgroup])

    prefix, suffix = myutils.splitcommonpresuffix(
        map(lambda s: basename(s), filenames))
    outputbasename, _ = splitext(prefix + suffix)
Exemple #16
0
def DiffRadiusToConsCoeff(rd, kd):
  return kd/(rd*rd)

chb_of_rbcs = 340./64458. # in mol/l, entspricht 34 g/dl


def doit(fn, pattern, (parameters, parameters_name)):
  #fnpath = dirname(fn)
  fnbase = basename(fn).rsplit('.h5')[0]
  #outfn_no_ext = join(fnpath, fnbase+'_detailedpo2')

  output_links = []

  with h5py.File(fn, 'r') as f_in_doit:
    dirs = myutils.walkh5(f_in_doit['.'], pattern)
  
  for group_path in dirs:
    #cachelocation = (outfn_no_ext+'.h5', group_path+'_'+parameters_name)
    #output_file_name = 'o2_' + fnbase+'_'+parameters_name+'.h5'
    #vessel_path = group_path
    parameters['input_file_name'] = fn
    parameters['input_group_path'] = group_path
    parameters['output_file_name']='o2_' + fnbase+'_'+parameters_name+'.h5'
    #cachelocation = ('o2_' + fnbase+'_'+parameters_name+'.h5', group_path)
    computePO2(parameters)
    print('computed po2 stored in: %s' % parameters['output_file_name'])
    #output_links.append(ref)
  #return output_links
  
if __name__ == '__main__':
Exemple #17
0
    filenames = args[:-1]
    pattern = args[-1]
    datalist = map(lambda s: s, map(str.strip, options.datalist.split(',')))

    labels = {
        'flow': 'Flow Rate /nl',
        'shearforce': '$log_{10}$ Shear Force',
        'hematocrit': 'Hematocrit',
        'pressure': 'Blood Pressure',
        'flags': ' Flags',
    }

    for fn in filenames:
        f = h5py.File(fn, 'r')
        dirs = myutils.walkh5(f['.'], pattern)
        for d in dirs:
            filenamepostfix = ''
            vesselgroup = f[d]
            if (vesselgroup.attrs['CLASS'] == 'GRAPH'):
                worldbox = krebsutils.read_lattice_data_from_hdf(
                    vesselgroup['lattice']).GetWorldBox()
            if (vesselgroup.attrs['CLASS'] == 'REALWORLD'):
                pos = vesselgroup['nodes/world_pos']
                x_min = np.min(pos[:, 0])
                x_max = np.max(pos[:, 0])
                y_min = np.min(pos[:, 1])
                y_max = np.max(pos[:, 1])
                z_min = np.min(pos[:, 2])
                z_max = np.max(pos[:, 2])
                worldbox = np.asarray(
## ---------------- ------- -----------------------
## ---------------- ------- -----------------------
if __name__ == '__main__':
    filenames = sys.argv[1:-1]
    pattern = sys.argv[-1]

    files = [h5py.File(fn, 'r') for fn in filenames]

    dataman = myutils.DataManager(100, [
        krebs.analyzeGeneral.DataTumorTissueSingle(),
        krebs.analyzeGeneral.DataBasicVessel(),
        krebs.analyzeGeneral.DataDistanceFromCenter(),
        DataTumorBloodFlow()
    ])

    groupnames = myutils.walkh5(files[0], pattern, return_h5objects=False)
    allgroups = list(
        itertools.chain.from_iterable(
            (f[g] for g in groupnames) for f in files))

    # determination of storage file, copied from analyzeVesselsBulkTumor
    prefix, suffix = myutils.splitcommonpresuffix(
        map(lambda s: basename(s), filenames))
    outputbasename, _ = splitext(prefix + suffix)
    fn_measure = 'common-radial-cache.h5'
    f_measure = h5files.open(fn_measure, 'a')

    def cachelocation(g):
        path = posixpath.join(
            'FileCS_' + myutils.checksum(basename(g.file.filename)),
            g.name.strip(posixpath.sep))
Exemple #19
0
import h5py
import os, sys
import optparse  #Note: Deprecated since version 2.7: The optparse module is deprecated and will not be developed further; development will continue with the argparse module.
import numpy as np
import myutils

if __name__ == '__main__':
    parser = optparse.OptionParser(
        usage=
        'Usage: %prog FILENAME ... H5PATH ATTRIBUTE-NAME VALUE\n\nNote:H5PATH supports wildcards. VALUE is evaluated as python expression.'
    )
    options, args = parser.parse_args()
    filenames, pattern, attrname, attrdatastr = args[:-3], args[-3], args[
        -2], args[-1]

    data = eval(attrdatastr, {'os': os, 'np': np, 'sys': sys}, {})

    for fn in filenames:
        with h5py.File(fn, 'r+') as f:
            items = myutils.walkh5(f, pattern)
            items = [f[i] for i in items]
            for item in items:
                print '%s:%s.attrs["%s"] = %s' % (fn, item.name, attrname,
                                                  str(data))
                a = item.attrs
                try:
                    del a[attrname]
                except KeyError:
                    pass
                a[attrname] = data
Exemple #20
0
                        action="store_true")
    parser.add_argument("-s",
                        "--singel_type",
                        dest="single",
                        help="",
                        default=False,
                        action="store_true")
    goodArguments, otherArguments = parser.parse_known_args()

    try:
        dirs = set()
        for fn in goodArguments.vesselFileNames:
            if not os.path.isfile(fn.name):
                raise AssertionError('The file %s is not present!' % fn)
            with h5py.File(fn.name, 'r') as f:
                d = myutils.walkh5(f, goodArguments.grp_pattern)
                if not len(d) > 0:
                    raise AssertionError('pattern "%s" not found in "%s"!' %
                                         (grp_pattern, fn))
                else:
                    dirs = set.union(dirs, d)
    except Exception, e:
        print e.message
        sys.exit(-1)

    print('Resolved groups: %s' % ','.join(dirs))
    #create filename due to former standards
    filenames = []
    for fn in goodArguments.vesselFileNames:
        filenames.append(fn.name)
        ''' hack to center'''
Exemple #21
0
        default=False,
        action="store_true")
    options, args = parser.parse_args()
    if options.write_flow:
        options.force_flow_recompute = True

    dataman = myutils.DataManager(100, [DataBasicVessel()])

    filenames, pattern = args[:-1], args[-1]
    files = [
        h5files.open(fn, 'a' if options.add_resistor_bc else 'r+')
        for fn in filenames
    ]
    groups = list(
        itertools.chain.from_iterable(
            myutils.walkh5(f, pattern, return_h5objects=True) for f in files))

    for vesselgroup in groups:
        if options.force_flow_recompute and not options.add_resistor_bc:
            (pressure, flow,
             shearforce) = krebsutils.calc_vessel_hydrodynamics(vesselgroup)
            vessels = dataman.obtain_data('vessel_graph', vesselgroup,
                                          ['flags', 'radius'])
            vessels.edges['flow'] = flow
            vessels.nodes['pressure'] = pressure
        else:
            vessels = dataman.obtain_data(
                'vessel_graph', vesselgroup,
                ['flow', 'pressure', 'flags', 'radius'])

        def DoBC():
Exemple #22
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)
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''
#!/usr/bin/env python2
if __name__ == '__main__':
    import os.path, sys
    sys.path.append(
        os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))

from krebs.extractVtkFields import Extractor
import h5py
import os, sys
from os.path import basename, splitext
import myutils

if __name__ == '__main__':
    fn = sys.argv[1]
    fn, _ = myutils.splitH5PathsFromFilename(fn)
    grpnames = [sys.argv[2]]
    prefix = sys.argv[3] if len(sys.argv) >= 4 else ""
    f = h5py.File(fn, 'r')
    dirs = myutils.walkh5(f['.'], grpnames[0])
    print 'found items %s to search pattern %s' % (str(dirs), grpnames[0])
    for d in dirs:
        e = Extractor(f, [d], recursive=True)
        print 'found datasets:', e.getDatasetPaths(), 'in', d
        e.write("%s%s%s.vtk" % (prefix, splitext(
            basename(fn))[0], d.replace('/', '-') if d != '/' else ''))
Exemple #24
0
  #create filename due to former standards
  filenames=[]
  for fn in goodArguments.vesselFileNames:
    filenames.append(fn.name)
    
  datalist = map(lambda s: s, map(str.strip, goodArguments.datalist.split(',')))
  pattern   = goodArguments.grp_pattern
  
  '''check input'''
  try:
    dirs = set()
    for fn in goodArguments.vesselFileNames:
      if not os.path.isfile(fn.name):
        raise AssertionError('The file %s is not present!'%fn)
      with h5py.File(fn.name, 'r') as f:
        d = myutils.walkh5(f, goodArguments.grp_pattern)
        if not len(d)>0:
          raise AssertionError('pattern "%s" not found in "%s"!' % (grp_pattern, fn))
        else:
          dirs = set.union(dirs,d)
  except Exception, e:
    print e.message
    sys.exit(-1)

  for fn in filenames:
    fn, _ = myutils.splitH5PathsFromFilename(fn)
    f = h5py.File(fn, 'r')
    dirs = myutils.walkh5(f['/'], pattern)
    if goodArguments.outfn:
      print("you chose: %s as outfilename" % goodArguments.outfn)
      goodArguments.outfn = goodArguments.outfn + '_%s.vtk'
                        raise AssertionError('Unknown key %s in file\n' % key)
                    else:
                        setattr(goodArguments, key, params_from_file[key])

        for fn in filenames:
            if not os.path.isfile(fn):
                raise AssertionError('The file %s is not present!' % fn)
    except Exception, e:
        print e.message
        sys.exit(-1)
    """ create job"""
    jobs = []
    a = jobs.append
    for fn in filenames:
        with h5files.open(fn, 'r') as f:
            paths = myutils.walkh5(f, pattern)
            if goodArguments.plot_auc:
                path = myutils.walkh5(f, 'out0000')
                j = RenderJob(f, path[0], postfix, goodArguments)
            for path in paths:
                j = RenderJob(f, path, postfix, goodArguments)
                a(j)

    for job in jobs:
        t, m = job.runtime_and_mem
        print 'submit %s, %i mb, %f h' % (job.imageFilename, m, t)
        qsub.submit(qsub.func(clientfunc, job, os.getcwd()),
                    name='job_render_' + basename(job.imageFilename),
                    num_cpus=job.params.threads,
                    mem=('%iMB' % m),
                    days=0.001)
    parser.add_argument('-g', '--grp_pattern', default='*/vessels')
    parser.add_argument('--noPov', default=False, action='store_true')
    parser.add_argument('-v', '--movie', default=False, action='store_true')

    goodArguments, otherArguments = parser.parse_known_args()
    qsub.parse_args(otherArguments)
    dbg_fn = goodArguments.debug_filename.name
    options = getattr(krebs.povrayRenderSettings, 'dbg_vessels')
    f = h5py.File(dbg_fn, 'r')
    if not goodArguments.noPov:
        jobs = []
        a = jobs.append

        #for fn in filenames:
        with h5files.open(dbg_fn, 'r') as f:
            paths = myutils.walkh5(f, '*/vessels')
            for path in paths:
                j = RenderJob(f, path, '', options)
                a(j)

        for job in jobs:
            t, m = job.runtime_and_mem
            print('submit %s, %i mb, %f h' % (job.imageFilename, m, t))
            qsub.submit(qsub.func(clientfunc, job, os.getcwd()),
                        name='job_render_' + basename(job.imageFilename),
                        num_cpus=job.params['num_threads'],
                        mem=('%iMB' % m),
                        days=t / 24.)

    if goodArguments.movie:
        time.sleep(30)  #wait 30 seconds for the queing system to finish
Exemple #27
0
def printBarPlot_vesseltype_on_root_node_configuration(filenames, pdfpages):
  means_veins = []
  means_arteries = []
  means_capillaries = []
  
  std_veins = []
  std_arteries = []
  std_capillaries = []
  
  
  counter = 0
  for t in 'typeA- typeB- typeC- typeD- typeE- typeF- typeG- typeH- typeI-'.split():
    #print('rBF for type: %s' % t)
    filteredFiles = filter( lambda fn: t in fn,filenames)
  
    files = [h5files.open(fn, 'r+') for fn in filteredFiles]
    groups_of_type = list(itertools.chain.from_iterable(myutils.walkh5(f, 'adaption/recomputed', return_h5objects=True) for f in files))
    
    veins, arteries, capillaries = getVesselTypes(groups_of_type)
    
    #print("something: %i " % len(perfusion_data_without))
    #print("something: %i " % len(perfusion_data_with))
    if (len(veins)>0):
      means_veins.append( np.average(veins))
      std_veins.append( np.std(veins))
    
    if (len(arteries)>0):
      means_arteries.append( np.average(arteries))
      std_arteries.append( np.std(arteries))
      
    if (len(capillaries)>0):
      means_capillaries.append( np.average(capillaries))
      std_capillaries.append( np.std(capillaries))
    counter= counter +1
  
  print("means_arteries: %i " % len(means_arteries))
  print(means_arteries)
  print("means_veins: %i " % len(means_veins))
  print(means_veins)
  print("means_capillaries: %i " % len(means_capillaries))
  print(means_capillaries)
  if not counter == len(means_arteries):
    raise AssertionError("Not all configurations found!")
  plt = pyplot
  fig, ax = plt.subplots()
  index = np.arange(counter)
  bar_width = 0.25
  opacity = 0.4
  error_config = {'ecolor': '0.3'}
  rects1 = plt.bar(index, means_arteries, bar_width,
                 alpha=opacity,
                 color='r',
                 yerr=std_arteries,
                 error_kw=error_config,
                 label='Arteries')
  rects2 = plt.bar(index + bar_width, means_veins, bar_width,
                 alpha=opacity,
                 color='b',
                 yerr=std_veins,
                 error_kw=error_config,
                 label='Veins',
                 hatch="/")
  rects3 = plt.bar(index + 3*bar_width, means_capillaries, bar_width,
                 alpha=opacity,
                 color='y',
                 yerr=std_capillaries,
                 error_kw=error_config,
                 label='Capillaries',
                 hatch="/")

  plt.xlabel('Configuration')
  plt.ylabel(r'no. vessels')
  plt.title('Vessels per type and root node configuration')
  plt.xticks(index + bar_width, ('RC1', 'RC2', 'RC3', 'RC4', 'RC5', 'RC6', 'RC7', 'RC8', 'RC9'))
  #plt.legend(loc='best')
  lgd = ax.legend(loc='center left', bbox_to_anchor=(1,1))
  
  
  plt.tight_layout()
  pdfpages.savefig(fig, bbox_extra_artists=(lgd,), bbox_inches='tight')