<commit_before>"""
Experiment resources.

FOG Mar 21, 2011
"""

from datetime import datetime
from everest.querying.specifications import AscendingOrderSpecification
from everest.querying.specifications import DescendingOrderSpecification
from everest.representers.dataelements import DataElementAttributeProxy
from everest.resources.base import Collection
from everest.resources.base import Member
from everest.resources.descriptors import attribute_alias
from everest.resources.descriptors import collection_attribute
from everest.resources.descriptors import member_attribute
from everest.resources.descriptors import terminal_attribute
from everest.resources.staging import create_staging_collection
from pyramid.httpexceptions import HTTPBadRequest
from thelma.automation.tools.metadata.ticket import IsoRequestTicketDescriptionRemover
from thelma.automation.tools.metadata.ticket \
    import IsoRequestTicketDescriptionUpdater
from thelma.automation.tools.metadata.ticket import IsoRequestTicketActivator
from thelma.automation.tools.metadata.ticket import IsoRequestTicketCreator
from thelma.automation.tools.stock.base import STOCKMANAGEMENT_USER
from thelma.interfaces import IExperiment
from thelma.interfaces import IExperimentDesign
from thelma.interfaces import IExperimentDesignRack
from thelma.interfaces import IExperimentJob
from thelma.interfaces import IExperimentMetadata
from thelma.interfaces import IExperimentMetadataType
from thelma.interfaces import IExperimentRack
from thelma.interfaces import IIsoRequest
from thelma.interfaces import IMoleculeDesignPoolSet
from thelma.interfaces import IPlate
from thelma.interfaces import IRack
from thelma.interfaces import IRackLayout
from thelma.interfaces import IRackShape
from thelma.interfaces import IRackSpecs
from thelma.interfaces import ISubproject
from thelma.interfaces import ITag
from thelma.models.racklayout import RackLayout
from thelma.models.utils import get_current_user
from thelma.resources.base import RELATION_BASE_URL
import logging
# from thelma.models.experiment import ExperimentDesignRack

__docformat__ = 'reStructuredText en'

__author__ = '<NAME>'
__date__ = '$Date: 2013-05-13 14:13:15 +0200 (Mon, 13 May 2013) $'
__revision__ = '$Rev: 13338 $'
__source__ = '$URL::                                                          $'

__all__ = ['ExperimentMetadataTypeMember',
           'ExperimentCollection',
           'ExperimentDesignCollection',
           'ExperimentDesignMember',
           'ExperimentDesignRackCollection',
           'ExperimentDesignRackMember',
           'ExperimentMember',
           'ExperimentMetadataCollection',
           'ExperimentMetadataMember',
           'ExperimentRackCollection',
           'ExperimentRackMember',
           ]


class ExperimentMetadataTypeMember(Member):
    relation = '%s/experiment-metadata-type' % RELATION_BASE_URL
    title = attribute_alias('display_name')
    display_name = terminal_attribute(str, 'display_name')


class ExperimentDesignRackMember(Member):
    relation = "%s/experiment-design-rack" % RELATION_BASE_URL
    title = attribute_alias('label')
    label = terminal_attribute(str, 'label')
    rack_layout = member_attribute(IRackLayout, 'layout')
    tags = collection_attribute(ITag, 'tags')


class ExperimentDesignRackCollection(Collection):
    title = 'Experiment Design Racks'
    root_name = 'experiment-design-racks'
    description = 'Manage experiment design racks'
#    default_order = asc('label')


