Repository navigation
feat: Add NIRData conversion functionalities #53
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
benkroehs
wants to merge
2
commits into
main
Choose a base branch
from
nirdata
base: main
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
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """ | ||
| Convert NIRGraphData to a torch-compatible spike representation. | ||
| """ | ||
|
|
||
| import torch | ||
| from nir.data_ir import NIRGraphData, NIRNodeData | ||
|
|
||
|
|
||
| def from_nir_data( | ||
| nir_graph_data: NIRGraphData, | ||
| dt: float, | ||
| dynamic_before_transition: bool = True, | ||
| dimension_order: tuple = ("time", "batch", "neuron"), | ||
| time_unit: float = 1.0, | ||
| ) -> dict: | ||
| """ | ||
| Args: | ||
| nir_graph_data: A NIRGraphData object containing the spike data. | ||
| dt: The time step size of the spike data. | ||
| dynamic_before_transition: bool, optional | ||
| If True, it is assumed that the framework evolves the state (e.g., | ||
| membrane potential) of the neurons before checking if the threshold | ||
| has been crossed and generating an event (transition). If False, | ||
| the state is updated after the event generation. | ||
| dimension_order: The desired ordering of dimensions in the output spike | ||
| tensors. Defaults to ('time', 'batch', 'neuron'). | ||
| If the output spike tensors should have a different dimension | ||
| ordering, this can be used to specify the correct ordering. | ||
| time_unit: The unit of time for the spike data and dt. Defaults to 1.0, | ||
| which corresponds to seconds. For milliseconds, set this to 1e-3. | ||
| Returns: | ||
| A dictionary mapping node names to spike tensors. | ||
| """ | ||
| torch_dict = {} | ||
|
|
||
| for node_key, nir_node_data in nir_graph_data.nodes.items(): | ||
| if isinstance(nir_node_data, NIRNodeData): | ||
| for observable, data in nir_node_data.observables.items(): | ||
| if observable == "spikes": | ||
| data = data.get_time_gridded( | ||
| dt=dt * time_unit, | ||
| dimension_order=dimension_order, | ||
| dynamic_before_transition=dynamic_before_transition, | ||
| ) | ||
| torch_spikes = torch.tensor(data.data).float() | ||
| else: | ||
| raise NotImplementedError( | ||
| "Only spikes are supported as observables yet." | ||
| ) | ||
| else: | ||
| raise NotImplementedError( | ||
| "The translation of nested NIRGraphData is not supported." | ||
| ) | ||
|
|
||
| torch_dict[node_key] = torch_spikes | ||
|
|
||
| return torch_dict |
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,50 @@ | ||
| """ | ||
| Convert spikes from a torch tensor to a NIRGraphData object. | ||
| """ | ||
|
|
||
| from nir.data_ir import NIRGraphData, NIRNodeData, TimeGriddedData | ||
|
|
||
|
|
||
| def to_nir_data( | ||
| torch_dict: dict, | ||
| dt: float, | ||
| time_unit: float = 1.0, | ||
| dimension_order: tuple = ("time", "batch", "neuron"), | ||
| dynamic_before_transition: bool = True, | ||
| ) -> NIRGraphData: | ||
| """ | ||
| Args: | ||
| torch_dict: A dictionary mapping node names to spike tensors. If the | ||
| shape is not (n_time_steps, batch_size, n_neurons), the `shape` | ||
| argument can be used to specify the correct ordering of dimensions. | ||
| dt: The time step size of the spike data. | ||
| time_unit: The unit of time for the spike data and dt. Defaults to 1.0, | ||
| which corresponds to seconds. For milliseconds, set this to 1e-3. | ||
| dimension_order: The order of dimensions in the input spike tensors. | ||
| Defaults to ("time", "batch", "neuron"). | ||
| dynamic_before_transition: If True, the membrane potential is updated | ||
| before checking if the threshold has been crossed and generating an | ||
| event (transition). If False, the state is updated after the event | ||
| generation. Default is True. | ||
| Returns: | ||
| A NIRGraphData object containing the spike data. | ||
| """ | ||
| nir_nodes = {} | ||
|
|
||
| for key, spikes in torch_dict.items(): | ||
| spikes = spikes.detach().cpu().numpy().astype(bool) | ||
|
|
||
| nir_node_data = NIRNodeData( | ||
| { | ||
| "spikes": TimeGriddedData( | ||
| spikes, | ||
| dt * time_unit, | ||
| dimension_order=dimension_order, | ||
| dynamic_before_transition=dynamic_before_transition, | ||
| ) | ||
| } | ||
| ) | ||
| nir_nodes[key] = nir_node_data | ||
|
|
||
| nir_data = NIRGraphData(nir_nodes) | ||
| return nir_data |
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,50 @@ | ||
| import nir | ||
| from nirtorch import from_nir_data, to_nir_data | ||
| import numpy as np | ||
| import torch | ||
|
Check failure on line 4 in tests/test_nir_data.py
|
||
|
|
||
|
|
||
| def test_event_data_from_nir(): | ||
| nir_data = nir.NIRGraphData( | ||
| nodes={ | ||
| "lif": nir.NIRNodeData( | ||
| observables={ | ||
| "spikes": nir.EventData( | ||
| idx=np.random.randint(0, 10, (3, 5)), | ||
| time=np.random.rand(3, 5) * 0.1, | ||
| n_neurons=10, | ||
| t_max=0.1, | ||
| ) | ||
| } | ||
| ) | ||
| } | ||
| ) | ||
|
|
||
| torch_dict = from_nir_data(nir_data, dt=0.001) | ||
|
|
||
| assert "lif" in torch_dict, "Node 'lif' not found in the converted dictionary" | ||
| assert torch_dict["lif"].shape == (100, 3, 10), ( | ||
| f"Expected shape (100, 3, 10) for node 'lif', but got {torch_dict['lif'].shape}" | ||
| ) | ||
|
|
||
|
|
||
| def test_stable_conversion(): | ||
| original_spikes = {"lif": torch.randint(0, 2, (4, 10, 10), dtype=torch.float32)} | ||
|
|
||
| nir_data = to_nir_data(original_spikes, dt=0.001) | ||
| converted_spikes = from_nir_data(nir_data, dt=0.001) | ||
|
|
||
| assert torch.equal(original_spikes["lif"], converted_spikes["lif"]), ( | ||
| f"Mismatch in spikes for node 'lif'. Got {converted_spikes['lif'].shape}, expected {original_spikes['lif'].shape}" | ||
| ) | ||
|
|
||
|
|
||
| def test_stable_conversion_with_time_unit(): | ||
| original_spikes = {"lif": torch.randint(0, 2, (4, 10, 10), dtype=torch.float32)} | ||
|
|
||
| nir_data = to_nir_data(original_spikes, dt=1e-3, time_unit=1e-3) | ||
| converted_spikes = from_nir_data(nir_data, dt=1e-3, time_unit=1e-3) | ||
|
|
||
| assert torch.equal(original_spikes["lif"], converted_spikes["lif"]), ( | ||
| f"Mismatch in spikes for node 'lif' with time unit conversion. Got {converted_spikes['lif'].shape}, expected {original_spikes['lif'].shape}" | ||
| ) | ||
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.
Ruff complaint!