Skip to content
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

feat: adding json conversion and unit test for ProtoAxis #4045

Merged
merged 10 commits into from
Jan 24, 2025

Conversation

asalzburger
Copy link
Contributor

@asalzburger asalzburger commented Jan 22, 2025

This PR adds json writing/reading to the new ProtoAxis. It supersedes #4038

--- END COMMIT MESSAGE ---

Any further description goes here, @-mentions are ok here!

  • Use a conventional commits prefix: quick summary
    • We mostly use feat, fix, refactor, docs, chore and build types.
  • A milestone will be assigned by one of the maintainers

Summary by CodeRabbit

  • New Features

    • Added JSON conversion support for ProtoAxis objects.
    • Introduced ability to serialize and deserialize ProtoAxis to/from JSON format.
  • Tests

    • Added comprehensive unit tests for ProtoAxis JSON conversion.
    • Implemented test cases for different axis types (Equidistant, Autorange, Variable).
    • Included validation tests for handling valid and invalid JSON inputs.

@asalzburger asalzburger added this to the next milestone Jan 22, 2025
Copy link

coderabbitai bot commented Jan 22, 2025

Walkthrough

A new JSON converter for ProtoAxis, discovered we have! In the ACTS project's Plugins/Json directory, changes have emerged. A comprehensive implementation for serializing and deserializing ProtoAxis objects using the nlohmann::json library, this pull request brings. Unit tests accompany the implementation, ensuring correctness of the conversion process, they do.

Changes

File Change Summary
Plugins/Json/CMakeLists.txt Added ProtoAxisJsonConverter.cpp to ActsPluginJson library
Plugins/Json/include/Acts/Plugins/Json/ProtoAxisJsonConverter.hpp Introduced toJson and fromJson methods for ProtoAxis JSON conversion
Plugins/Json/src/ProtoAxisJsonConverter.cpp Implemented JSON serialization and deserialization logic for ProtoAxis
Tests/UnitTests/Plugins/Json/CMakeLists.txt Added unit test for ProtoAxisJsonConverter
Tests/UnitTests/Plugins/Json/ProtoAxisJsonConverterTests.cpp Created test suite with five test cases for JSON conversion

Sequence Diagram

sequenceDiagram
    participant PA as ProtoAxis
    participant JC as JsonConverter
    participant JSON as JSON Object

    PA->>JC: Convert to JSON
    JC-->>JSON: Serialize properties
    JSON->>JC: Return JSON representation
    JC-->>PA: Validate conversion

    JSON->>JC: Deserialize
    JC-->>PA: Reconstruct ProtoAxis
    JC->>PA: Verify original properties
Loading

Possibly related PRs

Suggested labels

Component - Core, automerge

Suggested reviewers

  • paulgessinger

Poem

In realms of JSON, a ProtoAxis takes flight,
Serialized data, a most elegant sight! 🚀
Conversion magic, with nlohmann's might,
From object to bytes, then back with delight!
A dance of data, precise and bright ✨

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (5)
Tests/UnitTests/Plugins/Json/ProtoAxisJsonConverterTests.cpp (2)

43-63: Additional test scenarios, consider you should.

Good coverage of basic autorange functionality, you have. But edge cases, test we must - negative bin counts and extreme values, important they are.

// Add these test cases
Acts::ProtoAxis epaEdge1(AxisX, Bound, 1); // Minimum bins
Acts::ProtoAxis epaEdge2(AxisX, Bound, std::numeric_limits<int>::max()); // Maximum bins

70-71: Misleading, this comment is. Fix it, we must.

The comment speaks of equidistant axis, yet variable binning the code implements. Clarity in documentation, essential it is.

-  // Bound, equidistant axis
+  // Bound, variable-binned axis
Plugins/Json/include/Acts/Plugins/Json/ProtoAxisJsonConverter.hpp (2)

16-22: Fix the typo in documentation, you must. Clarify the explanation, you should.

