Skip to content

Commit

Permalink
Add napytau import to headless mock (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benedikt-Brunner authored Jan 18, 2025
1 parent e539672 commit 47b6d15
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
8 changes: 4 additions & 4 deletions napytau/cli/cli_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class CLIArguments:
dataset_format: str
data_files_directory: str
fit_file_path: Optional[str]
setup_file_path: Optional[str]
setup_identifier: Optional[str]

def __init__(self, raw_args: Namespace):
self.headless = coalesce(raw_args.headless, False)
self.dataset_format = raw_args.dataset_format
self.data_files_directory = coalesce(raw_args.data_files_directory, getcwd())
self.fit_file_path = raw_args.fit_file
self.setup_file_path = raw_args.setup_file
self.setup_identifier = raw_args.setup_identifier

def is_headless(self) -> bool:
return self.headless
Expand All @@ -31,5 +31,5 @@ def get_data_files_directory_path(self) -> str:
def get_fit_file_path(self) -> Optional[str]:
return self.fit_file_path

def get_setup_file_path(self) -> Optional[str]:
return self.setup_file_path
def get_setup_identifier(self) -> Optional[str]:
return self.setup_identifier
12 changes: 7 additions & 5 deletions napytau/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@ def parse_cli_arguments() -> CLIArguments:
parser.add_argument(
"--data_files_directory",
type=str,
help="Path to the directory containing either data files or subdirectories "
"with data files",
help="""Path to the directory containing either data files or subdirectories
with data files""",
)
parser.add_argument(
"--fit_file",
type=str,
help="Path to a fit file to use instead of the one found in the setup files",
help="""Path to a fit file to use instead of the one found in the setup files,
only relevant for legacy format""",
)

parser.add_argument(
"--setup_file",
"--setup_identifier",
type=str,
help="Path to a setup file to load",
help="""Identifier of the setup to use with the dataset, file path for legacy
format, or setup name for NaPyTau format""",
)

return CLIArguments(parser.parse_args())
51 changes: 50 additions & 1 deletion napytau/headless/headless_mockup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
from napytau.cli.cli_arguments import CLIArguments
from napytau.import_export.import_export import (
IMPORT_FORMAT_LEGACY,
IMPORT_FORMAT_NAPYTAU,
import_legacy_format_from_files,
read_legacy_setup_data_into_data_set,
import_napytau_format_from_files,
read_napytau_setup_data_into_data_set,
)
from napytau.import_export.model.dataset import DataSet
from napytau.util.coalesce import coalesce
Expand Down Expand Up @@ -49,7 +52,7 @@ def init(cli_arguments: CLIArguments) -> None:
print("-" * 80)
print("=" * 80)

setup_file_path = cli_arguments.get_setup_file_path()
setup_file_path = cli_arguments.get_setup_identifier()
if setup_file_path is not None:
for dataset in datasets:
read_legacy_setup_data_into_data_set(dataset, PurePath(setup_file_path))
Expand All @@ -76,7 +79,53 @@ def init(cli_arguments: CLIArguments) -> None:
print(f" Active: {datapoint.is_active()} ")
print("-" * 80)
print("=" * 80)
elif cli_arguments.get_dataset_format() == IMPORT_FORMAT_NAPYTAU:
setup_files_directory_path = cli_arguments.get_data_files_directory_path()
setup_identifier = cli_arguments.get_setup_identifier()

for dataset, raw_setups in import_napytau_format_from_files(
PurePath(setup_files_directory_path)
):
if setup_identifier is not None:
read_napytau_setup_data_into_data_set(
dataset, raw_setups, setup_identifier
)

print("Dataset:")
print(f" Velocity: {dataset.relative_velocity.value.get_velocity()}")
print(f" Velocity Error: {dataset.relative_velocity.error.get_velocity()}")
print(f" Tau factor: {dataset.get_tau_factor()}")
print(f" Polynomial count: {dataset.get_polynomial_count()}")

for index, sampling_point in enumerate(
coalesce(dataset.get_sampling_points(), [])
):
print(f" Sampling point #{index}: {sampling_point}")

for datapoint in dataset.datapoints:
print(" Datapoint:")
print(
f" Distance: Value: {datapoint.get_distance().value} "
f"Error: {datapoint.get_distance().error}"
)
print(
f" Calibration: Value: {datapoint.get_calibration().value} "
f"Error: {datapoint.get_calibration().error}"
)
shifted_intensity, unshifted_intensity = datapoint.get_intensity()
print(
f" Shifted Intensity: Value: {shifted_intensity.value} "
f"Error: {shifted_intensity.error}"
)
print(
f" Unshifted Intensity: Value: {unshifted_intensity.value} "
f"Error: {unshifted_intensity.error}"
)
print(
f" Distance: Value: {datapoint.get_distance().value} "
f"Error: {datapoint.get_distance().error}"
)
print(f" Active: {datapoint.is_active()} ")
else:
raise ValueError(
f"Unknown dataset format: {cli_arguments.get_dataset_format()}"
Expand Down
11 changes: 7 additions & 4 deletions tests/cli/parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def test_createsAnArgumentParserInstanceAndConfiguresIt(self):
("--data_files_directory",),
{
"type": str,
"help": "Path to the directory containing either data files or subdirectories with data files",
"help": """Path to the directory containing either data files or subdirectories
with data files""",
},
),
)
Expand All @@ -69,18 +70,20 @@ def test_createsAnArgumentParserInstanceAndConfiguresIt(self):
("--fit_file",),
{
"type": str,
"help": "Path to a fit file to use instead of the one found in the setup files", # noqa E501
"help": """Path to a fit file to use instead of the one found in the setup files,
only relevant for legacy format"""
},
),
)

self.assertEqual(
argument_parser_mock.add_argument.mock_calls[4],
(
("--setup_file",),
("--setup_identifier",),
{
"type": str,
"help": "Path to a setup file to load",
"help": """Identifier of the setup to use with the dataset, file path for legacy
format, or setup name for NaPyTau format""",
},
),
)
Expand Down

0 comments on commit 47b6d15

Please sign in to comment.