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

Task/281/adding both #326

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion configuration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def uncon_studies_form(self, request, **kwargs):
available_populations_types = UnConSampleChoices.values
available_mode_of_presentation = PresentationModeChoices.values
available_consciousness_measure_phase_type = UnConsciousnessMeasurePhase.objects.all()
available_consciousness_measure_type = UnConsciousnessMeasureType.objects.all()
# it's a "synthetic" type not to be used directly
available_consciousness_measure_type = UnConsciousnessMeasureType.objects.exclude(name__iexact="Both")
available_consciousness_measure_sub_type = UnConsciousnessMeasureSubType.objects.all()
available_tasks_types = UnConTaskType.objects.all()
available_suppression_method_types = UnConSuppressionMethodType.objects.all()
Expand Down
810 changes: 409 additions & 401 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ drf-nested-routers = "^0.94.1"
drf-spectacular = "^0.27.2"
boto3 = ">=1.34.112"
django-nested-inline = "^0.4.6"
# following needed as original package is unmaintained
django-modeladmin-reorder = {git = "https://github.com/bloodwithmilk25/django-modeladmin-reorder.git", rev = "master"}
django-spa = {git = "https://github.com/VBobCat/django-spa.git", rev = "master"}
django-countries = {git = "https://github.com/SmileyChris/django-countries", rev="58f258402072a18756a15daa81325428382bd946"}
#django-countries = {git = "https://github.com/SmileyChris/django-countries", rev="58f258402072a18756a15daa81325428382bd946"}
django-countries = "7.6.1"
numpy = "^1.26.4"
pandas = "^1.5.3"
whitenoise = "6.2.0" # currently should be hardcoded to 6.2.0
Expand Down
16 changes: 16 additions & 0 deletions uncontrast_studies/migrations/0005_auto_20241122_0913.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 5.1.2 on 2024-11-22 09:13

from django.db import migrations


def bootstrap_both_measure_type(apps, schema_editor):
UnConsciousnessMeasureType = apps.get_model("uncontrast_studies", "UnConsciousnessMeasureType")
UnConsciousnessMeasureType.objects.get_or_create(name="Both")


class Migration(migrations.Migration):
dependencies = [
("uncontrast_studies", "0004_alter_historicalunconsample_type_and_more"),
]

operations = [migrations.RunPython(bootstrap_both_measure_type, reverse_code=migrations.RunPython.noop)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by Django 5.1.2 on 2024-11-22 18:56

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("uncontrast_studies", "0005_auto_20241122_0913"),
]

operations = [
migrations.AlterField(
model_name="historicalunconsciousnessmeasure",
name="type",
field=models.ForeignKey(
blank=True,
db_constraint=False,
limit_choices_to=models.Q(("name", "Both"), _negated=True),
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="uncontrast_studies.unconsciousnessmeasuretype",
),
),
migrations.AlterField(
model_name="unconsciousnessmeasure",
name="type",
field=models.ForeignKey(
limit_choices_to=models.Q(("name", "Both"), _negated=True),
on_delete=django.db.models.deletion.PROTECT,
related_name="unconsciousness_measures",
to="uncontrast_studies.unconsciousnessmeasuretype",
),
),
migrations.AlterField(
model_name="unconsciousnessmeasuresubtype",
name="type",
field=models.ForeignKey(
limit_choices_to=models.Q(("name", "Both"), _negated=True),
on_delete=django.db.models.deletion.PROTECT,
related_name="unconsciousness_measure_sub_types",
to="uncontrast_studies.unconsciousnessmeasuretype",
),
),
]
4 changes: 3 additions & 1 deletion uncontrast_studies/models/consciousness_measure.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import models
from django.db.models import CASCADE, PROTECT
from django.db.models import CASCADE, PROTECT, Q
from simple_history.models import HistoricalRecords


Expand All @@ -24,6 +24,7 @@ class UnConsciousnessMeasureSubType(models.Model):
null=False,
on_delete=PROTECT,
to=UnConsciousnessMeasureType,
limit_choices_to=~Q(name="Both"), # both is synthetic, not to be used directly
related_name="unconsciousness_measure_sub_types",
)

