diff --git a/hexrd/cli/find_orientations.py b/hexrd/cli/find_orientations.py index 4ff57f16..7b3792b8 100644 --- a/hexrd/cli/find_orientations.py +++ b/hexrd/cli/find_orientations.py @@ -1,11 +1,16 @@ from __future__ import print_function, division, absolute_import import os +import logging +import sys + import numpy as np from hexrd import constants as const +from hexrd import config from hexrd import instrument from hexrd.transforms import xfcapi +from hexrd.findorientations import find_orientations, write_scored_orientations descr = 'Process rotation image series to find grain orientations' @@ -52,10 +57,7 @@ def configure_parser(sub_parsers): def write_results(results, cfg): # Write scored orientations. - np.savez_compressed( - cfg.find_orientations.orientation_maps.scored_orientations_file, - **results['scored_orientations'] - ) + write_scored_orientations(results, cfg) # Write accepted orientations. qbar_filename = str(cfg.find_orientations.accepted_orientations_file) @@ -73,12 +75,6 @@ def write_results(results, cfg): def execute(args, parser): - import logging - import sys - - from hexrd import config - from hexrd.findorientations import find_orientations - # make sure hkls are passed in as a list of ints try: if args.hkls is not None: diff --git a/hexrd/cli/fit_grains.py b/hexrd/cli/fit_grains.py index cd7c34b2..e4762940 100644 --- a/hexrd/cli/fit_grains.py +++ b/hexrd/cli/fit_grains.py @@ -183,7 +183,6 @@ def execute(args, parser): raise(RuntimeError, "error loading indexing results '%s'" % quats_f) else: - quats_f = cfg.find_orientations.accepted_orientations_file logger.info("Missing %s, running find-orientations", quats_f) logger.removeHandler(ch) results = find_orientations(cfg) diff --git a/hexrd/config/fitgrains.py b/hexrd/config/fitgrains.py index 8f94bd99..8a708efe 100644 --- a/hexrd/config/fitgrains.py +++ b/hexrd/config/fitgrains.py @@ -34,8 +34,6 @@ def tth(self): class FitGrainsConfig(Config): - _fit_grains = "fit-grains" - def _active_material_str(self): return self.parent.material.active.strip().replace(' ', '-') @@ -47,7 +45,7 @@ def __init__(self, cfg): @property def logfile(self): """Name of log file""" - return self.parent.analysis_dir / f"fit-grains.log" + return self.parent.analysis_dir / "fit-grains.log" @property def grains_file(self): diff --git a/hexrd/config/root.py b/hexrd/config/root.py index 900ebac4..b8944b90 100644 --- a/hexrd/config/root.py +++ b/hexrd/config/root.py @@ -21,22 +21,23 @@ class RootConfig(Config): def working_dir(self): """Working directory, either specified in file or current directory - This directory is certain to exist. If the specified directory does - not exist, it defaults to the current working directory. + If the directory is not specified in the config file, then it will + default to the current working directory. If it is specified, the + director must exist, or it will throw and IOError. """ - try: - temp = Path(self.get('working_dir')) - if not temp.exists(): - raise IOError(f'"working_dir": {temp} does not exist') - return temp - - except RuntimeError: - temp = Path.cwd() - self.working_dir = temp + wdir = self.get('working_dir', default=None) + if wdir is not None: + wdir = Path(wdir) + if not wdir.exists(): + raise IOError(f'"working_dir": {str(wdir)} does not exist') + else: + # Working directory has not been specified, so we use current + # directory. + wdir = Path.cwd() logger.info( - '"working_dir" not specified, defaulting to "%s"' % temp - ) - return temp + '"working_dir" not specified, defaulting to "%s"' % wdir + ) + return wdir @working_dir.setter def working_dir(self, val): @@ -47,7 +48,7 @@ def working_dir(self, val): @property def analysis_name(self): - """Name (Path) of the analysis + """Name of the analysis This will be used to set up the output directory. The name can contain slash ("/") characters, which will generate a subdirectory @@ -64,7 +65,7 @@ def analysis_dir(self): """Analysis directory, where output files go The name is derived from `working_dir` and `analysis_name`. This - propetry returns a Path object. The directory and any intermediate + property returns a Path object. The directory and any intermediate directories can be created with the `mkdir()` method, e.g. >>> analysis_dir.mkdir(parents=True, exist_ok=True) diff --git a/hexrd/findorientations.py b/hexrd/findorientations.py index 3088a9d5..04d5318c 100755 --- a/hexrd/findorientations.py +++ b/hexrd/findorientations.py @@ -42,6 +42,22 @@ # ============================================================================= +def write_scored_orientations(results, fname): + """Write scored orientations to a file + + PARAMETERS + ---------- + results: dict + output of main `find_orientations` function + cfg: Config instance + the main Config input file for `find-orientations` + """ + np.savez_compressed( + cfg.find_orientations.orientation_maps.scored_orientations_file, + **results['scored_orientations'] + ) + + def _process_omegas(omegaimageseries_dict): """Extract omega period and ranges from an OmegaImageseries dictionary.""" oims = next(iter(omegaimageseries_dict.values())) @@ -384,7 +400,10 @@ def quat_distance(x, y): def load_eta_ome_maps(cfg, pd, image_series, hkls=None, clean=False): """ - Load the eta-ome maps specified by the config and CLI flags. + Load the eta-ome maps specified by the config and CLI flags. If the + maps file exists, it will return those values. If the file does not exist, + it will generate them using the passed HKLs (if not None) or the HKLs + specified in the config file (if passed HKLs are Noe). Parameters ---------- @@ -395,7 +414,7 @@ def load_eta_ome_maps(cfg, pd, image_series, hkls=None, clean=False): image_series: ImageSeries instance stack of images hkls: list, default = None - list of HKLs used in the eta-omega maps. + list of HKLs used to generate the eta-omega maps clean: bool, default = False flag indicating whether (if True) to overwrite existing maps file @@ -415,7 +434,7 @@ def load_eta_ome_maps(cfg, pd, image_series, hkls=None, clean=False): try: res = EtaOmeMaps(str(fn)) pd = res.planeData - logger.info(f'loaded eta/ome orientation maps from {str(fn)}') + logger.info(f'loaded eta/ome orientation maps from {fn}') shkls = pd.getHKLs(*res.iHKLList, asStr=True) logger.info( 'hkls used to generate orientation maps: %s', @@ -429,7 +448,7 @@ def load_eta_ome_maps(cfg, pd, image_series, hkls=None, clean=False): ) res = generate_eta_ome_maps(cfg, hkls=hkls) - filter_maps_if_requested(res, cfg) + filter_maps_if_requested(res, cfg) return res