Skip to content

Commit

Permalink
Closes #128 & clean up for loading results in om.Extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
fzeiser committed May 19, 2020
1 parent 5b669d0 commit 32572d5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 64 deletions.
2 changes: 1 addition & 1 deletion ompy/abstract_normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class AbstractNormalizer():
""" Abstract class for Matrix and Vector.
""" Abstract class for Normalizers.
Do not initialize itself.
Expand Down
22 changes: 11 additions & 11 deletions ompy/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Ensemble:
def __init__(self, raw: Optional[Matrix] = None,
bg: Optional[Matrix] = None,
bg_ratio: float = 1,
path: Optional[Union[str, Path]] = None):
path: Optional[Union[str, Path]] = 'saved_run/ensemble'):
""" Sets up attributes and loads a saved ensemble if provided.
Args:
Expand All @@ -97,7 +97,8 @@ def __init__(self, raw: Optional[Matrix] = None,
self.prompt_w_bg: Optional[Matrix] = raw

self.unfolder: Optional[Callable[[Matrix], Matrix]] = None
self.first_generation_method: Optional[Callable[[Matrix], Matrix]] = None
self.first_generation_method: \
Optional[Callable[[Matrix], Matrix]] = None
self.size = 0
self.regenerate = False
self.action_prompt_w_bg = Action('matrix')
Expand All @@ -117,9 +118,11 @@ def __init__(self, raw: Optional[Matrix] = None,
self.seed: int = 987654
self.nprocesses: int = cpu_count()-1 if cpu_count() > 1 else 1

self.path = (Path(path) if path is not None
else Path('saved_run/normalizers'))
self.path.mkdir(exist_ok=True, parents=True)
if path is None:
self.path = None
else:
self.path = Path(path)
self.path.mkdir(exist_ok=True, parents=True)

self.raw.state = "raw"

Expand All @@ -133,6 +136,7 @@ def load(self, path: Optional[Union[str, Path]] = None):
value is None, 'self.path' will be used.
"""
path = Path(path) if path is not None else Path(self.path)
LOG.info(f"loading from {path}")