class ExperimentDesignMember(Member):
    relation = "%s/experiment-design" % RELATION_BASE_URL
    title = terminal_attribute(str, 'slug')
    rack_shape = member_attribute(IRackShape, 'rack_shape')
    experiment_design_racks = collection_attribute(IExperimentDesignRack,
                                                   'design_racks')
    experiments = collection_attribute(IExperiment, 'experiments')
    experiment_metadata = member_attribute(IExperimentMetadata,
                                           'experiment_metadata')


    def __getitem__(self, name):
        if name == 'experiment-design-racks':
            return self.experiment_design_racks
        elif name == 'experiment_metadata':
            return self.experiment_metadata
        else:
            raise KeyError(name)

    def update_from_entity(self, new_entity):
        entity = self.get_entity()
        while entity.design_racks:
            entity.design_racks.pop()
        while new_entity.design_racks:
            new_rack = new_entity.design_racks.pop()
            entity.design_racks.append(new_rack)
#            ws = rack.worklist_series
#            rack.worklist_series = None
#            # remove the back reference to avoid conflicts
#            new_rack = ExperimentDesignRack(rack.label,
#                                            rack.layout,
#                                            worklist_series=ws)
#            entity.design_racks.append(new_rack)
        entity.rack_shape = new_entity.rack_shape
        entity.worklist_series = new_entity.worklist_series


class ExperimentDesignCollection(Collection):
    title = 'Experiment Designs'
    root_name = 'experiment-designs'
    description = 'Manage experiment designs'
    default_order = AscendingOrderSpecification('label')


class ExperimentMember(Member):
    relation = '%s/experiment' % RELATION_BASE_URL
    label = terminal_attribute(str, 'label')
    title = attribute_alias('label')
    destination_rack_specs = member_attribute(IRackSpecs,
                                              'destination_rack_specs')
    source_rack = member_attribute(IRack, 'source_rack')
    experiment_design = member_attribute(IExperimentDesign,
                                         'experiment_design')
    experiment_racks = collection_attribute(IExperimentRack,
                                            'experiment_racks')
    experiment_job = member_attribute(IExperimentJob, 'job')
    experiment_metadata_type = \
        member_attribute(IExperimentMetadataType,
            'experiment_design.experiment_metadata.experiment_metadata_type')

    def __getitem__(self, name):
        if name == 'experiment-racks':
            return self.experiment_racks
        else:
            raise KeyError(name)


class ExperimentCollection(Collection):
    title = 'Experiments'
    root_name = 'experiments'
    description = 'Manage experiments'
    default_order = AscendingOrderSpecification('label')


class ExperimentMetadataMember(Member):
    relation = '%s/experiment-metadata' % RELATION_BASE_URL
    label = terminal_attribute(str, 'label')
    title = attribute_alias('label')
    ticket_number = terminal_attribute(int, 'ticket_number')
    subproject = member_attribute(ISubproject, 'subproject')
    number_replicates = terminal_attribute(int, 'number_replicates')
    molecule_design_pool_set = member_attribute(IMoleculeDesignPoolSet,
                                                'molecule_design_pool_set',
                                                is_nested=True)
    experiment_design = member_attribute(IExperimentDesign,
                                         'experiment_design',
                                         is_nested=True)
    experiment_design_racks = \
            collection_attribute(IExperimentDesignRack,
                                 'experiment_design.design_racks')
    iso_request = member_attribute(IIsoRequest, 'iso_request')
    creation_date = terminal_attribute(datetime, 'creation_date')
    experiment_metadata_type = member_attribute(IExperimentMetadataType,
                                                'experiment_metadata_type')

    def __getitem__(self, name):
        if name == 'tags':
            tags_dict = {}
            for rack in self.experiment_design.experiment_design_racks:
                for tp in rack.rack_layout.tagged_rack_position_sets:
                    for tag in tp.tags:
                        tags_dict[tag.get_entity().slug] = tag
            tag_coll = create_staging_collection(ITag)
            for tag in tags_dict.values():
                tag_coll.add(tag)
            result = tag_coll
        else:
            result = Member.__getitem__(self, name)<commit_msg>Fixed problem loading experiment-less metadata<commit_after>"""
Experiment resources.

FOG Mar 21, 2011
"""

from datetime import datetime
from everest.querying.specifications import AscendingOrderSpecification
from everest.querying.specifications import DescendingOrderSpecification
from everest.representers.dataelements import DataElementAttributeProxy
from everest.resources.base import Collection
from everest.resources.base import Member
from everest.resources.descriptors import attribute_alias
from everest.resources.descriptors import collection_attribute
from everest.resources.descriptors import member_attribute
from everest.resources.descriptors import terminal_attribute
from everest.resources.staging import create_staging_collection
from pyramid.httpexceptions import HTTPBadRequest
from thelma.automation.tools.metadata.ticket import IsoRequestTicketDescriptionRemover
from thelma.automation.tools.metadata.ticket \
    import IsoRequestTicketDescriptionUpdater
from thelma.automation.tools.metadata.ticket import IsoRequestTicketActivator
from thelma.automation.tools.metadata.ticket import IsoRequestTicketCreator
from thelma.automation.tools.stock.base import STOCKMANAGEMENT_USER
from thelma.interfaces import IExperiment
from thelma.interfaces import IExperimentDesign
from thelma.interfaces import IExperimentDesignRack
from thelma.interfaces import IExperimentJob
from thelma.interfaces import IExperimentMetadata
from thelma.interfaces import IExperimentMetadataType
from thelma.interfaces import IExperimentRack
from thelma.interfaces import IIsoRequest
from thelma.interfaces import IMoleculeDesignPoolSet
from thelma.interfaces import IPlate
from thelma.interfaces import IRack
from thelma.interfaces import IRackLayout
from thelma.interfaces import IRackShape
from thelma.interfaces import IRackSpecs
from thelma.interfaces import ISubproject
from thelma.interfaces import ITag
from thelma.models.racklayout import RackLayout
from thelma.models.utils import get_current_user
from thelma.resources.base import RELATION_BASE_URL
import logging
# from thelma.models.experiment import ExperimentDesignRack

__docformat__ = 'reStructuredText en'

__author__ = '<NAME>'
__date__ = '$Date: 2013-05-13 14:13:15 +0200 (Mon, 13 May 2013) $'
__revision__ = '$Rev: 13338 $'
__source__ = '$URL::                                                          $'

__all__ = ['ExperimentMetadataTypeMember',
           'ExperimentCollection',
           'ExperimentDesignCollection',
           'ExperimentDesignMember',
           'ExperimentDesignRackCollection',
           'ExperimentDesignRackMember',
           'ExperimentMember',
           'ExperimentMetadataCollection',
           'ExperimentMetadataMember',
           'ExperimentRackCollection',
           'ExperimentRackMember',
           ]


class ExperimentMetadataTypeMember(Member):
    relation = '%s/experiment-metadata-type' % RELATION_BASE_URL
    title = attribute_alias('display_name')
    display_name = terminal_attribute(str, 'display_name')


class ExperimentDesignRackMember(Member):
    relation = "%s/experiment-design-rack" % RELATION_BASE_URL
    title = attribute_alias('label')
    label = terminal_attribute(str, 'label')
    rack_layout = member_attribute(IRackLayout, 'layout')
    tags = collection_attribute(ITag, 'tags')


class ExperimentDesignRackCollection(Collection):
    title = 'Experiment Design Racks'
    root_name = 'experiment-design-racks'
    description = 'Manage experiment design racks'
#    default_order = asc('label')


class ExperimentDesignMember(Member):
    relation = "%s/experiment-design" % RELATION_BASE_URL
    title = terminal_attribute(str, 'slug')
    rack_shape = member_attribute(IRackShape, 'rack_shape')
    experiment_design_racks = collection_attribute(IExperimentDesignRack,
                                                   'design_racks')
    experiments = collection_attribute(IExperiment, 'experiments')
    experiment_metadata = member_attribute(IExperimentMetadata,
                                           'experiment_metadata')


    def __getitem__(self, name):
        if name == 'experiment-design-racks':
            return self.experiment_design_racks
        elif name == 'experiment_metadata':
            return self.experiment_metadata
        else:
            raise KeyError(name)

    def update_from_entity(self, new_entity):
        entity = self.get_entity()
        while entity.design_racks:
            entity.design_racks.pop()
        while new_entity.design_racks:
            new_rack = new_entity.design_racks.pop()
            entity.design_racks.append(new_rack)
