示例#1
0
def get_models(request):
    '''
    Endpoint for API GET Model Collections request.

    Returns:
        1. ModelsCollection protobuf message if "accept" header is "application/x-protobuf"
        2. JSON response otherwise
    '''
    # Tries to construct collection of model families, returning an error if failure.
    try:
        # Creates protobuf collection object
        response = Model_pb2.ModelsCollection()

        # Iterates through all model familys and adds to collection
        for model_fam in ModelFamily.objects.all():

            # Check if pb file exists.
            if model_fam.pb == '':
                fname = model_fam.canonical_name
                fname.replace(' ', '-')
                pb_msg = model_fam.model_to_pb().SerializeToString()
                file_io = io.BytesIO(bytes(pb_msg))
                model_fam.pb.save(fname + ".pb", File(file_io))
                model_fam.save()

            mf_file = open(model_fam.pb.path, 'rb')
            file_pb = mf_file.read()
            mf_file.close()

            # Check if fields are filled.
            if model_fam.canonical_name == '':
                model_fam.pb_to_model(file_pb)
                model_fam.save()

            mf_pb = Model_pb2.ModelFamily()
            mf_pb.ParseFromString(file_pb)
            response.Models.append(mf_pb)

        return format_and_send_response(request, response)

    # On exception, returns invalid request error message pb.
    except:
        msg = "Invalid request."
        details = "The request is not acceptable."
        return bad_request_helper(request, msg, details, 406)
示例#2
0
    def model_to_pb(self, filename=None):  # Not implemented
        '''
        Converts the db model into a protobuf message.

        Parameters: None

        Returns:
            m_f - protobuf message for a model family.
        '''
        m_f = Model_pb2.ModelFamily()

        # Protobuf top-level fields
        m_f.CanonicalName = self.canonical_name
        m_f.LanguageCode = self.language_code

        # Family Description fields
        m_f.FamilyDescription.Name = self.name
        m_f.FamilyDescription.Description = self.description
        m_f.FamilyDescription.ClinicalDomain = self.clinical_domain
        m_f.FamilyDescription.AnatomicalRegion = self.anatomical_region
        m_f.FamilyDescription.PrimaryStructure = self.primary_structure
        m_f.FamilyDescription.Modalities = self.modalities

        # Family Description Input Channels
        for chan_desc in self.modelchanneldescription_set.all():

            model_channel = Model_pb2.ModelChannelDescription()
            model_channel.ChannelID = chan_desc.channel_id

            for mode in ast.literal_eval(chan_desc.accepted_modalities_pb):
                model_channel.Constraints.AcceptedModalities.append(mode)

            model_channel.Constraints.OriginalDataRequired = chan_desc.req_original
            model_channel.Constraints.IsAxial = chan_desc.is_axial

            model_channel.Constraints.SpacingMinInMillimeters.X = chan_desc.spacing_min_x
            model_channel.Constraints.SpacingMinInMillimeters.Y = chan_desc.spacing_min_y
            model_channel.Constraints.SpacingMinInMillimeters.Z = chan_desc.spacing_min_z

            model_channel.Constraints.SpacingMaxInMillimeters.X = chan_desc.spacing_max_x
            model_channel.Constraints.SpacingMaxInMillimeters.Y = chan_desc.spacing_max_y
            model_channel.Constraints.SpacingMaxInMillimeters.Z = chan_desc.spacing_max_z

            model_channel.Constraints.DimensionsMinInPixels.X = chan_desc.dimensions_min_x
            model_channel.Constraints.DimensionsMinInPixels.Y = chan_desc.dimensions_min_y
            model_channel.Constraints.DimensionsMinInPixels.Z = chan_desc.dimensions_min_z

            model_channel.Constraints.DimensionsMaxInPixels.X = chan_desc.dimensions_max_x
            model_channel.Constraints.DimensionsMaxInPixels.Y = chan_desc.dimensions_max_y
            model_channel.Constraints.DimensionsMaxInPixels.Z = chan_desc.dimensions_max_z

            m_f.FamilyDescription.InputChannels.append(model_channel)

        # Family Description Constraint fields
        m_f.FamilyDescription.Constraints.Gender = self.gender
        for bpe in self.bodypartexamined_set.all():
            m_f.FamilyDescription.Constraints.BodyPartsExamined.append(
                bpe.part_type)

        # Model Version fields
        for m_v in self.modelversion_set.all():
            model_version = Model_pb2.ModelVersion()
            model_version.ID = m_v.model_version_id
            model_version.VersionDescription = m_v.model_version_desc

            timestamp = Timestamp()
            timestamp.FromDatetime(m_v.created_time)
            model_version.CreatedOn.seconds = timestamp.seconds
            model_version.CreatedOn.nanos = timestamp.nanos

            model_version.NumberOfCreditsRequired = m_v.credits_req
            model_version.MajorVersion = m_v.major_version
            model_version.MinorVersion = m_v.minor_version
            model_version.LanguageCode = m_v.language_code

            for m_struct in m_v.structure_set.all():
                model_structure = Model_pb2.Structure()
                model_structure.Name = m_struct.name
                model_structure.Color.R = m_struct.color_r
                model_structure.Color.G = m_struct.color_g
                model_structure.Color.B = m_struct.color_b
                model_structure.Type = m_struct.structure_type
                model_structure.FMACode = m_struct.FMA_code
                model_structure.InputChannelID = m_struct.input_channel_id
                model_structure.StructureID = m_struct.structure_id

                model_version.Structures.append(model_structure)

            m_f.ModelVersions.append(model_version)

        return m_f
示例#3
0
    def pb_to_model(self, pb_str):
        '''
        Converts a protobuf message into attributes for the model.
        DoubleValues are just stored as their values within the database.

        Parameters:
        pb_str - (bytestring) - protobuf message to interpret into django db model

        Returns: none
        '''
        model_family = Model_pb2.ModelFamily()
        model_family.ParseFromString(pb_str)

        # Protobuf top-level fields
        self.canonical_name = model_family.CanonicalName
        self.language_code = model_family.LanguageCode

        # Model family description
        self.name = model_family.FamilyDescription.Name
        self.description = model_family.FamilyDescription.Description
        self.clinical_domain = model_family.FamilyDescription.ClinicalDomain
        self.anatomical_region = model_family.FamilyDescription.AnatomicalRegion
        self.primary_structure = model_family.FamilyDescription.PrimaryStructure
        self.modalities = model_family.FamilyDescription.Modalities
        self.gender = model_family.FamilyDescription.Constraints.Gender

        # Model family constraints body parts examined
        for body_part in model_family.FamilyDescription.Constraints.BodyPartsExamined:
            self.bodypartexamined_set.create(part_type=body_part)

        # Model channel descriptions
        for model_channel in model_family.FamilyDescription.InputChannels:
            self.modelchanneldescription_set.create(
                channel_id=model_channel.ChannelID,
                spacing_min_x=model_channel.Constraints.
                SpacingMinInMillimeters.X,
                spacing_min_y=model_channel.Constraints.
                SpacingMinInMillimeters.Y,
                spacing_min_z=model_channel.Constraints.
                SpacingMinInMillimeters.Z,
                spacing_max_x=model_channel.Constraints.
                SpacingMaxInMillimeters.X,
                spacing_max_y=model_channel.Constraints.
                SpacingMaxInMillimeters.Y,
                spacing_max_z=model_channel.Constraints.
                SpacingMaxInMillimeters.Z,
                dimensions_min_x=model_channel.Constraints.
                DimensionsMinInPixels.X,
                dimensions_min_y=model_channel.Constraints.
                DimensionsMinInPixels.Y,
                dimensions_min_z=model_channel.Constraints.
                DimensionsMinInPixels.Z,
                dimensions_max_x=model_channel.Constraints.
                DimensionsMaxInPixels.X,
                dimensions_max_y=model_channel.Constraints.
                DimensionsMaxInPixels.Y,
                dimensions_max_z=model_channel.Constraints.
                DimensionsMaxInPixels.Z,
                req_original=model_channel.Constraints.OriginalDataRequired,
                is_axial=model_channel.Constraints.IsAxial,
                accepted_modalities_pb=model_channel.Constraints.
                AcceptedModalities)

        # Model Versions
        for model_version in model_family.ModelVersions:
            # Create model version in db
            db_mv = self.modelversion_set.create(
                model_version_id=model_version.ID,
                model_version_desc=model_version.VersionDescription,
                created_time=model_version.CreatedOn.ToDatetime().replace(
                    tzinfo=pytz.utc),
                credits_req=model_version.NumberOfCreditsRequired,
                major_version=model_version.MajorVersion,
                minor_version=model_version.MinorVersion,
                language_code=model_version.LanguageCode)
            # Iterative add structures
            for struc in model_version.Structures:
                db_mv.structure_set.create(
                    name=struc.Name,
                    color_r=struc.Color.R,
                    color_g=struc.Color.G,
                    color_b=struc.Color.B,
                    structure_type=struc.Type,
                    FMA_code=struc.FMACode,
                    input_channel_id=struc.InputChannelID,
                    structure_id=struc.StructureID)