-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Dask tools for VDS creation and simple image binning (#14)
* Add VDS creation command-line tool * Add Dask image binner command-line tool, with options for single-image, multi-image sweep and multi-image pump-probe binning. * Add command line tool to inspect cue messages * Avoid Dask v2021.03.0 due to a regression in that release — the dask.array.bincount function does not permit slicing in that version (see dask/dask#7391). * Add more cue message info and tidy up the functions for discovering the timestamps of chosen cues
- Loading branch information
1 parent
9957949
commit d546b45
Showing
11 changed files
with
940 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
dask | ||
dask[array,diagnostics] != 2021.03.0 | ||
h5py | ||
hdf5plugin | ||
numpy | ||
pint | ||
zarr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
dask | ||
dask[array,diagnostics] != 2021.03.0 | ||
h5py | ||
hdf5plugin | ||
numpy | ||
pint | ||
pytest | ||
pytest-cov | ||
zarr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""Tools for binning events to images.""" | ||
|
||
|
||
from operator import mul | ||
from typing import Dict, Tuple | ||
|
||
import numpy as np | ||
from dask import array as da | ||
|
||
try: | ||
from numpy.typing import ArrayLike | ||
except ImportError: | ||
# NumPy versions compatible with Python 3.6 do not have the numpy.typing module. | ||
ArrayLike = np.ndarray | ||
|
||
from . import ( | ||
event_location_key, | ||
event_time_key, | ||
first_cue_time, | ||
pixel_index, | ||
shutter_close, | ||
shutter_open, | ||
) | ||
|
||
|
||
def find_start_end(data): | ||
start_time = first_cue_time(data, shutter_open) | ||
end_time = first_cue_time(data, shutter_close) | ||
return da.compute(start_time, end_time) | ||
|
||
|
||
def make_single_image( | ||
data: Dict[str, da.Array], image_size: Tuple[int, int], start: int, end: int | ||
) -> da.Array: | ||
event_times = data[event_time_key] | ||
event_locations = data[event_location_key] | ||
|
||
valid_events = (start <= event_times) & (event_times < end) | ||
event_locations = event_locations[valid_events] | ||
event_locations = pixel_index(event_locations, image_size) | ||
|
||
image = da.bincount(event_locations, minlength=mul(*image_size)) | ||
return image.astype(np.uint32).reshape(1, *image_size) | ||
|
||
|
||
def make_multiple_images( | ||
data: Dict[str, da.Array], image_size: Tuple[int, int], bins: ArrayLike | ||
) -> da.Array: | ||
event_times = data[event_time_key] | ||
event_locations = data[event_location_key] | ||
|
||
valid_events = (bins[0] <= event_times) & (event_times < bins[-1]) | ||
event_times = event_times[valid_events] | ||
event_locations = event_locations[valid_events] | ||
|
||
image_indices = da.digitize(event_times, bins) - 1 | ||
event_locations = pixel_index(event_locations, image_size) | ||
num_images = bins.size - 1 | ||
|
||
image_indices = [ | ||
image_indices == image_number for image_number in range(num_images) | ||
] | ||
images = da.stack( | ||
[ | ||
da.bincount(event_locations[indices], minlength=mul(*image_size)) | ||
for indices in image_indices | ||
] | ||
) | ||
|
||
return images.astype(np.uint32).reshape(num_images, *image_size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
"""General utilities for the command-line tools.""" | ||
|
||
import argparse | ||
|
||
import pint | ||
|
||
from .. import __version__ | ||
|
||
__all__ = ("version_parser", "input_parser", "image_output_parser", "exposure_parser") | ||
|
||
|
||
version_parser = argparse.ArgumentParser(add_help=False) | ||
version_parser.add_argument( | ||
"-V", | ||
"--version", | ||
action="version", | ||
version="%(prog)s: Tristan tools {version}".format(version=__version__), | ||
) | ||
|
||
|
||
input_parser = argparse.ArgumentParser(add_help=False) | ||
input_parser.add_argument( | ||
"input_file", | ||
help="Tristan metadata ('_meta.h5') or raw data ('_000001.h5', etc.) file. " | ||
"This file must be in the same directory as the HDF5 files containing all the " | ||
"corresponding raw events data.", | ||
metavar="input-file", | ||
) | ||
|
||
|
||
image_output_parser = argparse.ArgumentParser(add_help=False) | ||
image_output_parser.add_argument( | ||
"-o", | ||
"--output-file", | ||
help="File name or location for output image file, defaults to the working " | ||
"directory. If only a directory location is given, the pattern of the raw data " | ||
"files will be used, with '<name>_meta.h5' replaced with '<name>_single_image.h5'.", | ||
) | ||
image_output_parser.add_argument( | ||
"-f", | ||
"--force", | ||
help="Force the output image file to over-write any existing file with the same " | ||
"name.", | ||
action="store_true", | ||
) | ||
|
||
image_output_parser.add_argument( | ||
"-s", | ||
"--image-size", | ||
help="Dimensions of the detector in pixels, separated by a comma, as 'x,y', i.e. " | ||
"'fast,slow'.", | ||
) | ||
|
||
|
||
exposure_parser = argparse.ArgumentParser(add_help=False) | ||
group = exposure_parser.add_mutually_exclusive_group(required=True) | ||
group.add_argument( | ||
"-e", | ||
"--exposure-time", | ||
help="Duration of each image. This will be used to calculate the number of " | ||
"images. Specify a value with units like '--exposure-time .5ms', '-e 500µs' or " | ||
"'-e 500us'.", | ||
type=pint.Quantity, | ||
) | ||
group.add_argument("-n", "--num-images", help="Number of images.", type=int) |
Oops, something went wrong.