trxasviewer 0.6.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.
Files changed (36) hide show
  1. trxasviewer/__init__.py +29 -0
  2. trxasviewer/cli.py +75 -0
  3. trxasviewer/core/__init__.py +48 -0
  4. trxasviewer/core/array_ops.py +86 -0
  5. trxasviewer/core/constants.py +7 -0
  6. trxasviewer/core/dataset.py +902 -0
  7. trxasviewer/core/file_io.py +248 -0
  8. trxasviewer/core/fitting.py +491 -0
  9. trxasviewer/core/graph.py +296 -0
  10. trxasviewer/core/io.py +177 -0
  11. trxasviewer/core/plot.py +388 -0
  12. trxasviewer/core/result.py +327 -0
  13. trxasviewer/core/utilities.py +277 -0
  14. trxasviewer/gui/__init__.py +2 -0
  15. trxasviewer/gui/control/__init__.py +2 -0
  16. trxasviewer/gui/control/viewer_controller.py +237 -0
  17. trxasviewer/gui/control/workers.py +186 -0
  18. trxasviewer/gui/model/__init__.py +2 -0
  19. trxasviewer/gui/model/viewer_model.py +41 -0
  20. trxasviewer/gui/view/__init__.py +2 -0
  21. trxasviewer/gui/view/generated_ui.py +1058 -0
  22. trxasviewer/gui/view/pg_plot.py +76 -0
  23. trxasviewer/gui/view/viewer.ui +1712 -0
  24. trxasviewer/gui/view/viewer_view.py +1051 -0
  25. trxasviewer/gui/view/widgets.py +563 -0
  26. trxasviewer/modeling_gui/__init__.py +2 -0
  27. trxasviewer/modeling_gui/generated_modeling_ui.py +882 -0
  28. trxasviewer/modeling_gui/kinetics_optimizer.py +176 -0
  29. trxasviewer/modeling_gui/modeling.ui +1403 -0
  30. trxasviewer/modeling_gui/trxas_modeling.py +679 -0
  31. trxasviewer-0.6.1.dist-info/METADATA +150 -0
  32. trxasviewer-0.6.1.dist-info/RECORD +36 -0
  33. trxasviewer-0.6.1.dist-info/WHEEL +5 -0
  34. trxasviewer-0.6.1.dist-info/entry_points.txt +2 -0
  35. trxasviewer-0.6.1.dist-info/licenses/LICENSE +182 -0
  36. trxasviewer-0.6.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,29 @@
