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

LIU-417: Add GraphConfig to translator #296

Merged
merged 3 commits into from
Nov 15, 2024
Merged

LIU-417: Add GraphConfig to translator #296

merged 3 commits into from
Nov 15, 2024

Conversation

myxie
Copy link
Collaborator

@myxie myxie commented Nov 8, 2024

JIRA Ticket

https://icrar.atlassian.net/browse/LIU-417

Type

  • Feature (addition)
  • [ ] Bug fix
  • Refactor (change)
  • [ ] Documentation

Problem/Issue

When the translator is sent a LogicalGraphTemplate with an activeGraphConfig, we need a way of applying that config to the graph.

Solution

We apply the GraphConfig prior to updating the logical graph constructs and extracting the input/output apps. This makes sure the value updates occur to the construct nodes in the graph, not the application nodes in the graph. This is because it is from the Construct nodes that we are getting the 'unrolling' parameters.

Checklist

  • Unittests added
    • Reason for not adding unittests (remove this line if added)
  • [ ] Documentation added
    • This is not user-facing behaviour.

Summary by Sourcery

Add support for applying GraphConfig to LogicalGraphTemplate in the translator, enabling configuration of graph constructs. Refactor the LG class to support configuration application during initialization and introduce unit tests to validate the new functionality.

New Features:

  • Introduce the ability to apply GraphConfig to LogicalGraphTemplate in the translator, allowing configuration of graph constructs before updating logical graph constructs.

Enhancements:

  • Refactor the LG class to include an optional parameter for applying configuration during initialization.

Tests:

  • Add unit tests for the new GraphConfig functionality, covering scenarios with empty configurations, loops, and scatter nodes.

Copy link
Contributor

sourcery-ai bot commented Nov 8, 2024

Reviewer's Guide by Sourcery

This PR adds functionality to apply GraphConfig to the translator, allowing configuration values to be applied to construct nodes before graph processing. The implementation includes a new graph_config module with utility functions for handling graph configurations, modifications to the LG class to support config application, and comprehensive unit tests.

Class diagram for LG class with GraphConfig changes

classDiagram
    class LG {
        -f
        -ssid
        -apply_config
        +__init__(f, ssid=None, apply_config=True)
    }
    class GraphConfig {
        +apply_active_configuration(logical_graph: dict) dict
        +is_config_invalid(logical_graph: dict) bool
        +get_key_idx_from_list(key: str, dictList: list) int
    }
    LG --> GraphConfig : uses
Loading

Class diagram for new graph_config module

classDiagram
    class GraphConfig {
        +apply_active_configuration(logical_graph: dict) dict
        +is_config_invalid(logical_graph: dict) bool
        +get_key_idx_from_list(key: str, dictList: list) int
    }
Loading

File-Level Changes

Change Details Files
Added new graph configuration handling module
  • Created utility functions to apply active configurations to logical graphs
  • Implemented validation checks for graph configuration data
  • Added helper function to find node indices in graph data arrays
daliuge-translator/dlg/dropmake/graph_config.py
Modified LG class to support graph configuration
  • Added apply_config parameter to LG class constructor
  • Integrated graph configuration application into graph loading process
  • Updated import statements to use absolute imports
daliuge-translator/dlg/dropmake/lg.py
Added comprehensive test suite for graph configuration
  • Created tests for empty and invalid configurations
  • Added tests for Loop node configuration application
  • Added tests for Scatter node configuration application
  • Implemented helper functions for test data loading and value extraction
daliuge-translator/test/dropmake/test_graph_config.py
Refactored error handling in utility module
  • Updated import statement for GraphException
daliuge-translator/dlg/dropmake/dm_utils.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @myxie - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟡 Testing: 2 issues found
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

daliuge-translator/dlg/dropmake/graph_config.py Outdated Show resolved Hide resolved
try:
activeConfigurationID = logical_graph[ACTIVE_CONFIG_KEY]
activeConfig = logical_graph[CONFIG_KEY][activeConfigurationID]
nodeDataArray = logical_graph[GRAPH_NODES]
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider converting list structures to dictionaries for node and field lookups.

The use of lists requiring linear search for ID lookups adds unnecessary complexity. Convert the list structures to dictionaries for O(1) lookup performance:

# Instead of:
nodeDataArray = logical_graph[GRAPH_NODES]  # list of dicts
idx = get_key_idx_from_list(node_id, nodeDataArray)
node = nodeDataArray[idx]

# Use:
nodeDataArray = {node['id']: node for node in logical_graph[GRAPH_NODES]}
node = nodeDataArray[node_id]

# Similarly for fields:
fields = {field['id']: field for field in node['fields']}
field = fields[field_id]

This eliminates the need for get_key_idx_from_list while maintaining all functionality. The main loop becomes cleaner:

nodeDataArray = {node['id']: node for node in logical_graph[GRAPH_NODES]}
for node_id, fields in activeConfig["nodes"].items():
    if node_id not in nodeDataArray:
        LOGGER.warning("%s present in activeConfig but not available in Logical Graph.")
        continue
    node = nodeDataArray[node_id]
    node_fields = {f['id']: f for f in node['fields']}
    for field_id, config_field in fields['fields'].items():
        if field_id not in node_fields:
            continue
        field = node_fields[field_id]
        prev_value = field["value"]
        field["value"] = config_field["value"]
        LOGGER.info("Updating: Node %s, Field %s, from %s to %s",
                   node['name'], field['name'], str(prev_value), str(field["value"]))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this is an important step for moving towards the new schema approach (see LIU-57), and something we can do even without modifying the schema.

However, in this case, we are trying to modify the logical_graph itself and so I think we transform it to a dictionary only to re-transform it back to an array. Definitely an approach to keep in mind moving forward to resolve all the searching we do across the different lists.

Copy link
Contributor

Choose a reason for hiding this comment

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

You're absolutely right, @myxie. Converting the list to a dictionary for efficient lookups is a great step towards optimizing the current implementation, especially as we move towards the new schema approach. While it does involve an additional transformation step, the performance benefits during the lookup phase can be significant, particularly for larger graphs.

If the transformation back to a list is a concern, you might consider maintaining both structures temporarily during processing. This way, you can perform efficient lookups and updates using the dictionary, and then easily convert back to the list format if needed for compatibility with existing code or output requirements.

As you mentioned, this approach can be a valuable interim solution to reduce complexity and improve performance without requiring immediate schema changes. It's definitely worth considering as part of a broader refactoring strategy. Let me know if you need any further assistance or if there's anything else you'd like to discuss!

daliuge-translator/dlg/dropmake/graph_config.py Outdated Show resolved Hide resolved
daliuge-translator/dlg/dropmake/graph_config.py Outdated Show resolved Hide resolved
daliuge-translator/test/dropmake/test_graph_config.py Outdated Show resolved Hide resolved
daliuge-translator/test/dropmake/test_graph_config.py Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Nov 14, 2024

Coverage Status

coverage: 79.79% (+0.04%) from 79.752%
when pulling ae76261 on LIU-417
into c09b85e on master.

@myxie myxie merged commit 20b29ec into master Nov 15, 2024
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants