diff --git a/.github/workflows/matrix-and-codecov-on-merge-to-main.yml b/.github/workflows/matrix-and-codecov-on-merge-to-main.yml deleted file mode 100644 index 3114aa2..0000000 --- a/.github/workflows/matrix-and-codecov-on-merge-to-main.yml +++ /dev/null @@ -1,62 +0,0 @@ -name: CI - -on: - push: - branches: - - main - release: - types: - - prereleased - - published - workflow_dispatch: - -jobs: - matrix-coverage: - defaults: - run: - shell: bash -l {0} - - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, windows-latest, macos-13, macos-14] - python-version: ["3.11", "3.12", "3.13"] - env: - LATEST_PYTHON_VERSION: "3.13" - steps: - - name: Check out diffpy.labpdfproc - uses: actions/checkout@v4 - - - name: Initialize miniconda - uses: conda-incubator/setup-miniconda@v3 - with: - activate-environment: test - auto-update-conda: true - environment-file: environment.yml - auto-activate-base: false - python-version: ${{ matrix.python-version }} - - - name: Conda config - run: >- - conda config --set always_yes yes - --set changeps1 no - - - name: Install diffpy.labpdfproc and requirements - run: | - conda install --file requirements/tests.txt - conda install --file requirements/conda.txt - pip install gooey - python -m pip install . --no-deps - - - name: Validate diffpy.labpdfproc - run: | - pytest --cov - coverage report -m - codecov - - - name: Upload coverage to Codecov - if: matrix.os == 'ubuntu-latest' && matrix.python-version == env.LATEST_PYTHON_VERSION - uses: codecov/codecov-action@v4 - env: - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/docs/source/api/diffpy.labpdfproc.rst b/docs/source/api/diffpy.labpdfproc.rst index 1bcc98d..a3f53e1 100644 --- a/docs/source/api/diffpy.labpdfproc.rst +++ b/docs/source/api/diffpy.labpdfproc.rst @@ -1,9 +1,7 @@ :tocdepth: -1 -|title| -======= - -.. |title| replace:: diffpy.labpdfproc package +diffpy.labpdfproc package +========================= .. automodule:: diffpy.labpdfproc :members: @@ -14,17 +12,33 @@ Subpackages ----------- .. toctree:: - diffpy.labpdfproc.example_package + :titlesonly: + + diffpy.labpdfproc.data Submodules ---------- -|module| --------- +diffpy.labpdfproc.functions module +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. automodule:: diffpy.labpdfproc.functions + :members: + :undoc-members: + :show-inheritance: + +diffpy.labpdfproc.tools module +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. automodule:: diffpy.labpdfproc.tools + :members: + :undoc-members: + :show-inheritance: -.. |module| replace:: diffpy.labpdfproc.example_submodule module +diffpy.labpdfproc.labpdfprocapp module +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. automodule:: diffpy.labpdfproc.example_submodule +.. automodule:: diffpy.labpdfproc.labpdfprocapp :members: :undoc-members: :show-inheritance: diff --git a/docs/source/examples/example-data/CeO2_635um_zscan_200umSlit_chanClose_exported.xy b/docs/source/examples/example-data/CeO2_635um_zscan.xy similarity index 100% rename from docs/source/examples/example-data/CeO2_635um_zscan_200umSlit_chanClose_exported.xy rename to docs/source/examples/example-data/CeO2_635um_zscan.xy diff --git a/docs/source/examples/labpdfprocapp-example.rst b/docs/source/examples/labpdfprocapp-example.rst index a553c8f..c33cadf 100644 --- a/docs/source/examples/labpdfprocapp-example.rst +++ b/docs/source/examples/labpdfprocapp-example.rst @@ -10,121 +10,206 @@ to apply absorption correction to your 1D diffraction data using the command-lin Check ``labpdfproc --help`` for more information. A graphical user interface (GUI) is also available and is designed to be intuitive and easy to use. +There are three ways to correct dirraction data within ``diffpy.labpdfproc``, these are, -1. To use this application, you will need: -(1) your input diffraction data file(s), and (2) information required to compute the muD value. -To launch the GUI, use ``labpdfproc`` or ``labpdfproc --gui``. -Note that the GUI is currently not supported on Python 3.13. +1. ``labpdfproc mud``: Provide the diffraction data file(s) and muD value directly. +2. ``labpdfproc zscan``: Provide the diffraction data file(s) and a z-scan file, which will be used to calculate the muD value. +3. ``labpdfproc sample``: Provide the diffraction data file(s) and information about the sample to calculate the theoretical muD value. +We will go through all three methods in this tutorial. -2. Here we first provide a basic CLI example. -Assume you have an uncorrected diffraction data file named ``zro2_mo.xy`` in the current directory -with a muD of 2.5 on the two-theta x-axis. Then the minimum command would be: +.. admonition:: Example Data -.. code-block:: python + Example data for these examples can be found at: https://github.com/diffpy/diffpy.labpdfproc/tree/main/docs/source/examples/example-data - labpdfproc zro2_mo.xy --mud 2.5 +Launching the Graphical User Interface (GUI) +-------------------------------------------- -3. You must provide at least one file path, and filepath(s) should immediately follow ``labpdfproc``. +To launch the GUI, run one of the following commands in your terminal, -To process multiple files at once in the CLI, separate each file path with a whitespace. -In general, avoid spaces in filenames, but if necessary, enclose them in quotes; otherwise, quotes are optional. -For example, the following is a valid and more complex CLI command: +.. code-block:: bash -.. code-block:: python + labpdfproc + labpdfproc --gui - labpdfproc "SiO2 uncorrected.xy" input_dir file_list.txt ./*.chi data* --mud 2.5 +.. note:: Note that the GUI is currently not supported on Python>=3.12. -This command will process ``"SiO2 uncorrected.xy"``, -all files in the ``input_dir`` directory, all files listed in ``file_list.txt``, -all ``.chi`` files in the current directory, and all files matching the pattern ``data``, -using a muD value of 2.5. -Check ``labpdfproc --help`` to see all supported file types. +This will open the GUI, which should look something like, -In the GUI, you can select multiple individual files via the file browser, but not entire directories. -To include directories, you can either: -(1) create a text file named ``file_list.txt`` containing the desired paths and load it, -(2) manually select all files in a folder, or -(3) enter paths manually separated by a colon with no spaces. +.. image:: ../img/labpdfproc-gui.jpeg + :align: center -We will now continue using ``zro2_mo.xy`` as the example input throughout the rest of this tutorial. +Below we will go through all the commands using the CLI, but the same principles apply to the GUI. +.. note:: The first time you run any of the below commands, + you will be prompted to enter your name, email, and ORCID. This will be + appended to metadata in the output file header for reproducibility and tracking purposes. -4. The muD value is required for absorption correction, and you can specify it in one of the four ways: -.. code-block:: python +``labpdfproc mud`` Command +--------------------------- - # Option 1: Manual value - labpdfproc zro2_mo.xy --mud 2.5 - # Option 2: From a z-scan file - labpdfproc zro2_mo.xy -z zscan.xy - # Option 3: Using sample mass density - labpdfproc zro2_mo.xy -d ZrO2,17.45,1.2 - # Option 4: Using packing fraction - labpdfproc zro2_mo.xy -p ZrO2,17.45,0.2 +If you know the muD value for your sample, you can use the ``labpdfproc mud`` +command to apply absorption correction directly. -Note that you can only use one method at a time. The following examples are not allowed: +To see all the options for this command, type, -.. code-block:: python +.. code-block:: bash - labpdfproc zro2_mo.xy --mud 2.5 -z zscan.xy - labpdfproc zro2_mo.xy --mud 2.5 -d ZrO2,17.45,1.2 + labpdfproc mud -h -If the packing fraction option is not supported at the moment, you can approximate the sample mass density as: -``mass density = packing fraction * material density``, where the material density can be looked up manually. +To run the correction, specify the path to your diffraction data file(s) and the muD value, +.. code-block:: bash -5. You can specify the wavelength in two ways: + labpdfproc mud + labpdfproc mud zro2_mo.xy 2.5 -.. code-block:: python +If the flag ``--wavelength`` is not specified, the program will attempt to fetch +the wavelength from a configuration file. +If the wavelength is not found in the configuration file, a ``ValueError`` +will be raised, prompting you to provide the wavelength either through the CLI or the configuration file. - # Option 1: Manually enter wavelength - labpdfproc zro2_mo.xy --mud 2.5 -w 0.71303 - # Option 2: Use a known anode type - labpdfproc zro2_mo.xy --mud 2.5 -a Mo +To provide the wavelength through the CLI, you can use the ``-w`` or ``--wavelength`` flag followed by the wavelength value in angstroms or the X-ray source. +For example, -Do not use both ``-w`` and ``-a`` at the same time. For example: +.. code-block:: bash -.. code-block:: python + labpdfproc mud zro2_mo.xy 2.5 -w 0.71303 + labpdfproc mud zro2_mo.xy 2.5 -w Mo - labpdfproc zro2_mo.xy --mud 2.5 -w 0.71303 -a Mo +This will then save the corrected file in the same directory as the input file with the name ``zro2_mo_corrected.chi``. -is not valid. This is to avoid confusion when wavelength conflicts with anode type. -To avoid retyping, you can save your wavelength or anode type in a diffpy configuration file. -See full instructions at https://www.diffpy.org/diffpy.labpdfproc/examples/tools_example.html. +To save the correction file, specify the ``-c`` or ``--output-correction`` flag, +.. code-block:: bash -6. You are also encouraged to provide your information (name, email, and orcid) for reproducibility: + labpdfproc mud zro2_mo.xy 2.5 -w 0.71303 -c -.. code-block:: python +This will then save the correction file in the same directory as the input file with the name ``zro2_mo_cve.chi``. - labpdfproc zro2_mo.xy --mud 2.5 -n Joe -e Joe@email.com --orcid 0000-0000-0000-0000 +``labpdfproc zscan`` Command +---------------------------- +The ``labpdfproc zscan`` command allows you to calculate the muD value from a z-scan file and apply absorption correction to your diffraction data. +For more information on what a z-scan is, please see the "Examples --> Linear Absorption Coefficient Examples" +section in the ``diffpy.utils`` documentation: https://www.diffpy.org/diffpy.utils/examples/mu_calc_examples.html. -Alternatively, you can enter this information during the interactive prompts -or save it in your diffpy configuration file. -For more details, refer to https://www.diffpy.org/diffpy.utils/examples/tools_example.html. +To see the options for this command, type, +.. code-block:: bash -7. You can further customize the diffraction correction process using the following options: + labpdfproc zscan -h -- Choose xtype: use ``-x`` to specify your input data's xtype, which will be used for the output. -- Select correction method: use ``-m`` to choose between "brute_force" or "polynomial_interpolation" (faster and preferred for muD 0.5-7). -- Specify output directory: use ``-o`` to save the corrected file(s) to a specific folder. -- Add custom metadata: use ``-u`` to provide key-value pair for information tracking (e.g., experimental details). -- Output the cve file: use ``-c`` to export the cve file along with the corrected data. -- Overwrite existing files: use ``-f`` to replace any previous corrected files with the same names. +To run this command, provide the path to your diffraction data file(s) and the z-scan file, +.. code-block:: bash -8. To summarize, a full command might look like this: + labpdfproc zscan + labpdfproc zscan CeO2_635um_accum_0.xy CeO2_635um_zscan.xy -.. code-block:: python +Like the ``labpdfproc mud`` command, you can also specify the +wavelength or source type using the ``-w`` flag if it's not found in the configuration file, - labpdfproc zro2_mo.xy --mud 2.5 -w 0.71303 -n Joe -x q -m brute_force -o results -u "facility=NSLS II" beamline=28ID-2 -c -f +.. code-block:: bash -After running the command, check your output folder (in this case, ``results``) -for the corrected data file and cve file (if ``-c`` was used). -In this example, the corrected and cve files are called ``zro2_mo_corrected.chi`` and ``zro2_mo_cve.chi``. -The headers include all the arguments you provided -—such as diffraction settings, personal information, and metadata—making it easy to track your analysis. + labpdfproc zscan CeO2_635um_accum_0.xy CeO2_635um_zscan.xy -w 0.71303 + labpdfproc zscan CeO2_635um_accum_0.xy CeO2_635um_zscan.xy -w Mo + +This will save the corrected file in the same directory as the input file with the name ``CeO2_635um_accum_0.chi``. + +To save the correction file, specify the ``-c`` or ``--output-correction`` flag, + +.. code-block:: bash + + labpdfproc zscan CeO2_635um_accum_0.xy CeO2_635um_zscan.xy -w 0.71303 -c + +This will then save the correction file in the same directory as the input file with the name ``CeO2_635um_accum_0_cve.chi``. + +``labpdfproc sample`` Command +----------------------------- + +The ``labpdfproc sample`` command allows you to calculate the muD value from information +about your sample and apply absorption correction to your diffraction data. + +To see the options for this command, type, + +.. code-block:: bash + + labpdfproc sample -h + +To run this command, provide the path to your diffraction data file(s) along with: + +1) ``sample_composition``: The chemical formula of your sample. +2) ``sample_mass_density``: The mass density of your sample in g/cm^3. If you don't know the mass density, a good approximation is typically ~1/3 of the theoretical packing fraction. +3) ``diameter``: The outer diameter of your capillary. + +.. code-block:: bash + + labpdfproc sample + labpdfproc sample zro2_mo.xy ZrO2 17.45 1.2 + +Once again, you can specify the wavelength or source type using the ``-w`` flag if it's not found in the configuration file, + +.. code-block:: bash + + labpdfproc sample zro2_mo.xy ZrO2 17.45 1.2 -w 0.71303 + labpdfproc sample zro2_mo.xy ZrO2 17.45 1.2 -w Mo + +This will save the corrected file in the same directory as the input file with the name ``zro2_mo.chi``. + +To save the correction file, specify the ``-c`` or ``--output-correction`` flag, + +.. code-block:: bash + + labpdfproc sample zro2_mo.xy ZrO2 17.45 1.2 -w 0.71303 -c + +This will then save the correction file in the same directory as the input file with the name ``zro2_mo_cve.chi``. + +Additional CLI options +---------------------- + +Below is a summary of all the additional flags that can be used with all three commands, + +- ``-h, --help`` + Show this help message and exit. + +- ``-w, --wavelength WAVELENGTH`` + X-ray wavelength in angstroms (numeric) or X-ray source name. + Allowed: ``Ag``, ``AgKa1``, ``AgKa1Ka2``, ``Cu``, ``CuKa1``, ``CuKa1Ka2``, ``Mo``, ``MoKa1``, ``MoKa1Ka2``. + Will be loaded from config files if not specified. + +- ``-x, --xtype XTYPE`` + X-axis type (default: ``tth``). Allowed values: ``angle``, ``tth``, ``twotheta``, ``2theta``, ``d``, ``dspace``, ``q``. + +- ``-m, --method {brute_force, polynomial_interpolation}`` + Method for cylindrical volume element (CVE) calculation (default: ``polynomial_interpolation``). + Allowed methods: ``brute_force``, ``polynomial_interpolation``. + +- ``-o, --output-directory OUTPUT_DIRECTORY`` + Directory to save corrected files (created if needed). Defaults to current directory. + +- ``-f, --force`` + Overwrite existing files. + +- ``-c, --output-correction`` + Also output the absorption correction to a separate file. + +- ``-u, --user-metadata KEY=VALUE [KEY=VALUE ...]`` + Specify key-value pairs to be loaded into metadata. Format: ``key=value``. + - Separate multiple pairs with whitespace. + - No spaces before or after ``=``. + - Avoid using ``=`` in keys. Only the first ``=`` separates key and value. + - If a key or value contains whitespace, enclose it in quotes. + Examples: + ``facility='NSLS II', beamline=28ID-2, 'favorite color'=blue``. + +- ``--username USERNAME`` + Your name (optional, for dataset credit). Will be loaded from config files if not specified. + +- ``--email EMAIL`` + Your email (optional, for dataset credit). Will be loaded from config files if not specified. + +- ``--orcid ORCID`` + Your ORCID ID (optional, for dataset credit). Will be loaded from config files if not specified. diff --git a/docs/source/examples/tools-example.rst b/docs/source/examples/tools-example.rst index 674d244..7291658 100644 --- a/docs/source/examples/tools-example.rst +++ b/docs/source/examples/tools-example.rst @@ -20,15 +20,13 @@ You can do this in one of the following four ways: # Option 2: From a z-scan file args = Namespace(z_scan_file="zscan.xy") # Option 3: Using sample mass density - args = Namespace(theoretical_from_density="ZrO2,17.45,1.2") - # Option 4: Using packing fraction - args = Namespace(theoretical_from_packing="ZrO2,17.45,0.3") + args = Namespace(sample_composition="ZrO2", sample_mass_density="17.45", diameter="1.2") # Set and view the computed mu*D value args = set_mud(args) print(args.mud) -2. Next, we load the input files for correction using ``set_input_lists(args)``: +1. Next, we load the input files for correction using ``set_input_lists(args)``: .. code-block:: python @@ -59,24 +57,13 @@ If no output directory is specified, it defaults to the current working director # Option 1: Specify wavelength directly args = Namespace(wavelength=0.7) # Option 2: Use a valid anode type - args = Namespace(anode_type="Mo") + args = Namespace(wavelength="Mo") args = set_wavelength(args) Note that you should specify either a wavelength or an anode type, not both, to avoid conflicts. If you provide an anode type, the corresponding wavelength will be retrieved from global parameters. You may use ``labpdfproc --help`` to view a list of valid anode types. If neither is given, it's only acceptable if the input diffraction data is already on a two-theta grid. -To simplify workflows and avoid re-entering it every time, -we recommend saving the wavelength or anode type to a diffpy config file. For example: - -.. code-block:: python - - from pathlib import Path - import json - home_dir = Path.home() - wavelength_data = {"wavelength": 0.3} - with open(home_dir / "diffpyconfig.json", "w") as f: - json.dump(wavelength_data, f) To set the x-axis type (xtype) for your diffraction data: @@ -88,7 +75,7 @@ To set the x-axis type (xtype) for your diffraction data: This sets the xtype to ``tth``. Other valid options including ``q`` and ``d`` spacing. -5. Finally, we load user metadata, user information, and package information into ``args``. +1. Finally, we load user metadata, user information, and package information into ``args``. To load metadata, pass key-value pairs as a list: .. code-block:: python @@ -137,3 +124,19 @@ Using the function ``load_metadata(args, filepath)`` requires both the ``argument.Namespace`` and the current input file path. For more details about working with diffraction objects and how they are written to output files, see https://www.diffpy.org/diffpy.utils/examples/diffraction_objects_example.html. + + +Creating ``diffpyconfig.json`` file +----------------------------------- + +To simplify workflows and avoid re-entering it every time, +we recommend saving the wavelength or anode type to a diffpy config file. For example, + +.. code-block:: python + + from pathlib import Path + import json + home_dir = Path.home() + wavelength_data = {"wavelength": 0.3} + with open(home_dir / "diffpyconfig.json", "w") as f: + json.dump(wavelength_data, f) diff --git a/docs/source/img/.placeholder b/docs/source/img/.placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/docs/source/img/labpdfproc-gui.jpeg b/docs/source/img/labpdfproc-gui.jpeg new file mode 100644 index 0000000..9dc8de8 Binary files /dev/null and b/docs/source/img/labpdfproc-gui.jpeg differ diff --git a/news/doc0.3.0.rst b/news/doc0.3.0.rst new file mode 100644 index 0000000..b0af63d --- /dev/null +++ b/news/doc0.3.0.rst @@ -0,0 +1,23 @@ +**Added:** + +* Added documentation for new CLI updates. + +**Changed:** + +* Return a ``ValueError`` if no wavelength is found on config file or if its not specified. + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/src/diffpy/labpdfproc/labpdfprocapp.py b/src/diffpy/labpdfproc/labpdfprocapp.py index e5575ec..e5dd808 100644 --- a/src/diffpy/labpdfproc/labpdfprocapp.py +++ b/src/diffpy/labpdfproc/labpdfprocapp.py @@ -1,4 +1,5 @@ import argparse +import os import sys from gooey import Gooey, GooeyParser @@ -15,6 +16,17 @@ from diffpy.utils.parsers import load_data +def _running_in_gui(): + """Determine if the application is running in a GUI environment. + + This function checks if the standard output (sys.stdout) is + connected to a terminal. If not, it assumes the application is + running in a GUI environment, such as when launched by Gooey. This + is used to prevent ANSI escape sequences from rendering in the GUI. + """ + return not sys.stdout.isatty() + + def _add_common_args(parser, use_gui=False): parser.add_argument( "-w", @@ -172,6 +184,13 @@ def apply_absorption_correction(args): def create_parser(use_gui=False): Parser = GooeyParser if use_gui else argparse.ArgumentParser + + # Force no colors when gui is running to avoid ANSI escape codes + # in Gooey output + if use_gui or _running_in_gui(): + os.environ["NO_COLOR"] = "1" + os.environ["CLICOLOR"] = "0" + parser = Parser( prog="labpdfproc", description=( diff --git a/src/diffpy/labpdfproc/tools.py b/src/diffpy/labpdfproc/tools.py index 01c4a0e..2aa6754 100644 --- a/src/diffpy/labpdfproc/tools.py +++ b/src/diffpy/labpdfproc/tools.py @@ -224,29 +224,43 @@ def load_wavelength_from_config_file(args): args : argparse.Namespace The updated arguments with the updated wavelength and anode type. """ - global_config = _load_config(Path().home() / "diffpyconfig.json") - local_config = _load_config(Path().cwd() / "diffpyconfig.json") - local_has_data = local_config and "wavelength" in local_config - global_has_data = global_config and "wavelength" in global_config - if not local_has_data and not global_has_data: - print( - "No configuration file was found containing information " - "about the wavelength or anode type. \n" - "You can add the wavelength or anode type " - "to a configuration file on the current computer " - "and it will be automatically associated with " - "subsequent diffpy data by default. \n" - "You will only have to do that once. \n" - "For more information, please refer to www.diffpy.org/" - "diffpy.labpdfproc/examples/toolsexample.html" - ) + if args.wavelength is not None: return normalize_wavelength(args) - config = local_config if local_has_data else global_config - if config: - args.wavelength = config.get("wavelength") + + global_config_file = _load_config(Path().home() / "diffpyconfig.json") + local_config_file = _load_config(Path().cwd() / "diffpyconfig.json") + config_file = None + if ( + isinstance(local_config_file, dict) + and "wavelength" in local_config_file + ): + config_file = local_config_file + elif ( + isinstance(global_config_file, dict) + and "wavelength" in global_config_file + ): + config_file = global_config_file + if config_file is not None: + args.wavelength = config_file.get("wavelength") return normalize_wavelength(args) - return args + else: + raise ValueError( + "\nThe wavelength was not specified and no " + "configuration file 'diffpyconfig.json' containing " + "the wavelength or X-ray source was found in either the " + "local or home directories. Either specify the wavelength " + "or source using the -w/--wavelength option or " + "create a configuration file.\n\n" + "You can add the wavelength or anode type to a " + "configuration file on this computer. Once created, it " + "will be automatically used for subsequent diffpy data " + "by default, and you will only need to do this once.\n\n" + "For detailed instructions on creating the configuration " + "file, please refer to:\n" + "https://www.diffpy.org/diffpy.labpdfproc/examples/" + "toolsexample.html" + ) def set_wavelength(args): diff --git a/tests/test_tools.py b/tests/test_tools.py index 05ff84d..e7a137e 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -311,6 +311,74 @@ def test_load_wavelength_from_config_file_with_local_conf_file( assert actual_args.wavelength == expected["wavelength"] +@pytest.mark.parametrize( + "local_config, home_config", + [ + # C1: no config files exist + # expected: raise ValueError + (None, None), + # C2: local config is empty, no home config + # expected: raise ValueError + ({}, None), + # C3: no local config, home config is empty + # expected: raise ValueError + (None, {}), + # C4: both config files are empty + # expected: raise ValueError + ({}, {}), + ], +) +def test_load_wavelength_from_config_file_without_conf_files_bad( + mocker, + user_filesystem, + local_config, + home_config, +): + # User tries to correct data without specifying wavelength and + # no config files + # with wavelength exist -- expected to raise ValueError + cwd = Path(user_filesystem) + home_dir = cwd / "home_dir" + mocker.patch("pathlib.Path.home", return_value=home_dir) + os.chdir(cwd) + + local_config_file = cwd / "diffpyconfig.json" + if local_config_file.exists(): + local_config_file.unlink() + home_config_file = home_dir / "diffpyconfig.json" + if home_config_file.exists(): + home_config_file.unlink() + + if local_config is not None: + with open(local_config_file, "w") as f: + json.dump(local_config, f) + if home_config is not None: + with open(home_config_file, "w") as f: + json.dump(home_config, f) + + cli_inputs = ["mud", "data.xy", "2.5"] + actual_args = get_args_cli(cli_inputs) + + msg = re.escape( + "\nThe wavelength was not specified and no " + "configuration file 'diffpyconfig.json' containing " + "the wavelength or X-ray source was found in either the " + "local or home directories. Either specify the wavelength " + "or source using the -w/--wavelength option or " + "create a configuration file.\n\n" + "You can add the wavelength or anode type to a " + "configuration file on this computer. Once created, it " + "will be automatically used for subsequent diffpy data " + "by default, and you will only need to do this once.\n\n" + "For detailed instructions on creating the configuration " + "file, please refer to:\n" + "https://www.diffpy.org/diffpy.labpdfproc/examples/" + "toolsexample.html" + ) + with pytest.raises(ValueError, match=msg): + load_wavelength_from_config_file(actual_args) + + @pytest.mark.parametrize( "inputs, expected", [ @@ -318,8 +386,6 @@ def test_load_wavelength_from_config_file_with_local_conf_file( # expect to return args without modification. # This test only checks loading behavior, # not value validation (which is handled by `set_wavelength`). - # C1: no args - ([], {"wavelength": None}), # C1: wavelength provided (["--wavelength", "0.25"], {"wavelength": 0.25}), # C2: anode type provided