-
Notifications
You must be signed in to change notification settings - Fork 5
Implement a JSON writer replacing NaN with null #199
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
yingyue2030699
wants to merge
3
commits into
simularium:main
Choose a base branch
from
JohnsonBiophysicsLab:json_writer_replacing_nan
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
3 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,74 @@ | ||
| """ | ||
| test_json_writer.py | ||
|
|
||
| Unit tests for the JsonWriter class in simulariumio.writers. | ||
|
|
||
| Current only tests `save_replacing_nan()` to ensure that: | ||
| * All NaN values are replaced with null (None in Python). | ||
| * Output JSON is valid and does not contain any NaN. | ||
| * Output file is written correctly in `.simularium` format. | ||
|
|
||
| Status: | ||
| - Uses pytest with fixtures to generate a minimal TrajectoryData mock. | ||
| - Tests are self-contained and do not require writing to real disk locations. | ||
| - Assumes simulariumio is installed or importable via editable mode (`pip install -e .`). | ||
|
|
||
| To run: | ||
| > pytest simulariumio/tests/writers/test_json_writer.py | ||
| """ | ||
| import json | ||
| import os | ||
| import tempfile | ||
| import numpy as np | ||
| import pytest | ||
| from simulariumio import TrajectoryData, AgentData, UnitData, DisplayData | ||
| from simulariumio.constants import DISPLAY_TYPE, VALUES_PER_3D_POINT, VIZ_TYPE | ||
| from simulariumio.writers import JsonWriter | ||
|
|
||
| @pytest.fixture | ||
| def trajectory_with_nan(): | ||
| # Create mock AgentData with NaNs | ||
| agent_data = AgentData.from_dimensions(dimensions=(1, 1, VALUES_PER_3D_POINT)) | ||
| agent_data.positions[0][0] = [np.nan, 1.0, 2.0] | ||
| agent_data.radii[0][0] = np.nan | ||
| agent_data.subpoints[0][0] = [0.0, np.nan, 0.0, 1.0, 1.0, 1.0] | ||
| agent_data.types[0].append("A") | ||
| agent_data.unique_ids[0][0] = 1 | ||
| agent_data.n_agents[0] = 1 | ||
| agent_data.n_subpoints[0][0] = 6 | ||
| agent_data.viz_types[0][0] = VIZ_TYPE.FIBER | ||
|
|
||
| # Wrap into a TrajectoryData object | ||
| traj_data = TrajectoryData( | ||
| meta_data=None, | ||
| agent_data=agent_data, | ||
| time_units=UnitData(name="s"), | ||
| spatial_units=UnitData(name="nm"), | ||
| plots=None, | ||
| ) | ||
| return traj_data | ||
|
|
||
| def test_save_replacing_nan(trajectory_with_nan): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| output_path = os.path.join(tmpdir, "test_output") | ||
|
|
||
| # Act: Call method under test | ||
| JsonWriter.save_replacing_nan(trajectory_with_nan, output_path, validate_ids=False) | ||
|
|
||
| # Verify file exists | ||
| output_file = output_path + ".simularium" | ||
| assert os.path.exists(output_file) | ||
|
|
||
| # Verify contents | ||
| with open(output_file, "r") as f: | ||
| json_data = json.load(f) | ||
|
|
||
| # Assert that NaN was replaced with null | ||
| positions = json_data["trajectoryInfo"]["agentData"]["positions"][0][0] | ||
| radii = json_data["trajectoryInfo"]["agentData"]["radii"][0][0] | ||
| subpoints = json_data["trajectoryInfo"]["agentData"]["subpoints"][0][0] | ||
|
|
||
| assert positions[0] is None | ||
| assert isinstance(positions[1], float) | ||
| assert radii is None | ||
| assert subpoints[1] is None |
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
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.
I wonder if we could do this with just an additional function argument to the main save function rather than having an entirely different function?