Spotted issues in the documentation, I have:

  • "nemclature" -> "nomenclature", the correct spelling is
  • "to to" -> "due to", the proper wording should be
- /// This uses a custom API and nemclature as it would
- /// otherwise require the ProtoAxis to have a default
- /// constructor which is disallowed to to the unique_ptr
+ /// This uses a custom API and nomenclature as it would
+ /// otherwise require the ProtoAxis to have a default
+ /// constructor which is disallowed due to the unique_ptr

23-32: Document the exceptions, we should.

Well-structured the functions are, but missing information about potential exceptions, I sense. Document what exceptions these functions may throw, we must.

 /// @brief Write the ProtoAxis to a json object
 ///
 /// @param pa the proto axis to be written out
+ /// @throws nlohmann::json::exception if serialization fails
 nlohmann::json toJson(const ProtoAxis& pa);

 /// Create a ProtoAxis from a json object
 ///
 /// @param j the json object to be read from
+ /// @throws nlohmann::json::exception if json format is invalid
+ /// @throws std::runtime_error if required fields are missing
 Acts::ProtoAxis fromJson(const nlohmann::json& j);
Plugins/Json/src/ProtoAxisJsonConverter.cpp (1)

15-21: Document the ways of the Force, we must.

Missing documentation for this function, I sense. Help future Padawans understand its purpose and usage, we should.

Add this documentation before the function:

