-
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
19
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.
+512
−7
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4dcffd3
Increase wait timeouts
GDYendell b332b0e
Update script to support stream2
GDYendell 6276aa8
Add function to create interleaved VDS
jacob720 cd4b2a2
Fix bug
jacob720 5021e2f
Add h5py to dependancies
jacob720 8d49c1c
Add tests
jacob720 3c7dd3c
Add more tests
jacob720 a6b7073
Clearer naming
jacob720 49f0005
Take datatype as argument
jacob720 b8054c5
Add attributes
jacob720 955381b
Account for number of data writers
jacob720 86d82fb
Improve tests
jacob720 6b2bc8c
Typo
jacob720 62e5519
Fix naming
jacob720 57b8619
Add datasets as a parameter
jacob720 45ad43e
Clean up and add more tests
jacob720 78970b3
Use correct attributes
jacob720 3ba8038
Update pyproject.toml
jacob720 7672841
Hardcode dataset names
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
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, | ||||||
|
|
@@ -35,7 +41,7 @@ async def arm_when_ready(self): | |||||
| await super().arm_when_ready() | ||||||
|
|
||||||
| try: | ||||||
| await self.OD.EF.ready.wait_for_value(True, timeout=1) | ||||||
| await self.OD.EF.ready.wait_for_value(True, timeout=3) | ||||||
| except TimeoutError as e: | ||||||
| raise TimeoutError("Eiger fan not ready") from e | ||||||
|
|
||||||
|
|
@@ -55,6 +61,21 @@ async def start_writing(self): | |||||
| await self.OD.FP.start_writing() | ||||||
|
|
||||||
| try: | ||||||
| await self.OD.writing.wait_for_value(True, timeout=1) | ||||||
| await self.OD.writing.wait_for_value(True, timeout=3) | ||||||
| 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=["data1", "data2", "data3"], | ||||||
|
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. I told you the wrong thing. This should actually be
Suggested change
|
||||||
| 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(), | ||||||
GDYendell marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| frame_shape=( | ||||||
| self.OD.FP.data_dims_1.get(), | ||||||
| self.OD.FP.data_dims_0.get(), | ||||||
| ), | ||||||
GDYendell marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| 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.
path, prefix and frames actually get cleared after file writing starts, so unfortunately we need to take local copies of these before the
start_writingcall.