def get_varlist(self, pos=False, particle=False): """Get a list of all existing VAR# file names. pos = False: give full list pos = 'last'/'first': give latest/first var file pos = 'lastXXX' / 'firstXXX' give last/first XXX varfiles pos = list of numbers: give varfiles at this positions particle = True: return PVAR- instead of VAR-list""" import glob from os.path import join as join from os.path import basename from pencilnew.math import natural_sort key = 'VAR' if particle == True: key = 'PVAR' varlist = natural_sort([ basename(i) for i in glob.glob(join(self.data_dir, 'proc0') + '/' + key + '*') ]) #if particle: varlist = ['P'+i for i in varlist] if pos == False: return varlist if pos == 'first': return [varlist[0]] if pos == 'last': return [varlist[-1]] if pos.startswith('last'): return varlist[-int(pos[4:]):] if pos.startswith('first'): return varlist[:int(pos[4:])] if type(pos) == type([]): if pos[0].startswith('VAR'): pos = [i[3:] for i in pos] if pos[0].startswith('PVAR'): pos = [i[3:] for i in pos] return [varlist[int(i)] for i in pos] return varlist
def get_varfiles(self, pos=False, particle=False): """Get a list of all existing VAR# file names. pos = False: give full list pos = 'last'/'first': give newest/first var file post = list of numbers: give varfiles at this positions particle = True: return PVAR isntead of VAR list""" import glob from os.path import join as __join__ from os.path import basename from pencilnew.math import natural_sort varlist = natural_sort([ basename(i) for i in glob.glob(__join__(self.data_dir, 'proc0') + '/VAR*') ]) if particle: varlist = ['P' + i for i in varlist] if pos == False: return varlist elif pos == 'first': return [varlist[0]] elif pos == 'last': return [varlist[-1]] elif pos.startswith('last'): return varlist[-int(pos[4:]):] elif pos.startswith('first'): return varlist[:int(pos[4:])] elif type(pos) == type([]): if pos[0].startswith('VAR'): pos = [i[3:] for i in pos] if pos[0].startswith('PVAR'): pos = [i[3:] for i in pos] return [varlist[i] for i in pos] else: return varlist
def get_varlist(self, pos=False, particle=False): """Get a list of all existing VAR# file names. pos = False: give full list pos = 'last'/'first': give latest/first var file pos = 'lastXXX' / 'firstXXX' give last/first XXX varfiles pos = list of numbers: give varfiles at this positions particle = True: return PVAR- instead of VAR-list""" import glob from os.path import join as join from os.path import basename from pencilnew.math import natural_sort key = 'VAR' if particle == True: key = 'PVAR' varlist = natural_sort([basename(i) for i in glob.glob(join(self.datadir, 'proc0')+'/'+key+'*')]) #if particle: varlist = ['P'+i for i in varlist] if pos == False: return varlist if pos == 'first': return [varlist[0]] if pos == 'last': return [varlist[-1]] if pos.startswith('last'): return varlist[-int(pos[4:]):] if pos.startswith('first'): return varlist[:int(pos[4:])] if type(pos) == type([]): if pos[0].startswith('VAR'): pos = [i[3:] for i in pos] if pos[0].startswith('PVAR'): pos = [i[3:] for i in pos] return [varlist[int(i)] for i in pos] return varlist
def group(sim_list, groupby, sort=True): """Group simulation by a quantity. Each Simulation object can only be part of one group. Args: sim_list: put here a list of Simulation objects, i.e. [sim1, sim2, ...] groupby: put here the heyword after which the grouping shall happen sort: set True to sort returned dictionary naturally Return: a dictionary with keywords are the group entries and values are lists of simulations in that group """ from collections import OrderedDict from pencilnew.math import natural_sort sim_dict_grouped = {} # case the groupby-keyword can be found in param.keys() if groupby in sim_list[0].param.keys(): for sim in sim_list: q = str(sim.param[groupby]) if (not q in sim_dict_grouped.keys()): sim_dict_grouped[q] = [sim] else: sim_dict_grouped[q].append(sim) # special cases: elif groupby in ['Lx', 'Ly', 'Lz']: for sim in sim_list: q = str(sim.param['lxyz'][0]) if (not q in sim_dict_grouped.keys()): sim_dict_grouped[q] = [sim] else: sim_dict_grouped[q].append(sim) else: print '!! ERROR: Coudnt group simulations, no fitting groupby-keyword has been found to match "'+groupby+'"!' return False if sort: sim_dict_grouped_n_sorted = OrderedDict() for key in natural_sort(sim_dict_grouped.keys()): sim_dict_grouped_n_sorted[key] = sim_dict_grouped[key] sim_dict_grouped = sim_dict_grouped_n_sorted return sim_dict_grouped
def group(simulations, groupby, sort=True, only_started=False, reverse=False): """Group simulation by a quantity. Each Simulation object can only be part of one group. Args: simulations: put here a Simulations object or a list of simulations [sim1, sim2, ...] groupby: put here the heyword after which the grouping shall happen sort: set True to sort returned dictionary naturally only_started: only group simulations that already has started Return: a dictionary with keywords are the group entries and values are lists of simulations in that group """ from collections import OrderedDict from pencilnew.math import natural_sort sim_dict_grouped = {} if type(simulations) == type(['list']): sim_list = simulations #elif type(simulations) == Simulations: # sim_list = simulations.sims else: print('!! ERROR: Dont know how to interprated simulations argument..') return False # sort out simulations that has not started if only_started == True: sim_list = [s for s in sim_list if s.started()] # special cases: if groupby in ['Lx', 'Ly', 'Lz']: if groupby[-1] == 'x': ii = 0 elif groupby[-1] == 'y': ii = 1 elif groupby[-1] == 'z': ii = 2 for sim in sim_list: q = str(sim.param['lxyz'][ii]) if (not q in sim_dict_grouped.keys()): sim_dict_grouped[q] = [sim] else: sim_dict_grouped[q].append(sim) elif groupby in ['nx', 'ny', 'nz']: for sim in sim_list: q = str(getattr(sim.dim, groupby)) if (not q in sim_dict_grouped.keys()): sim_dict_grouped[q] = [sim] else: sim_dict_grouped[q].append(sim) # case the groupby-keyword can be found via __simulation__.get_value elif sim_list[0].get_value(groupby) != None: for sim in sim_list: q = str(sim.get_value(groupby)) if (not q in sim_dict_grouped.keys()): sim_dict_grouped[q] = [sim] else: sim_dict_grouped[q].append(sim) else: print( '!! ERROR: Coudnt group simulations, no fitting groupby-keyword has been found to match "' + groupby + '"!') return False if sort: sim_dict_grouped_n_sorted = OrderedDict() keys = sim_dict_grouped.keys() for key in natural_sort(keys, reverse=reverse): sim_dict_grouped_n_sorted[key] = sim_dict_grouped[key] return sim_dict_grouped_n_sorted return sim_dict_grouped
def group(simulations, groupby, sort=True, only_started=False, reverse=False): """Group simulation by a quantity. Each Simulation object can only be part of one group. Args: simulations: put here a Simulations object or a list of simulations [sim1, sim2, ...] groupby: put here the heyword after which the grouping shall happen sort: set True to sort returned dictionary naturally only_started: only group simulations that already has started Return: a dictionary with keywords are the group entries and values are lists of simulations in that group """ from collections import OrderedDict from pencilnew.math import natural_sort sim_dict_grouped = {} if type(simulations) == type(['list']): sim_list = simulations #elif type(simulations) == Simulations: # sim_list = simulations.sims else: print('!! ERROR: Dont know how to interprated simulations argument..') return False # sort out simulations that has not started if only_started == True: sim_list = [s for s in sim_list if s.started()] # special cases: if groupby in ['Lx', 'Ly', 'Lz']: if groupby[-1] == 'x': ii = 0 elif groupby[-1] == 'y': ii = 1 elif groupby[-1] == 'z': ii = 2 for sim in sim_list: q = str(sim.param['lxyz'][ii]) if (not q in sim_dict_grouped.keys()): sim_dict_grouped[q] = [sim] else: sim_dict_grouped[q].append(sim) elif groupby in ['nx', 'ny', 'nz']: for sim in sim_list: q = str(getattr(sim.dim, groupby)) if (not q in sim_dict_grouped.keys()): sim_dict_grouped[q] = [sim] else: sim_dict_grouped[q].append(sim) # case the groupby-keyword can be found via __simulation__.get_value elif sim_list[0].get_value(groupby) != None: for sim in sim_list: q = str(sim.get_value(groupby)) if (not q in sim_dict_grouped.keys()): sim_dict_grouped[q] = [sim] else: sim_dict_grouped[q].append(sim) else: print('!! ERROR: Coudnt group simulations, no fitting groupby-keyword has been found to match "'+groupby+'"!') return False if sort: sim_dict_grouped_n_sorted = OrderedDict() keys = sim_dict_grouped.keys() for key in natural_sort(keys, reverse=reverse): sim_dict_grouped_n_sorted[key] = sim_dict_grouped[key] return sim_dict_grouped_n_sorted return sim_dict_grouped