Skip to content

load args into DO metadata #82

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

Merged
merged 7 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion src/diffpy/labpdfproc/labpdfprocapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from diffpy.labpdfproc.functions import apply_corr, compute_cve
from diffpy.labpdfproc.tools import (
known_sources,
load_metadata,
load_package_info,
load_user_info,
load_user_metadata,
Expand Down Expand Up @@ -144,7 +145,7 @@ def main():
"tth",
scat_quantity="x-ray",
name=filepath.stem,
metadata={"muD": args.mud, "anode_type": args.anode_type},
metadata=load_metadata(args, filepath),
)

absorption_correction = compute_cve(input_pattern, args.mud, args.wavelength)
Expand Down
55 changes: 55 additions & 0 deletions src/diffpy/labpdfproc/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from diffpy.labpdfproc.labpdfprocapp import get_args
from diffpy.labpdfproc.tools import (
known_sources,
load_metadata,
load_package_info,
load_user_info,
load_user_metadata,
Expand Down Expand Up @@ -277,3 +278,57 @@ def test_load_package_info(mocker):
actual_args = get_args(cli_inputs)
actual_args = load_package_info(actual_args)
assert actual_args.package_info == {"diffpy.labpdfproc": "1.2.3", "diffpy.utils": "3.3.0"}


def _setup(mocker, user_filesystem):
cwd = Path(user_filesystem)
home_dir = cwd / "home_dir"
mocker.patch("pathlib.Path.home", lambda _: home_dir)
os.chdir(cwd)
mocker.patch(
"importlib.metadata.version",
side_effect=lambda package_name: "3.3.0" if package_name == "diffpy.utils" else "1.2.3",
)


def _preprocess_args(args):
args = load_package_info(args)
args = load_user_info(args)
args = set_input_lists(args)
args.output_directory = set_output_directory(args)
args.wavelength = set_wavelength(args)
args = load_user_metadata(args)
return args


def test_load_metadata(mocker, user_filesystem):
_setup(mocker, user_filesystem)
cli_inputs = [
"2.5",
".",
"--wavelength",
"1.54",
"--user-metadata",
"key=value",
"--username",
"cli_username",
"--email",
"[email protected]",
]
actual_args = get_args(cli_inputs)
actual_args = _preprocess_args(actual_args)
for filepath in actual_args.input_paths:
actual_metadata = load_metadata(actual_args, filepath)
expected_metadata = {
"mud": 2.5,
"input_directory": str(filepath),
"anode_type": "Cu",
"wavelength": 1.54,
"output_directory": str(Path.cwd().resolve()),
"xtype": "tth",
"key": "value",
"username": "cli_username",
"email": "[email protected]",
"package_info": {"diffpy.labpdfproc": "1.2.3", "diffpy.utils": "3.3.0"},
}
assert actual_metadata == expected_metadata
33 changes: 33 additions & 0 deletions src/diffpy/labpdfproc/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,36 @@ def load_package_info(args):
metadata = get_package_info("diffpy.labpdfproc")
setattr(args, "package_info", metadata["package_info"])
return args


def _set_anode_type(args):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may be doing too much. i.e., "magic"?

I think the ucs are
uc1

  1. user specifies a standard anode material
  2. labpdfproc looks up the wavelength
  3. lpp stores the wavelength and anode type in metadata

UC2

  1. user specifies a (numeric) wavelength
  2. lpp stores the (numeric) wavelength in metadata.

I don't thing there is a reverse UC where a numeric value stored and lpp saves the anode type.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we only took care of wavelength in the current code. anode type will be Mo if user doesn't specify it. So an input of wavelength=1.54 will produce anode_type=Mo in the current code. Shall I modify this in the wavelength function, or should I add a new function that take care of anode type?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what you are saying here. I want to remove the magic: "input of wavelength=1.54 will produce anode_type=Mo"

We don't want to guess where the user got the 1.54 A data from, maybe from a synchrotron?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean we might need a separate function (or I can modify the existing wavelength function?) to remove the magic because in the previous issues we only checked that args has the correct wavelength (i.e. we didn't remove the anode type for UC2, but anode type is loaded automatically into the args because it's one of the arguments).
I got your point on not doing the reverse case.

if args.wavelength in WAVELENGTHS.values():
args.anode_type = next(key for key, value in WAVELENGTHS.items() if value == args.wavelength)
else:
delattr(args, "anode_type")
return args


def load_metadata(args, filepath):
"""
Load metadata from args,
except for anode type if wavelength does not match, and do not load output_correction or force_overwrite

Parameters
----------
args argparse.Namespace
the arguments from the parser

Returns
-------
A dictionary with all arguments from the parser
"""

args = _set_anode_type(args)
metadata = vars(args)
exclude_keys = ["output_correction", "force_overwrite", "input", "input_paths"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

save these as a global variabl above.

for key in exclude_keys:
metadata.pop(key, None)
metadata["input_directory"] = str(filepath)
metadata["output_directory"] = str(metadata["output_directory"])
return metadata
Loading