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

Add files as argument in CLI #246

Merged
merged 12 commits into from
Feb 21, 2024
10 changes: 5 additions & 5 deletions examples/apm/Write.NXapm.Example.1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@
" INPUT_RANGE = input_range_file_name[case_id]\n",
" OUTPUT = output_file_name[case_id]\n",
"\n",
" ! dataconverter --reader apm --nxdl NXapm --input-file $ELN --input-file $OASIS \\\n",
" --input-file $INPUT_RECON --input-file $INPUT_RANGE --output $OUTPUT"
" ! dataconverter --reader apm --nxdl NXapm $ELN $OASIS \\\n",
" $INPUT_RECON $INPUT_RANGE --output $OUTPUT"
]
},
{
Expand Down Expand Up @@ -369,7 +369,7 @@
"metadata": {},
"outputs": [],
"source": [
"# use matplotlib and numpy to plot histogram data \n",
"# use matplotlib and numpy to plot histogram data\n",
"xy = np.zeros([nbins, 2], np.float64)\n",
"xy[:,0] = np.linspace(mqmin + mqincr, mqmax + mqincr, nbins, endpoint=True)\n",
"xy[:,1] = 0.5 # * np.ones([nbins], np.float64) # 0.5 to be able to plot logarithm you can not measure half an atom\n",
Expand Down Expand Up @@ -417,7 +417,7 @@
"source": [
"# save the figure\n",
"figfn = nxs_file_name + \".MassToChargeStateRatio.png\"\n",
"fig.savefig(figfn, dpi=300, facecolor=\"w\", edgecolor=\"w\", orientation=\"landscape\", format=\"png\", \n",
"fig.savefig(figfn, dpi=300, facecolor=\"w\", edgecolor=\"w\", orientation=\"landscape\", format=\"png\",\n",
" transparent=False, bbox_inches=\"tight\", pad_inches=0.1, metadata=None)\n",
"# plt.close(\"all\")\n",
"print(f\"{figfn} stored to disk.\")"
Expand Down Expand Up @@ -484,7 +484,7 @@
},
"outputs": [],
"source": [
"! dataconverter --reader apm --nxdl NXapm --input-file synthesize1 --output apm.case0.nxs"
"! dataconverter --reader apm --nxdl NXapm synthesize1 --output apm.case0.nxs"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/ellipsometry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This is an example to use the dataconvert with the `ellips` reader and the `NXel
Just execute

```shell
dataconverter --reader ellips --nxdl NXellipsometry --input-file eln_data.yaml --output SiO2onSi.nxs
dataconverter --reader ellips --nxdl NXellipsometry eln_data.yaml --output SiO2onSi.nxs
```

in this directory.
Expand Down
2 changes: 1 addition & 1 deletion examples/em_nion/Write.NXem_nion.Example.1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
" SWIFT = swift_proj_file_name[case_id]\n",
" OUTPUT = output_file_name[case_id]\n",
"\n",
" ! dataconverter --reader em_nion --nxdl NXem --input-file $ELN --input-file $SWIFT --output $OUTPUT"
" ! dataconverter --reader em_nion --nxdl NXem $ELN $SWIFT --output $OUTPUT"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions examples/em_om/Write.NXem_ebsd.Example.1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
"! dataconverter \\\n",
"--reader em_om \\\n",
"--nxdl NXem_ebsd \\\n",
"--input-file eln_data_em_om.yaml \\\n",
"eln_data_em_om.yaml \\\n",
"--output \"em_om.case0.nxs\""
]
},
Expand Down Expand Up @@ -280,7 +280,7 @@
" INPUT = input_data_file_name[case_id]\n",
" OUTPUT = output_file_name[case_id]\n",
"\n",
" ! dataconverter --reader em_om --nxdl NXem_ebsd --input-file $ELN --input-file $INPUT --output $OUTPUT"
" ! dataconverter --reader em_om --nxdl NXem_ebsd $ELN $INPUT --output $OUTPUT"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions examples/em_spctrscpy/Write.NXem.Example.1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
" INPUT = input_data_file_name[case_id]\n",
" OUTPUT = output_file_name[case_id]\n",
"\n",
" ! dataconverter --reader em_spctrscpy --nxdl NXem --input-file $ELN --input-file $INPUT --output $OUTPUT"
" ! dataconverter --reader em_spctrscpy --nxdl NXem $ELN $INPUT --output $OUTPUT"
]
},
{
Expand Down Expand Up @@ -266,7 +266,7 @@
},
"outputs": [],
"source": [
"! dataconverter --reader em_spctrscpy --nxdl NXem --input-file synthesize1 --output em_sp.case0.nxs"
"! dataconverter --reader em_spctrscpy --nxdl NXem synthesize1 --output em_sp.case0.nxs"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions examples/json_map/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ user@box:~$ dataconverter --nxdl NXmynxdl --generate-template > mynxdl.mapping.j

### Automatically merge partial NeXus files
```console
user@box:~$ dataconverter --nxdl NXiv_temp --input-file voltage_and_temperature.nxs --input-file current.nxs --output auto_merged.nxs
user@box:~$ dataconverter --nxdl NXiv_temp voltage_and_temperature.nxs current.nxs --output auto_merged.nxs
```

### Map and copy over data to new NeXus file
```console
user@box:~$ dataconverter --nxdl NXiv_temp --mapping merge_copied.mapping.json --input-file voltage_and_temperature.nxs --input-file current.nxs --output merged_copied.nxs
user@box:~$ dataconverter --nxdl NXiv_temp --mapping merge_copied.mapping.json voltage_and_temperature.nxs current.nxs --output merged_copied.nxs
```

### Map and link over data to new NeXus file
```console
user@box:~$ dataconverter --nxdl NXiv_temp --mapping merge_linked.mapping.json --input-file voltage_and_temperature.nxs --input-file current.nxs --output merged_linked.nxs
user@box:~$ dataconverter --nxdl NXiv_temp --mapping merge_linked.mapping.json voltage_and_temperature.nxs current.nxs --output merged_linked.nxs
```

## Contact person in FAIRmat for this reader
Expand Down
10 changes: 5 additions & 5 deletions examples/mpes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ If you want to use some example data you can find small example files in [`tests
```shell
dataconverter --reader mpes \\
--nxdl NXmpes \\
--input-file xarray_saved_small_calibration \\
--input-file config_file.json \\
--input-file eln_data.yaml \\
xarray_saved_small_calibration \\
config_file.json \\
eln_data.yaml \\
--output mpes_example.nxs
```

Expand All @@ -23,8 +23,8 @@ For XPS data you may use the data in [`tests/data/dataconverter/readers/xps`](ht
```shell
dataconverter --reader xps \\
--nxdl NXmpes \\
--input-file eln_data.yaml \\
--input-file In-situ_PBTTT_XPS_SPECS.xml \\
eln_data.yaml \\
In-situ_PBTTT_XPS_SPECS.xml \\
--output xps_example.nxs
```

Expand Down
19 changes: 11 additions & 8 deletions pynxtools/dataconverter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ Converts experimental data to NeXus/HDF5 files based on any provided NXDL.

```console
user@box:~$ dataconverter --help
Usage: dataconverter [OPTIONS]
Usage: dataconverter [OPTIONS] [FILES]...

The CLI entrypoint for the convert function

Options:
--input-file TEXT The path to the input data file to read.
(Repeat for more than one file.)
--reader [apm|ellips|em_nion|em_om|em_spctrscpy|example|hall|json_map|json_yml|mpes|rii_database|sts|transmission|xps]
--input-file TEXT Deprecated: Please use the positional file
arguments instead. The path to the input
data file to read. (Repeat for more than one
file.)
--reader [apm|ellips|em_nion|em_om|em_spctrscpy|example|hall|json_map|json_yml|rii_database|transmission|xps|xrd|mpes]
The reader to use. default="example"
--nxdl TEXT The name of the NXDL file to use without
extension.
extension.This option is required if no '--
params-file' is supplied.
--output TEXT The path to the output NeXus file to be
generated.
--generate-template Just print out the template generated from
Expand All @@ -45,13 +48,13 @@ Options:
#### Merge partial NeXus files into one

```console
user@box:~$ dataconverter --nxdl nxdl --input-file partial1.nxs --input-file partial2.nxs
user@box:~$ dataconverter --nxdl nxdl partial1.nxs partial2.nxs
```

#### Map an HDF5/JSON/(Python Dict pickled in a pickle file)

```console
user@box:~$ dataconverter --nxdl nxdl --input-file any_data.hdf5 --mapping my_custom_map.mapping.json
user@box:~$ dataconverter --nxdl nxdl any_data.hdf5 --mapping my_custom_map.mapping.json
```

#### You can find actual examples with data files at [`examples/json_map`](../../examples/json_map/).
Expand All @@ -60,7 +63,7 @@ user@box:~$ dataconverter --nxdl nxdl --input-file any_data.hdf5 --mapping my_cu
#### Use with multiple input files

```console
user@box:~$ dataconverter --nxdl nxdl --input_file metadata --input_file data.raw --input_file otherfile
user@box:~$ dataconverter --nxdl nxdl metadata data.raw otherfile
```

## Writing a Reader
Expand Down
65 changes: 44 additions & 21 deletions pynxtools/dataconverter/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@
import logging
import os
import sys
from typing import List, Tuple, Optional
import xml.etree.ElementTree as ET
from pathlib import Path
from typing import List, Optional, Tuple

import click
import yaml

from pynxtools.dataconverter.readers.base.reader import BaseReader
from pynxtools.dataconverter import helpers
from pynxtools.dataconverter.writer import Writer
from pynxtools.dataconverter.readers.base.reader import BaseReader
from pynxtools.dataconverter.template import Template
from pynxtools.dataconverter.writer import Writer
from pynxtools.nexus import nexus

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -280,11 +281,15 @@ def parse_params_file(params_file):


@click.command()
@click.argument("files", nargs=-1, type=click.Path(exists=True))
@click.option(
"--input-file",
default=[],
multiple=True,
help="The path to the input data file to read. (Repeat for more than one file.)",
help=(
"Deprecated: Please use the positional file arguments instead. "
"The path to the input data file to read. (Repeat for more than one file.)"
),
)
@click.option(
"--reader",
Expand All @@ -295,8 +300,10 @@ def parse_params_file(params_file):
@click.option(
"--nxdl",
default=None,
required=False,
help="The name of the NXDL file to use without extension.",
help=(
"The name of the NXDL file to use without extension."
"This option is required if no '--params-file' is supplied."
),
)
@click.option(
"--output",
Expand Down Expand Up @@ -333,6 +340,7 @@ def parse_params_file(params_file):
)
# pylint: disable=too-many-arguments
def convert_cli(
files: Tuple[str, ...],
input_file: Tuple[str, ...],
reader: str,
nxdl: str,
Expand All @@ -348,29 +356,44 @@ def convert_cli(
if params_file:
try:
convert(**parse_params_file(params_file))
return
except TypeError as exc:
sys.tracebacklimit = 0
raise TypeError(
raise click.UsageError(
(
"Please make sure you have the following entries in your "
"parameter file:\n\n# NeXusParser Parameter File - v0.0.1"
"\n\ndataconverter:\n\treader: value\n\tnxdl: value\n\tin"
"put-file: value"
)
) from exc
else:
if nxdl is None:
sys.tracebacklimit = 0
raise IOError(
"\nError: Please supply an NXDL file with the option:"
" --nxdl <path to NXDL>"
)
if mapping:
reader = "json_map"
if mapping:
input_file = input_file + tuple([mapping])
convert(input_file, reader, nxdl, output, generate_template, fair, undocumented)
if nxdl is None:
raise click.UsageError("Missing option '--nxdl'")
if mapping:
reader = "json_map"
input_file = input_file + tuple([mapping])
sherjeelshabih marked this conversation as resolved.
Show resolved Hide resolved

file_list = []
for file in files:
if os.path.isdir(file):
p = Path(file)
for f in p.rglob("*"):
if f.is_file():
file_list.append(str(f))
continue
file_list.append(file)

if input_file:
logger.warning(
"The --input-file option is deprecated. Please use the positional arguments instead."
)

if __name__ == "__main__":
convert_cli() # pylint: disable=no-value-for-parameter
convert(
tuple(file_list) + input_file,
reader,
nxdl,
output,
generate_template,
fair,
undocumented,
)
8 changes: 4 additions & 4 deletions pynxtools/dataconverter/readers/json_map/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ There are some example files you can use:
[data.json](/tests/data/dataconverter/readers/json_map/data.json)

```console
user@box:~$ dataconverter --nxdl NXtest --input-file data.json --mapping data.mapping.json
user@box:~$ dataconverter --nxdl NXtest data.json --mapping data.mapping.json
```

##### [Example](/examples/json_map/) with HDF5 files.
Expand All @@ -34,23 +34,23 @@ The mapping files will always be based on the Template the dataconverter generat
The right hand side values of the Template keys are what you can modify.

Here are the three different ways you can fill the right hand side of the Template keys:
* Write the nested path in your datafile. This is indicated by a leading `/` before the word `entry` to make `/entry/data/current_295C` below.
* Write the nested path in your datafile. This is indicated by a leading `/` before the word `entry` to make `/entry/data/current_295C` below.
Example:

```json
"/ENTRY[entry]/DATA[data]/current_295C": "/entry/data/current_295C",
"/ENTRY[entry]/NXODD_name/posint_value": "/a_level_down/another_level_down/posint_value",
```

* Write the values directly in the mapping file for missing data from your data file.
* Write the values directly in the mapping file for missing data from your data file.

```json

"/ENTRY[entry]/PROCESS[process]/program": "Bluesky",
"/ENTRY[entry]/PROCESS[process]/program/@version": "1.6.7"
```

* Write JSON objects with a link key. This follows the same link mechanism that the dataconverter implements. In the context of this reader, you can only use external links to your data files. In the example below, `current.nxs` is an already existing HDF5 file that we link to in our new NeXus file without copying over the data. The format is as follows:
* Write JSON objects with a link key. This follows the same link mechanism that the dataconverter implements. In the context of this reader, you can only use external links to your data files. In the example below, `current.nxs` is an already existing HDF5 file that we link to in our new NeXus file without copying over the data. The format is as follows:
`"link": "<filename>:<path_in_file>"`
Note: This only works for HDF5 files currently.

Expand Down
7 changes: 4 additions & 3 deletions pynxtools/dataconverter/readers/json_map/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
# limitations under the License.
#
"""An example reader implementation for the DataConverter."""
from typing import Tuple, Any
import json
import pickle
from typing import Any, Tuple

import numpy as np
import xarray
from mergedeep import merge

from pynxtools.dataconverter import hdfdict
from pynxtools.dataconverter.readers.base.reader import BaseReader
from pynxtools.dataconverter.template import Template
from pynxtools.dataconverter import hdfdict


def parse_slice(slice_string):
Expand Down Expand Up @@ -211,7 +212,7 @@ def read(
{x: "/hierarchical/path/in/your/datafile" for x in template}
)
raise IOError(
"Please supply a JSON mapping file: --input-file"
"Please supply a JSON mapping file: "
" my_nxdl_map.mapping.json\n\n You can use this "
"template for the required fields: \n" + str(template)
)
Expand Down
4 changes: 2 additions & 2 deletions pynxtools/dataconverter/readers/xps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ An example script to run the XPS reader:
! dataconverter \
--reader xps \
--nxdl NXmpes \
--input-file $<xps-file path> \
--input-file $<eln-file path> \
$<xps-file path> \
$<eln-file path> \
--output <output-file path>.test.nxs
```
Note that none of the supported file format have data/values for all required and recommended fields and attributes in NXmpes. In order for the validation step of the **XPS** reader to pass,
Expand Down
4 changes: 2 additions & 2 deletions pynxtools/dataconverter/readers/xrd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ The reader can be run from Jupyter-notebook or Jupyter-lab with the following co
! dataconverter \
--reader xrd \
--nxdl NXxrd_pan \
--input-file $<xps-file location> \
--input-file $<eln-file location> \
$<xps-file location> \
$<eln-file location> \
--output <output-file location>.nxs
```

Expand Down
Loading
Loading