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

Feature/sckan-250 - UI improvements for undefined values #216

Merged
merged 16 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions backend/composer/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,48 +486,49 @@ def get_statement_preview(self, instance):
self.context['journey'] = instance.get_journey()
return self.create_statement_preview(instance, self.context['journey'])


def create_statement_preview(self, instance, journey):
sex = instance.sex.name if instance.sex else ""
sex = instance.sex.sex_str if instance.sex else None

species_list = [specie.name for specie in instance.species.all()]
species = join_entities(species_list)
if not species:
species = ""

phenotype = instance.phenotype.name.lower() if instance.phenotype else ""
phenotype = instance.phenotype.phenotype_str if instance.phenotype else ''
origin_names = [origin.name for origin in instance.origins.all()]
origins = join_entities(origin_names)
if not origins:
origins = ""

circuit_type = instance.get_circuit_type_display().lower() if instance.circuit_type else ""
projection = instance.get_projection_display().lower() if instance.projection else ""
circuit_type = instance.get_circuit_type_display() if instance.circuit_type else None
projection = instance.get_projection_display() if instance.projection else None

laterality_description = instance.get_laterality_description()
if not laterality_description:
laterality_description = ""

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

# Creating the statement
if sex != "" or species != "":
statement = f"In {sex} {species}, the {phenotype} connection goes {journey_sentence}.\n"
if sex or species != "":
statement = f"In {sex or ''} {species}, the {phenotype.lower()} connection goes {journey_sentence}.\n"
else:
statement = f"A {phenotype} connection goes {journey_sentence}.\n"
statement = f"A {phenotype.lower()} connection goes {journey_sentence}.\n"

statement += f"This "
if projection != "not specified":
statement += f"{projection} "
if circuit_type != "not specified":
statement += f"{circuit_type} "
if projection:
statement += f"{projection.lower()} "
if circuit_type:
statement += f"{circuit_type.lower()} "

statement += f"connection projects from the {origins}."
if laterality_description != "":
if laterality_description:
statement = statement[:-1] + f" and is found {laterality_description}.\n"

if apinatomy:
statement += f" It is described in {apinatomy} model."

return statement.strip()
return statement.strip().replace(" ", " ")

def get_errors(self, instance) -> List:
return get_connectivity_errors(instance)
Expand Down
3 changes: 0 additions & 3 deletions backend/composer/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
class Laterality(models.TextChoices):
RIGHT = "RIGHT", "Right"
LEFT = "LEFT", "Left"
UNKNOWN = "UNKNOWN", "Not specified"


class Projection(models.TextChoices):
IPSI = "IPSI", "Ipsilateral"
CONTRAT = "CONTRAT", "Contralateral"
BI = "BI", "Bilateral"
UNKNOWN = "UNKNOWN", "Not specified"


# todo: motor and sensory should move to phenotype options per csv
Expand All @@ -25,7 +23,6 @@ class CircuitType(models.TextChoices):
INTRINSIC = "INTRINSIC", "Intrinsic"
PROJECTION = "PROJECTION", "Projection"
ANAXONIC = "ANAXONIC", "Anaxonic"
UNKNOWN = "UNKNOWN", "Not specified"


class ViaType(models.TextChoices):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
# Generated by Django 4.1.4 on 2024-02-01 13:26

from django.db import migrations, models


def change_not_specified_to_null(apps, schema_editor):
ConnectivityStatement = apps.get_model('composer', 'ConnectivityStatement')
ConnectivityStatement.objects.filter(laterality='UNKNOWN').update(laterality=None)
ConnectivityStatement.objects.filter(projection='UNKNOWN').update(projection=None)
ConnectivityStatement.objects.filter(circuit_type='UNKNOWN').update(circuit_type=None)


class Migration(migrations.Migration):
dependencies = [
("composer", "0034_remove_connectivitystatement_state_valid_and_more"),
]

