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 boxcountTumorVessels(fn, opts): items_to_analyze = ['complete', 'withintumor'] with h5py.File(fn, 'r') as f: groups = myutils.getTimeSortedGroups(f['.'], "out") for g in groups[-1:]: data = {} print '%s : %s' % (fn, g.name) for item_to_analyze in items_to_analyze: calcBoxCounts(data, g['vessels'], item_to_analyze, opts) with myutils.MeasurementFile(fn, h5files, opts.prefix) as fdst: dstgroup = myutils.require_snapshot_group_(fdst, g) fdst.attrs.create('tumorcode_file_type', data='fractaldim_analyze_bulktum') #gdst = fdst.require_snapshot_group_(g) #gdst = gdst.recreate_group('fractaldim') #myutils.hdf_write_dict_hierarchy(gdst, '.', data) myutils.hdf_write_dict_hierarchy(dstgroup, 'fractaldim', data)
def getSortedTimeGroupNames(f): gg = myutils.getTimeSortedGroups(f['.'], "out") return [g.name for g in gg]
def plot_many(filenames, pdfpages): groups_by_time = defaultdict(list) files = [h5py.File(fn, 'r') for fn in filenames] for f in files: groups = myutils.getTimeSortedGroups(f['.']) for g in groups: groups_by_time[round(g.attrs['time'])].append(g) times = groups_by_time.keys() times.sort() times = np.asarray(times) radius, radius_std = averaged_global((groups_by_time, times), 'geometry/radius') volume, volume_std = averaged_global((groups_by_time, times), 'geometry/volume') sphere_equiv_radius, sphere_equiv_radius_std = averaged_global( (groups_by_time, times), 'geometry/sphere_equiv_radius') #sphere_equiv_radius, sphere_equiv_radius_std = averaged_global((groups_by_time, times), 'geometry/cylinder_equiv_radius') sphericity, sphericity_std = averaged_global((groups_by_time, times), 'geometry/sphericity') # estimate velocity data = np.asarray((times, radius)) if len(data[0]) > 1: from scipy.optimize import leastsq func = lambda p, x, y: (x * p[0] + p[1] - y) p, success = leastsq(func, (1, 0), args=(data[0], data[1])) velocity = p[0] print 'estimated velocity: %f' % velocity print 'fit params: %s' % str(p) else: velocity = 0. if 1: #plot by time fig, axes = pyplot.subplots(2, 2, figsize=(mastersize[0] * 0.5, mastersize[0] * 0.5)) mpl_utils.subplots_adjust_abs( fig, left=mpl_utils.mm_to_inch * 13, right=-mpl_utils.mm_to_inch * 5, top=-mpl_utils.mm_to_inch * 5, bottom=mpl_utils.mm_to_inch * 10, hspace=mpl_utils.mm_to_inch * 30, wspace=mpl_utils.mm_to_inch * 40, ) axes = axes.ravel() def plt_rad(ax): ax.set(ylabel='[mm]', xlabel='t [h]') mpl_utils.errorbar(ax, times, 1.e-3 * radius, yerr=1.e-3 * radius_std, label='r', lw=0., marker='x', color='k', markersize=5.) label = u'$r_0 + v_{fit} t$\n$v_{fit} = %s$ [\u03BCm/h]' % f2s( velocity) ax.plot(times, 1.e-3 * (p[1] + p[0] * times), label=label, color='r') ax.legend() #ax.text(0.6, 0.2, r'$v_{fit} = %s$' % f2s(velocity), transform = ax.transAxes) def plt_vol(ax): ax.set(ylabel='volume', xlabel='t [h]') ax.errorbar(times, 1.e-9 * volume, yerr=1.e-9 * volume_std) def plt_sprad(ax): ax.set(ylabel='sphere equiv. radius', xlabel='t [h]') ax.errorbar(times, 1.e-3 * sphere_equiv_radius, yerr=1.e-3 * sphere_equiv_radius_std) def plt_sphereicity(ax): ax.set(ylabel='sphericity', xlabel='t [h]') ax.errorbar(times, sphericity, yerr=sphericity_std) for ax, func in zip(axes, [plt_rad, plt_vol, plt_sprad, plt_sphereicity]): l = MaxNLocator(nbins=4) ax.xaxis.set_major_locator(l) func(ax) pdfpages.savefig(fig) times = times[0::2] radial = averaged_radial((groups_by_time, times), 'vs_dr', [ 'phi_tumor', 'mvd', 'radius', 'shearforce', 'flow', 'sources', 'vel', 'oxy', 'maturation' ]) bins = np.asarray(groups_by_time[times[0]][0]['radial/vs_dr/bins']) bins *= 1. / 1000. xlim = -1.5, 1.0 mask = np.logical_and(bins < xlim[1], bins >= xlim[0]) def plot_times(ax, name, **kwargs): f = kwargs.pop('value_prefactor', 1.) colors = 'rgbmk' markers = 'os<>d' for i, t in enumerate(times): avg, std = radial[name, t] avg, std = avg[mask], std[mask] #ax.errorbar(bins[mask], f*avg, yerr=f*std, label = 't = %s' % f2s(t), **kwargs) mpl_utils.errorbar(ax, bins[mask], f * avg, yerr=f * std, label='t = $%s$' % f2s(t), marker=markers[i], color=colors[i], every=5, **kwargs) def text1(ax, txt): ax.text(0.95, 0.9, txt, ha="right", transform=ax.transAxes) def text2(ax, txt): ax.text(0.01, 0.9, txt, ha="left", transform=ax.transAxes) def mkfig(nrows, ncols): fig, axes = mpl_utils.subplots_abs_mm( (mastersize[0] / mpl_utils.mm_to_inch * 0.5 * ncols, mastersize[0] / mpl_utils.mm_to_inch * 0.2 * nrows), nrows, ncols, 10, 20, a4size[0] / mpl_utils.mm_to_inch * 0.38, a4size[0] / mpl_utils.mm_to_inch * 0.16, 15, 5) return fig, axes # fig, axes = pyplot.subplots(4, 2, figsize = (mastersize[0], mastersize[0]*0.25*4.)) # mpl_utils.subplots_adjust_abs(fig, left = mpl_utils.mm_to_inch*20, # right = -mpl_utils.mm_to_inch*10, # top = -mpl_utils.mm_to_inch*5, # bottom = mpl_utils.mm_to_inch*10, # hspace = mpl_utils.mm_to_inch*30,) def plt_mvd(ax): # mvd can be written as N^3 * L0/N * 3 / V = (V=L0^3) ... = N^2 / L0^2 ax.set(ylabel=ur'$\times 10^3$ [\u03BCm$^{-2}$]', xlim=xlim) plot_times(ax, 'mvd', value_prefactor=1e3) text1(ax, r'$L/V$') ax.legend(loc=mpl_utils.loc.lower_left, frameon=True) def plt_rad(ax): ax.set(ylabel=ur'[\u03BCm]', xlim=xlim) plot_times(ax, 'radius') text1(ax, r'$r_v$') def plt_vel(ax): ax.set(ylabel=ur'[\u03BCm/h]', xlim=xlim) text1(ax, r'$v_\phi$') plot_times(ax, 'vel') def plt_tum(ax): ax.set(ylabel=ur'', xlim=xlim, ylim=(-0.1, 0.7)) plot_times(ax, 'phi_tumor') text1(ax, r'$\phi_t$') def plt_oxy(ax): ax.set(ylabel=ur'', xlim=xlim) plot_times(ax, 'oxy') text1(ax, r'$c_o$') def plt_sf(ax): ax.set(ylabel=ur'[Pa]', xlim=xlim) plot_times(ax, 'shearforce', value_prefactor=1e3) text1(ax, r'$f_v$') def plt_wall(ax): ax.set(ylabel=ur'[\u03BCm]', xlim=xlim) plot_times(ax, 'maturation') text1(ax, r'$w_v$') def plt_qv(ax): ax.set(ylabel=ur'[\u03BCm]', xlim=xlim) ax.set(xlabel=ur'$\theta$ [mm]') plot_times(ax, 'flow') text1(ax, r'$q_v$') fig, axes = mkfig(3, 2) plt_mvd(axes[0, 0]) plt_rad(axes[0, 1]) plt_vel(axes[1, 0]) plt_oxy(axes[1, 1]) plt_wall(axes[2, 0]) plt_sf(axes[2, 1]) for ax in axes[2, :]: ax.set(xlabel=ur'$\theta$ [mm]') axes = axes.ravel() for i, ax in enumerate(axes): ax.grid(linestyle=':', linewidth=0.5, color=gridcolor) mpl_utils.add_crosshair(ax, (0, 0), color=gridcolor) if not ax.get_xlabel(): ax.set(xticklabels=[]) text2(ax, '(%s)' % 'abcdefghij'[i]) pdfpages.savefig(fig)
for i, ax in enumerate(axes): ax.grid(linestyle=':', linewidth=0.5, color=gridcolor) mpl_utils.add_crosshair(ax, (0, 0), color=gridcolor) if not ax.get_xlabel(): ax.set(xticklabels=[]) text2(ax, '(%s)' % 'abcdefghij'[i]) pdfpages.savefig(fig) if __name__ == '__main__': filenames = sys.argv[1:] if not myutils.is_measurement_file(filenames[0]): for fn in filenames: with h5py.File(fn, 'r') as f: with myutils.MeasurementFile(f, h5files) as fdst: grps = myutils.getTimeSortedGroups(f['.'], "out") for grp in grps: dstgroup = myutils.require_snapshot_group_(fdst, grp) generate_data(grp, dstgroup) else: rc = matplotlib.rc rc( 'figure', **{ 'subplot.left': 0.15, 'subplot.right': 1. - 0.15, 'subplot.bottom': 0.2, 'subplot.top': 1. - 0.1, 'subplot.wspace': 0.2, 'subplot.hspace': 0.2 }) fnmeasure = commonOutputName(filenames)
def __init__(self, name): f = h5py.File(name + '.h5', 'r') self.ld = ld = krebsutils.read_lattice_data_from_hdf(f['field_ld']) self.groups = myutils.getTimeSortedGroups(f['.'], 'out') self.f = f