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

Transformer to prune graph #638

Merged
merged 10 commits into from
Oct 2, 2024
Merged

Transformer to prune graph #638

merged 10 commits into from
Oct 2, 2024

Conversation

Juliamg
Copy link
Contributor

@Juliamg Juliamg commented Sep 27, 2024

This implementation contains two new graph transformers means for pruning of a graph.

  1. PruneDanglingNodes:
    Removes nodes from the graph that do not have any edges to any other nodes.

  2. ResolveValues:
    Traverses the graph to find source nodes that point to other nodes with an edge to a value predicate. It then resolves this value directly to the source node, with the original predicate, so that the intermediate node can be skipped. Option to delete the intermediate node is present as well.

Used with the IODD extractor -

Graph summary before running these two transformers on the graph:

            Type  Occurrence
0     TextObject       166
1     IoddDevice           1
2  ProcessDataIn        1

Graph summary running ResolveValues:

ResolveValues(destination_node_type=IODD.TextObject, property_value=IODD.value).transform(graph)

            Type  Occurrence
0     TextObject         163
1     IoddDevice           1
2  ProcessDataIn           1

Graph summary after also running PruneDanglingNodes:

PruneDanglingNodes(node_prune_types=[IODD.TextObject]).transform(graph)

            Type  Occurrence
0     IoddDevice           1
1  ProcessDataIn         1

Copy link

github-actions bot commented Sep 27, 2024

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
12751 8713 68% 60% 🟢

New Files

File Coverage Status
cognite/neat/graph/transformers/_iodd.py 0% 🟢
cognite/neat/graph/transformers/_prune_graph.py 0% 🟢
TOTAL 0% 🟢

Modified Files

File Coverage Status
cognite/neat/graph/extractors/_iodd.py 34% 🟢
TOTAL 34% 🟢

updated for commit: 2d57ce3 by action🐍

@Juliamg Juliamg marked this pull request as ready for review September 27, 2024 09:31
@Juliamg Juliamg requested a review from a team as a code owner September 27, 2024 09:31
Copy link
Collaborator

@nikokaoja nikokaoja left a comment

Choose a reason for hiding this comment

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

Some changes of code requested

cognite/neat/graph/transformers/_prune_graph.py Outdated Show resolved Hide resolved
cognite/neat/graph/transformers/_prune_graph.py Outdated Show resolved Hide resolved
cognite/neat/graph/transformers/_prune_graph.py Outdated Show resolved Hide resolved
cognite/neat/graph/transformers/_prune_graph.py Outdated Show resolved Hide resolved
triples_to_delete.append((object, predicate, subject))

for delete_triple in triples_to_delete:
graph.remove(triple=delete_triple)
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can do the same as suggested above and make customized PruneDanglingTextObjectNodes transformer

Copy link
Collaborator

Choose a reason for hiding this comment

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

but make sure to add attribute:

_use_only_once: bool = True
_need_changes = frozenset(
    {
        str(extractors.IODDExtractor.__name__),
        str(ResolveValues.__name__), 
    }
)

from ._base import BaseTransformer


class ResolveValues(BaseTransformer):
Copy link
Collaborator

Choose a reason for hiding this comment

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

What this transformer is doing is reducing:

sourceNode - [propertyX] -> destinatioNode -[propertyY] -> value
sourceNode - [propertyX] -> value

so you are reducing from 2 hops to get value to single hop, so maybe more descriptive name of method would be

FlattenDualHop


for node in nodes_without_neighbours:
# Remove node and its property triples in the graph
graph.remove(triple=(node["subject"], None, None))
Copy link
Collaborator

Choose a reason for hiding this comment

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

ok I did not know you can access subject by stating this :) ! NICE!

@Juliamg Juliamg merged commit 512164f into main Oct 2, 2024
7 checks passed
@Juliamg Juliamg deleted the transformer-iodd-textobjects branch October 2, 2024 10:06
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