-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feature/sckan-324 - journey computation #381
Open
D-GopalKrishna
wants to merge
13
commits into
develop
Choose a base branch
from
feature/SCKAN-324
base: develop
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.
+143
−46
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ddd7d9c
SCKAN-324 - Add journey_description and journey_entities fields to Co…
D-GopalKrishna 22ba03f
SCKAN-324 - Add migration to populate journey_description and journey…
D-GopalKrishna ea1297f
SCKAN-324 - adapt partial_update method in ViaViewSet and Destination…
D-GopalKrishna 5b03c6d
Merge remote-tracking branch 'refs/remotes/origin/develop'
D-GopalKrishna ae6bcb0
Merge remote-tracking branch 'origin/develop' into feature/SCKAN-324
D-GopalKrishna 01436c0
SCKAN-324 - refactor: replace journey_description and journey_entitie…
D-GopalKrishna b8a1d9e
SCKAN-324 - refactor: simplify journey compilation and entity buildin…
D-GopalKrishna 50978d1
SCKAN-324 - refactor: remove save entities logic from partial_update …
D-GopalKrishna 51b3838
SCKAN-324 - refactor tests to adapt to new consolidated_path service
D-GopalKrishna b934de3
SCKAN-324 - cleanup - simplify the assignment of compile_journey in s…
D-GopalKrishna 99d1238
SCKAN-324 - do not save journey on every save of ConnectivityStatement
D-GopalKrishna 466d8b3
SCKAN-324 refactor: move journey recompilation to signal handlers
D-GopalKrishna cc18966
SCKAN-324 add journey_path to readonly fields in ConnectivityStatemen…
D-GopalKrishna 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 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 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
18 changes: 18 additions & 0 deletions
18
backend/composer/migrations/0066_connectivitystatement_journey_path.py
This file contains 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,18 @@ | ||
# Generated by Django 4.1.4 on 2024-12-18 08:25 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("composer", "0065_alter_statementalert_text"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="connectivitystatement", | ||
name="journey_path", | ||
field=models.JSONField(blank=True, null=True), | ||
), | ||
] |
This file contains 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,23 @@ | ||
# Generated by Django 4.1.4 on 2024-12-18 08:28 | ||
|
||
from django.db import migrations | ||
from django.db import migrations | ||
from composer.services.graph_service import compile_journey | ||
|
||
|
||
def update_journey_fields(apps, schema_editor): | ||
ConnectivityStatement = apps.get_model('composer', 'ConnectivityStatement') | ||
for cs in ConnectivityStatement.objects.all(): | ||
cs.journey_path = compile_journey(cs) | ||
cs.save(update_fields=["journey_path"]) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("composer", "0066_connectivitystatement_journey_path"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(update_journey_fields), | ||
] |
This file contains 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 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 |
---|---|---|
|
@@ -88,9 +88,7 @@ def consolidate_paths(paths): | |
|
||
paths = consolidated + [paths[i] for i in range(len(paths)) if i not in used_indices] | ||
|
||
return paths, [[((node[1].replace(JOURNEY_DELIMITER, ' or '), node[2]) if ( | ||
node[2] == 0 or path.index(node) == len(path) - 1) else ( | ||
node[1].replace(JOURNEY_DELIMITER, ', '), node[2])) for node in path] for path in paths] | ||
return paths | ||
|
||
|
||
def can_merge(path1, path2): | ||
|
@@ -144,7 +142,7 @@ def merge_paths(path1, path2): | |
return merged_path | ||
|
||
|
||
def compile_journey(connectivity_statement) -> dict: | ||
def compile_journey(connectivity_statement) -> List[str]: | ||
""" | ||
Generates a string of descriptions of journey paths for a given connectivity statement. | ||
|
||
|
@@ -157,17 +155,55 @@ def compile_journey(connectivity_statement) -> dict: | |
# Extract origins, vias, and destinations from the connectivity statement | ||
Via = apps.get_model('composer', 'Via') | ||
Destination = apps.get_model('composer', 'Destination') | ||
Origin = apps.get_model('composer', 'AnatomicalEntity') | ||
|
||
origins = list(connectivity_statement.origins.all()) | ||
|
||
vias = list(Via.objects.filter(connectivity_statement=connectivity_statement)) | ||
destinations = list(Destination.objects.filter(connectivity_statement=connectivity_statement)) | ||
vias = list(Via.objects.filter(connectivity_statement__id=connectivity_statement.id)) | ||
destinations = list(Destination.objects.filter(connectivity_statement__id=connectivity_statement.id)) | ||
origins = list(Origin.objects.filter( | ||
origins_relations__id=connectivity_statement.id).distinct()) | ||
|
||
# Generate all paths and then consolidate them | ||
all_paths2 = generate_paths(origins, vias, destinations) | ||
consolidated_paths, journey_paths = consolidate_paths(all_paths2) | ||
consolidated_paths = consolidate_paths(all_paths2) | ||
return consolidated_paths | ||
|
||
|
||
def get_journey_path_from_consolidated_paths(consolidated_paths): | ||
journey_paths = [[((node[1].replace(JOURNEY_DELIMITER, ' or '), node[2]) if ( | ||
node[2] == 0 or path.index(node) == len(path) - 1) else ( | ||
node[1].replace(JOURNEY_DELIMITER, ', '), node[2])) for node in path] for path in consolidated_paths] | ||
return journey_paths | ||
|
||
|
||
def build_journey_description(consolidated_paths, connectivity_statement): | ||
journey_paths = get_journey_path_from_consolidated_paths( | ||
consolidated_paths) | ||
Via = apps.get_model('composer', 'Via') | ||
vias = list(Via.objects.filter( | ||
connectivity_statement=connectivity_statement)) | ||
|
||
# Create sentences for each journey path | ||
journey_descriptions = [] | ||
for path in journey_paths: | ||
origin_names = path[0][0] | ||
destination_names = path[-1][0] | ||
via_names = ' via '.join([node for node, layer in path if 0 < layer < len(vias) + 1]) | ||
|
||
if via_names: | ||
sentence = f"from {origin_names} to {destination_names} via {via_names}" | ||
else: | ||
sentence = f"from {origin_names} to {destination_names}" | ||
|
||
journey_descriptions.append(sentence) | ||
return journey_descriptions | ||
|
||
|
||
def build_journey_entities(consolidated_paths, connectivity_statement): | ||
entities = [] | ||
Via = apps.get_model('composer', 'Via') | ||
vias = list(Via.objects.filter( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
connectivity_statement=connectivity_statement)) | ||
|
||
for path in consolidated_paths: | ||
origin_splits = path[0][0].split(JOURNEY_DELIMITER) | ||
destination_splits = path[-1][0].split(JOURNEY_DELIMITER) | ||
|
@@ -181,22 +217,9 @@ def compile_journey(connectivity_statement) -> dict: | |
'vias': [{'label': node, 'id': node_id} for node_id, node, layer in path if 0 < layer < len(vias) + 1] | ||
} | ||
entities.append(entity) | ||
return entities | ||
|
||
# Create sentences for each journey path | ||
journey_descriptions = [] | ||
for path in journey_paths: | ||
origin_names = path[0][0] | ||
destination_names = path[-1][0] | ||
via_names = ' via '.join([node for node, layer in path if 0 < layer < len(vias) + 1]) | ||
|
||
if via_names: | ||
sentence = f"from {origin_names} to {destination_names} via {via_names}" | ||
else: | ||
sentence = f"from {origin_names} to {destination_names}" | ||
|
||
journey_descriptions.append(sentence) | ||
|
||
return { | ||
'journey': journey_descriptions, | ||
'entities': entities | ||
} | ||
def recompile_journey_path(instance): | ||
instance.journey_path = compile_journey(instance) | ||
instance.save(update_fields=["journey_path"]) |
This file contains 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.
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.
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.
question: Why do we need to fetch the vias here? can't we rely entirely on the new journey paths attribute created?
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 understand what you mean yes, this is only to get the len(vias) later with -
0<layer<len(vias) + 1]
here is the journey_paths
and in this case, len(vias from the CS) is 1.
[<Via: Via object (37)>]
where the value of that via is:
I am unable to see the pattern to relate them. Do you think there is one @afonsobspinto that I might be missing?
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.
If I understand the code correctly we don't need the vias count; we can just do something on the lines of:
which identifies the vias by looking for nodes with layers between origin (0) and destination (highest layer)