Skip to content

Commit

Permalink
SCKAN-283: journey styling
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelpiano committed Apr 10, 2024
1 parent 6a9c428 commit fd8fa7b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
3 changes: 1 addition & 2 deletions backend/composer/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ def create_statement_preview(self, instance, journey):
laterality_description = instance.get_laterality_description()

apinatomy = instance.apinatomy_model if instance.apinatomy_model else ""
journey_sentence = ', '.join(journey)
journey_sentence = '; '.join(journey)

# Creating the statement
if sex or species != "":
Expand Down Expand Up @@ -679,4 +679,3 @@ class Meta:
"statement_preview",
"errors"
)

23 changes: 15 additions & 8 deletions backend/composer/services/graph_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
JOURNEY_DELIMITER = '\\'


def get_entity_name(entity):
if entity.region_layer is not None:
return f"{entity.region_layer.region.name} ({entity.region_layer.layer.name})"
else:
return entity.name


def generate_paths(origins, vias, destinations):
paths = []
# Calculate the total number of layers, including origins and destinations
Expand All @@ -15,13 +22,13 @@ def generate_paths(origins, vias, destinations):
# Directly use pre-fetched 'from_entities' without triggering additional queries
if origin in destination.from_entities.all() or (not destination.from_entities.exists() and len(vias) == 0):
for dest_entity in destination.anatomical_entities.all():
paths.append([(origin.name, 0), (dest_entity.name, destination_layer)])
paths.append([(get_entity_name(origin), 0), (get_entity_name(dest_entity), destination_layer)])

# Handle connections involving vias
if vias:
for origin in origins:
# Generate paths through vias for each origin
paths.extend(create_paths_from_origin(origin, vias, destinations, [(origin.name, 0)], destination_layer))
paths.extend(create_paths_from_origin(origin, vias, destinations, [(get_entity_name(origin), 0)], destination_layer))

# Remove duplicates from the generated paths
unique_paths = [list(path) for path in set(tuple(path) for path in paths)]
Expand All @@ -33,10 +40,10 @@ def create_paths_from_origin(origin, vias, destinations, current_path, destinati
# Base case: if there are no more vias to process
if not vias:
# Generate direct connections from the current path to destinations
return [current_path + [(dest_entity.name, destination_layer)] for dest in destinations
return [current_path + [(get_entity_name(dest_entity), destination_layer)] for dest in destinations
for dest_entity in dest.anatomical_entities.all()
if current_path[-1][0] in list(
a.name for a in dest.from_entities.all()) or not dest.from_entities.exists()]
get_entity_name(a) for a in dest.from_entities.all()) or not dest.from_entities.exists()]

new_paths = []
for idx, current_via in enumerate(vias):
Expand All @@ -46,20 +53,20 @@ def create_paths_from_origin(origin, vias, destinations, current_path, destinati
# In other words, it checks if there is a valid connection
# from the last node in the current path to the current via.
if current_path[-1][0] in list(
a.name for a in current_via.from_entities.all()) or not current_via.from_entities.exists():
get_entity_name(a) for a in current_via.from_entities.all()) or not current_via.from_entities.exists():
for entity in current_via.anatomical_entities.all():
# Build new sub-paths including the current via entity
new_sub_path = current_path + [(entity.name, via_layer)]
new_sub_path = current_path + [(get_entity_name(entity), via_layer)]
# Recursively call to build paths from the next vias
new_paths.extend(
create_paths_from_origin(origin, vias[idx + 1:], destinations, new_sub_path, destination_layer))

# Check for direct connections to destinations from the current via
for dest in destinations:
for dest_entity in dest.anatomical_entities.all():
if entity.name in list(a.name for a in dest.from_entities.all()):
if get_entity_name(entity) in list(get_entity_name(a) for a in dest.from_entities.all()):
# Add path to destinations directly from the current via
new_paths.append(new_sub_path + [(dest_entity.name, destination_layer)])
new_paths.append(new_sub_path + [(get_entity_name(dest_entity), destination_layer)])

return new_paths

Expand Down
8 changes: 5 additions & 3 deletions frontend/src/components/ProofingTab/ProofingTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ const ProofingTab = (props: any) => {
<Stack spacing={2}>
<Typography variant="h5">Journey</Typography>
{statement.journey.map((journeyStep: string, index: number) => {
if (index === 0) {
return <>{journeyStep.charAt(0).toUpperCase() + journeyStep.slice(1)}, </>;
if (index === 0 && index === statement.journey.length - 1) {
return <>{journeyStep.charAt(0).toUpperCase() + journeyStep.slice(1)}. </>;
} else if (index === 0) {
return <>{journeyStep.charAt(0).toUpperCase() + journeyStep.slice(1)}; </>;
} else {
if (index === statement.journey.length - 1) {
return <>{journeyStep}.</>;
} else {
return <>{journeyStep}, </>;
return <>{journeyStep}; </>;
}
}
})}
Expand Down

0 comments on commit fd8fa7b

Please sign in to comment.