self.raw = Matrix(path=path / 'raw.npy')
self.firstgen = Matrix(path=path / 'firstgen.npy')
Expand Down Expand Up @@ -238,7 +242,7 @@ def step(self, step: int, rsequence: np.random.SeedSequence,
Returns:
raw, unfolded, firstgen
"""
LOG.info(f"Generating {step}")
LOG.info(f"Generating/loading {step}")
rstate = np.random.default_rng(rsequence)
if self.bg is not None:
prompt_w_bg = self.generate_perturbed(step, method,
Expand Down Expand Up @@ -281,14 +285,10 @@ def generate_perturbed(self, step: int, method: str, state: str,
allowed = ["raw", "prompt+bg", "bg"]
if state not in allowed:
raise NotImplementedError(f"Matrix must be a state in {allowed}")
LOG.debug(f"Generating {state} ensemble {step}")
LOG.debug(f"Working on {state} ensemble {step}")
path = self.path / f"{state}_{step}.npy"
mat = self.load_matrix(path)
if mat is None:
# if np.any(self.mat.values < 0):
# raise ValueError("input matrix has to have positive"
# "entries only. Consider using fill and or"
# "remove negatives")
LOG.debug(f"(Re)generating {path} using {method} process")
if method == 'gaussian':
values = self.generate_gaussian(state, rstate)
Expand Down
11 changes: 6 additions & 5 deletions ompy/ensembleNormalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self, *, extractor: Extractor,
normalizer_nld: Optional[NormalizerNLD] = None,
normalizer_gsf: Optional[NormalizerGSF] = None,
normalizer_simultan: Optional[NormalizerSimultan] = None,
path: Optional[Union[str, Path]] = None,
path: Optional[Union[str, Path]] = 'saved_run/normalizers',
regenerate: bool = False):
"""
Args:
Expand All @@ -89,9 +89,11 @@ def __init__(self, *, extractor: Extractor,

self.res: Optional[List[ResultsNormalized]] = None

self.path = (Path(path) if path is not None
else Path('saved_run/normalizers'))
self.path.mkdir(exist_ok=True, parents=True)
if path is None:
self.path = None
else:
self.path = Path(path)
self.path.mkdir(exist_ok=True, parents=True)

def normalize(self) -> None:
""" Normalize ensemble """
Expand Down Expand Up @@ -623,7 +625,6 @@ def save_results_txt(self, path: Optional[Union[str, Path]] = None,
path = Path(path) if path is not None else Path(self.path)
path.mkdir(exist_ok=True, parents=True)
for i, res in enumerate(self.res):

super().save_results_txt(path, nld=res.nld, gsf=res.gsf,
samples=res.samples, suffix=i)

Expand Down
45 changes: 7 additions & 38 deletions ompy/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class Extractor:
"""
def __init__(self, ensemble: Optional[Ensemble] = None,
trapezoid: Optional[Action] = None,
path: Optional[Union[str, Path]] = None):
path: Optional[Union[str, Path]] =
'saved_run/extractor'):
"""
ensemble (Ensemble, optional): see above
trapezoid (Action[Matrix], optional): see above
Expand All @@ -81,12 +82,10 @@ def __init__(self, ensemble: Optional[Ensemble] = None,
self.gsf: List[Vector] = []
self.trapezoid = trapezoid

if path is not None:
self.path = Path(path)
self.path.mkdir(exist_ok=True, parents=True)
self.load(self.path)
if path is None:
self.path = None
else:
self.path = Path('saved_run/extraction_ensemble')
self.path = Path(path)
self.path.mkdir(exist_ok=True, parents=True)

self.x0 = None
Expand Down Expand Up @@ -132,7 +131,7 @@ def extract_from(self, ensemble: Optional[Ensemble] = None,
raise ValueError("A 'trapezoid' cut must be given'")
if regenerate is None:
regenerate = self.regenerate
self.path = Path(self.path) # TODO: Fix
self.path = Path(self.path)

nlds = []
gsfs = []
Expand All @@ -141,6 +140,7 @@ def extract_from(self, ensemble: Optional[Ensemble] = None,
nld_path = self.path / f'nld_{i}.npy'
gsf_path = self.path / f'gsf_{i}.npy'
if nld_path.exists() and gsf_path.exists() and not regenerate:
LOG.debug(f"loading from {nld_path} and {gsf_path}")
nlds.append(Vector(path=nld_path))
gsfs.append(Vector(path=gsf_path))
else:
Expand Down Expand Up @@ -489,37 +489,6 @@ def fFWHM(E, p):
fwhm_pars = np.array([73.2087, 0.50824, 9.62481e-05])
return fFWHM(matrix.Ex, fwhm_pars)

def load(self, path: Optional[Union[str, Path]] = None) -> None:
""" Load already extracted nld and gsf from file
Args:
path: The path to the directory containing the
files.
"""
if path is not None:
path = Path(path)
else:
path = Path(self.path) # TODO: Fix pathing
LOG.debug("Loading from %s", path)

if not path.exists():
raise IOError(f"The path {path} does not exist.")
if self.nld or self.gsf:
warnings.warn("Loading nld and gsf into non-empty instance")

for fname in path.glob("nld_[0-9]*.npy"):
self.nld.append(Vector(path=fname))

for fname in path.glob("gsf_[0-9]*.npy"):
self.gsf.append(Vector(path=fname))

assert len(self.nld) == len(self.gsf), "Corrupt files"

if not self.nld:
warnings.warn("Found no files")

self.size = len(self.nld)

def plot(self, ax: Optional[Any] = None, scale: str = 'log',
plot_mean: bool = False,
color='k', **kwargs) -> None:
Expand Down
13 changes: 8 additions & 5 deletions ompy/normalizer_gsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, *, normalizer_nld: Optional[NormalizerNLD] = None,
nld_model: Optional[Callable[..., Any]] = None,
alpha: Optional[float] = None,
gsf: Optional[Vector] = None,
path: Optional[Union[str, Path]] = None,
path: Optional[Union[str, Path]] = 'saved_run/normalizers',
regenerate: bool = False,
norm_pars: Optional[NormalizationParameters] = None,
) -> None:
Expand All @@ -71,7 +71,8 @@ def __init__(self, *, normalizer_nld: Optional[NormalizerNLD] = None,
Args:
normalizer_nld (Optional[NormalizerNLD], optional): NormalizerNLD
to retrieve parameters. If `nld` and/or `nld_model` are not set, they are taken from `normalizer_nld.res` in `normalize`.
to retrieve parameters. If `nld` and/or `nld_model` are not
set, they are taken from `normalizer_nld.res` in `normalize`.
nld (Optional[Vector], optional): NLD. If not set it is taken from
`normalizer_nld.res` in `normalize`.
nld_model (Optional[Callable[..., Any]], optional): Model for nld
Expand Down Expand Up @@ -115,9 +116,11 @@ def __init__(self, *, normalizer_nld: Optional[NormalizerNLD] = None,
self._saved_spincutPars = None
self._saved_SpinSum_args = None

self.path = (Path(path) if path is not None
else Path('saved_run/normalizers'))
self.path.mkdir(exist_ok=True, parents=True)
if path is None:
self.path = None
else:
self.path = Path(path)
self.path.mkdir(exist_ok=True, parents=True)

def normalize(self, *, gsf: Optional[Vector] = None,
normalizer_nld: Optional[NormalizerNLD] = None,
Expand Down
10 changes: 6 additions & 4 deletions ompy/normalizer_simultan.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, *,
nld: Optional[Vector] = None,
normalizer_nld: Optional[NormalizerNLD] = None,
normalizer_gsf: Optional[NormalizerGSF] = None,
path: Optional[Union[str, Path]] = None,
path: Optional[Union[str, Path]] = 'saved_run/normalizers',
regenerate: bool = False):
"""
TODO:
Expand Down Expand Up @@ -91,9 +91,11 @@ def __init__(self, *,
self.multinest_path: Optional[Path] = Path('multinest')
self.multinest_kwargs: dict = {"seed": 65498, "resume": False}

self.path = (Path(path) if path is not None
else Path('saved_run/normalizers'))
self.path.mkdir(exist_ok=True, parents=True)
if path is None:
self.path = None
else:
self.path = Path(path)
self.path.mkdir(exist_ok=True, parents=True)

def normalize(self, *, num: int = 0,
gsf: Optional[Vector] = None,
Expand Down

0 comments on commit 32572d5

Please sign in to comment.