Expand All @@ -47,6 +48,7 @@ class UnConsciousnessMeasure(models.Model):
null=False,
on_delete=PROTECT,
to=UnConsciousnessMeasureType,
limit_choices_to=~Q(name="Both"), # both is synthetic, not to be used directly
related_name="unconsciousness_measures",
)
sub_type = models.ForeignKey(
Expand Down
10 changes: 10 additions & 0 deletions uncontrast_studies/open_api_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"year_of_publication",
]

UNCONTRAST_GRAPH_BREAKDOWN_OPTIONS_WITH_SIGNIFICANCE = UNCONTRAST_GRAPH_BREAKDOWN_OPTIONS + ["significance"]

number_of_experiments_parameter = OpenApiParameter(name="min_number_of_experiments", type=int, required=False)
bin_size_parameter = OpenApiParameter(name="bin_size", type=int, required=False)

Expand All @@ -55,6 +57,14 @@
required=True,
)

breakdown_parameter_with_significance = OpenApiParameter(
name="breakdown",
description="breakdown needed for certain graphs",
type=str,
enum=UNCONTRAST_GRAPH_BREAKDOWN_OPTIONS_WITH_SIGNIFICANCE,
required=True,
)

paradigms_multiple_optional_parameter = OpenApiParameter(
name="paradigms", description="paradigms optional", type=int, many=True, required=False
)
Expand Down
48 changes: 39 additions & 9 deletions uncontrast_studies/processors/experiments_comparison.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db.models import QuerySet, F, Count
from django.db.models.functions import JSONObject
from django.db.models import QuerySet, F, Count, Exists, OuterRef, Q, Case, Subquery, When, IntegerField
from django.db.models.functions import JSONObject, Coalesce

from contrast_api.choices import SignificanceChoices