operations = [
# ADD the null=True option to the circuit_type, laterality and projection fields
migrations.AlterField(
model_name="connectivitystatement",
name="circuit_type",
field=models.CharField(
choices=[
("SENSORY", "Sensory"),
("MOTOR", "Motor"),
("INTRINSIC", "Intrinsic"),
("PROJECTION", "Projection"),
("ANAXONIC", "Anaxonic"),
("UNKNOWN", "Not specified"),
],
max_length=20,
null=True,
),
),
migrations.AlterField(
model_name="connectivitystatement",
name="laterality",
field=models.CharField(
choices=[
("RIGHT", "Right"),
("LEFT", "Left"),
("UNKNOWN", "Not specified"),
],
max_length=20,
null=True,
),
),
migrations.AlterField(
model_name="connectivitystatement",
name="projection",
field=models.CharField(
choices=[
("IPSI", "Ipsilateral"),
("CONTRAT", "Contralateral"),
("BI", "Bilateral"),
("UNKNOWN", "Not specified"),
],
max_length=20,
null=True,
),
),

# CONVERT UNKNOWN TO NULL
migrations.RunPython(change_not_specified_to_null),


# Remove the UNKNOWN option from the circuit_type, laterality and projection fields
migrations.RemoveConstraint(
model_name="connectivitystatement",
name="circuit_type_valid",
),
migrations.RemoveConstraint(
model_name="connectivitystatement",
name="laterality_valid",
),
migrations.RemoveConstraint(
model_name="connectivitystatement",
name="projection_valid",
),
migrations.AlterField(
model_name="connectivitystatement",
name="circuit_type",
field=models.CharField(
choices=[
("SENSORY", "Sensory"),
("MOTOR", "Motor"),
("INTRINSIC", "Intrinsic"),
("PROJECTION", "Projection"),
("ANAXONIC", "Anaxonic"),
],
max_length=20,
null=True,
),
),
migrations.AlterField(
model_name="connectivitystatement",
name="laterality",
field=models.CharField(
choices=[("RIGHT", "Right"), ("LEFT", "Left")], max_length=20, null=True
),
),
migrations.AlterField(
model_name="connectivitystatement",
name="projection",
field=models.CharField(
choices=[
("IPSI", "Ipsilateral"),
("CONTRAT", "Contralateral"),
("BI", "Bilateral"),
],
max_length=20,
null=True,
),
),
migrations.AddConstraint(
model_name="connectivitystatement",
constraint=models.CheckConstraint(
check=models.Q(("laterality__in", ["RIGHT", "LEFT"])),
name="laterality_valid",
),
),
migrations.AddConstraint(
model_name="connectivitystatement",
constraint=models.CheckConstraint(
check=models.Q(
(
"circuit_type__in",
["SENSORY", "MOTOR", "INTRINSIC", "PROJECTION", "ANAXONIC"],
)
),
name="circuit_type_valid",
),
),
migrations.AddConstraint(
model_name="connectivitystatement",
constraint=models.CheckConstraint(
check=models.Q(("projection__in", ["IPSI", "CONTRAT", "BI"])),
name="projection_valid",
),
),


# add blank=True to Laterality, Projections, Circuit Type
migrations.AlterField(
model_name="connectivitystatement",
name="circuit_type",
field=models.CharField(
blank=True,
choices=[
("SENSORY", "Sensory"),
("MOTOR", "Motor"),
("INTRINSIC", "Intrinsic"),
("PROJECTION", "Projection"),
("ANAXONIC", "Anaxonic"),
],
max_length=20,
null=True,
),
),
migrations.AlterField(
model_name="connectivitystatement",
name="laterality",
field=models.CharField(
blank=True,
choices=[("RIGHT", "Right"), ("LEFT", "Left")],
max_length=20,
null=True,
),
),
migrations.AlterField(
model_name="connectivitystatement",
name="projection",
field=models.CharField(
blank=True,
choices=[
("IPSI", "Ipsilateral"),
("CONTRAT", "Contralateral"),
("BI", "Bilateral"),
],
max_length=20,
null=True,
),
),

]
18 changes: 13 additions & 5 deletions backend/composer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ class Phenotype(models.Model):

def __str__(self):
return self.name

@property
def phenotype_str(self):
return str(self.name) if self.name else ''

class Meta:
ordering = ["name"]
Expand Down Expand Up @@ -182,6 +186,10 @@ class Sex(models.Model):

def __str__(self):
return self.name

@property
def sex_str(self):
return str(self.name) if self.name else ''

class Meta:
ordering = ["name"]
Expand Down Expand Up @@ -386,14 +394,15 @@ class ConnectivityStatement(models.Model):
owner = models.ForeignKey(
User, verbose_name="Curator", on_delete=models.SET_NULL, null=True, blank=True
)

laterality = models.CharField(
max_length=20, default=Laterality.UNKNOWN, choices=Laterality.choices
max_length=20, choices=Laterality.choices, null=True, blank=True
)
projection = models.CharField(
max_length=20, default=Projection.UNKNOWN, choices=Projection.choices
max_length=20, choices=Projection.choices, null=True, blank=True
)
circuit_type = models.CharField(
max_length=20, default=CircuitType.UNKNOWN, choices=CircuitType.choices
max_length=20, choices=CircuitType.choices, null=True, blank=True
)
# TODO for next releases we could have only 1 field for phenotype + an intermediate table with the phenotype's categories such as circuit_type, laterality, projection, functional_circuit_role, projection_phenotype among others
phenotype = models.ForeignKey(
Expand Down Expand Up @@ -546,9 +555,8 @@ def get_laterality_description(self):
laterality_map = {
Laterality.RIGHT.value: "on the right side of the body",
Laterality.LEFT.value: "on the left side of the body",
Laterality.UNKNOWN.value: "",
}
return laterality_map.get(self.laterality, "")
return laterality_map.get(self.laterality, None)

def assign_owner(self, request):
if ConnectivityStatementService(self).should_set_owner(request):
Expand Down
17 changes: 17 additions & 0 deletions frontend/src/components/Forms/StatementForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const StatementForm = (props: any) => {
isDisabled,
label: "Circuit Type",
classNames: "col-xs-12 col-md-6",
placeholder: "Enter Circuit Type",
},
};

Expand All @@ -71,6 +72,7 @@ const StatementForm = (props: any) => {
isDisabled,
label: "Laterality",
classNames: "col-xs-12 col-md-6",
placeholder: "Enter Laterality",
},
};

Expand All @@ -80,6 +82,7 @@ const StatementForm = (props: any) => {
isDisabled,
label: "Projection",
classNames: "col-xs-12 col-md-6",
placeholder: "Enter Projection",
},
};

Expand Down Expand Up @@ -653,6 +656,20 @@ const StatementForm = (props: any) => {
},
};

// Add null option to the fields which have null type in dropdown.
Object.keys(copiedSchema.properties).forEach((key) => {
if (copiedSchema.properties[key].type.includes("null") && copiedSchema.properties[key]?.enum && copiedSchema.properties[key]?.enumNames) {
copiedSchema.properties[key].enum.push(null);
copiedSchema.properties[key].enumNames.push("---------");
}
});

Object.keys(copiedUISchema).forEach((key) => {
if (copiedUISchema[key]["ui:options"] && copiedUISchema[key]["ui:options"].data) {
copiedUISchema[key]["ui:options"].data.push({ label: "---------", value: null })
}
});

const widgets = {
AnatomicalEntitiesField,
CustomSingleSelect,
Expand Down
Loading