#            ws = rack.worklist_series
#            rack.worklist_series = None
#            # remove the back reference to avoid conflicts
#            new_rack = ExperimentDesignRack(rack.label,
#                                            rack.layout,
#                                            worklist_series=ws)
#            entity.design_racks.append(new_rack)
        entity.rack_shape = new_entity.rack_shape
        entity.worklist_series = new_entity.worklist_series


class ExperimentDesignCollection(Collection):
    title = 'Experiment Designs'
    root_name = 'experiment-designs'
    description = 'Manage experiment designs'
    default_order = AscendingOrderSpecification('label')


class ExperimentMember(Member):
    relation = '%s/experiment' % RELATION_BASE_URL
    label = terminal_attribute(str, 'label')
    title = attribute_alias('label')
    destination_rack_specs = member_attribute(IRackSpecs,
                                              'destination_rack_specs')
    source_rack = member_attribute(IRack, 'source_rack')
    experiment_design = member_attribute(IExperimentDesign,
                                         'experiment_design')
    experiment_racks = collection_attribute(IExperimentRack,
                                            'experiment_racks')
    experiment_job = member_attribute(IExperimentJob, 'job')
    experiment_metadata_type = \
        member_attribute(IExperimentMetadataType,
            'experiment_design.experiment_metadata.experiment_metadata_type')

    def __getitem__(self, name):
        if name == 'experiment-racks':
            return self.experiment_racks
        else:
            raise KeyError(name)


class ExperimentCollection(Collection):
    title = 'Experiments'
    root_name = 'experiments'
    description = 'Manage experiments'
    default_order = AscendingOrderSpecification('label')


class ExperimentMetadataMember(Member):
    relation = '%s/experiment-metadata' % RELATION_BASE_URL
    label = terminal_attribute(str, 'label')
    title = attribute_alias('label')
    ticket_number = terminal_attribute(int, 'ticket_number')
    subproject = member_attribute(ISubproject, 'subproject')
    number_replicates = terminal_attribute(int, 'number_replicates')
    molecule_design_pool_set = member_attribute(IMoleculeDesignPoolSet,
                                                'molecule_design_pool_set',
                                                is_nested=True)
    experiment_design = member_attribute(IExperimentDesign,
                                         'experiment_design',
                                         is_nested=True)
    experiment_design_racks = \
            collection_attribute(IExperimentDesignRack,
                                 'experiment_design.design_racks')
    iso_request = member_attribute(IIsoRequest, 'iso_request')
    creation_date = terminal_attribute(datetime, 'creation_date')
    experiment_metadata_type = member_attribute(IExperimentMetadataType,
                                                'experiment_metadata_type')

    def __getitem__(self, name):
        if name == 'tags':
            tags_dict = {}
            if not self.experiment_design is None:
                for rack in self.experiment_design.experiment_design_racks:
                    for tp in rack.rack_layout.tagged_rack_position_sets:
                        for tag in tp.tags:
                            tags_dict[tag.get_entity().slug] = tag
            tag_coll = create_staging_collection(ITag)
            for tag in tags_dict.values():
                tag_coll.add(tag)
            result = tag_coll
        else:
            result = Member.__getitem__(self, name)
{'fraction_non_alphanumeric': 0.06938750904268146, 'fraction_numerical': 0.004219918013021461, 'mean_word_length': 2.9935002407318247, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 54, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2465967', 'n_tokens_mistral': 4429, 'n_tokens_neox': 4279, 'n_words': 876}