Expand Down Expand Up @@ -153,12 +153,44 @@ def process_consciousness_measure_phase(self, experiments: QuerySet[UnConExperim
return subquery

def process_consciousness_measure_type(self, experiments: QuerySet[UnConExperiment]):
experiments_with_both = experiments.annotate(
has_subjective=Exists(
UnConsciousnessMeasureType.objects.filter(
unconsciousness_measures__experiment=OuterRef("pk"), name="Subjective"
)
),
has_objective=Exists(
UnConsciousnessMeasureType.objects.filter(
unconsciousness_measures__experiment=OuterRef("pk"), name="Objective"
)
),
).filter(has_subjective=True, has_objective=True)
subquery = (
UnConsciousnessMeasureType.objects.filter(unconsciousness_measures__experiment__in=experiments)
.distinct()
.values("name")
.annotate(experiment_count=Count("unconsciousness_measures__experiment", distinct=True))
UnConsciousnessMeasureType.objects.filter(
Q(unconsciousness_measures__experiment__in=experiments) | Q(name="Both")
)
.annotate(
experiment_count=Case(
When(
name="Both",
then=Subquery(experiments_with_both.annotate(count=Count("id")).values("count")[:1]),
),
When(
# to remove double counting the "both" experiments
Q(name="Objective") | Q(name="Subjective"),
then=Count(
"unconsciousness_measures__experiment",
distinct=True,
filter=~Q(unconsciousness_measures__experiment__in=experiments_with_both),
),
),
default=Count("unconsciousness_measures__experiment", distinct=True),
output_field=IntegerField(),
)
)
.annotate(key=F("name"))
.values("name", "experiment_count", "key")
.distinct()
)

return subquery
Expand Down Expand Up @@ -244,12 +276,10 @@ def aggregate_query_by_breakdown(self):
experiment_ids = []
for sig_option in significance_options:
experiments_for_option = (
self.experiments.filter(significance=sig_option).distinct().values_list("id", flat=True)
self.experiments.filter(significance=sig_option).distinct() # .values_list("id", flat=True)
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

why?

option_experiment_ids = experiments_for_option.values_list("id", flat=True)
if self.is_csv:


experiment_ids += option_experiment_ids
else:
subquery = self.get_query(experiments_for_option)
Expand Down
8 changes: 0 additions & 8 deletions uncontrast_studies/processors/grand_overview_pie.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,5 @@ def _aggregate_query_by_breakdown(self, experiments: QuerySet[UnConExperiment]):
.values("series_name", "value", "series")
)[0:1]

# qs = (experiments
# .aggregate(
# series=ArraySubquery(subquery),
# series_name=Value("Grand Overview", output_field=CharField()),
# value=Count("id", distinct=True)
# )
# )

# Note we're filtering out empty timeseries with the cardinality option
return qs
30 changes: 26 additions & 4 deletions uncontrast_studies/processors/parameters_distribution_bar.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import itertools

from django.contrib.postgres.expressions import ArraySubquery
from django.db.models import QuerySet, OuterRef, F, Func, Count
from django.db.models import QuerySet, OuterRef, F, Func, Count, Case, When, Q, Value, CharField, Exists
from django.db.models.functions import JSONObject

from uncontrast_studies.processors.base import BaseProcessor
Expand Down Expand Up @@ -201,9 +201,31 @@ def process_consciousness_measure_phase(self):
return qs

def process_consciousness_measure_type(self):
experiments_subquery_by_breakdown = self.filtered_experiments.filter(
unconsciousness_measures__type=OuterRef("pk")
).values("id", "significance")
experiments_subquery_by_breakdown = self.filtered_experiments.annotate(
measure_type=Case(
When(
Exists(UnConsciousnessMeasure.objects.filter(experiment=OuterRef("pk"), type__name="Objective"))
& Exists(UnConsciousnessMeasure.objects.filter(experiment=OuterRef("pk"), type__name="Subjective")),
then=Value("Both"),
),
default=Value(None),
output_field=CharField(null=True),
)
).filter(
Q(measure_type=OuterRef("name"))
|
# either it's not subjective or objective - if so we can just check the measure type name
(
Q(unconsciousness_measures__type__name=OuterRef("name"))
& ~Q(unconsciousness_measures__type__name__in=["Objective", "Subjective"])
)
# or it is subjective or objective - if so we can need to rule out the both option
| (
Q(unconsciousness_measures__type__name=OuterRef("name"))
& Q(unconsciousness_measures__type__name__in=["Objective", "Subjective"])
& Q(measure_type__isnull=True)
),
)

breakdown_query = UnConsciousnessMeasureType.objects.values("name").distinct().annotate(series_name=F("name"))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import itertools

from django.contrib.postgres.expressions import ArraySubquery
from django.db.models import Func, F, Count, QuerySet, OuterRef, Case, When, Value
from django.db.models import Func, F, Count, QuerySet, OuterRef, Case, When, Value, Q, CharField, Exists

from contrast_api.utils import cast_as_boolean
from uncontrast_studies.processors.base import BaseProcessor
Expand Down Expand Up @@ -297,9 +297,37 @@ def process_consciousness_measure_phase(self):
return qs

def process_consciousness_measure_type(self):
experiments_subquery_by_breakdown = self.filtered_experiments.filter(
unconsciousness_measures__type=OuterRef("pk")
).values("id")
experiments_subquery_by_breakdown = (
self.filtered_experiments.annotate(
measure_type=Case(
When(
Exists(UnConsciousnessMeasure.objects.filter(experiment=OuterRef("pk"), type__name="Objective"))
& Exists(
UnConsciousnessMeasure.objects.filter(experiment=OuterRef("pk"), type__name="Subjective")
),
then=Value("Both"),
),
default=Value(None),
output_field=CharField(null=True),
)
)
.filter(
Q(measure_type=OuterRef("name"))
|
# either it's not subjective or objective - if so we can just check the measure type name
(
Q(unconsciousness_measures__type__name=OuterRef("name"))
& ~Q(unconsciousness_measures__type__name__in=["Objective", "Subjective"])
)
# or it is subjective or objective - if so we can need to rule out the both option
| (
Q(unconsciousness_measures__type__name=OuterRef("name"))
& Q(unconsciousness_measures__type__name__in=["Objective", "Subjective"])
& Q(measure_type__isnull=True)
),
)
.values("id")
)

breakdown_query = UnConsciousnessMeasureType.objects.values("name").distinct().annotate(series_name=F("name"))

Expand Down
Loading
Loading