py4D-browser-transform 0.0.2__tar.gz → 0.1.0__tar.gz
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.
- py4d_browser_transform-0.1.0/PKG-INFO +93 -0
- py4d_browser_transform-0.1.0/README.md +74 -0
- {py4d_browser_transform-0.0.2 → py4d_browser_transform-0.1.0}/pyproject.toml +1 -1
- py4d_browser_transform-0.1.0/src/py4D_browser_transform.egg-info/PKG-INFO +93 -0
- {py4d_browser_transform-0.0.2 → py4d_browser_transform-0.1.0}/src/py4D_browser_transform.egg-info/SOURCES.txt +5 -1
- {py4d_browser_transform-0.0.2 → py4d_browser_transform-0.1.0}/src/py4d_browser_plugin/transform/__init__.py +1 -1
- py4d_browser_transform-0.1.0/src/py4d_browser_plugin/transform/checkpoints.py +51 -0
- py4d_browser_transform-0.1.0/src/py4d_browser_plugin/transform/datacube_ops.py +147 -0
- py4d_browser_transform-0.1.0/src/py4d_browser_plugin/transform/dialogs.py +295 -0
- py4d_browser_transform-0.1.0/src/py4d_browser_plugin/transform/transform.py +284 -0
- py4d_browser_transform-0.1.0/tests/test_transform_datacube_updates.py +365 -0
- py4d_browser_transform-0.0.2/PKG-INFO +0 -67
- py4d_browser_transform-0.0.2/README.md +0 -48
- py4d_browser_transform-0.0.2/src/py4D_browser_transform.egg-info/PKG-INFO +0 -67
- py4d_browser_transform-0.0.2/src/py4d_browser_plugin/transform/transform.py +0 -237
- {py4d_browser_transform-0.0.2 → py4d_browser_transform-0.1.0}/LICENSE.txt +0 -0
- {py4d_browser_transform-0.0.2 → py4d_browser_transform-0.1.0}/setup.cfg +0 -0
- {py4d_browser_transform-0.0.2 → py4d_browser_transform-0.1.0}/src/py4D_browser_transform.egg-info/dependency_links.txt +0 -0
- {py4d_browser_transform-0.0.2 → py4d_browser_transform-0.1.0}/src/py4D_browser_transform.egg-info/requires.txt +0 -0
- {py4d_browser_transform-0.0.2 → py4d_browser_transform-0.1.0}/src/py4D_browser_transform.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py4D-browser-transform
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: py4D-browser plugin that adds utility functions which transform the datacube
|
|
5
|
+
Author-email: Chia-Hao Lee <chia-hao.lee@cornell.edu>
|
|
6
|
+
Project-URL: Homepage, https://github.com/chiahao3/py4D-browser-transform
|
|
7
|
+
Project-URL: Repository, https://github.com/chiahao3/py4D-browser-transform
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/chiahao3/py4D-browser-transform/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/chiahao3/py4D-browser-transform/blob/main/CHANGELOG.md
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.11
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE.txt
|
|
15
|
+
Requires-Dist: py4d-browser>=1.5.0
|
|
16
|
+
Requires-Dist: py4dstem>=0.14.9
|
|
17
|
+
Requires-Dist: numpy>=1.19
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# py4D-browser-transform
|
|
21
|
+
|
|
22
|
+
`py4D-browser-transform` is a plugin for [py4D-browser](https://github.com/sezelt/py4D-browser) that adds in-memory datacube transformation tools. It currently supports axis permutation, diffraction flips/transposes, RAM checkpoints and restore, and datacube slicing, subsampling, and binning.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
You can install `py4D-browser-transform` with pip or conda:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install py4d-browser-transform
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
> 💡 **Note:**
|
|
32
|
+
> - If you install into a fresh Python environment, `py4D-browser` and `py4DSTEM` will be automatically installed as dependencies so you don't need to install them first.
|
|
33
|
+
> - If you already have `py4D-browser` installed, you can install this plugin into the same Python environment.
|
|
34
|
+
|
|
35
|
+
A step-by-step guide including creating a fresh Python environment via conda would look like this:
|
|
36
|
+
```bash
|
|
37
|
+
conda create -n py4dgui python=3.12
|
|
38
|
+
conda activate py4dgui
|
|
39
|
+
python -m pip install --upgrade pip
|
|
40
|
+
python -m pip cache purge
|
|
41
|
+
pip install py4d-browser-transform
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
Simply run the following command to start the browser once you activated the corresponding Python environment:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
py4dgui
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
After installing this plugin, you should see the "Transform" submenu appear under the **"Plugins"** menu.
|
|
52
|
+
From here, you can:
|
|
53
|
+
|
|
54
|
+
- **Set Axis Permutation**: reorder the four datacube axes when the loaded dataset is not in the expected order.
|
|
55
|
+
- **Set Diffraction Flips**: flip the diffraction pattern up/down, left/right, or transpose its X/Y axes.
|
|
56
|
+
- **Save RAM Checkpoint**: store a RAM-only snapshot of the current in-memory datacube.
|
|
57
|
+
- **Restore RAM Checkpoint**: restore a previous RAM checkpoint, rename checkpoints, replace checkpoints, or delete checkpoints.
|
|
58
|
+
- **Slice / Subsample / Bin**: slice, subsample with Python-style step syntax such as `::2`, and bin along the displayed axes `Ry`, `Rx`, `Qy`, and `Qx`.
|
|
59
|
+
|
|
60
|
+

|
|
61
|
+
|
|
62
|
+
These operations directly modify the loaded in-memory datacube, but do not affect the raw file stored on disk. You can export the transformed datacube to disk using **File > Export Datacube**.
|
|
63
|
+
|
|
64
|
+
### RAM checkpoints
|
|
65
|
+
|
|
66
|
+
RAM checkpoints are session-local snapshots of the datacube data at the moment they are saved. They are intended as recovery points before destructive in-memory transformations such as slicing, subsampling, and binning.
|
|
67
|
+
|
|
68
|
+
Checkpoint restore loads the saved datacube snapshot and resets the flip/permutation markers back to their default clean state. In other words, a checkpoint represents "the datacube exactly as it looked then", not a reversible history of every flip or permutation action that produced it.
|
|
69
|
+
|
|
70
|
+
Large checkpoints can require substantial memory. The plugin warns before saving a checkpoint larger than 1 GiB, or when total stored checkpoint data would exceed 4 GiB.
|
|
71
|
+
|
|
72
|
+
### Slicing, subsampling, and binning
|
|
73
|
+
|
|
74
|
+
The **Slice / Subsample / Bin** dialog provides one row for each axis:
|
|
75
|
+
|
|
76
|
+
- `Ry`
|
|
77
|
+
- `Rx`
|
|
78
|
+
- `Qy`
|
|
79
|
+
- `Qx`
|
|
80
|
+
|
|
81
|
+
Use slice text like `:`, `10:100`, or `::4` to select or subsample data. Use the bin control to sum neighboring pixels along an axis. The dialog previews the output shape before applying the transform.
|
|
82
|
+
|
|
83
|
+
Calibration pixel sizes are updated when both axes in real space or both axes in diffraction space change by the same spacing factor. When the change cannot be represented safely by py4DSTEM's shared real-space or diffraction-space pixel size, calibration is preserved.
|
|
84
|
+
|
|
85
|
+
> **Note:**
|
|
86
|
+
> The order of flipping and permutation matters. If you mix these operations, reversing them manually requires applying the opposite operations in the opposite order. RAM checkpoints provide a simpler way to return to a known datacube state.
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
GNU GPLv3
|
|
91
|
+
|
|
92
|
+
**py4D-browser-transform** is open source software distributed under a GPLv3 license.
|
|
93
|
+
It is free to use, alter, or build on, provided that any work derived from **py4D-browser-transform** is also kept free and open.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# py4D-browser-transform
|
|
2
|
+
|
|
3
|
+
`py4D-browser-transform` is a plugin for [py4D-browser](https://github.com/sezelt/py4D-browser) that adds in-memory datacube transformation tools. It currently supports axis permutation, diffraction flips/transposes, RAM checkpoints and restore, and datacube slicing, subsampling, and binning.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
You can install `py4D-browser-transform` with pip or conda:
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pip install py4d-browser-transform
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
> 💡 **Note:**
|
|
13
|
+
> - If you install into a fresh Python environment, `py4D-browser` and `py4DSTEM` will be automatically installed as dependencies so you don't need to install them first.
|
|
14
|
+
> - If you already have `py4D-browser` installed, you can install this plugin into the same Python environment.
|
|
15
|
+
|
|
16
|
+
A step-by-step guide including creating a fresh Python environment via conda would look like this:
|
|
17
|
+
```bash
|
|
18
|
+
conda create -n py4dgui python=3.12
|
|
19
|
+
conda activate py4dgui
|
|
20
|
+
python -m pip install --upgrade pip
|
|
21
|
+
python -m pip cache purge
|
|
22
|
+
pip install py4d-browser-transform
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
Simply run the following command to start the browser once you activated the corresponding Python environment:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
py4dgui
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
After installing this plugin, you should see the "Transform" submenu appear under the **"Plugins"** menu.
|
|
33
|
+
From here, you can:
|
|
34
|
+
|
|
35
|
+
- **Set Axis Permutation**: reorder the four datacube axes when the loaded dataset is not in the expected order.
|
|
36
|
+
- **Set Diffraction Flips**: flip the diffraction pattern up/down, left/right, or transpose its X/Y axes.
|
|
37
|
+
- **Save RAM Checkpoint**: store a RAM-only snapshot of the current in-memory datacube.
|
|
38
|
+
- **Restore RAM Checkpoint**: restore a previous RAM checkpoint, rename checkpoints, replace checkpoints, or delete checkpoints.
|
|
39
|
+
- **Slice / Subsample / Bin**: slice, subsample with Python-style step syntax such as `::2`, and bin along the displayed axes `Ry`, `Rx`, `Qy`, and `Qx`.
|
|
40
|
+
|
|
41
|
+

|
|
42
|
+
|
|
43
|
+
These operations directly modify the loaded in-memory datacube, but do not affect the raw file stored on disk. You can export the transformed datacube to disk using **File > Export Datacube**.
|
|
44
|
+
|
|
45
|
+
### RAM checkpoints
|
|
46
|
+
|
|
47
|
+
RAM checkpoints are session-local snapshots of the datacube data at the moment they are saved. They are intended as recovery points before destructive in-memory transformations such as slicing, subsampling, and binning.
|
|
48
|
+
|
|
49
|
+
Checkpoint restore loads the saved datacube snapshot and resets the flip/permutation markers back to their default clean state. In other words, a checkpoint represents "the datacube exactly as it looked then", not a reversible history of every flip or permutation action that produced it.
|
|
50
|
+
|
|
51
|
+
Large checkpoints can require substantial memory. The plugin warns before saving a checkpoint larger than 1 GiB, or when total stored checkpoint data would exceed 4 GiB.
|
|
52
|
+
|
|
53
|
+
### Slicing, subsampling, and binning
|
|
54
|
+
|
|
55
|
+
The **Slice / Subsample / Bin** dialog provides one row for each axis:
|
|
56
|
+
|
|
57
|
+
- `Ry`
|
|
58
|
+
- `Rx`
|
|
59
|
+
- `Qy`
|
|
60
|
+
- `Qx`
|
|
61
|
+
|
|
62
|
+
Use slice text like `:`, `10:100`, or `::4` to select or subsample data. Use the bin control to sum neighboring pixels along an axis. The dialog previews the output shape before applying the transform.
|
|
63
|
+
|
|
64
|
+
Calibration pixel sizes are updated when both axes in real space or both axes in diffraction space change by the same spacing factor. When the change cannot be represented safely by py4DSTEM's shared real-space or diffraction-space pixel size, calibration is preserved.
|
|
65
|
+
|
|
66
|
+
> **Note:**
|
|
67
|
+
> The order of flipping and permutation matters. If you mix these operations, reversing them manually requires applying the opposite operations in the opposite order. RAM checkpoints provide a simpler way to return to a known datacube state.
|
|
68
|
+
|
|
69
|
+
## License
|
|
70
|
+
|
|
71
|
+
GNU GPLv3
|
|
72
|
+
|
|
73
|
+
**py4D-browser-transform** is open source software distributed under a GPLv3 license.
|
|
74
|
+
It is free to use, alter, or build on, provided that any work derived from **py4D-browser-transform** is also kept free and open.
|
|
@@ -10,7 +10,7 @@ authors = [
|
|
|
10
10
|
]
|
|
11
11
|
description = "py4D-browser plugin that adds utility functions which transform the datacube"
|
|
12
12
|
readme = "README.md"
|
|
13
|
-
requires-python = ">=3.
|
|
13
|
+
requires-python = ">=3.11"
|
|
14
14
|
classifiers = [
|
|
15
15
|
"Programming Language :: Python :: 3",
|
|
16
16
|
"Operating System :: OS Independent",
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py4D-browser-transform
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: py4D-browser plugin that adds utility functions which transform the datacube
|
|
5
|
+
Author-email: Chia-Hao Lee <chia-hao.lee@cornell.edu>
|
|
6
|
+
Project-URL: Homepage, https://github.com/chiahao3/py4D-browser-transform
|
|
7
|
+
Project-URL: Repository, https://github.com/chiahao3/py4D-browser-transform
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/chiahao3/py4D-browser-transform/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/chiahao3/py4D-browser-transform/blob/main/CHANGELOG.md
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.11
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE.txt
|
|
15
|
+
Requires-Dist: py4d-browser>=1.5.0
|
|
16
|
+
Requires-Dist: py4dstem>=0.14.9
|
|
17
|
+
Requires-Dist: numpy>=1.19
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# py4D-browser-transform
|
|
21
|
+
|
|
22
|
+
`py4D-browser-transform` is a plugin for [py4D-browser](https://github.com/sezelt/py4D-browser) that adds in-memory datacube transformation tools. It currently supports axis permutation, diffraction flips/transposes, RAM checkpoints and restore, and datacube slicing, subsampling, and binning.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
You can install `py4D-browser-transform` with pip or conda:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install py4d-browser-transform
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
> 💡 **Note:**
|
|
32
|
+
> - If you install into a fresh Python environment, `py4D-browser` and `py4DSTEM` will be automatically installed as dependencies so you don't need to install them first.
|
|
33
|
+
> - If you already have `py4D-browser` installed, you can install this plugin into the same Python environment.
|
|
34
|
+
|
|
35
|
+
A step-by-step guide including creating a fresh Python environment via conda would look like this:
|
|
36
|
+
```bash
|
|
37
|
+
conda create -n py4dgui python=3.12
|
|
38
|
+
conda activate py4dgui
|
|
39
|
+
python -m pip install --upgrade pip
|
|
40
|
+
python -m pip cache purge
|
|
41
|
+
pip install py4d-browser-transform
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
Simply run the following command to start the browser once you activated the corresponding Python environment:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
py4dgui
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
After installing this plugin, you should see the "Transform" submenu appear under the **"Plugins"** menu.
|
|
52
|
+
From here, you can:
|
|
53
|
+
|
|
54
|
+
- **Set Axis Permutation**: reorder the four datacube axes when the loaded dataset is not in the expected order.
|
|
55
|
+
- **Set Diffraction Flips**: flip the diffraction pattern up/down, left/right, or transpose its X/Y axes.
|
|
56
|
+
- **Save RAM Checkpoint**: store a RAM-only snapshot of the current in-memory datacube.
|
|
57
|
+
- **Restore RAM Checkpoint**: restore a previous RAM checkpoint, rename checkpoints, replace checkpoints, or delete checkpoints.
|
|
58
|
+
- **Slice / Subsample / Bin**: slice, subsample with Python-style step syntax such as `::2`, and bin along the displayed axes `Ry`, `Rx`, `Qy`, and `Qx`.
|
|
59
|
+
|
|
60
|
+

|
|
61
|
+
|
|
62
|
+
These operations directly modify the loaded in-memory datacube, but do not affect the raw file stored on disk. You can export the transformed datacube to disk using **File > Export Datacube**.
|
|
63
|
+
|
|
64
|
+
### RAM checkpoints
|
|
65
|
+
|
|
66
|
+
RAM checkpoints are session-local snapshots of the datacube data at the moment they are saved. They are intended as recovery points before destructive in-memory transformations such as slicing, subsampling, and binning.
|
|
67
|
+
|
|
68
|
+
Checkpoint restore loads the saved datacube snapshot and resets the flip/permutation markers back to their default clean state. In other words, a checkpoint represents "the datacube exactly as it looked then", not a reversible history of every flip or permutation action that produced it.
|
|
69
|
+
|
|
70
|
+
Large checkpoints can require substantial memory. The plugin warns before saving a checkpoint larger than 1 GiB, or when total stored checkpoint data would exceed 4 GiB.
|
|
71
|
+
|
|
72
|
+
### Slicing, subsampling, and binning
|
|
73
|
+
|
|
74
|
+
The **Slice / Subsample / Bin** dialog provides one row for each axis:
|
|
75
|
+
|
|
76
|
+
- `Ry`
|
|
77
|
+
- `Rx`
|
|
78
|
+
- `Qy`
|
|
79
|
+
- `Qx`
|
|
80
|
+
|
|
81
|
+
Use slice text like `:`, `10:100`, or `::4` to select or subsample data. Use the bin control to sum neighboring pixels along an axis. The dialog previews the output shape before applying the transform.
|
|
82
|
+
|
|
83
|
+
Calibration pixel sizes are updated when both axes in real space or both axes in diffraction space change by the same spacing factor. When the change cannot be represented safely by py4DSTEM's shared real-space or diffraction-space pixel size, calibration is preserved.
|
|
84
|
+
|
|
85
|
+
> **Note:**
|
|
86
|
+
> The order of flipping and permutation matters. If you mix these operations, reversing them manually requires applying the opposite operations in the opposite order. RAM checkpoints provide a simpler way to return to a known datacube state.
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
GNU GPLv3
|
|
91
|
+
|
|
92
|
+
**py4D-browser-transform** is open source software distributed under a GPLv3 license.
|
|
93
|
+
It is free to use, alter, or build on, provided that any work derived from **py4D-browser-transform** is also kept free and open.
|
|
@@ -7,4 +7,8 @@ src/py4D_browser_transform.egg-info/dependency_links.txt
|
|
|
7
7
|
src/py4D_browser_transform.egg-info/requires.txt
|
|
8
8
|
src/py4D_browser_transform.egg-info/top_level.txt
|
|
9
9
|
src/py4d_browser_plugin/transform/__init__.py
|
|
10
|
-
src/py4d_browser_plugin/transform/
|
|
10
|
+
src/py4d_browser_plugin/transform/checkpoints.py
|
|
11
|
+
src/py4d_browser_plugin/transform/datacube_ops.py
|
|
12
|
+
src/py4d_browser_plugin/transform/dialogs.py
|
|
13
|
+
src/py4d_browser_plugin/transform/transform.py
|
|
14
|
+
tests/test_transform_datacube_updates.py
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
from .transform import TransformPlugin
|
|
2
|
-
__version__ = '0.0
|
|
2
|
+
__version__ = '0.1.0' # 2026.05.23
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""RAM checkpoint records and datacube snapshot helpers."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
CHECKPOINT_WARNING_BYTES = 1024**3
|
|
7
|
+
CHECKPOINT_TOTAL_WARNING_BYTES = 4 * 1024**3
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class DatacubeCheckpoint:
|
|
12
|
+
checkpoint_id: int
|
|
13
|
+
name: str
|
|
14
|
+
created_at: object
|
|
15
|
+
shape: tuple
|
|
16
|
+
dtype: str
|
|
17
|
+
estimated_bytes: int
|
|
18
|
+
datacube: object
|
|
19
|
+
metadata: dict
|
|
20
|
+
window_title: str
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def copy_datacube(datacube):
|
|
24
|
+
if hasattr(datacube, "copy"):
|
|
25
|
+
return datacube.copy()
|
|
26
|
+
copied = type(datacube)(datacube.data.copy())
|
|
27
|
+
if hasattr(datacube, "calibration"):
|
|
28
|
+
copied.calibration = datacube.calibration.copy()
|
|
29
|
+
return copied
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def datacube_nbytes(datacube):
|
|
33
|
+
return int(getattr(datacube.data, "nbytes", 0))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def format_bytes(nbytes):
|
|
37
|
+
value = float(nbytes)
|
|
38
|
+
for unit in ("B", "KiB", "MiB", "GiB", "TiB"):
|
|
39
|
+
if value < 1024 or unit == "TiB":
|
|
40
|
+
if unit == "B":
|
|
41
|
+
return f"{int(value)} {unit}"
|
|
42
|
+
return f"{value:.1f} {unit}"
|
|
43
|
+
value /= 1024
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def checkpoint_metadata(datacube):
|
|
47
|
+
return {
|
|
48
|
+
"shape": tuple(datacube.data.shape),
|
|
49
|
+
"dtype": str(datacube.data.dtype),
|
|
50
|
+
"estimated_bytes": datacube_nbytes(datacube),
|
|
51
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"""Pure datacube slicing, subsampling, binning, and calibration helpers."""
|
|
2
|
+
|
|
3
|
+
from .checkpoints import copy_datacube
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
AXIS_LABELS = ("Ry", "Rx", "Qy", "Qx")
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _parse_slice(text):
|
|
10
|
+
text = text.strip()
|
|
11
|
+
if text in ("", ":"):
|
|
12
|
+
return slice(None)
|
|
13
|
+
if text.startswith("[") and text.endswith("]"):
|
|
14
|
+
text = text[1:-1].strip()
|
|
15
|
+
if ":" not in text:
|
|
16
|
+
index = int(text)
|
|
17
|
+
return slice(index, index + 1, 1)
|
|
18
|
+
parts = text.split(":")
|
|
19
|
+
if len(parts) > 3:
|
|
20
|
+
raise ValueError(f"Invalid slice syntax: {text!r}")
|
|
21
|
+
|
|
22
|
+
values = []
|
|
23
|
+
for part in parts:
|
|
24
|
+
values.append(None if part.strip() == "" else int(part))
|
|
25
|
+
values.extend([None] * (3 - len(values)))
|
|
26
|
+
result = slice(values[0], values[1], values[2])
|
|
27
|
+
if result.step is not None and result.step <= 0:
|
|
28
|
+
raise ValueError("Slice steps must be positive")
|
|
29
|
+
return result
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _bin_axis(data, axis, factor):
|
|
33
|
+
if factor <= 1:
|
|
34
|
+
return data
|
|
35
|
+
length = data.shape[axis]
|
|
36
|
+
usable = length - (length % factor)
|
|
37
|
+
if usable <= 0:
|
|
38
|
+
raise ValueError(
|
|
39
|
+
f"Axis {AXIS_LABELS[axis]} with length {length} is too small for bin {factor}"
|
|
40
|
+
)
|
|
41
|
+
if usable != length:
|
|
42
|
+
data = data[
|
|
43
|
+
tuple(
|
|
44
|
+
slice(0, usable) if i == axis else slice(None)
|
|
45
|
+
for i in range(data.ndim)
|
|
46
|
+
)
|
|
47
|
+
]
|
|
48
|
+
shape = list(data.shape)
|
|
49
|
+
shape[axis : axis + 1] = [usable // factor, factor]
|
|
50
|
+
return data.reshape(shape).sum(axis=axis + 1)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _set_calibration_value(calibration, setter_name, value):
|
|
54
|
+
if value is None:
|
|
55
|
+
return
|
|
56
|
+
setter = getattr(calibration, setter_name, None)
|
|
57
|
+
if setter is not None:
|
|
58
|
+
setter(value)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _update_transform_calibration(datacube, starts, spacing_factors):
|
|
62
|
+
calibration = getattr(datacube, "calibration", None)
|
|
63
|
+
if calibration is None:
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
real_factors = (spacing_factors[0], spacing_factors[1])
|
|
67
|
+
if real_factors[0] == real_factors[1] and real_factors[0] != 1:
|
|
68
|
+
pixel_size = calibration.get_R_pixel_size()
|
|
69
|
+
_set_calibration_value(
|
|
70
|
+
calibration, "set_R_pixel_size", pixel_size * real_factors[0]
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
q_factors = (spacing_factors[2], spacing_factors[3])
|
|
74
|
+
old_q_pixel_size = calibration.get_Q_pixel_size()
|
|
75
|
+
if q_factors[0] == q_factors[1] and q_factors[0] != 1:
|
|
76
|
+
_set_calibration_value(
|
|
77
|
+
calibration, "set_Q_pixel_size", old_q_pixel_size * q_factors[0]
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
origin = calibration.get_origin() if hasattr(calibration, "get_origin") else None
|
|
81
|
+
if origin is not None and old_q_pixel_size is not None:
|
|
82
|
+
qx0, qy0 = origin
|
|
83
|
+
if qx0 is not None and qy0 is not None:
|
|
84
|
+
calibration.set_origin(
|
|
85
|
+
(
|
|
86
|
+
qx0 - starts[2] * old_q_pixel_size,
|
|
87
|
+
qy0 - starts[3] * old_q_pixel_size,
|
|
88
|
+
)
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def apply_datacube_operations(datacube, operations):
|
|
93
|
+
"""Apply validated slice/subsample/bin operations to a copied datacube."""
|
|
94
|
+
if len(operations) != 4:
|
|
95
|
+
raise ValueError("Expected one operation for each of the four datacube axes")
|
|
96
|
+
|
|
97
|
+
slices = []
|
|
98
|
+
starts = []
|
|
99
|
+
spacing_factors = []
|
|
100
|
+
bins = []
|
|
101
|
+
shape = datacube.data.shape
|
|
102
|
+
if len(shape) != 4:
|
|
103
|
+
raise ValueError(f"Expected a 4D datacube, got shape {shape}")
|
|
104
|
+
|
|
105
|
+
for axis, operation in enumerate(operations):
|
|
106
|
+
axis_slice = _parse_slice(operation.get("slice", ":"))
|
|
107
|
+
start, stop, step = axis_slice.indices(shape[axis])
|
|
108
|
+
if stop <= start:
|
|
109
|
+
raise ValueError(f"Axis {AXIS_LABELS[axis]} slice selects no data")
|
|
110
|
+
bin_factor = int(operation.get("bin", 1))
|
|
111
|
+
if bin_factor < 1:
|
|
112
|
+
raise ValueError(f"Axis {AXIS_LABELS[axis]} bin factor must be positive")
|
|
113
|
+
slices.append(slice(start, stop, step))
|
|
114
|
+
starts.append(start)
|
|
115
|
+
spacing_factors.append(step * bin_factor)
|
|
116
|
+
bins.append(bin_factor)
|
|
117
|
+
|
|
118
|
+
transformed = copy_datacube(datacube)
|
|
119
|
+
transformed.data = transformed.data[tuple(slices)].copy()
|
|
120
|
+
for axis, bin_factor in enumerate(bins):
|
|
121
|
+
transformed.data = _bin_axis(transformed.data, axis, bin_factor)
|
|
122
|
+
|
|
123
|
+
_update_transform_calibration(transformed, starts, spacing_factors)
|
|
124
|
+
if hasattr(transformed, "calibrate"):
|
|
125
|
+
transformed.calibrate()
|
|
126
|
+
return transformed
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def estimate_transformed_shape(shape, operations):
|
|
130
|
+
transformed_shape = list(shape)
|
|
131
|
+
for axis, operation in enumerate(operations):
|
|
132
|
+
axis_slice = _parse_slice(operation.get("slice", ":"))
|
|
133
|
+
start, stop, step = axis_slice.indices(transformed_shape[axis])
|
|
134
|
+
if stop <= start:
|
|
135
|
+
raise ValueError(f"Axis {AXIS_LABELS[axis]} slice selects no data")
|
|
136
|
+
transformed_shape[axis] = len(range(start, stop, step))
|
|
137
|
+
bin_factor = int(operation.get("bin", 1))
|
|
138
|
+
if bin_factor < 1:
|
|
139
|
+
raise ValueError(f"Axis {AXIS_LABELS[axis]} bin factor must be positive")
|
|
140
|
+
if bin_factor > 1:
|
|
141
|
+
usable = transformed_shape[axis] - (transformed_shape[axis] % bin_factor)
|
|
142
|
+
if usable <= 0:
|
|
143
|
+
raise ValueError(
|
|
144
|
+
f"Axis {AXIS_LABELS[axis]} with length {transformed_shape[axis]} is too small for bin {bin_factor}"
|
|
145
|
+
)
|
|
146
|
+
transformed_shape[axis] = usable // bin_factor
|
|
147
|
+
return tuple(transformed_shape)
|