[refactoring] removed star imports, renamed cst in U
and applied flake8 guidelines
This commit is contained in:
+22
-59
@@ -1,44 +1,18 @@
|
||||
# coding: utf-8
|
||||
|
||||
import copy
|
||||
import glob as glob
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from abc import ABCMeta, abstractmethod
|
||||
from abc import ABCMeta
|
||||
from functools import partial
|
||||
|
||||
import numpy as np
|
||||
import pymses
|
||||
import tables
|
||||
from astrophysix.simdm import SimulationStudy
|
||||
from astrophysix.simdm.experiment import (
|
||||
AppliedAlgorithm,
|
||||
ParameterSetting,
|
||||
ParameterVisibility,
|
||||
Simulation,
|
||||
)
|
||||
from astrophysix.simdm.results import GenericResult, Snapshot
|
||||
from numpy.polynomial.polynomial import polyfit
|
||||
from pymses.analysis import (
|
||||
Camera,
|
||||
FractionOperator,
|
||||
MaxLevelOperator,
|
||||
ScalarOperator,
|
||||
raytracing,
|
||||
slicing,
|
||||
splatting,
|
||||
)
|
||||
from pymses.filters import CellsToPoints
|
||||
from pymses.sources.hop.file_formats import *
|
||||
from pymses.sources.ramses import output
|
||||
from scipy.stats import linregress
|
||||
from tables import HDF5ExtError
|
||||
|
||||
from ramses_astrophysix import ramses
|
||||
from run_selector import *
|
||||
from units import *
|
||||
import tables
|
||||
|
||||
from tables import HDF5ExtError
|
||||
from pp_params import default_params, load_params
|
||||
from units import U
|
||||
|
||||
|
||||
class Rule:
|
||||
@@ -51,7 +25,7 @@ class Rule:
|
||||
dependencies=[],
|
||||
is_valid=lambda arg: True,
|
||||
kind="classic",
|
||||
unit=cst.none,
|
||||
unit=U.none,
|
||||
):
|
||||
self.postproc = postproc
|
||||
self.process_fn = process
|
||||
@@ -63,24 +37,11 @@ class Rule:
|
||||
self.kind = kind
|
||||
|
||||
def process(self, arg, **kwargs):
|
||||
if not arg is None:
|
||||
if arg is not None:
|
||||
return self.process_fn(arg, **kwargs)
|
||||
else:
|
||||
return self.process_fn(**kwargs)
|
||||
|
||||
def is_valid(self, arg):
|
||||
# save = self.postproc.save
|
||||
# valid = True
|
||||
# for dep in self.dependencies:
|
||||
# if dep in self.postproc.rules:
|
||||
# rule_dep = self.postproc.rules[dep]
|
||||
# if not arg is None:
|
||||
# valid = valid and rule_dep.group + '/' + dep + '_' + str(arg) in save
|
||||
# else:
|
||||
# valid = valid and rule_dep.group + '/' + dep in save
|
||||
# return valid and self.is_valid_add(arg)
|
||||
return self.is_valid_add(arg)
|
||||
|
||||
|
||||
class BaseProcessor:
|
||||
"""
|
||||
@@ -134,7 +95,9 @@ class BaseProcessor:
|
||||
)
|
||||
else:
|
||||
self._log(
|
||||
"{} is unknown, allowed rules are {}".format(name, self.rules.keys()),
|
||||
"{} is unknown, allowed rules are {}".format(
|
||||
to_process, self.rules.keys()
|
||||
),
|
||||
"ERROR",
|
||||
)
|
||||
|
||||
@@ -176,13 +139,13 @@ class BaseProcessor:
|
||||
return overwrite
|
||||
|
||||
def _process_rule(self, name, rule, arg, overwrite=False, **kwargs):
|
||||
if not arg is None:
|
||||
if arg is not None:
|
||||
name_full = rule.group + "/" + name + "_" + str(arg)
|
||||
else:
|
||||
name_full = rule.group + "/" + name
|
||||
|
||||
if rule.is_valid(arg):
|
||||
if not name_full in self.just_done:
|
||||
if name_full not in self.just_done:
|
||||
if self._needs_computation(overwrite, name_full):
|
||||
self._log("Processing {}".format(name_full))
|
||||
data = rule.process(arg, **kwargs)
|
||||
@@ -255,7 +218,7 @@ class HDF5Container(BaseProcessor):
|
||||
)
|
||||
else:
|
||||
value = node.read()
|
||||
if not (unit is None or unit_old is None or unit_old == cst.none):
|
||||
if not (unit is None or unit_old is None or unit_old == U.none):
|
||||
value = value * unit_old.express(unit)
|
||||
finally:
|
||||
if not open_before:
|
||||
@@ -266,7 +229,7 @@ class HDF5Container(BaseProcessor):
|
||||
"""
|
||||
Get real units from info files
|
||||
unit is either:
|
||||
1. An instance of cst.Unit (pymses unit class)
|
||||
1. An instance of U.Unit (pymses unit class)
|
||||
2. A string beginning by "unit_", referring to a code unit,
|
||||
available in self.info
|
||||
3. A dict {unit1 : exp1, unit2: exp2, ...} with unitX as 2.
|
||||
@@ -276,10 +239,10 @@ class HDF5Container(BaseProcessor):
|
||||
and unit the corresponding unit (on one on the above format)
|
||||
|
||||
Returns:
|
||||
1-3. : a cst.Unit instance
|
||||
4. : a dict {key: unit, ...} with same key as input and unit being cst.Unit instances
|
||||
1-3. : a U.Unit instance
|
||||
4. : a dict {key: unit, ...} with same key as input and unit being U.Unit instances
|
||||
"""
|
||||
if isinstance(unit, cst.Unit):
|
||||
if isinstance(unit, U.Unit):
|
||||
return unit
|
||||
if isinstance(unit, str) and unit[:5] == "unit_":
|
||||
res = self.info[unit]
|
||||
@@ -287,13 +250,13 @@ class HDF5Container(BaseProcessor):
|
||||
res = res / self.info["boxlen"]
|
||||
return res
|
||||
if list(unit)[0][:5] == "unit_":
|
||||
new_unit = cst.none
|
||||
new_unit = U.none
|
||||
for base_unit_str in unit:
|
||||
expo = unit[base_unit_str]
|
||||
base_unit = self._get_units(base_unit_str)
|
||||
new_unit = new_unit * base_unit ** expo
|
||||
return new_unit
|
||||
if (not data is None) and isinstance(data, dict) and list(unit)[0] in data:
|
||||
if (data is not None) and isinstance(data, dict) and list(unit)[0] in data:
|
||||
for key in unit:
|
||||
unit[key] = self._get_units(unit[key])
|
||||
return unit
|
||||
@@ -364,7 +327,7 @@ class HDF5Container(BaseProcessor):
|
||||
)
|
||||
self.save.get_node(name_full).attrs.unit = unit
|
||||
|
||||
if not attrs is None:
|
||||
if attrs is not None:
|
||||
for key in attrs:
|
||||
key = str(key)
|
||||
self.save.get_node(name_full)._v_attrs[key] = attrs[key]
|
||||
@@ -448,7 +411,7 @@ class HDF5Container(BaseProcessor):
|
||||
fn,
|
||||
name_array_in,
|
||||
name_array_out,
|
||||
unit_out=cst.none,
|
||||
unit_out=U.none,
|
||||
description="",
|
||||
recursive=True,
|
||||
):
|
||||
|
||||
Reference in New Issue
Block a user