pytesprocess 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytesprocess/__init__.py +9 -0
- pytesprocess/_version.py +2 -0
- pytesprocess/cli/__init__.py +1 -0
- pytesprocess/cli/commands/__init__.py +5 -0
- pytesprocess/cli/commands/event.py +66 -0
- pytesprocess/cli/commands/filter.py +17 -0
- pytesprocess/cli/commands/ivsweep.py +29 -0
- pytesprocess/cli/common.py +86 -0
- pytesprocess/cli/main.py +81 -0
- pytesprocess/config/__init__.py +4 -0
- pytesprocess/config/loader.py +94 -0
- pytesprocess/config/manager.py +297 -0
- pytesprocess/config/resolvers/__init__.py +5 -0
- pytesprocess/config/resolvers/common.py +56 -0
- pytesprocess/config/resolvers/feature.py +293 -0
- pytesprocess/config/resolvers/salting.py +86 -0
- pytesprocess/config/resolvers/trigger.py +84 -0
- pytesprocess/config/selectors.py +108 -0
- pytesprocess/config/validation.py +314 -0
- pytesprocess/config/warnings.py +2 -0
- pytesprocess/core/__init__.py +10 -0
- pytesprocess/core/algorithms.py +1455 -0
- pytesprocess/core/didv.py +1648 -0
- pytesprocess/core/eventbuilder.py +495 -0
- pytesprocess/core/filterbuilder.py +81 -0
- pytesprocess/core/filterdata.py +1849 -0
- pytesprocess/core/ivsweep.py +2072 -0
- pytesprocess/core/noise.py +923 -0
- pytesprocess/core/noisemodel.py +1408 -0
- pytesprocess/core/oftrigger.py +1035 -0
- pytesprocess/core/template.py +450 -0
- pytesprocess/process/__init__.py +6 -0
- pytesprocess/process/data_source.py +185 -0
- pytesprocess/process/event_context.py +35 -0
- pytesprocess/process/feature_plan.py +186 -0
- pytesprocess/process/feature_resources.py +267 -0
- pytesprocess/process/features.py +1024 -0
- pytesprocess/process/filterprocess.py +1176 -0
- pytesprocess/process/ivprocess.py +1380 -0
- pytesprocess/process/processing_data.py +967 -0
- pytesprocess/process/randoms.py +921 -0
- pytesprocess/process/triggers.py +1011 -0
- pytesprocess/salting/__init__.py +7 -0
- pytesprocess/salting/generator.py +364 -0
- pytesprocess/salting/injector.py +329 -0
- pytesprocess/salting/sampling.py +84 -0
- pytesprocess/utils/__init__.py +5 -0
- pytesprocess/utils/arg_utils.py +122 -0
- pytesprocess/utils/dataframe_output.py +120 -0
- pytesprocess/utils/filter_hdf5.py +594 -0
- pytesprocess/utils/utils.py +701 -0
- pytesprocess/workflows/__init__.py +3 -0
- pytesprocess/workflows/processing.py +317 -0
- pytesprocess/workflows/salting.py +133 -0
- pytesprocess-0.1.1.dist-info/METADATA +211 -0
- pytesprocess-0.1.1.dist-info/RECORD +60 -0
- pytesprocess-0.1.1.dist-info/WHEEL +5 -0
- pytesprocess-0.1.1.dist-info/entry_points.txt +2 -0
- pytesprocess-0.1.1.dist-info/licenses/LICENSE +21 -0
- pytesprocess-0.1.1.dist-info/top_level.txt +1 -0
pytesprocess/__init__.py
ADDED
pytesprocess/_version.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .main import main
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Default event-processing CLI arguments."""
|
|
2
|
+
|
|
3
|
+
from pytesprocess.workflows.processing import run_processing_workflow
|
|
4
|
+
|
|
5
|
+
_PROCESS_STEPS = ("randoms", "salting", "trigger", "feature")
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def configure(parser):
|
|
9
|
+
parser.add_argument(
|
|
10
|
+
"--steps",
|
|
11
|
+
nargs="+",
|
|
12
|
+
required=True,
|
|
13
|
+
metavar="STEP[,STEP...]",
|
|
14
|
+
help=(
|
|
15
|
+
"Processing steps to run. Values may be space-separated, "
|
|
16
|
+
"comma-separated, or mixed (for example '--steps randoms trigger "
|
|
17
|
+
"feature' or '--steps randoms,trigger,feature'). User order is "
|
|
18
|
+
"ignored; execution order is always randoms -> salting -> trigger "
|
|
19
|
+
"-> feature. Valid steps: " + ", ".join(_PROCESS_STEPS) + "."
|
|
20
|
+
),
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
# Trigger/feature options
|
|
24
|
+
parser.add_argument("--ntriggers", type=int, default=-1)
|
|
25
|
+
parser.add_argument("--nevents", type=int, default=-1)
|
|
26
|
+
parser.add_argument(
|
|
27
|
+
"--measurement-type",
|
|
28
|
+
default="background",
|
|
29
|
+
choices=["background", "calibration", "threshold"],
|
|
30
|
+
help=(
|
|
31
|
+
"Raw measurement type for trigger/feature processing. Threshold "
|
|
32
|
+
"data may be processed directly by the feature step without a "
|
|
33
|
+
"trigger dataframe."
|
|
34
|
+
),
|
|
35
|
+
)
|
|
36
|
+
parser.add_argument(
|
|
37
|
+
"--trigger-dataframe",
|
|
38
|
+
help="Existing trigger dataframe group/file for feature processing.",
|
|
39
|
+
)
|
|
40
|
+
parser.add_argument(
|
|
41
|
+
"--salting-dataframe",
|
|
42
|
+
help=(
|
|
43
|
+
"Existing salting dataframe to inject during trigger/feature "
|
|
44
|
+
"processing. Do not combine with the salting generation step."
|
|
45
|
+
),
|
|
46
|
+
)
|
|
47
|
+
parser.add_argument("--external-features")
|
|
48
|
+
parser.add_argument(
|
|
49
|
+
"--partition-duration",
|
|
50
|
+
type=float,
|
|
51
|
+
default=10.0,
|
|
52
|
+
dest="partition_duration",
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
# Random-generation options. They are validated only when randoms is
|
|
56
|
+
# requested, so the same top-level command works for other step sets.
|
|
57
|
+
count = parser.add_mutually_exclusive_group(required=False)
|
|
58
|
+
count.add_argument("--nrandoms", type=int)
|
|
59
|
+
count.add_argument("--random-rate", type=float)
|
|
60
|
+
parser.add_argument("--min-separation-msec", type=float, default=0.0)
|
|
61
|
+
parser.add_argument("--edge-exclusion-msec", type=float, default=0.0)
|
|
62
|
+
parser.add_argument("--random-seed", type=int)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def run(args):
|
|
66
|
+
return run_processing_workflow(args)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from pytesprocess.process import FilterDataProcessing
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def configure(parser):
|
|
5
|
+
parser.add_argument("--channels", nargs="+")
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def run(args):
|
|
9
|
+
proc = FilterDataProcessing(
|
|
10
|
+
args.acquisition, streams=args.streams, restricted=args.restricted,
|
|
11
|
+
config_file=args.config, verbose=not args.quiet,
|
|
12
|
+
)
|
|
13
|
+
proc.process(
|
|
14
|
+
channels=args.channels, processing_label=args.processing_label,
|
|
15
|
+
lgc_output=False, lgc_save=True,
|
|
16
|
+
save_file_path=args.output, ncores=args.ncores,
|
|
17
|
+
)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from pytesprocess.process import IVSweepProcessing
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def configure(parser):
|
|
5
|
+
parser.add_argument("--channels", nargs="+")
|
|
6
|
+
parser.add_argument("--iv-only", action="store_true")
|
|
7
|
+
parser.add_argument("--didv-only", action="store_true")
|
|
8
|
+
parser.add_argument("--trace-length-msec", type=float, default=100.0)
|
|
9
|
+
parser.add_argument("--nrandoms-iv", type=int)
|
|
10
|
+
parser.add_argument("--random-seed", type=int)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def run(args):
|
|
14
|
+
if args.iv_only and args.didv_only:
|
|
15
|
+
raise ValueError("Use only one of --iv-only or --didv-only")
|
|
16
|
+
proc = IVSweepProcessing(
|
|
17
|
+
args.acquisition, processing_label=args.processing_label,
|
|
18
|
+
verbose=not args.quiet,
|
|
19
|
+
)
|
|
20
|
+
proc.process(
|
|
21
|
+
channels=args.channels,
|
|
22
|
+
enable_iv=not args.didv_only,
|
|
23
|
+
enable_didv=not args.iv_only,
|
|
24
|
+
trace_length_iv_msec=args.trace_length_msec,
|
|
25
|
+
nrandoms_iv=args.nrandoms_iv,
|
|
26
|
+
random_seed=args.random_seed,
|
|
27
|
+
lgc_output=False, lgc_save=True,
|
|
28
|
+
save_path=args.output, ncores=args.ncores,
|
|
29
|
+
)
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""Shared helpers for the pytesprocess command-line interface."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from pytesdaqx.io import AcquisitionCatalog
|
|
8
|
+
|
|
9
|
+
from pytesprocess.config import ProcessingConfig
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def add_common_acquisition_args(parser, *, config=False, ncores=True):
|
|
13
|
+
parser.add_argument("acquisition", help="Raw pytesdaqx acquisition path")
|
|
14
|
+
if config:
|
|
15
|
+
parser.add_argument("--config", required=True, help="Processing YAML file")
|
|
16
|
+
parser.add_argument("--streams", nargs="+", help="Optional stream IDs")
|
|
17
|
+
parser.add_argument("--output", help="Base output directory")
|
|
18
|
+
parser.add_argument("--processing-label", help="Optional human-readable processing label")
|
|
19
|
+
if ncores:
|
|
20
|
+
parser.add_argument("--ncores", type=int, default=1, help="Number of local worker processes")
|
|
21
|
+
parser.add_argument("--restricted", action="store_true", help="Process restricted background data only")
|
|
22
|
+
parser.add_argument("--quiet", action="store_true", help="Reduce informational output")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def catalog_for(args, measurement_types=None):
|
|
26
|
+
measurement = measurement_types
|
|
27
|
+
if isinstance(measurement, (list, tuple, set)):
|
|
28
|
+
unique = {str(item) for item in measurement}
|
|
29
|
+
background_only = unique == {"background"}
|
|
30
|
+
else:
|
|
31
|
+
background_only = (measurement is None or str(measurement) == "background")
|
|
32
|
+
|
|
33
|
+
if args.restricted and not background_only:
|
|
34
|
+
raise ValueError(
|
|
35
|
+
"--restricted is only valid for background data. It selects "
|
|
36
|
+
"restricted background data only, not open + restricted data."
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
catalog = AcquisitionCatalog(args.acquisition, verbose=not args.quiet)
|
|
40
|
+
return catalog.filter(
|
|
41
|
+
streams=args.streams,
|
|
42
|
+
measurement_types=measurement_types,
|
|
43
|
+
restricted=bool(args.restricted) if background_only else False,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def processing_config(args, *, workflows, measurement_types="background", check_resources=True):
|
|
48
|
+
catalog = catalog_for(args, measurement_types=measurement_types)
|
|
49
|
+
config = ProcessingConfig(
|
|
50
|
+
args.config,
|
|
51
|
+
catalog.record_channels,
|
|
52
|
+
sample_rate=catalog.sample_rate_hz,
|
|
53
|
+
verbose=not args.quiet,
|
|
54
|
+
)
|
|
55
|
+
config.validate(workflows, check_resources=check_resources, display=not args.quiet)
|
|
56
|
+
return config, catalog
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def default_output_base(acquisition):
|
|
60
|
+
path = Path(acquisition)
|
|
61
|
+
parent = path.parent
|
|
62
|
+
if parent.name == "raw":
|
|
63
|
+
return str(parent.parent / "processed")
|
|
64
|
+
return str(parent / "processed")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def output_base(args):
|
|
68
|
+
return args.output or default_output_base(args.acquisition)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def trigger_edge_parameters(config, catalog):
|
|
72
|
+
"""Return trigger edge exclusion and legacy-HDF5 livetime."""
|
|
73
|
+
from pytesprocess.core import FilterData
|
|
74
|
+
from pytesprocess.utils import get_trigger_template_info
|
|
75
|
+
|
|
76
|
+
trigger_config = config.get_config("trigger")
|
|
77
|
+
filter_file = trigger_config["overall"]["filter_file"]
|
|
78
|
+
filter_data = FilterData()
|
|
79
|
+
filter_data.load_hdf5(filter_file, overwrite=True)
|
|
80
|
+
info = get_trigger_template_info(trigger_config, filter_data)
|
|
81
|
+
edge_msec = float(info["max_edge_exclusion"])
|
|
82
|
+
livetime = None
|
|
83
|
+
if catalog.storage_format == "hdf5":
|
|
84
|
+
nsegments = sum(int(entry.get("n_segments") or 0) for entry in catalog.entries)
|
|
85
|
+
livetime = float(catalog.get_duration()) - nsegments * 2.0 * edge_msec * 1e-3
|
|
86
|
+
return edge_msec, livetime
|
pytesprocess/cli/main.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""Installed ``pytesprocess`` command-line interface."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import multiprocessing
|
|
7
|
+
import sys
|
|
8
|
+
|
|
9
|
+
from .common import add_common_acquisition_args
|
|
10
|
+
from .commands import event, filter as filter_command, ivsweep
|
|
11
|
+
|
|
12
|
+
_SECONDARY_COMMANDS = {
|
|
13
|
+
"filter": filter_command,
|
|
14
|
+
"ivsweep": ivsweep,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _event_parser():
|
|
19
|
+
parser = argparse.ArgumentParser(
|
|
20
|
+
prog="pytesprocess",
|
|
21
|
+
description=(
|
|
22
|
+
"TES event-processing workflow. Provide an acquisition and the "
|
|
23
|
+
"requested --steps. Separate filter and ivsweep workflows are "
|
|
24
|
+
"available as 'pytesprocess filter ...' and "
|
|
25
|
+
"'pytesprocess ivsweep ...'."
|
|
26
|
+
),
|
|
27
|
+
)
|
|
28
|
+
add_common_acquisition_args(parser, config=True, ncores=True)
|
|
29
|
+
event.configure(parser)
|
|
30
|
+
return parser
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _secondary_parser(name, module):
|
|
34
|
+
parser = argparse.ArgumentParser(
|
|
35
|
+
prog=f"pytesprocess {name}",
|
|
36
|
+
description=f"pytesprocess {name} workflow",
|
|
37
|
+
)
|
|
38
|
+
add_common_acquisition_args(
|
|
39
|
+
parser,
|
|
40
|
+
config=(name == "filter"),
|
|
41
|
+
ncores=True,
|
|
42
|
+
)
|
|
43
|
+
module.configure(parser)
|
|
44
|
+
return parser
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def build_parser(command=None):
|
|
48
|
+
"""Build one CLI parser.
|
|
49
|
+
|
|
50
|
+
``command=None`` builds the default event-processing parser. ``filter``
|
|
51
|
+
and ``ivsweep`` build the two explicit secondary workflow parsers.
|
|
52
|
+
"""
|
|
53
|
+
if command is None:
|
|
54
|
+
return _event_parser()
|
|
55
|
+
if command not in _SECONDARY_COMMANDS:
|
|
56
|
+
raise ValueError(f"Unknown pytesprocess command: {command}")
|
|
57
|
+
return _secondary_parser(command, _SECONDARY_COMMANDS[command])
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def main(argv=None):
|
|
61
|
+
try:
|
|
62
|
+
multiprocessing.set_start_method("spawn")
|
|
63
|
+
except RuntimeError:
|
|
64
|
+
pass
|
|
65
|
+
|
|
66
|
+
args_in = list(sys.argv[1:] if argv is None else argv)
|
|
67
|
+
|
|
68
|
+
if args_in and args_in[0] in _SECONDARY_COMMANDS:
|
|
69
|
+
command = args_in.pop(0)
|
|
70
|
+
module = _SECONDARY_COMMANDS[command]
|
|
71
|
+
parser = build_parser(command)
|
|
72
|
+
args = parser.parse_args(args_in)
|
|
73
|
+
return module.run(args)
|
|
74
|
+
|
|
75
|
+
parser = build_parser()
|
|
76
|
+
args = parser.parse_args(args_in)
|
|
77
|
+
return event.run(args)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
if __name__ == "__main__":
|
|
81
|
+
main()
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import copy
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
import warnings
|
|
6
|
+
import yaml
|
|
7
|
+
from yaml.loader import SafeLoader
|
|
8
|
+
|
|
9
|
+
from .warnings import ConfigDeprecationWarning
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class UniqueKeyLoader(SafeLoader):
|
|
13
|
+
def construct_mapping(self, node, deep=False):
|
|
14
|
+
if not isinstance(node, yaml.MappingNode):
|
|
15
|
+
raise yaml.constructor.ConstructorError(
|
|
16
|
+
None, None, 'expected a mapping node, but found %s' % node.id,
|
|
17
|
+
node.start_mark)
|
|
18
|
+
mapping = {}
|
|
19
|
+
for key_node, value_node in node.value:
|
|
20
|
+
key = self.construct_object(key_node, deep=deep)
|
|
21
|
+
if key in mapping:
|
|
22
|
+
raise ValueError(
|
|
23
|
+
f'ERROR: Duplicate key "{key}" found in YAML file. '
|
|
24
|
+
'Duplicate configuration keys are not allowed.'
|
|
25
|
+
)
|
|
26
|
+
mapping[key] = self.construct_object(value_node, deep=deep)
|
|
27
|
+
return mapping
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _read_yaml(path: Path):
|
|
31
|
+
with path.open('r') as handle:
|
|
32
|
+
data = yaml.load(handle, Loader=UniqueKeyLoader)
|
|
33
|
+
if data is None:
|
|
34
|
+
data = {}
|
|
35
|
+
if not isinstance(data, dict):
|
|
36
|
+
raise ValueError(f'ERROR: Top-level YAML content in "{path}" must be a mapping.')
|
|
37
|
+
return data
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _deep_merge(base, override):
|
|
41
|
+
out = copy.deepcopy(base)
|
|
42
|
+
for key, value in override.items():
|
|
43
|
+
if key in out and isinstance(out[key], dict) and isinstance(value, dict):
|
|
44
|
+
out[key] = _deep_merge(out[key], value)
|
|
45
|
+
else:
|
|
46
|
+
out[key] = copy.deepcopy(value)
|
|
47
|
+
return out
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def load_processing_yaml(yaml_file):
|
|
51
|
+
"""Load one processing YAML, including relative include files.
|
|
52
|
+
|
|
53
|
+
config_version >= 2 uses deep merge with included files as bases and the
|
|
54
|
+
main file as the final override. Legacy files retain the old include
|
|
55
|
+
precedence (included top-level keys override the main file) for backward
|
|
56
|
+
compatibility, but include paths are still resolved relative to the parent.
|
|
57
|
+
"""
|
|
58
|
+
path = Path(yaml_file).expanduser().resolve()
|
|
59
|
+
if not path.is_file():
|
|
60
|
+
raise ValueError(f'ERROR: Configuration file "{yaml_file}" not found!')
|
|
61
|
+
|
|
62
|
+
root = _read_yaml(path)
|
|
63
|
+
version = int(root.get('config_version', 1) or 1)
|
|
64
|
+
includes = root.pop('include', None)
|
|
65
|
+
if includes is None:
|
|
66
|
+
return root, version, path
|
|
67
|
+
if isinstance(includes, (str, Path)):
|
|
68
|
+
includes = [includes]
|
|
69
|
+
if not isinstance(includes, list):
|
|
70
|
+
raise ValueError('ERROR: "include" must be a filename or list of filenames.')
|
|
71
|
+
|
|
72
|
+
include_data = {}
|
|
73
|
+
for include_name in includes:
|
|
74
|
+
include_path = Path(include_name).expanduser()
|
|
75
|
+
if not include_path.is_absolute():
|
|
76
|
+
include_path = path.parent / include_path
|
|
77
|
+
child, _, _ = load_processing_yaml(include_path)
|
|
78
|
+
if version >= 2:
|
|
79
|
+
include_data = _deep_merge(include_data, child)
|
|
80
|
+
else:
|
|
81
|
+
include_data.update(copy.deepcopy(child))
|
|
82
|
+
|
|
83
|
+
if version >= 2:
|
|
84
|
+
merged = _deep_merge(include_data, root)
|
|
85
|
+
else:
|
|
86
|
+
warnings.warn(
|
|
87
|
+
'Legacy YAML include precedence is deprecated. In config_version: 2, '
|
|
88
|
+
'included files are merged first and the current file overrides them.',
|
|
89
|
+
ConfigDeprecationWarning,
|
|
90
|
+
stacklevel=2,
|
|
91
|
+
)
|
|
92
|
+
merged = copy.deepcopy(root)
|
|
93
|
+
merged.update(include_data)
|
|
94
|
+
return merged, version, path
|