Esempio n. 1
0
 def get_form(self, request, instance, **kwargs):
     # Show only measurement definitions with the content_type set to
     # Session.
     form = super().get_form(request, instance, **kwargs)
     content_type = ContentType.objects.get_for_model(Session)
     MeasurementDefinition = get_measurement_model()
     queryset = MeasurementDefinition.objects.filter(
         content_type=content_type)
     form.base_fields["measurement"] = forms.ModelChoiceField(
         queryset=queryset)
     return form
Esempio n. 2
0
"""
from typing import Tuple

from django.urls import reverse
from django_mri.models.irb_approval import IrbApproval
from django_mri.models.session import Session
from django_mri.serializers.irb_approval import IrbApprovalSerializer
from django_mri.serializers.utils import (
    MiniGroupSerializer,
    MiniMeasurementSerializer,
    MiniSubjectSerializer,
)
from django_mri.utils import get_measurement_model, get_subject_model
from rest_framework import serializers

Measurement = get_measurement_model()
Subject = get_subject_model()

SESSION_SERIALIZER_FIELDS: Tuple[str] = (
    "id",
    "subject",
    "comments",
    "time",
    "measurement",
    "irb",
)
SESSION_READ_FIELDS: Tuple[str] = (
    "dicom_zip",
    "nifti_zip",
    "n_scans",
    "study_groups",
Esempio n. 3
0
class Session(TimeStampedModel):
    """
    Represents a single MRI scanning session.
    """

    #: The associated `Subject` model (optional).
    subject = models.ForeignKey(
        get_subject_model(),
        on_delete=models.CASCADE,
        related_name="mri_session_set",
        blank=True,
        null=True,
    )

    #: Any other information about this scanning sequence.
    comments = models.TextField(
        max_length=1000,
        blank=True,
        null=True,
        help_text=help_text.SESSION_COMMENTS,
    )

    #: The associated `Measurement` model (optional).
    measurement = models.ForeignKey(
        get_measurement_model(),
        related_name="mri_session_set",
        blank=True,
        null=True,
        on_delete=models.PROTECT,
    )

    #: The date and time in which this scanning sequence began.
    time = models.DateTimeField()

    #: Associated IRB approval.
    irb = models.ForeignKey(
        "django_mri.IrbApproval",
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
        verbose_name="IRB approval",
    )

    objects = SessionQuerySet.as_manager()

    class Meta:
        ordering = ("-time", )

    def __str__(self) -> str:
        """
        Returns the string representation of this instance.

        Returns
        -------
        str
            String representation
        """
        date = self.time.date()
        if self.subject:
            return f"Subject #{self.subject.id} MRI session from {date}"
        return f"Unclaimed MRI session from {date}"

    @property
    def study_groups(self) -> QuerySet:
        """
        The experimental groups with which scans in this session are
        associated. This property is only relevant if `STUDY_GROUP_MODEL` is
        set in the project's settings.

        Returns
        -------
        QuerySet
            The associated study groups
        """

        ids = self.scan_set.values_list("study_groups", flat=True)
        return Group.objects.filter(id__in=ids)

    @property
    def subject_age(self) -> float:
        """
        Returns the subject's age in years at the time of the session
        acquisition. If the subject's date of birth or the session's
        acquisition time are not available, returns `None`.

        Returns
        -------
        float
            Subject age in years at the time of the session's acquisition
        """

        conditions = self.time and self.subject and self.subject.date_of_birth
        if conditions:
            delta = self.time.date() - self.subject.date_of_birth
            return delta.total_seconds() / (60 * 60 * 24 * 365)
Esempio n. 4
0
from pathlib import Path
from typing import List

from django.db import models
from django_extensions.db.models import TimeStampedModel
from django_mri.models import help_text
from django_mri.models.managers.session import SessionQuerySet
from django_mri.utils import (
    get_group_model,
    get_measurement_model,
    get_study_model,
    get_subject_model,
)

Group = get_group_model()
MeasurementDefinition = get_measurement_model()
Study = get_study_model()
Subject = get_subject_model()

SECONDS_IN_YEAR: int = 60 * 60 * 24 * 365
CLAIMED_SESSION_STRING: str = "Subject #{subject_id} MRI session from {date}"
UNCLAIMED_SESSION_STRING: str = "Unclaimed MRI session from {date}"
DICOM_FILES_KEY: str = "dicom__image__dcm"
NIFTI_FILES_KEY: str = "_nifti__path"


class Session(TimeStampedModel):
    """
    Represents a single MRI scanning session.
    """