1
+ # Copyright © UChicago Argonne LLC
2
+ # See LICENSE file for details
3
+ """Top-level package for TrXASViewer."""
4
+
5
+ from importlib.metadata import version, PackageNotFoundError
6
+
7
+ __author__ = "Miaoqi Chu"
8
+ __email__ = "mqichu@anl.gov"
9
+
10
+ try:
11
+ __version__ = version("trxasviewer")
12
+ except PackageNotFoundError:
13
+ __version__ = "0.1.0"
14
+
15
+ # Core data class is always safe to expose — no Qt dependency
16
+ from .core.dataset import TrXASDataset
17
+
18
+ __all__ = ["TrXASDataset", "__version__", "main_gui", "main_modeling_gui"]
19
+
20
+
21
+ def __getattr__(name: str):
22
+ """Lazy-load GUI entry points so bare ``import trxasviewer`` does not pull Qt."""
23
+ if name == "main_gui":
24
+ from .gui.view.viewer_view import main_gui
25
+ return main_gui
26
+ if name == "main_modeling_gui":
27
+ from .modeling_gui.trxas_modeling import main_modeling_gui
28
+ return main_modeling_gui
29
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
trxasviewer/cli.py ADDED
@@ -0,0 +1,75 @@
1
+ # Copyright © UChicago Argonne LLC
2
+ # See LICENSE file for details
3
+ import argparse
4
+ import sys
5
+ from trxasviewer import main_gui, main_modeling_gui, __version__
6
+
7
+
8
+ def create_argparser():
9
+ """Creates and returns an argument parser for trxasviewer CLI."""
10
+ if len(sys.argv) == 1 or sys.argv[1] not in {"view", "model"}:
11
+ sys.argv.insert(1, "view") # Default subcommand is 'view'
12
+
13
+ parser = argparse.ArgumentParser(
14
+ prog="trxasviewer",
15
+ description="TRXAS Viewer - A GUI application for TRXAS data visualization.",
16
+ )
17
+
18
+ subparsers = parser.add_subparsers(dest="command", required=True)
19
+
20
+ # --- View Subcommand ---
21
+ view_parser = subparsers.add_parser("view", help="Visualize raw TrXAS datasets")
22
+ view_parser.add_argument(
23
+ "--version", action="version", version=f"trxasviewer {__version__}"
24
+ )
25
+ view_parser.add_argument(
26
+ "--rawfolder", "-r", type=str, help="Path to the raw data folder.", default=None
27
+ )
28
+ view_parser.add_argument(
29
+ "--syncbunch", "-s", type=int, help="Sync bunch index number.", default=None
30
+ )
31
+ view_parser.add_argument(
32
+ "--disable-autoload",
33
+ "-d",
34
+ action="store_true",
35
+ help="Disable automatically load previous settings.",
36
+ default=False,
37
+ )
38
+ view_parser.add_argument(
39
+ "--cachefolder", "-c",
40
+ type=str,
41
+ default=None,
42
+ help="Folder for preprocessed dataset cache (NPZ files). If not set, caching is disabled.",
43
+ )
44
+ view_parser.set_defaults(func=run_view)
45
+
46
+ # --- Model Subcommand ---
47
+ model_parser = subparsers.add_parser("model", help="Modeling TRXAS results.")
48
+ model_parser.set_defaults(func=run_model) # Placeholder
49
+
50
+ return parser
51
+
52
+
53
+ def run_view(args):
54
+ sys.exit(
55
+ main_gui(
56
+ rawfolder=args.rawfolder,
57
+ syncbunch=args.syncbunch,
58
+ autoload=(not args.disable_autoload),
59
+ cache_folder=args.cachefolder,
60
+ )
61
+ )
62
+
63
+
64
+ def run_model(args):
65
+ sys.exit(main_modeling_gui(args))
66
+
67
+
68
+ def main():
69
+ parser = create_argparser()
70
+ args = parser.parse_args()
71
+ args.func(args) # Dispatch to the correct handler
72
+
73
+
74
+ if __name__ == "__main__":
75
+ main()
@@ -0,0 +1,48 @@
1
+ # Copyright © UChicago Argonne LLC
2
+ # See LICENSE file for details
3
+ """
4
+ trxasviewer.core — headless data-processing library for TrXAS datasets.
5
+
6
+ All public symbols are Qt-free and importable in scripts and Jupyter notebooks
7
+ without a display backend.
8
+
9
+ Typical usage::
10
+
11
+ from trxasviewer.core import TrXASDataset, TrXASDatasetManager, TrXASResult
12
+ from trxasviewer.core import save_results
13
+
14
+ """
15
+
16
+ from .dataset import TrXASDataset, TrXASDatasetManager, create_trxas_dataset
17
+ from .result import TrXASResult
18
+ from .io import save_results, save_as_hdf5, save_as_origin_format, save_as_json
19
+ from .utilities import format_time, prepare_binning_matrix
20
+ from .fitting import (
21
+ create_q_matrix,
22
+ calculate_concentrations,
23
+ global_fit_kinetic_model,
24
+ )
25
+ from .constants import TIME_SCALES
26
+
27
+ __all__ = [
28
+ # Dataset loading
29
+ "TrXASDataset",
30
+ "TrXASDatasetManager",
31
+ "create_trxas_dataset",
32
+ # Results
33
+ "TrXASResult",
34
+ # Saving
35
+ "save_results",
36
+ "save_as_hdf5",
37
+ "save_as_origin_format",
38
+ "save_as_json",
39
+ # Utilities
40
+ "format_time",
41
+ "prepare_binning_matrix",
42
+ # Fitting
43
+ "create_q_matrix",
44
+ "calculate_concentrations",
45
+ "global_fit_kinetic_model",
46
+ # Constants
47
+ "TIME_SCALES",
48
+ ]
@@ -0,0 +1,86 @@
1
+ # Copyright © UChicago Argonne LLC
2
+ # See LICENSE file for details
3
+ import warnings
4
+ import logging
5
+
6
+ import numpy as np
7
+
8
+ from .utilities import remove_outlier
9
+
10
+
11
+ logger = logging.getLogger(__name__)
12
+
13
+
14
+ def pad_last_dim(data, target_size):
15
+ """Pad the last dimension of a 3D array with NaN to reach target_size.
16
+
17
+ Args:
18
+ data: Input array of shape ``(a, b, n)`` where ``n <= target_size``.
19
+ target_size: Desired size of the last dimension.
20
+
21
+ Returns:
22
+ Array of shape ``(a, b, target_size)``.
23
+ If ``n == target_size``, returns the input array unchanged.
24
+ """
25
+ if data.shape[2] >= target_size:
26
+ return data
27
+ padded = np.full((*data.shape[:2], target_size), np.nan, dtype=data.dtype)
28
+ padded[:, :, : data.shape[2]] = data
29
+ return padded
30
+
31
+
32
+ def normalize_by_orbitalmean_and_background(xas_data_3d, shape, num_rows):
33
+ """Normalize XAS signal channels by the per-orbital background mean.
34
+
35
+ Background channel (ch0) is averaged over each orbital to obtain a per-bunch
36
+ normalization factor. Signal channels (ch1, ch2) are divided by this factor.
37
+
38
+ Args:
39
+ xas_data_3d: Shape ``(num_rows, num_channel, total_bunches)`` float32 array.
40
+ shape: ``(num_channel, num_orbital, num_bunch)`` tuple from the file header.
41
+ num_rows: Number of energy/delay rows.
42
+
43
+ Returns:
44
+ norm_data: Shape ``(num_rows, total_bunches)`` normalized float32 array.
45
+ """
46
+ num_channel, num_orbital, num_bunch = shape
47
+ shape_4d = (num_rows, num_channel, num_orbital, num_bunch)
48
+
49
+ # pad with nan if needed, so that we can reshape to 4d for easier processing;
50
+ xas_data_3d = pad_last_dim(xas_data_3d, num_orbital * num_bunch)
51
+
52
+ xas_data_4d = xas_data_3d.reshape(shape_4d)
53
+
54
+ # ch0 is background; ch1 and ch2 are signals
55
+ orbital_mean_ch0 = np.nanmean(xas_data_4d[:, 0], axis=1) # rows x [orbital] x bunch
56
+
57
+ # it's (num_rows, total_bunches)
58
+ # Suppress numpy RuntimeWarning: NaN-padded bunches from incomplete acquisitions
59
+ # produce all-NaN slices, which nanmean handles correctly by returning NaN.
60
+ signal_channels = xas_data_3d[:, 1:3]
61
+ with warnings.catch_warnings():
62
+ warnings.simplefilter("ignore", RuntimeWarning)
63
+ norm_data = np.nanmean(signal_channels, axis=(1,))
64
+ cycle_indices = np.arange(norm_data.shape[1]) % num_bunch
65
+ norm_data /= orbital_mean_ch0[:, cycle_indices]
66
+ return norm_data
67
+
68
+
69
+ def preprocess_xas_data(xas_data_3d, shape, num_rows, **outlier_kwargs):
70
+ """Remove outliers from the background channel and normalize XAS data.
71
+
72
+ Args:
73
+ xas_data_3d: Shape ``(num_rows, num_channel, total_bunches)`` float32 array.
74
+ shape: ``(num_channel, num_orbital, num_bunch)`` tuple from the file header.
75
+ num_rows: Number of energy/delay rows.
76
+ **outlier_kwargs: Forwarded to :func:`remove_outlier`
77
+ (``outlier_method``, ``outlier_threshold``).
78
+
79
+ Returns:
80
+ Tuple of (xas_data_corrected, norm_data). xas_data_corrected has the same
81
+ shape as the input with outlier rows set to NaN; norm_data has shape
82
+ ``(num_rows, total_bunches)``.
83
+ """
84
+ xas_data_3d = remove_outlier(xas_data_3d, **outlier_kwargs)
85
+ norm_data = normalize_by_orbitalmean_and_background(xas_data_3d, shape, num_rows)
86
+ return xas_data_3d, norm_data
@@ -0,0 +1,7 @@
1
+ # Copyright © UChicago Argonne LLC
2
+ # See LICENSE file for details
3
+ # Map from time unit label to scale factor (relative to seconds)
4
+ TIME_SCALES = {"ns": 1e-9, "µs": 1e-6, "ms": 1e-3, "s": 1}
5
+
6
+ # Inverse map: scale factor → time unit label
7
+ SCALE_TO_TIME = {v: k for k, v in TIME_SCALES.items()}