Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix updating metadata format #35

Merged
merged 3 commits into from
Jun 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions magmap/io/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,16 +541,23 @@ def load_metadata(path, check_ver=False, assign=True):
if output:
# metadata is in first document
output = output[0]
except FileNotFoundError:
except FileNotFoundError as err:
# fall back to pre-v1.4 NPZ file format
libmag.warn("Could not find metadata file '{}', will check NPZ format"
.format(path))
_logger.warn("Could not load metadata file '%s', will check NPZ format",
path)
_logger.debug(err)
path_npz = f"{os.path.splitext(path)[0]}.npz"
try:
# load NPZ and resave as YML format
output = np_io.read_np_archive(np.load(path_npz))
except FileNotFoundError:
libmag.warn("Could not find metadata file '{}', skipping"
.format(path_npz))
path_yml = f"{os.path.splitext(path)[0]}.yml"
yaml_io.save_yaml(path_yml, output, True)
_logger.info(
"Metadata file from '%s' updated to '%s'", path_npz, path_yml)
except FileNotFoundError as err2:
_logger.warn(
"Could not load metadata file '%s', skipping", path_npz)
_logger.debug(err2)
return None, image5d_ver_num

try:
Expand Down
31 changes: 18 additions & 13 deletions magmap/io/yaml_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Author: David Young, 2020
"""YAML file format input/output."""

import numpy as np
import yaml

from magmap.io import libmag
Expand Down Expand Up @@ -49,6 +48,9 @@ def load_yaml(path, enums=None):
Returns:
List[dict]: Sequence of parsed dictionaries for each document within
a YAML file.

Raises:
FileNotFoundError: if ``path`` could not be found or loaded.

"""
def parse_enum_val(val):
Expand All @@ -63,18 +65,21 @@ def parse_enum_val(val):
# replace with the corresponding Enum class
val = enums[val_split[0]][val_split[1]]
return val

with open(path) as yaml_file:
# load all documents into a generator
docs = yaml.load_all(yaml_file, Loader=yaml.FullLoader)
data = []
for doc in docs:
if not doc:
# skip empty document
continue
if enums:
doc = _filter_dict(doc, parse_enum_val)
data.append(doc)

try:
with open(path) as yaml_file:
# load all documents into a generator
docs = yaml.load_all(yaml_file, Loader=yaml.FullLoader)
data = []
for doc in docs:
if not doc:
# skip empty document
continue
if enums:
doc = _filter_dict(doc, parse_enum_val)
data.append(doc)
except (FileNotFoundError, UnicodeDecodeError) as e:
raise FileNotFoundError(e)
return data


Expand Down
19 changes: 12 additions & 7 deletions magmap/settings/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from magmap.io import yaml_io
from magmap.settings import config

_logger = config.logger.getChild(__name__)


class RegKeys(Enum):
"""Register setting enumerations."""
Expand Down Expand Up @@ -142,11 +144,14 @@ def add_modifier(self, mod_name, profiles, sep):
print(mod_path, "profile file not found, skipped")
return
self.timestamps[mod_path] = os.path.getmtime(mod_path)
yamls = yaml_io.load_yaml(mod_path, _PROFILE_ENUMS)
mods = {}
for yaml in yamls:
mods.update(yaml)
print("loaded {}:\n{}".format(mod_path, mods))
try:
yamls = yaml_io.load_yaml(mod_path, _PROFILE_ENUMS)
mods = {}
for yaml in yamls:
mods.update(yaml)
_logger.info("Loaded profile from '%s':\n%s", mod_path, mods)
except FileNotFoundError:
_logger.warn("Unable to load profile from: %s", mod_path)
else:
if mod_name == self.DEFAULT_NAME:
# update entries from a new instance for default values;
Expand Down Expand Up @@ -194,8 +199,8 @@ def update_settings(self, names_str):
self.add_modifier(profile, self.profiles, self.delimiter)

if config.verbose:
print("settings for {}:", self[self.NAME_KEY])
pprint.pprint(self)
_logger.debug("settings for '%s':", self[self.NAME_KEY])
_logger.debug(pprint.pprint(self))

def check_file_changed(self):
"""Check whether any profile files have changed since last loaded.
Expand Down