+/**
+ * @brief Convert a ProtoAxis to its JSON representation
+ * @param pa The ProtoAxis to convert
+ * @return JSON object containing the axis configuration
+ */
 nlohmann::json Acts::ProtoAxisJsonConverter::toJson(const Acts::ProtoAxis& pa) {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0011c14 and a2ef0bc.

📒 Files selected for processing (5)
  • Plugins/Json/CMakeLists.txt (1 hunks)
  • Plugins/Json/include/Acts/Plugins/Json/ProtoAxisJsonConverter.hpp (1 hunks)
  • Plugins/Json/src/ProtoAxisJsonConverter.cpp (1 hunks)
  • Tests/UnitTests/Plugins/Json/CMakeLists.txt (1 hunks)
  • Tests/UnitTests/Plugins/Json/ProtoAxisJsonConverterTests.cpp (1 hunks)
🧰 Additional context used
🪛 cppcheck (2.10-2)
Tests/UnitTests/Plugins/Json/ProtoAxisJsonConverterTests.cpp

[error] 19-19: There is an unknown macro here somewhere. Configuration is required. If BOOST_AUTO_TEST_SUITE is a macro then please configure it.

(unknownMacro)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: merge-sentinel
🔇 Additional comments (7)
Tests/UnitTests/Plugins/Json/ProtoAxisJsonConverterTests.cpp (1)

19-41: Well structured, this test case is!

Comprehensive testing of equidistant ProtoAxis serialization and deserialization, I see. All essential properties verified are - direction, axis type, autorange status, and string representation. Pleased with this implementation, I am.

🧰 Tools
🪛 cppcheck (2.10-2)

[error] 19-19: There is an unknown macro here somewhere. Configuration is required. If BOOST_AUTO_TEST_SUITE is a macro then please configure it.

(unknownMacro)

Tests/UnitTests/Plugins/Json/CMakeLists.txt (1)

13-13: In proper order, this test addition is.

Follows the established pattern, it does. Alphabetically placed and properly configured, the new test is.

Plugins/Json/CMakeLists.txt (1)

18-18: Correctly integrated, this new source file is.

In harmony with existing configuration, the ProtoAxisJsonConverter stands. Dependencies and build settings, properly maintained they are.

Plugins/Json/include/Acts/Plugins/Json/ProtoAxisJsonConverter.hpp (2)

1-8: Proper license and copyright information, I sense.

In order, all things are. The Mozilla Public License and copyright notice, correctly placed they are.


11-14: Verify the path of ActsJson.hpp, we must.

Correct the includes seem, but confirm the existence of ActsJson.hpp in the specified path, we should.

✅ Verification successful

Correct the path is, young padawan!

Located at its rightful place, ActsJson.hpp is. The Force flows strong through this include statement.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for ActsJson.hpp in the repository
fd --type f "ActsJson.hpp$"

Length of output: 81

Plugins/Json/src/ProtoAxisJsonConverter.cpp (2)

1-14: Proper organization of includes and license header, I sense.

Well-structured and complete, the foundation of this file is. The Force is strong with the licensing and dependencies.


1-41: Verify the ways of JSON conversion, we must.

Strong in the Force this implementation is, but test it thoroughly we should. Run the following script to verify JSON conversion with various axis configurations:

✅ Verification successful

Hmmmm... Safe and functional your code is, but stronger it could become.

Through the Force, improvements I sense:

  • Use at() consistently for all JSON access, safer from exceptions it is
  • Add explicit error handling, protect against the Dark Side of malformed input you must

Yet verified your implementation is, for all configurations it handles:

  • Equidistant axes
  • Autorange capabilities
  • Variable binning
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for test cases to ensure comprehensive coverage
echo "Searching for ProtoAxis test cases..."
rg -l "ProtoAxis.*Test" 

# Look for other JSON converters to ensure consistent patterns
echo "Checking consistency with other JSON converters..."
rg -l "JsonConverter" --type cpp

Length of output: 4273


Script:

#!/bin/bash
# Check test implementation details
echo "=== ProtoAxis Test Implementation ==="
rg -A 10 "TEST" Tests/UnitTests/Plugins/Json/ProtoAxisJsonConverterTests.cpp

# Check error handling patterns in other converters
echo "=== Error Handling Patterns ==="
rg "throw|at\(" "Plugins/Json/*JsonConverter.cpp" -A 2

Length of output: 1574


Script:

#!/bin/bash
# Check error handling in ProtoAxisJsonConverter
echo "=== ProtoAxis Error Handling ==="
rg "throw|at\(" Plugins/Json/src/ProtoAxisJsonConverter.cpp -A 2

# Check implementation of similar converters for validation patterns
echo "=== Similar Converter Patterns ==="
rg "at\(" Plugins/Json/src/GridJsonConverter.cpp -A 2

Length of output: 2422

@github-actions github-actions bot added the Component - Plugins Affects one or more Plugins label Jan 22, 2025
Copy link

github-actions bot commented Jan 22, 2025

📊: Physics performance monitoring for 911d7e3

Full contents

physmon summary

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
Tests/UnitTests/Plugins/Json/ProtoAxisJsonConverterTests.cpp (2)

21-41: Additional test cases, we must consider!

Strong the test case is, but stronger it could become! Consider these scenarios, you should:

  • Negative values for bounds
  • Zero-width axis testing
  • Invalid bin count handling

Help you implement these additional test cases, shall I?


65-84: More complex variable configurations, test we should!

Simple the current test is. Expand it with these scenarios, we must:

  • Non-uniform bin sizes
  • Large number of bins
  • Ascending and descending bin arrangements
// Example test to add:
std::vector<float> edges = {0.0, 0.1, 0.3, 0.6, 1.0};  // Non-uniform bins
Acts::ProtoAxis vpab(AxisX, Bound, edges);
nlohmann::json jProtoAxis = Acts::ProtoAxisJsonConverter::toJson(vpab);
Acts::ProtoAxis vpabRead = Acts::ProtoAxisJsonConverter::fromJson(jProtoAxis);
BOOST_CHECK_EQUAL(vpabRead.getBinEdges(), vpab.getBinEdges());
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a2ef0bc and b720497.

📒 Files selected for processing (2)
  • Plugins/Json/src/ProtoAxisJsonConverter.cpp (1 hunks)
  • Tests/UnitTests/Plugins/Json/ProtoAxisJsonConverterTests.cpp (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • Plugins/Json/src/ProtoAxisJsonConverter.cpp
🧰 Additional context used
🪛 cppcheck (2.10-2)
Tests/UnitTests/Plugins/Json/ProtoAxisJsonConverterTests.cpp

[error] 19-19: There is an unknown macro here somewhere. Configuration is required. If BOOST_AUTO_TEST_SUITE is a macro then please configure it.

(unknownMacro)

⏰ Context from checks skipped due to timeout of 90000ms (21)
  • GitHub Check: merge-sentinel
  • GitHub Check: unused_files
  • GitHub Check: linux_ubuntu_extra (ubuntu2204, 20, clang++)
  • GitHub Check: macos
  • GitHub Check: missing_includes
  • GitHub Check: linux_ubuntu_extra (ubuntu2204, 20, g++)
  • GitHub Check: linux_ubuntu
  • GitHub Check: CI Bridge / lcg_106a: [alma9, gcc14]
  • GitHub Check: CI Bridge / lcg_106a: [alma9, clang16]
  • GitHub Check: CI Bridge / lcg_106a: [alma9, gcc13]
  • GitHub Check: CI Bridge / lcg_105: [alma9, clang16]
  • GitHub Check: build_debug
  • GitHub Check: CI Bridge / lcg_105: [alma9, gcc13]
  • GitHub Check: CI Bridge / linux_ubuntu_2204
  • GitHub Check: CI Bridge / build_exatrkx
  • GitHub Check: CI Bridge / build_linux_ubuntu
  • GitHub Check: CI Bridge / linux_ubuntu_2204_clang
  • GitHub Check: CI Bridge / clang_tidy
  • GitHub Check: CI Bridge / build_exatrkx_cpu
  • GitHub Check: CI Bridge / build_exatrkx_cpu
  • GitHub Check: CI Bridge / clang_tidy
🔇 Additional comments (1)
Tests/UnitTests/Plugins/Json/ProtoAxisJsonConverterTests.cpp (1)

1-18: Proper organization of includes and licensing, I sense!

Well-structured the header section is, with all necessary components present. The Force is strong with this one!

@asalzburger asalzburger requested review from AJPfleger and removed request for paulgessinger January 23, 2025 14:32
@asalzburger
Copy link
Contributor Author

Hey @AJPfleger - little PR which should be uncontroversial

@asalzburger asalzburger requested a review from AJPfleger January 24, 2025 04:55
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
Tests/UnitTests/Plugins/Json/ProtoAxisJsonConverterTests.cpp (2)

21-41: Additional test scenarios, we must consider!

Good, the basic conversion test is. But strengthen it further with edge cases, we must:

  • Negative range values
  • Very large numbers
  • Special floating-point values (infinity, NaN)
// Additional test cases to add:
Acts::ProtoAxis epabNeg(AxisX, Bound, -10.0, -1.0, 10);
Acts::ProtoAxis epabLarge(AxisX, Bound, 1e6, 1e7, 10);
Acts::ProtoAxis epabSpecial(AxisX, Bound, -std::numeric_limits<double>::infinity(),
                           std::numeric_limits<double>::infinity(), 10);

86-153: More validation scenarios, require we do!

Well-tested, the basic error cases are. But expand our test coverage, we should:

  • Missing required JSON fields
  • Invalid axis direction values
  • Invalid boundary type values
  • Invalid axis type values
// Additional invalid test cases to add:
nlohmann::json jMissingField = {
    {"axis_dir", "AxisX"}, 
    {"autorange", false}
    // Missing "axis" field
};

nlohmann::json jInvalidAxisDir = jValidEq;
jInvalidAxisDir["axis_dir"] = "InvalidAxis";

nlohmann::json jInvalidBoundary = jValidEqAxis;
jInvalidBoundary["boundary_type"] = "InvalidBoundary";

BOOST_CHECK_THROW(Acts::ProtoAxisJsonConverter::fromJson(jMissingField),
                  std::invalid_argument);
BOOST_CHECK_THROW(Acts::ProtoAxisJsonConverter::fromJson(jInvalidAxisDir),
                  std::invalid_argument);
BOOST_CHECK_THROW(Acts::ProtoAxisJsonConverter::fromJson(jInvalidBoundary),
                  std::invalid_argument);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e78d80e and 911d7e3.

📒 Files selected for processing (3)
  • Plugins/Json/include/Acts/Plugins/Json/ProtoAxisJsonConverter.hpp (1 hunks)
  • Plugins/Json/src/ProtoAxisJsonConverter.cpp (1 hunks)
  • Tests/UnitTests/Plugins/Json/ProtoAxisJsonConverterTests.cpp (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • Plugins/Json/include/Acts/Plugins/Json/ProtoAxisJsonConverter.hpp
  • Plugins/Json/src/ProtoAxisJsonConverter.cpp
🧰 Additional context used
🪛 cppcheck (2.10-2)
Tests/UnitTests/Plugins/Json/ProtoAxisJsonConverterTests.cpp

[error] 19-19: There is an unknown macro here somewhere. Configuration is required. If BOOST_AUTO_TEST_SUITE is a macro then please configure it.

(unknownMacro)

⏰ Context from checks skipped due to timeout of 90000ms (17)
  • GitHub Check: CI Bridge / lcg_106a: [alma9, clang16]
  • GitHub Check: CI Bridge / lcg_106a: [alma9, gcc14]
  • GitHub Check: CI Bridge / lcg_106a: [alma9, gcc13]
  • GitHub Check: CI Bridge / lcg_105: [alma9, clang16]
  • GitHub Check: CI Bridge / lcg_105: [alma9, gcc13]
  • GitHub Check: CI Bridge / linux_ubuntu_2204_clang
  • GitHub Check: CI Bridge / linux_ubuntu_2204
  • GitHub Check: CI Bridge / build_linux_ubuntu
  • GitHub Check: missing_includes
  • GitHub Check: linux_ubuntu_extra (ubuntu2204, 20, clang++)
  • GitHub Check: linux_ubuntu_extra (ubuntu2204, 20, g++)
  • GitHub Check: macos
  • GitHub Check: linux_ubuntu
  • GitHub Check: build_debug
  • GitHub Check: CI Bridge / build_exatrkx
  • GitHub Check: CI Bridge / build_exatrkx_cpu
  • GitHub Check: CI Bridge / clang_tidy
🔇 Additional comments (2)
Tests/UnitTests/Plugins/Json/ProtoAxisJsonConverterTests.cpp (2)

1-18: Well-organized, the includes are!

Proper license header and necessary includes for testing framework and functionality, you have included. Pleased with this organization, I am.


43-63: Test the dynamic behavior of autorange, we must!

Missing, the tests for dynamic range adjustment are. Add tests for:

  • Range expansion behavior
  • Initial state validation
  • Edge case handling when range updates occur
// Example test to add:
Acts::ProtoAxis epa(AxisX, Bound, 10);
// Simulate range updates
epa.extend(-5.0);
epa.extend(15.0);
nlohmann::json jProtoAxis = Acts::ProtoAxisJsonConverter::toJson(epa);
Acts::ProtoAxis epaRead = Acts::ProtoAxisJsonConverter::fromJson(jProtoAxis);
BOOST_CHECK_EQUAL(epaRead.getMin(), epa.getMin());
BOOST_CHECK_EQUAL(epaRead.getMax(), epa.getMax());

@kodiakhq kodiakhq bot merged commit 429e34d into acts-project:main Jan 24, 2025
45 checks passed
@acts-project-service acts-project-service added the Breaks Athena build This PR breaks the Athena build label Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaks Athena build This PR breaks the Athena build Component - Plugins Affects one or more Plugins
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants