-
Notifications
You must be signed in to change notification settings - Fork 1
Add vds generation #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jacob720
wants to merge
15
commits into
top-level-attributes
Choose a base branch
from
85_add_vds_generation
base: top-level-attributes
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a12b10f
Add function to create interleaved VDS
jacob720 f86b01b
Fix bug
jacob720 71def19
Add h5py to dependancies
jacob720 8f99c7f
Add tests
jacob720 334b0ca
Add more tests
jacob720 371e9a0
Clearer naming
jacob720 b5f26e8
Take datatype as argument
jacob720 b10c763
Add attributes
jacob720 8557d03
Account for number of data writers
jacob720 96f4511
Improve tests
jacob720 7797200
Typo
jacob720 2cc760a
Fix naming
jacob720 15bba9d
Add datasets as a parameter
jacob720 3a1ec0f
Clean up and add more tests
jacob720 e41097c
Use correct attributes
jacob720 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,15 +1,21 @@ | ||
| import asyncio | ||
| from pathlib import Path | ||
|
|
||
| from fastcs.attributes import AttrRW | ||
| from fastcs.connections import IPConnectionSettings | ||
| from fastcs.datatypes import Bool | ||
| from fastcs.methods import command | ||
|
|
||
| from fastcs_eiger.controllers.eiger_controller import EigerController | ||
| from fastcs_eiger.controllers.odin.generate_vds import create_interleave_vds | ||
| from fastcs_eiger.controllers.odin.odin_controller import OdinController | ||
|
|
||
|
|
||
| class EigerOdinController(EigerController): | ||
| """Eiger controller with Odin sub controller""" | ||
|
|
||
| enable_vds_creation = AttrRW(Bool()) | ||
|
|
||
| def __init__( | ||
| self, | ||
| detector_connection_settings: IPConnectionSettings, | ||
|
|
@@ -58,3 +64,18 @@ async def start_writing(self): | |
| await self.OD.writing.wait_for_value(True, timeout=1) | ||
| except TimeoutError as e: | ||
| raise TimeoutError("File writers failed to start") from e | ||
|
|
||
| if self.enable_vds_creation.get(): | ||
| create_interleave_vds( | ||
| path=Path(self.OD.file_path.get()), | ||
| prefix=self.OD.file_prefix.get(), | ||
| datasets=["data"], # NEED TO GET THIS FROM SOMEWHERE | ||
| frame_count=self.OD.FP.frames.get(), | ||
| frames_per_block=self.OD.block_size.get(), | ||
| blocks_per_file=self.OD.FP.process_blocks_per_file.get(), | ||
| frame_shape=( | ||
| self.OD.FP.data_dims_1.get(), | ||
| self.OD.FP.data_dims_0.get(), | ||
| ), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be OD.FP.data_dims_0/1. These probably don't change if it is in ROI mode. |
||
| dtype=self.OD.FP.data_datatype.get(), | ||
| ) | ||
This file contains hidden or 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,118 @@ | ||
| import math | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
|
|
||
| import h5py | ||
|
|
||
|
|
||
| @dataclass | ||
| class FileFrames: | ||
| frames: int | ||
| start: int | ||
| frames_per_block: int | ||
|
|
||
| @property | ||
| def blocks(self): | ||
| return self.frames // self.frames_per_block | ||
|
|
||
| @property | ||
| def remainder_frames(self): | ||
| return self.frames % self.frames_per_block | ||
|
|
||
|
|
||
| def _get_frames_per_file_writer( | ||
| frame_count: int, frames_per_block: int, n_file_writers: int | ||
| ) -> list[int]: | ||
| n_blocks = math.ceil(frame_count / frames_per_block) | ||
| min_blocks_per_file = n_blocks // n_file_writers | ||
| remainder = n_blocks - min_blocks_per_file * n_file_writers | ||
|
|
||
| frames_per_file_writer = [] | ||
| for i in range(n_file_writers): | ||
| blocks = min_blocks_per_file + (i < remainder) | ||
| frames_per_file_writer.append(blocks * frames_per_block) | ||
|
|
||
| overflow = sum(frames_per_file_writer) - frame_count | ||
| frames_per_file_writer[remainder - 1] -= overflow | ||
| return frames_per_file_writer | ||
|
|
||
|
|
||
| def _calculate_frame_distribution( | ||
| frame_count, frames_per_block, blocks_per_file, n_file_writers | ||
| ) -> dict[int, FileFrames]: | ||
|
|
||
| frame_distribution: dict[int, FileFrames] = {} | ||
|
|
||
| max_frames_per_file = ( | ||
| frames_per_block * blocks_per_file if blocks_per_file else frame_count | ||
| ) | ||
| # total frames written before one of the file writers has to start a new file | ||
| frames_before_new_file = n_file_writers * max_frames_per_file | ||
| frames_per_file_writer = _get_frames_per_file_writer( | ||
| frame_count, frames_per_block, n_file_writers | ||
| ) | ||
| for file_writer_idx, n_frames in enumerate(frames_per_file_writer): | ||
| n_files = math.ceil(n_frames / max_frames_per_file) | ||
| for i in range(n_files): | ||
| file_idx = file_writer_idx + i * n_file_writers | ||
|
|
||
| frame_distribution[file_idx + 1] = FileFrames( | ||
| frames=min(n_frames - i * max_frames_per_file, max_frames_per_file), | ||
| frames_per_block=frames_per_block, | ||
| start=frames_per_block * file_writer_idx | ||
| + file_idx // n_file_writers * frames_before_new_file, | ||
| ) | ||
|
|
||
| return frame_distribution | ||
|
|
||
|
|
||
| def create_interleave_vds( | ||
| path: Path, | ||
| prefix: str, | ||
| datasets: list[str], | ||
| frame_count: int, | ||
| frames_per_block: int, | ||
| blocks_per_file: int, | ||
| frame_shape: tuple[int, int], | ||
| dtype: str = "float", | ||
| n_file_writers: int = 4, | ||
| ) -> None: | ||
| frame_distribution = _calculate_frame_distribution( | ||
| frame_count, frames_per_block, blocks_per_file, n_file_writers | ||
| ) | ||
| stride = n_file_writers * frames_per_block | ||
|
|
||
| with h5py.File(f"{path / prefix}_vds.h5", "w", libver="latest") as f: | ||
| for dataset_name in datasets: | ||
| v_layout = h5py.VirtualLayout( | ||
| shape=(frame_count, frame_shape[0], frame_shape[1]), | ||
| dtype=dtype, | ||
| ) | ||
| for file_number, file_frames in frame_distribution.items(): | ||
| full_block_frames = file_frames.blocks * frames_per_block | ||
| v_source = h5py.VirtualSource( | ||
| f"{path / prefix}_{str(file_number).zfill(6)}.h5", | ||
| name=dataset_name, | ||
| shape=(file_frames.frames, frame_shape[0], frame_shape[1]), | ||
| dtype=dtype, | ||
| ) | ||
| if file_frames.blocks: | ||
| source = v_source[:full_block_frames, :, :] | ||
| v_layout[ | ||
| h5py.MultiBlockSlice( | ||
| start=file_frames.start, | ||
| stride=stride, | ||
| count=file_frames.blocks, | ||
| block=frames_per_block, | ||
| ), | ||
| :, | ||
| :, | ||
| ] = source | ||
| if file_frames.remainder_frames: | ||
| # Last few frames that don't fit into a block | ||
| source = v_source[full_block_frames : file_frames.frames, :, :] | ||
| v_layout[ | ||
| frame_count - file_frames.remainder_frames : frame_count, :, : | ||
| ] = source | ||
|
|
||
| f.create_virtual_dataset(dataset_name, v_layout) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
process_blocks_per_fileneeds adding to theEigerFrameProcessorPluginThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DiamondLightSource/fastcs-odin#98
Have added this and the other attributes here