-
Notifications
You must be signed in to change notification settings - Fork 89
Handle Device conflicts between core schema and extensions #2132
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c486029
device model mapper - wip
stephprince 1263c24
add device mapper for schema conflicts
stephprince 0084b34
update warning stack levels
stephprince f68197e
update device mapping warnings for clarity
stephprince b7bcba6
add extension back compatibility test files
stephprince 37189ef
add tests for reading old extension files with nwb schema 2.9.0
stephprince e2bec22
update CHANGELOG
stephprince fed02a0
fix test name
stephprince a69f50f
Merge branch 'dev' into handle-core-extension-conflicts
stephprince 68258c9
bump hdmf minimum requirement
stephprince f281060
update requirements.txt
stephprince 7bbcee2
Update dandi download URL for testing data
stephprince 8156574
Update src/pynwb/io/device.py
stephprince 8535bfc
Update src/pynwb/io/device.py
stephprince 851cb6f
update test warning assertion
stephprince 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
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 |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| from warnings import warn | ||
|
|
||
| from .. import register_map | ||
| from ..device import Device, DeviceModel | ||
| from .core import NWBContainerMapper | ||
|
|
||
|
|
||
| @register_map(Device) | ||
| class DeviceMapper(NWBContainerMapper): | ||
| """ | ||
| Custom mapper for Device objects to handle known schema conflicts between core schema and extensions. | ||
|
|
||
| This mapper detects when extensions define Device.model as a string attribute instead of | ||
| a link to DeviceModel, or when extensions define their own DeviceModel type. | ||
| """ | ||
|
|
||
| @NWBContainerMapper.constructor_arg("model") | ||
| def model_carg(self, builder, manager): | ||
| """ | ||
| Handle different model mapping strategies based on detected schema conflicts. | ||
|
|
||
| Args: | ||
| builder: The GroupBuilder for the Device | ||
| manager: The BuildManager | ||
|
|
||
| Returns: | ||
| The appropriate model object or value based on the mapping strategy | ||
| """ | ||
| model_builder = builder.get('model') | ||
| if isinstance(model_builder, str): | ||
| warn( | ||
| 'Device.model was detected as a string, but NWB 2.9 specifies Device.model as a link to a DeviceModel. ' | ||
| f'Remapping "{model_builder}" to a new DeviceModel.', | ||
| stacklevel=3) | ||
|
|
||
| # replace the model string with a DeviceModel object using the model name and device attributes | ||
| device_model_attributes = dict(name=model_builder, | ||
| description=builder.attributes.get('description'), | ||
| manufacturer=builder.attributes.get('manufacturer', ''), | ||
| model_number=builder.attributes.get('model_number')) | ||
| model = DeviceModel(**device_model_attributes) | ||
|
|
||
| return model | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def __new_container__(self, cls, container_source, parent, object_id, **kwargs): | ||
| # Override ObjectMapper.__new_container__ to handle the case where the Device.model argument | ||
| # is not a DeviceModel, which can happen in extensions written to be compatible with NWB<2.9. | ||
| # The original Device.model object will be accessible under a new attribute name based on the | ||
| # extension namespace. | ||
| model = kwargs.get('model', None) | ||
|
|
||
| if model is None or isinstance(model, DeviceModel): | ||
| device_obj = super().__new_container__(cls, container_source, parent, object_id, **kwargs) | ||
| else: | ||
| # create device object without model | ||
| kwargs.pop('model') | ||
| device_obj = super().__new_container__(cls, container_source, parent, object_id, **kwargs) | ||
|
|
||
| # add the conflicting Device.model object as a new attribute on Device | ||
| # e.g. Device.model in the file -> Device.ndx_optogenetics_model in the python object | ||
| warn(f'The model attribute of the Device "{device_obj.name}" was detected as a non-DeviceModel ' | ||
| f'object. Data associated with this object can be accessed at ' | ||
| f'"nwbfile.devices["{device_obj.name}"].{model.namespace.replace("-", "_")}_model"', | ||
| stacklevel=2) | ||
| setattr(device_obj, f"{model.namespace.replace('-', '_')}_model", model) | ||
|
|
||
| return device_obj | ||
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
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.
Uh oh!
There was an error while loading. Please reload this page.