[misc] May last round of bug corrections

This commit is contained in:
Noe Brucy
2022-05-31 16:15:33 +02:00
parent 6c912a5294
commit 124f24f92c
6 changed files with 129 additions and 107 deletions
+15 -16
View File
@@ -109,15 +109,15 @@ def quiver(ax, map_h, map_v, extent, key_v=None, label="", **kwargs):
# add vector key # add vector key
if key_v is None: if key_v is None:
key_v = (max_v + min_v) / 2.0 key_v = (max_v + min_v) / 2.0
ax.quiverkey( ax.quiverkey(
vec_field, vec_field,
0.6, 0.6,
0.98, 0.98,
key_v, key_v,
f"${key_v:g}$ {label}", f"${key_v:g}$ {label}",
labelpos="E", labelpos="E",
coordinates="figure", coordinates="figure",
) )
def line_integral_convolution(ax, map_h, map_v, extent, **kwargs): def line_integral_convolution(ax, map_h, map_v, extent, **kwargs):
@@ -793,7 +793,7 @@ class Plotter(Aggregator, BaseProcessor):
except AttributeError: except AttributeError:
cbar = plt.colorbar() cbar = plt.colorbar()
if label is not None: if label is not None:
if text_embeded: if colorbar_embeded:
cbar.set_label(" " + label, color=overtext_color, loc="bottom") cbar.set_label(" " + label, color=overtext_color, loc="bottom")
else: else:
cbar.set_label(label) cbar.set_label(label)
@@ -824,14 +824,13 @@ class Plotter(Aggregator, BaseProcessor):
plot_overlay = self.overlays[plot_overlay] plot_overlay = self.overlays[plot_overlay]
xlim = plt.xlim() xlim = plt.xlim()
ylim = plt.ylim() ylim = plt.ylim()
try: if len(overlays_kwargs) > i:
plot_overlay(ax_los, im_extent, **overlays_kwargs[i]) plot_overlay(ax_los, im_extent, **overlays_kwargs[i])
except IndexError: else:
plot_overlay(ax_los, im_extent) plot_overlay(ax_los, im_extent)
finally:
# Restore previous limits in case overlays changed it plt.xlim(xlim)
plt.xlim(xlim) plt.ylim(ylim)
plt.ylim(ylim)
return PlotInfo( return PlotInfo(
plot_type=PlotType.IMAGE, plot_type=PlotType.IMAGE,
+3 -3
View File
@@ -434,7 +434,7 @@ parser.add_argument(
"repo", help="RAMSES output repository", type=str, default=".", nargs="?" "repo", help="RAMSES output repository", type=str, default=".", nargs="?"
) )
parser.add_argument( parser.add_argument(
"iouts", help="output numbers", type=args.selection, default=":", nargs="?" "iouts", help="output numbers", type=int, default=[1], nargs="+"
) )
parser.add_argument( parser.add_argument(
"outfile", "outfile",
@@ -520,7 +520,7 @@ def main(arg):
iouts = arg.iouts # tools.select_outputs(arg.repo, arg.iouts) iouts = arg.iouts # tools.select_outputs(arg.repo, arg.iouts)
for iout in iouts: for iout in iouts:
print("Output %d" % iout) print(f"Output {iout}")
print("Load data") print("Load data")
read_lvl = None read_lvl = None
if True: if True:
@@ -933,7 +933,7 @@ if __name__ == "__main__":
def pspec(**kwargs): def pspec(**kwargs):
arg = parser.parse_args("") arg = parser.parse_args("1")
for kwarg in kwargs: for kwarg in kwargs:
setattr(arg, kwarg, kwargs[kwarg]) setattr(arg, kwarg, kwargs[kwarg])
main(arg) main(arg)
+80 -63
View File
@@ -56,39 +56,38 @@ class RunSelector:
Select runs and outputs with several filter options. Select runs and outputs with several filter options.
By default, all runs and outputs within path_in are considered By default, all runs and outputs within path_in are considered
Parameters Args:
---------
1. Define the set of runs and outputs considered 1. Define the set of runs and outputs considered
path_in : str, path to the folder of the runs path_in : str, path to the folder of the runs
2. Filter runs and outputs 2. Filter runs and outputs
in_runs : str or list of str. The name runs to consider. Default: all. in_runs : str or list of str. The name runs to consider. Default: all.
in_nums : int or list of int or str. in_nums : int or list of int or str.
The output numbers to consider. The output numbers to consider.
"last" select only the last output. "last" select only the last output.
"all" preselect all outputs (default) "all" preselect all outputs (default)
nml_filename : str name of the namelist (should be the same for all outputs) nml_filename : str name of the namelist (should be the same for all outputs)
filter_name : str, filter runs by name. Default "*" filter_name : str, filter runs by name. Default "*"
filter_nml : tuple or list of tupple. filter_nml : tuple or list of tupple.
Filter runs by namelist. Filter runs by namelist.
tuples are in the following form: tuples are in the following form:
(nml_key, operator, nml_value) (nml_key, operator, nml_value)
with nml_key a key from the namelist (eg. "cloud_params/dens0") with nml_key a key from the namelist (eg. "cloud_params/dens0")
operator within ("=", "!=", "<", ">", "in") operator within ("=", "!=", "<", ">", "in")
and nml_value a string, float or int and nml_value a string, float or int
time_min : float, select output where time >= time_min (in code units) time_min : float, select output where time >= time_min (in code units)
time_max : float, select output where time <= time_min (in code units) time_max : float, select output where time <= time_min (in code units)
time : float or list of float. For each value, select the output closer to it. time : float or list of float. For each value, select the output closer to it.
unit_time : astrophysix.Unit, unit for the time above. None is code unit. unit_time : astrophysix.Unit, unit for the time above. None is code unit.
3. Sort the runs 3. Sort the runs
sort_run_by : str, a key from the namelist used to sort the runs (by ascending order) sort_run_by : str, a key from the namelist used to sort the runs (by ascending order)
""" """
@@ -146,37 +145,35 @@ class RunSelector:
""" """
Sub-select runs and outputs from already selected runs and outputs Sub-select runs and outputs from already selected runs and outputs
Parameters Args:
---------
runs : str or list of str. The name runs to consider. Default: all.
nums : int or list of int or str.
The output numbers to consider.
"last" select only the last output.
"all" preselect all outputs (default)
filter_name : str. runs : str or list of str. The name runs to consider. Default: all.
glob pattern used to filter run names. nums : int or list of int or str.
default is "*" (all runs) The output numbers to consider.
"last" select only the last output.
"all" preselect all outputs (default)
filter_nml : tuple or list of tupple. filter_name : str.
Filter runs by namelist. glob pattern used to filter run names.
tuples are in the following form: default is "*" (all runs)
(nml_key, operator, nml_value)
with nml_key a key from the namelist (eg. "cloud_params/dens0") filter_nml : tuple or list of tupple.
operator within ("=", "!=", "<", ">", "in") Filter runs by namelist.
and nml_value a string, float or int tuples are in the following form:
time_min : float, select output where time >= time_min (in code units) (nml_key, operator, nml_value)
time_max : float, select output where time <= time_min (in code units) with nml_key a key from the namelist (eg. "cloud_params/dens0")
time : float or list of float. For each value, select the output closer to it. operator within ("=", "!=", "<", ">", "in")
unit_time : astrophysix.Unit, unit for the time above. None is code unit. and nml_value a string, float or int
time_min : float, select output where time >= time_min (in code units)
time_max : float, select output where time <= time_min (in code units)
time : float or list of float. For each value, select the output closer to it.
unit_time : astrophysix.Unit, unit for the time above. None is code unit.
sort_run_by : str, a key from the namelist used to sort the runs (by ascending order) sort_run_by : str, a key from the namelist used to sort the runs (by ascending order)
Returns Returns:
------- (selected_runs, selected_nums)
(selected_runs, selected_nums)
""" """
if runs is None: if runs is None:
@@ -301,20 +298,19 @@ class RunSelector:
""" """
Select snapshots from the disk Select snapshots from the disk
Parameters Args:
---------
in_nums : int or list of int or str. in_nums : int or list of int or str.
The output numbers to consider. The output numbers to consider.
"last" select only the last output. "last" select only the last output.
"all" preselect all outputs (default) "all" preselect all outputs (default)
time_min : float, select output where time >= time_min (in code units) time_min : float, select output where time >= time_min (in code units)
time_max : float, select output where time <= time_min (in code units) time_max : float, select output where time <= time_min (in code units)
time : float or list of float. For each value, select the output closer to it. time : float or list of float. For each value, select the output closer to it.
unit_time : astrophysix.Unit, unit for the time above. None is code unit. unit_time : astrophysix.Unit, unit for the time above. None is code unit.
do_tests : test if the snapshots are actually on disk. Not needed when subselecting snapshots. do_tests : test if the snapshots are actually on disk. Not needed when subselecting snapshots.
""" """
# -- Initialize info loader -- # -- Initialize info loader --
@@ -382,7 +378,7 @@ class RunSelector:
if position == "left": if position == "left":
return ileft return ileft
elif position == "right": elif position == "right":
return nums[iright] return iright
else: else:
dleft = np.abs(get_time(nums[ileft]) - time) dleft = np.abs(get_time(nums[ileft]) - time)
dright = np.abs(get_time(nums[iright]) - time) dright = np.abs(get_time(nums[iright]) - time)
@@ -457,3 +453,24 @@ class RunSelector:
nums = list(filter(try_load_info, nums)) nums = list(filter(try_load_info, nums))
return nums return nums
def write_paths(self, prefix=None, filename="~/list_file"):
"""
Write the paths of the selected runs on a file
Args:
prefix (str, optional): Prefix for the pathscd si. Defaults to path_in.
filename (str, optional): F. Defaults to "~/list_file".
"""
if prefix is None:
prefix = self.path_in
paths = []
for run in self.nums:
for num in nums[run]:
paths.append(f"{prefix}/{run}/output_{num:05}\n")
f = open(os.path.expanduser(filename), "w")
f.writelines(paths)
f.close()
+22 -21
View File
@@ -2,10 +2,6 @@ from run_selector import RunSelector
from plotter import Plotter, U from plotter import Plotter, U
import os import os
path_from_home = "simus/ismfeed/allmode"
names = "n6_st_2e5_seed3_T5Myr_nsink1e3_comp*"
def prep_mcons(study): def prep_mcons(study):
study.coarse_step_from_log() study.coarse_step_from_log()
@@ -18,12 +14,19 @@ def time_mcons(study, run, target=0.2):
return time_target return time_target
def find_nums(study, prep_function, time_function): def find_nums(study, prep_function, time_function, time_min=0):
"""
Once other filter are applied, select one output based on the time given by time function
Args:
prep_function (study:studyProcessor -> None): prepare a study object
time_function (study:studyProcessor, run:str -> time:float): compute selected time from the object
"""
nums = {} nums = {}
prep_function(study) prep_function(study)
for run in study.runs: for run in study.runs:
time_target = time_function(study, run) time_target = max(time_min, time_function(study, run))
rs = RunSelector(path_in=in_dir, in_runs=run, time=time_target, unit_time=U.Myr) rs = RunSelector(path_in=study.path, in_runs=run, time_min=time_min, time=time_target, unit_time=U.Myr)
nums.update(rs.nums) nums.update(rs.nums)
return nums return nums
@@ -38,18 +41,16 @@ def write_paths(nums, path_from_home, filename="~/list_file"):
f.close() f.close()
in_dir = os.path.expanduser(f"~/{path_from_home}") if __name__ == '__main__':
study = Plotter( path_from_home = "simus/ismfeed/allmode"
in_dir, names = "n6_st_2e5_seed3_T5Myr_nsink1e3_comp*"
filter_name=names,
nums="first",
tag="select",
).study
in_dir = os.path.expanduser(f"~/{path_from_home}")
nums = find_nums(study, prep_mcons, time_mcons) study = Plotter(
write_paths(nums, ".") in_dir,
filter_name=names,
pl = Plotter(in_dir, runs=nums.keys(), nums=nums) nums="first",
tag="select",
pl.rho_pdf() ).study
nums = find_nums(study, prep_mcons, time_mcons)
write_paths(nums, ".")
+1 -1
View File
@@ -1340,7 +1340,7 @@ class SnapshotProcessor(HDF5Container):
second_line = f.readline() second_line = f.readline()
f.close() f.close()
if first_line[1] == "#": if len(first_line) > 0 and first_line[1] == "#":
header = first_line[3:-2].split(",") header = first_line[3:-2].split(",")
units = second_line[3:-2].split(",") units = second_line[3:-2].split(",")
df = pd.read_csv(csv_name, header=None, names=header, skiprows=2) df = pd.read_csv(csv_name, header=None, names=header, skiprows=2)
+8 -3
View File
@@ -269,7 +269,7 @@ class StudyProcessor(Aggregator, HDF5Container):
return series return series
def _extract_stellar_from_log(self, stellar_objects, log_filename, run): def _extract_stellar_from_log(self, stellar_objects, log_filename, run):
cmd_grep = f"grep stellar {log_filename} -n" cmd_grep = f"grep 'stellar objects:' {log_filename} -n"
content = os.popen(cmd_grep).readlines() content = os.popen(cmd_grep).readlines()
nb_stellar = list(map(lambda s: int(s.split()[1]), content)) nb_stellar = list(map(lambda s: int(s.split()[1]), content))
line_numbers = list(map(lambda s: int(s.split(":")[0]), content)) line_numbers = list(map(lambda s: int(s.split(":")[0]), content))
@@ -440,8 +440,13 @@ class StudyProcessor(Aggregator, HDF5Container):
def get_coldens0(self, run): def get_coldens0(self, run):
mp = 1.4 * 1.66 * 10**(-24) * U.g mp = 1.4 * 1.66 * 10**(-24) * U.g
z0 = self.get_nml("galbox_params/height0", run) * U.pc try:
n0 = self.get_nml("galbox_params/dens0", run) * U.cm**(-3) z0 = self.get_nml("galbox_params/height0", run) * U.pc
n0 = self.get_nml("galbox_params/dens0", run) * U.cm**(-3)
except KeyError:
z0 = self.get_nml("cloud_params/height0", run) * U.pc
n0 = self.get_nml("cloud_params/dens0", run) * U.cm**(-3)
return (np.sqrt(2 * np.pi) * mp * z0 * n0).express(U.coldens) return (np.sqrt(2 * np.pi) * mp * z0 * n0).express(U.coldens)
def total_mass(self): def total_mass(self):