コード例 #1
0
    def test_parse_samples_valid(self):
        """
        Ensure a a parsed valid directory matches the expected samples
        :return:
        """
        sheet_file = path.join(path_to_module, "fake_ngs_data",
                               "SampleSheet.csv")

        sample1 = model.Sample(
            "01-1111", "Super bug", 1, {
                "Sample_Well": "01",
                "index": "AAAAAAAA",
                "Sample_Plate": "1",
                "I7_Index_ID": "N01",
                "sample_project": "6",
                "sequencer_sample_name": "01-1111",
                "I5_Index_ID": "S01",
                "index2": "TTTTTTTT",
            })

        sample2 = model.Sample(
            "02-2222", "Scary bug", 2, {
                "Sample_Well": "02",
                "index": "GGGGGGGG",
                "Sample_Plate": "2",
                "I7_Index_ID": "N02",
                "sample_project": "6",
                "sequencer_sample_name": "02-2222",
                "I5_Index_ID": "S02",
                "index2": "CCCCCCCC",
            })

        sample3 = model.Sample(
            "03-3333", "Deadly bug", 3, {
                "Sample_Well": "03",
                "index": "CCCCCCCC",
                "Sample_Plate": "3",
                "I7_Index_ID": "N03",
                "sample_project": "6",
                "sequencer_sample_name": "03-3333",
                "I5_Index_ID": "S03",
                "index2": "GGGGGGGG",
            })

        correct_samples = [sample1, sample2, sample3]

        res = sample_parser._parse_samples(sheet_file)
        for r_sample, c_sample in zip(res, correct_samples):
            self.assertEqual(r_sample.get_uploadable_dict(),
                             c_sample.get_uploadable_dict())
コード例 #2
0
ファイル: crawler.py プロジェクト: gt-elc-lab/gthealth
 def sample_from_praw_submission(submission):
     return model.Sample(r_id=submission.id,
                         title=submission.title,
                         content=submission.selftext,
                         date=datetime.utcfromtimestamp(
                             submission.created_utc),
                         subreddit=submission.subreddit.display_name)
コード例 #3
0
    def test_send_and_get_sample(self):
        """
        Tests sending and receiving sample data
        :return:
        """
        # set up a project to upload samples to
        project_name = "test_project_2"
        project_description = "test_project_description"
        project = model.Project(name=project_name,
                                description=project_description)

        proj_json_res = self.test_api.send_project(project)
        project_identifier = proj_json_res['resource']['identifier']

        # upload a sample
        sample_name = "test_sample"
        sample_desc = "test_sample_desc"
        sample = model.Sample(sample_name, sample_desc)

        sample_json_res = self.test_api.send_sample(sample, project_identifier)

        # make sure the returned values match what we tried to upload
        self.assertEqual(sample_json_res['resource']['sampleName'],
                         sample_name)
        self.assertEqual(sample_json_res['resource']['description'],
                         sample_desc)

        # get a list of samples on our project and make sure they match what we uploaded
        sample_list = self.test_api.get_samples(project_identifier)

        self.assertEqual(len(sample_list), 1)
        self.assertEqual(type(sample_list[0]), model.Sample)
        self.assertEqual(sample_list[0].sample_name, sample_name)
        self.assertEqual(sample_list[0].description, sample_desc)
コード例 #4
0
def small_sport_kb():
    experiment = model.Experiment()

    # Subjects
    # -----------------------------------------------------------------------------------
    so3 = model.Subject('subject3')
    experiment.put_subject(so3)

    # Setups
    # ===================================================================================
    setup = model.Setup(experiment)
    modality = model.Modality(setup=setup, frequency=4000, identifier='arm')
    model.Sample(modality, 'bizeps')
    model.Sample(modality, 'triceps')
    model.Sample(modality, 'extensor_digitorum')
    model.Sample(modality, 'flexor_digitorum')

    # Sessions
    # ===================================================================================

    # Session 3
    # -----------------------------------------------------------------------------------
    session = model.Session(experiment, setup, so3)
    rec = model.Recording(session,
                          location='data/Proband_01.pkl',
                          modality=modality.Identifier)
    t = model.Trial(rec, 0, 4, 'calm')
    t.add_marker((4, 'start_trials'))
    t.add_marker((4.1, 'another'))
    model.Trial(rec, 4, 4, 'reverse_kettle1', label='reverse_kettle')
    model.Trial(rec, 8, 4, 'reverse_kettle2', label='reverse_kettle')
    model.Trial(rec, 12, 4, 'reverse_kettle3', label='reverse_kettle')
    model.Trial(rec, 16, 4, 'hantel1', label='hantel')
    model.Trial(rec, 20, 4, 'hantel2', label='hantel')
    model.Trial(rec, 24, 4, 'hantel3', label='hantel')
    model.Trial(rec, 28, 4, 'stomach_kettle1', label='stomach_kettle')
    model.Trial(rec, 32, 4, 'stomach_kettle2', label='stomach_kettle')
    model.Trial(rec, 36, 4, 'stomach_kettle3', label='stomach_kettle')
    model.Trial(rec, 44, 4, 'max_triceps').add_marker((0, 'start_max_tests'))
    model.Trial(rec, 48, 4, 'max_flexor')
    model.Trial(rec, 52, 4, 'max_bizeps')
    model.Trial(rec, 60, 4, 'max_extensor')

    return experiment
コード例 #5
0
def build_sequencing_run_from_samples(sample_sheet_file):
    """
    Create a SequencingRun object with full project/sample/sequence_file structure

    :param sample_sheet_file:
    :return: SequencingRun
    """
    sample_list = _parse_sample_list(sample_sheet_file)

    logging.debug("Building SequencingRun from parsed data")

    # create list of projects and add samples to appropriate project
    project_list = []
    for sample_number, sample in enumerate(sample_list):
        # get data from data dict
        sample_name = sample['Sample_Name']
        project_id = sample['Project_ID']
        file_f = sample['File_Forward']
        file_r = sample['File_Reverse']

        project = None
        # see if project exists
        for p in project_list:
            if project_id == p.id:
                project = p
        # create project if it doesn't exitt yet
        if project is None:
            project = model.Project(id=project_id)
            project_list.append(project)

        # create sequence file
        if len(file_r) > 0:
            # paired end read
            sq = model.SequenceFile(properties_dict=None, file_list=[file_f, file_r])
        else:
            # single end read
            sq = model.SequenceFile(properties_dict=None, file_list=[file_f])

        # create sample
        sample_obj = model.Sample(sample_name=sample_name, sample_number=sample_number+1)

        # add sequence file to sample
        sample_obj.sequence_file = deepcopy(sq)

        # add sample to project
        project.add_sample(sample_obj)

    # add the layout type to the sequencing run so we know if it is paired or single end
    if project_list[0].sample_list[0].sequence_file.is_paired_end():
        metadata = {'layoutType': 'PAIRED_END'}
    else:
        metadata = {'layoutType': 'SINGLE_END'}

    sequence_run = model.SequencingRun(metadata=metadata, project_list=project_list)
    logging.debug("SequencingRun built")
    return sequence_run
コード例 #6
0
    def test_parse_samples_valid(self):
        """
        Verify samples created from parser match expected samples
        :return:
        """
        sheet_file = path.join(path_to_module, "fake_dir_data",
                               "SampleList.csv")

        sample1 = model.Sample(
            "my-sample-1",
            "",
        )

        sample2 = model.Sample(
            "my-sample-2",
            "",
        )

        sample3 = model.Sample(
            "my-sample-3",
            "",
        )

        res = sample_parser.build_sequencing_run_from_samples(sheet_file)

        self.assertEqual(res.metadata, {'layoutType': 'PAIRED_END'})
        self.assertEqual(res.project_list[0].id, "75")
        self.assertEqual(res.project_list[1].id, "76")
        self.assertEqual(
            res.project_list[0].sample_list[0].get_uploadable_dict(),
            sample1.get_uploadable_dict())
        self.assertEqual(
            res.project_list[0].sample_list[1].get_uploadable_dict(),
            sample2.get_uploadable_dict())
        self.assertEqual(
            res.project_list[1].sample_list[0].get_uploadable_dict(),
            sample3.get_uploadable_dict())
コード例 #7
0
    def test_parse_out_sequence_file(self):
        """
        Tests that parse out sequence file correctly filters sample related data from the extra params dict
        And ensures that the uploadable dict correctly includes all the needed data after removal
        :return:
        """
        sample = model.Sample(
            "03-3333", "Deadly bug", None, {
                "Sample_Well": "03",
                "index": "CCCCCCCC",
                "Sample_Plate": "3",
                "I7_Index_ID": "N03",
                "sampleName": "03-3333",
                "sampleProject": "6",
                "sequencerSampleId": "03-3333",
                "I5_Index_ID": "S03",
                "index2": "GGGGGGGG",
                "description": "Deadly bug"
            })

        uploadable_dict = {
            'Sample_Well': '03',
            'index': 'CCCCCCCC',
            'Sample_Plate': '3',
            'I7_Index_ID': 'N03',
            'sampleName': '03-3333',
            'sampleProject': '6',
            'sequencerSampleId': '03-3333',
            'I5_Index_ID': 'S03',
            'index2': 'GGGGGGGG',
            'description': 'Deadly bug'
        }

        sequence_file_dict = {
            'Sample_Well': '03',
            'index': 'CCCCCCCC',
            'Sample_Plate': '3',
            'I7_Index_ID': 'N03',
            'sampleProject': '6',
            'sequencerSampleId': '03-3333',
            'I5_Index_ID': 'S03',
            'index2': 'GGGGGGGG'
        }

        res = sample_parser._parse_out_sequence_file(sample)

        self.assertEqual(sample.get_uploadable_dict(), uploadable_dict)
        self.assertEqual(res, sequence_file_dict)
コード例 #8
0
ファイル: api_calls.py プロジェクト: happykhan/qi-irida-utils
    def get_samples(self, project_id):
        """
        API call to api/projects/project_id/samples

        arguments:
            project_id -- project identifier from irida

        returns list of samples for the given project.
            each sample is a Sample object.
        """

        logging.info("Getting samples from project '{}'".format(project_id))

        if project_id not in self.cached_samples:
            try:
                project_url = self._get_link(self.base_url, "projects")
                url = self._get_link(project_url,
                                     "project/samples",
                                     target_dict={
                                         "key": "identifier",
                                         "value": project_id
                                     })

            except StopIteration:
                logging.error(
                    "The given project ID doesn't exist: ".format(project_id))
                raise exceptions.IridaResourceError(
                    "The given project ID doesn't exist", project_id)

            response = self._session.get(url)
            result = response.json()["resource"]["resources"]

            sample_list = []
            for sample_dict in result:
                # use name and description from dictionary as base parameters when creating sample
                sample_name = sample_dict['sampleName']
                sample_desc = sample_dict['description']
                # remove them from the dict so we don't have useless duplicate data
                del sample_dict['sampleName']
                del sample_dict['description']
                sample_list.append(
                    model.Sample(sample_name=sample_name,
                                 description=sample_desc,
                                 samp_dict=sample_dict))
            self.cached_samples[project_id] = sample_list

        return self.cached_samples[project_id]
コード例 #9
0
    def test_valid(self):
        """
        Ensure a a parsed valid directory matches the expected sample list
        :return:
        """
        sheet_file = path.join(path_to_module, "fake_ngs_data",
                               "SampleSheet.csv")

        sample = model.Sample(
            "01-1111", "Super bug", 1, {
                "Sample_Well": "01",
                "index": "AAAAAAAA",
                "Sample_Plate": "1",
                "I7_Index_ID": "N01",
                "sample_project": "6",
                "sequencer_sample_name": "01-1111",
                "I5_Index_ID": "S01",
                "index2": "TTTTTTTT",
            })

        sequence_file_properties = {
            'Sample_Plate': '1',
            'Sample_Well': '01',
            'I7_Index_ID': 'N01',
            'index': 'AAAAAAAA',
            'I5_Index_ID': 'S01',
            'index2': 'TTTTTTTT'
        }

        file_path_1 = path.join(path_to_module, "fake_ngs_data", "Data",
                                "Intensities", "BaseCalls",
                                "01-1111_S1_L001_R1_001.fastq.gz")
        file_path_2 = path.join(path_to_module, "fake_ngs_data", "Data",
                                "Intensities", "BaseCalls",
                                "01-1111_S1_L001_R2_001.fastq.gz")
        file_list = [file_path_1, file_path_2]

        res = sample_parser._parse_sample_list(sheet_file)

        # Check sample is the same
        self.assertEqual(res[0].get_uploadable_dict(),
                         sample.get_uploadable_dict())
        # Check sequencing file is correct
        self.assertEqual(res[0].sequence_file.properties_dict,
                         sequence_file_properties)
        self.assertEqual(res[0].sequence_file.file_list.sort(),
                         file_list.sort())
コード例 #10
0
def build_subset_from_selected_attributes(datamatrix: m.DataMatrix, attributes: List[str]) -> m.DataMatrix:

	new_samples: List[m.Sample] = list()

	for sample in datamatrix.samples:
		new_samples.append(m.Sample(
			sample.get_datapoints([
				datamatrix.attributes.index(attribute.strip()) for attribute in attributes
			]),
			associated_attributes=copy.deepcopy(attributes),
			classlabel=sample.classlabel
		))

	return m.DataMatrix(
		new_samples,
		attributes=copy.deepcopy(attributes),
		classlabels=copy.deepcopy(datamatrix.classlabels),
		unique_classlabels=copy.deepcopy(datamatrix.unique_classlabels),
		dataset_name=datamatrix.dataset_name
	)
コード例 #11
0
    def test_send_and_get_sequence_files(self):
        """
        Tests sending and receiving sequence files
        :return:
        """
        # upload a project
        project_name = "test_project_2"
        project_description = "test_project_description"
        project = model.Project(name=project_name,
                                description=project_description)

        proj_json_res = self.test_api.send_project(project)
        project_identifier = proj_json_res['resource']['identifier']

        # upload a sample
        sample_name = "test_sample"
        sample_desc = "test_sample_desc"
        sample = model.Sample(sample_name, sample_desc)

        self.test_api.send_sample(sample, project_identifier)

        # upload sequence files
        sequence_file_list = [
            path.join(path_to_module, "fake_dir_data", "file_1.fastq.gz"),
            path.join(path_to_module, "fake_dir_data", "file_2.fastq.gz")
        ]
        sequence_file = model.SequenceFile(sequence_file_list)

        upload_id = self.test_api.create_seq_run({'layoutType': 'PAIRED_END'})

        self.test_api.send_sequence_files(sequence_file, sample_name,
                                          project_identifier, upload_id)

        # verify sequence files match what we sent to IRIDA
        returned_sequence_files = self.test_api.get_sequence_files(
            project_identifier, sample_name)

        self.assertEqual(returned_sequence_files[0]['fileName'],
                         'file_1.fastq.gz')
        self.assertEqual(returned_sequence_files[1]['fileName'],
                         'file_2.fastq.gz')
コード例 #12
0
    def test_sample_exists(self):
        """
        Upload a sample and make sure it can be found with the sample_exists method
        :return:
        """
        # create a project to upload samples to
        project_name = "test_project_exists"
        project_description = "test_project_exists_description"
        project = model.Project(name=project_name,
                                description=project_description)

        json_res = self.test_api.send_project(project)
        project_id = json_res['resource']['identifier']

        # create and upload a sample, and verify it exists
        sample_name = "test_sample_exists"
        sample_desc = "test_sample_exists_desc"
        sample = model.Sample(sample_name, sample_desc)

        self.test_api.send_sample(sample, project_id)
        self.assertTrue(self.test_api.sample_exists(sample_name, project_id))
コード例 #13
0
def create_kb_for_testing():
    experiment = model.Experiment()
    so1 = model.Subject('Robert')
    experiment.put_subject(so1)

    setup = model.Setup(experiment)
    modality1 = model.Modality(setup, 15, identifier='emg')
    model.Sample(modality1, 'm1')
    model.Sample(modality1, 'm2')
    model.Sample(modality1, 'm3')
    model.Sample(modality1, 'm4')

    modality2 = model.Modality(setup, 5, identifier='pos')
    model.Sample(modality2, 'length')
    model.Sample(modality2, 'width')

    session = model.Session(experiment, setup, so1)
    emg_data = np.column_stack(
        (np.arange(0, 150, dtype='float'), np.arange(200, 350, dtype='float'),
         np.arange(400, 550, dtype='float'), np.arange(600, 750,
                                                       dtype='float')))
    recording = model.Recording(session=session,
                                data=emg_data,
                                identifier='emg_recording',
                                modality=modality1.Identifier)
    model.Trial(recording, 1, 2, 'trial1', label='cool')
    model.Trial(recording, 4, 1, 'trial2', label='cool')
    model.Trial(recording, 6, 3, 'trial3', label='hot')

    pos_data = np.column_stack(
        (np.arange(800, 850, dtype='float'), np.arange(900, 950,
                                                       dtype='float')))
    recording = model.Recording(session=session,
                                data=pos_data,
                                identifier='pos_recording',
                                modality=modality2.Identifier)
    model.Trial(recording, 1, 2, 'trial1', label='hot')
    model.Trial(recording, 4, 1, 'trial2', label='hot')
    model.Trial(recording, 6, 3, 'trial3', label='cool')
    return experiment
コード例 #14
0
def create_kb(data=None):
    if data is None:
        print 'Create new dataset...'
        rec1 = np.arange(100)
        rec1 = np.column_stack((rec1, rec1, rec1))

        rec2 = np.arange(100, 200)
        rec2 = np.column_stack((rec2, rec2, rec2))

        rec3 = np.arange(200, 300)
        rec3 = np.column_stack((rec3, rec3, rec3))

        data = np.row_stack((rec1, rec2, rec3))
    df = pd.DataFrame(data, dtype='float')

    experiment = model.Experiment()

    # Subjects
    #==========
    so1 = model.Subject('Robert')
    so2 = model.Subject('Therese')
    so3 = model.Subject('Michael')
    experiment.put_subject(so1)
    experiment.put_subject(so2)
    experiment.put_subject(so3)

    # Setups
    # =======
    setup = model.Setup(experiment)
    modality = model.Modality(setup, 1, 'arm')
    model.Sample(modality, 'bizeps')
    model.Sample(modality, 'trizeps')

    modality = model.Modality(setup, 'leg')
    model.Sample(modality, 'gluteus maximus')

    # Sessions
    # =========

    # Session1
    #---------
    session = model.Session(experiment, setup, so1)
    recording = model.Recording(session=session, data=df)
    model.Trial(recording, 10, 10, 'curl_simple')
    model.Trial(recording, 30, 11, 'squat')
    model.Trial(recording, 50, 12, 'curl_difficult')
    model.Trial(recording, 70, 20, 'leg_lever')

    # Session 2
    # ----------
    df = pd.DataFrame(np.copy(data), dtype='float')
    session = model.Session(experiment, setup, so2)
    recording = model.Recording(session=session, data=df)
    model.Trial(recording, 10, 10, 'curl_simple')
    model.Trial(recording, 30, 11, 'squat')
    model.Trial(recording, 50, 12, 'curl_difficult')
    model.Trial(recording, 70, 20, 'leg_lever')

    df = pd.DataFrame(np.copy(data), dtype='float')
    recording = model.Recording(session=session, data=df)
    model.Trial(recording, 10, 10, 'curl_simple2')
    model.Trial(recording, 30, 11, 'squat2')
    model.Trial(recording, 50, 12, 'curl_difficult2')
    model.Trial(recording, 70, 20, 'leg_lever2')

    # Session 3
    # ----------
    df = pd.DataFrame(np.copy(data), dtype='float')
    session = model.Session(experiment, setup, so3)
    recording = model.Recording(session=session, data=df)
    model.Trial(recording, 10, 10, 'curl_simple')
    model.Trial(recording, 30, 11, 'squat')
    model.Trial(recording, 50, 12, 'curl_difficult')
    model.Trial(recording, 70, 20, 'leg_lever')

    return experiment
コード例 #15
0
def _parse_samples(sample_sheet_file):
    """
    Parse all the lines under "[Data]" in .csv file
    Keys in sample_key_translation_dict have their values changed for
        uploading to REST API
    All other keys keep the same name that they have in .csv file

    arguments:
            sample_sheet_file -- path to SampleSheet.csv

    returns	a list containing Sample objects that have been created by a
        dictionary from the parsed out key:pair values from .csv file
    """

    logging.info("Reading data from sample sheet {}".format(sample_sheet_file))

    csv_reader = get_csv_reader(sample_sheet_file)
    # start with an ordered dictionary so that keys are ordered in the same
    # way that they are inserted.
    sample_dict = OrderedDict()
    sample_list = []

    sample_key_translation_dict = {
        'Sample_Name': 'sequencer_sample_name',
        'Description': 'description',
        'Sample_ID': 'sampleName',
        'Sample_Project': 'sample_project'
    }

    _parse_samples.sample_key_translation_dict = sample_key_translation_dict

    # initilize dictionary keys from first line (data headers/attributes)
    set_attributes = False
    for line in csv_reader:

        if set_attributes:
            for item in line:

                if item in sample_key_translation_dict:
                    key_name = sample_key_translation_dict[item]
                else:
                    key_name = item

                sample_dict[key_name] = ""

            break

        if "[Data]" in line:
            set_attributes = True

    # fill in values for keys. line is currently below the [Data] headers
    for sample_number, line in enumerate(csv_reader):

        if len(sample_dict.keys()) != len(line):
            """
            if there is one more Data header compared to the length of
            data values then add an empty string to the end of data values
            i.e the Description will be empty string
            assumes the last Data header is going to be the Description
            this handles the case where the last trailing comma is trimmed

            Shaun said this issue may come up when a user edits the
            SampleSheet from within the MiSeq software
            """
            if len(sample_dict.keys()) - len(line) == 1:
                line.append("")
            else:
                raise exceptions.SampleSheetError((
                    "Your sample sheet is malformed. Expected to find {} "
                    "columns in the [Data] section, but only found {} columns "
                    "for line {}.".format(len(sample_dict.keys()), len(line),
                                          line)), sample_sheet_file)

        for index, key in enumerate(sample_dict.keys()):
            sample_dict[key] = line[index].strip(
            )  # assumes values are never empty

        new_sample_dict = deepcopy(sample_dict)
        new_sample_name = new_sample_dict['sampleName']
        new_sample_desc = new_sample_dict['description']
        del new_sample_dict['sampleName']
        del new_sample_dict['description']

        sample = model.Sample(sample_name=new_sample_name,
                              description=new_sample_desc,
                              sample_number=sample_number + 1,
                              samp_dict=new_sample_dict)
        sample_list.append(sample)

    return sample_list
コード例 #16
0
def row_to_sample(row):
    return model.Sample(row[0], row[1], row[2], row[3], row[4], row[5])
コード例 #17
0
def create_emg_eeg_kb(meta, sessions, markers=None):
    """ Builds an `emgframework.model.model.Experiment` using information extracted from
        `P#_AllLifts.mat` and data extracted from `HS_P#_S#.mat`.
        Builds the model with EMG and EEG data only (Kinematic, Environmental and
        Miscellaneous data not included)

        Args:
            meta (pandas.core.DataFrame): Meta information to sessions
            session (Dictionary): Data of one session

        Returns:
            emgframework.model.model.Experiment
    """
    cols = [
        col for col in meta.columns
        if col.startswith('t') or col.startswith('Dur_')
    ]
    meta.loc[:, cols] = meta.loc[:, cols] - 2.002
    # Define mapping for weights and surface
    weights = {}
    weights[1] = '165g'
    weights[2] = '330g'
    weights[4] = '660g'
    surface = {}
    surface[1] = 'sandpaper'
    surface[2] = 'suede'
    surface[3] = 'silk'

    # Create Experiment
    # ==================================================================================
    exp = model.Experiment()
    subj = model.Subject(sessions[0]['initials'])

    # Creating Setup and Modalities
    # ==================================================================================
    setup = model.Setup(exp)
    kin_cols = [
        'Px1 - position x sensor 1', 'Px2 - position x sensor 2',
        'Px3 - position x sensor 3', 'Px4 - position x sensor 4',
        'Py1 - position y sensor 1', 'Py2 - position y sensor 2',
        'Py3 - position y sensor 3', 'Py4 - position y sensor 4',
        'Pz1 - position z sensor 1', 'Pz2 - position z sensor 2',
        'Pz3 - position z sensor 3', 'Pz4 - position z sensor 4'
    ]
    mod_emg = model.Modality(setup, sessions[0]['emg_sr'], 'emg')
    for s in sessions[0]['emg_data'].columns:
        model.Sample(mod_emg, s)
    mod_eeg = model.Modality(setup, sessions[0]['eeg_sr'], 'eeg')
    for s in sessions[0]['eeg_data'].columns:
        model.Sample(mod_eeg, s)
    mod_kin = model.Modality(setup, sessions[0]['kin_sr'], 'kin')
    for s in sessions[0]['kin_data'].columns:
        if s in kin_cols:
            model.Sample(mod_kin, s)

    # Creating Session, Recordings and Trials
    # ==================================================================================
    for session in sessions:
        # Select data of from metadata belonging to in `session` specified session
        meta_session = meta.loc[meta.loc[:, 'Run'] == session['session'], :]
        meta_session.reset_index(inplace=True)
        sess = model.Session(exp, setup, subj,
                             'session_' + str(session['session']))
        # Creating Recordings
        # ----------------------------------------------------------------------------------
        rec_emg = model.Recording(sess,
                                  data=session['emg_data'],
                                  identifier='emg_data',
                                  modality=mod_emg.Identifier)
        rec_eeg = model.Recording(sess,
                                  data=session['eeg_data'],
                                  identifier='eeg_data',
                                  modality=mod_eeg.Identifier)
        rec_kin = model.Recording(sess,
                                  data=session['kin_data'].loc[:, kin_cols],
                                  identifier='kin_data',
                                  modality=mod_kin.Identifier)

        # Creating Trials
        # ----------------------------------------------------------------------------------
        for i in range(meta_session.shape[0]):
            start = meta_session.loc[i, 'StartTime']
            if i == meta_session.shape[0] - 1:
                duration = float(session['emg_data'].shape[0]
                                 ) / 4000. - meta_session.loc[i, 'StartTime']
            else:
                duration = meta_session.loc[
                    i + 1, 'StartTime'] - meta_session.loc[i, 'StartTime']

            if (start + duration
                ) * session['emg_sr'] > session['emg_data'].shape[0]:
                warning = (
                    'WARNING - EMG data does not contain enough data points. Has '
                    +
                    '{samples:d} data points but {needed:d} are required. Skipped '
                    + 'Lift {lift:d}').format(
                        samples=session['emg_data'].shape[0],
                        needed=int((start + duration) * session['emg_sr']),
                        lift=i)
                warnings.warn(warning)
                continue

            if (start + duration
                ) * session['eeg_sr'] > session['eeg_data'].shape[0]:
                warning = (
                    'WARNING - EEG data does not contain enough data points. Has '
                    +
                    '{samples:d} data points but {needed:d} are required. Skipped '
                    + 'Lift {lift:d}').format(
                        samples=session['eeg_data'].shape[0],
                        needed=int((start + duration) * session['eeg_sr']),
                        lift=i)
                warnings.warn(warning)
                continue

            t_emg = model.Trial(
                recording=rec_emg,
                start=start,
                duration=duration,
                identifier='emg_lift' + str(i),
                label=weights[meta_session.loc[
                    i, 'CurW']]  # + '_' + surface[meta_session.loc[i, 'CurS']]
            )
            t_eeg = model.Trial(
                recording=rec_eeg,
                start=start,
                duration=duration,
                identifier='eeg_lift' + str(i),
                label=weights[meta_session.loc[
                    i, 'CurW']]  # + '_' + surface[meta_session.loc[i, 'CurS']]
            )
            t_kin = model.Trial(
                recording=rec_kin,
                start=start,
                duration=duration,
                identifier='kin_lift' + str(i),
                label=weights[meta_session.loc[
                    i, 'CurW']]  # + '_' + surface[meta_session.loc[i, 'CurS']]
            )

            if markers is not None:
                for marker in markers:
                    t_emg.add_marker((meta_session.loc[i, marker], marker))
                    t_eeg.add_marker((meta_session.loc[i, marker], marker))
                    t_kin.add_marker((meta_session.loc[i, marker], marker))
    return exp
コード例 #18
0
def sport_kb():
    experiment = model.Experiment()

    # Subjects
    # -----------------------------------------------------------------------------------
    so3 = model.Subject('subject1')
    experiment.put_subject(so3)
    so3 = model.Subject('subject2')
    experiment.put_subject(so3)
    so3 = model.Subject('subject3')
    experiment.put_subject(so3)
    so3 = model.Subject('subject4')
    experiment.put_subject(so3)
    so3 = model.Subject('subject5')
    experiment.put_subject(so3)
    so3 = model.Subject('subject6')
    experiment.put_subject(so3)
    so3 = model.Subject('subject7')
    experiment.put_subject(so3)
    so3 = model.Subject('subject8')
    experiment.put_subject(so3)
    so3 = model.Subject('subject9')
    experiment.put_subject(so3)
    so3 = model.Subject('subject10')
    experiment.put_subject(so3)

    # Setups
    # ===================================================================================
    setup = model.Setup(experiment)
    modality = model.Modality(setup=setup, frequency=4000, identifier='arm')
    model.Sample(modality, 'bizeps')
    model.Sample(modality, 'triceps')
    model.Sample(modality, 'extensor_digitorum')
    model.Sample(modality, 'flexor_digitorum')

    # Sessions
    # ===================================================================================

    # Session 1
    # -----------------------------------------------------------------------------------
    session = model.Session(experiment, setup, so3)
    rec = model.Recording(session,
                          location='data/Proband_01.pkl',
                          modality=modality.Identifier)
    model.Trial(rec, 0, 4, 'calm')
    model.Trial(rec, 4, 4, 'hantel1', label='hantel')
    model.Trial(rec, 8, 4, 'hantel2', label='hantel')
    model.Trial(rec, 12, 4, 'hantel3', label='hantel')
    model.Trial(rec, 16, 4, 'stomach_kettle1', label='stomach_kettle')
    model.Trial(rec, 20, 4, 'stomach_kettle2', label='stomach_kettle')
    model.Trial(rec, 24, 4, 'stomach_kettle3', label='stomach_kettle')
    model.Trial(rec, 28, 4, 'reverse_kettle1', label='reverse_kettle')
    model.Trial(rec, 32, 4, 'reverse_kettle2', label='reverse_kettle')
    model.Trial(rec, 36, 4, 'reverse_kettle3', label='reverse_kettle')
    model.Trial(rec, 44, 4, 'max_bizeps')
    model.Trial(rec, 48, 4, 'max_triceps')
    model.Trial(rec, 52, 4, 'max_flexor')
    model.Trial(rec, 56, 4, 'max_extensor')

    # Sessions
    # ===================================================================================

    # Session 2
    # -----------------------------------------------------------------------------------
    session = model.Session(experiment, setup, so3)
    rec = model.Recording(session,
                          location='data/Proband_02.pkl',
                          modality=modality.Identifier)
    model.Trial(rec, 0, 4, 'calm')
    model.Trial(rec, 4, 4, 'hantel1', label='hantel')
    model.Trial(rec, 8, 4, 'hantel2', label='hantel')
    model.Trial(rec, 12, 4, 'hantel3', label='hantel')
    model.Trial(rec, 16, 4, 'reverse_kettle1', label='reverse_kettle')
    model.Trial(rec, 20, 4, 'reverse_kettle2', label='reverse_kettle')
    model.Trial(rec, 24, 4, 'reverse_kettle3', label='reverse_kettle')
    model.Trial(rec, 28, 4, 'stomach_kettle1', label='stomach_kettle')
    model.Trial(rec, 32, 4, 'stomach_kettle2', label='stomach_kettle')
    model.Trial(rec, 36, 4, 'stomach_kettle3', label='stomach_kettle')
    model.Trial(rec, 40, 4, 'max_bizeps')
    model.Trial(rec, 44, 4, 'max_triceps')
    model.Trial(rec, 48, 4, 'max_flexor')
    model.Trial(rec, 52, 4, 'max_extensor')

    # Sessions
    # ===================================================================================

    # Session 3
    # -----------------------------------------------------------------------------------
    session = model.Session(experiment, setup, so3)
    rec = model.Recording(session,
                          location='data/Proband_03.pkl',
                          modality=modality.Identifier)
    model.Trial(rec, 0, 4, 'calm')
    model.Trial(rec, 4, 4, 'stomach_kettle1', label='stomach_kettle')
    model.Trial(rec, 8, 4, 'stomach_kettle2', label='stomach_kettle')
    model.Trial(rec, 12, 4, 'stomach_kettle3', label='stomach_kettle')
    model.Trial(rec, 16, 4, 'hantel1', label='hantel')
    model.Trial(rec, 20, 4, 'hantel2', label='hantel')
    model.Trial(rec, 24, 4, 'hantel3', label='hantel')
    model.Trial(rec, 28, 4, 'reverse_kettle1', label='reverse_kettle')
    model.Trial(rec, 32, 4, 'reverse_kettle2', label='reverse_kettle')
    model.Trial(rec, 36, 4, 'reverse_kettle3', label='reverse_kettle')
    model.Trial(rec, 40, 4, 'max_bizeps')
    model.Trial(rec, 52, 4, 'max_triceps')
    model.Trial(rec, 48, 4, 'max_flexor')
    model.Trial(rec, 56, 4, 'max_extensor')

    # Sessions
    # ===================================================================================

    # Session 4
    # -----------------------------------------------------------------------------------
    session = model.Session(experiment, setup, so3)
    rec = model.Recording(session,
                          location='data/Proband_04.pkl',
                          modality=modality.Identifier)
    model.Trial(rec, 0, 4, 'calm')
    model.Trial(rec, 4, 4, 'reverse_kettle1', label='reverse_kettle')
    model.Trial(rec, 8, 4, 'reverse_kettle2', label='reverse_kettle')
    model.Trial(rec, 12, 4, 'reverse_kettle3', label='reverse_kettle')
    model.Trial(rec, 16, 4, 'hantel1', label='hantel')
    model.Trial(rec, 20, 4, 'hantel2', label='hantel')
    model.Trial(rec, 24, 4, 'hantel3', label='hantel')
    model.Trial(rec, 28, 4, 'stomach_kettle1', label='stomach_kettle')
    model.Trial(rec, 32, 4, 'stomach_kettle2', label='stomach_kettle')
    model.Trial(rec, 36, 4, 'stomach_kettle3', label='stomach_kettle')
    model.Trial(rec, 40, 4, 'max_bizeps')
    model.Trial(rec, 44, 4, 'max_triceps')
    model.Trial(rec, 48, 4, 'max_flexor')
    model.Trial(rec, 52, 4, 'max_extensor')

    # Sessions
    # ===================================================================================

    # Session 5
    # -----------------------------------------------------------------------------------
    session = model.Session(experiment, setup, so3)
    rec = model.Recording(session,
                          location='data/Proband_05.pkl',
                          modality=modality.Identifier)
    model.Trial(rec, 0, 4, 'calm')
    model.Trial(rec, 4, 4, 'stomach_kettle1', label='stomach_kettle')
    model.Trial(rec, 8, 4, 'stomach_kettle2', label='stomach_kettle')
    model.Trial(rec, 12, 4, 'stomach_kettle3', label='stomach_kettle')
    model.Trial(rec, 16, 4, 'reverse_kettle1', label='reverse_kettle')
    model.Trial(rec, 20, 4, 'reverse_kettle2', label='reverse_kettle')
    model.Trial(rec, 24, 4, 'reverse_kettle3', label='reverse_kettle')
    model.Trial(rec, 28, 4, 'hantel1', label='hantel')
    model.Trial(rec, 32, 4, 'hantel2', label='hantel')
    model.Trial(rec, 36, 4, 'hantel3', label='hantel')
    model.Trial(rec, 44, 4, 'max_bizeps')
    model.Trial(rec, 48, 4, 'max_triceps')
    model.Trial(rec, 52, 4, 'max_flexor')
    model.Trial(rec, 56, 4, 'max_extensor')

    # Sessions
    # ===================================================================================

    # Session 6
    # -----------------------------------------------------------------------------------
    session = model.Session(experiment, setup, so3)
    rec = model.Recording(session,
                          location='data/Proband_06.pkl',
                          modality=modality.Identifier)
    model.Trial(rec, 0, 4, 'calm')
    model.Trial(rec, 4, 4, 'reverse_kettle1', label='reverse_kettle')
    model.Trial(rec, 8, 4, 'reverse_kettle2', label='reverse_kettle')
    model.Trial(rec, 12, 4, 'reverse_kettle3', label='reverse_kettle')
    model.Trial(rec, 16, 4, 'stomach_kettle1', label='stomach_kettle')
    model.Trial(rec, 20, 4, 'stomach_kettle2', label='stomach_kettle')
    model.Trial(rec, 24, 4, 'stomach_kettle3', label='stomach_kettle')
    model.Trial(rec, 28, 4, 'hantel1', label='hantel')
    model.Trial(rec, 32, 4, 'hantel2', label='hantel')
    model.Trial(rec, 36, 4, 'hantel3', label='hantel')
    model.Trial(rec, 40, 4, 'max_bizeps')
    model.Trial(rec, 44, 4, 'max_triceps')
    model.Trial(rec, 48, 4, 'max_flexor')
    model.Trial(rec, 52, 4, 'max_extensor')

    # Sessions
    # ===================================================================================

    # Session 7
    # -----------------------------------------------------------------------------------
    session = model.Session(experiment, setup, so3)
    rec = model.Recording(session,
                          location='data/Proband_07.pkl',
                          modality=modality.Identifier)
    model.Trial(rec, 0, 4, 'calm')
    model.Trial(rec, 4, 4, 'hantel1', label='hantel')
    model.Trial(rec, 8, 4, 'hantel2', label='hantel')
    model.Trial(rec, 12, 4, 'hantel3', label='hantel')
    model.Trial(rec, 16, 4, 'stomach_kettle1', label='stomach_kettle')
    model.Trial(rec, 20, 4, 'stomach_kettle2', label='stomach_kettle')
    model.Trial(rec, 24, 4, 'stomach_kettle3', label='stomach_kettle')
    model.Trial(rec, 28, 4, 'reverse_kettle1', label='reverse_kettle')
    model.Trial(rec, 32, 4, 'reverse_kettle2', label='reverse_kettle')
    model.Trial(rec, 36, 4, 'reverse_kettle3', label='reverse_kettle')
    model.Trial(rec, 44, 4, 'max_bizeps')
    model.Trial(rec, 48, 4, 'max_triceps')
    model.Trial(rec, 52, 4, 'max_flexor')
    model.Trial(rec, 56, 4, 'max_extensor')

    # Sessions
    # ===================================================================================

    # Session 8
    # -----------------------------------------------------------------------------------
    session = model.Session(experiment, setup, so3)
    rec = model.Recording(session,
                          location='data/Proband_08.pkl',
                          modality=modality.Identifier)
    model.Trial(rec, 0, 4, 'calm')
    model.Trial(rec, 4, 4, 'stomach_kettle1', label='stomach_kettle')
    model.Trial(rec, 8, 4, 'stomach_kettle2', label='stomach_kettle')
    model.Trial(rec, 12, 4, 'stomach_kettle3', label='stomach_kettle')
    model.Trial(rec, 16, 4, 'hantel1', label='hantel')
    model.Trial(rec, 20, 4, 'hantel2', label='hantel')
    model.Trial(rec, 24, 4, 'hantel3', label='hantel')
    model.Trial(rec, 28, 4, 'reverse_kettle1', label='reverse_kettle')
    model.Trial(rec, 32, 4, 'reverse_kettle2', label='reverse_kettle')
    model.Trial(rec, 36, 4, 'reverse_kettle3', label='reverse_kettle')
    model.Trial(rec, 44, 4, 'max_bizeps')
    model.Trial(rec, 48, 4, 'max_triceps')
    model.Trial(rec, 52, 4, 'max_flexor')
    model.Trial(rec, 56, 4, 'max_extensor')

    # Sessions
    # ===================================================================================

    # Session 9
    # -----------------------------------------------------------------------------------
    session = model.Session(experiment, setup, so3)
    rec = model.Recording(session,
                          location='data/Proband_09.pkl',
                          modality=modality.Identifier)
    model.Trial(rec, 0, 4, 'calm')
    model.Trial(rec, 4, 4, 'stomach_kettle1', label='stomach_kettle')
    model.Trial(rec, 8, 4, 'stomach_kettle2', label='stomach_kettle')
    model.Trial(rec, 12, 4, 'stomach_kettle3', label='stomach_kettle')
    model.Trial(rec, 16, 4, 'reverse_kettle1', label='reverse_kettle')
    model.Trial(rec, 20, 4, 'reverse_kettle2', label='reverse_kettle')
    model.Trial(rec, 24, 4, 'reverse_kettle3', label='reverse_kettle')
    model.Trial(rec, 28, 4, 'hantel1', label='hantel')
    model.Trial(rec, 32, 4, 'hantel2', label='hantel')
    model.Trial(rec, 36, 4, 'hantel3', label='hantel')
    model.Trial(rec, 40, 4, 'max_bizeps')
    model.Trial(rec, 44, 4, 'max_triceps')
    model.Trial(rec, 48, 4, 'max_flexor')
    model.Trial(rec, 52, 4, 'max_extensor')

    # Sessions
    # ===================================================================================

    # Session 10
    # -----------------------------------------------------------------------------------
    session = model.Session(experiment, setup, so3)
    rec = model.Recording(session,
                          location='data/Proband_10.pkl',
                          modality=modality.Identifier)
    model.Trial(rec, 0, 4, 'calm')
    model.Trial(rec, 4, 4, 'hantel1', label='hantel')
    model.Trial(rec, 8, 4, 'hantel2', label='hantel')
    model.Trial(rec, 12, 4, 'hantel3', label='hantel')
    model.Trial(rec, 16, 4, 'reverse_kettle1', label='reverse_kettle')
    model.Trial(rec, 20, 4, 'reverse_kettle2', label='reverse_kettle')
    model.Trial(rec, 24, 4, 'reverse_kettle3', label='reverse_kettle')
    model.Trial(rec, 28, 4, 'stomach_kettle1', label='stomach_kettle')
    model.Trial(rec, 32, 4, 'stomach_kettle2', label='stomach_kettle')
    model.Trial(rec, 36, 4, 'stomach_kettle3', label='stomach_kettle')
    model.Trial(rec, 40, 4, 'max_bizeps')
    model.Trial(rec, 44, 4, 'max_triceps')
    model.Trial(rec, 48, 4, 'max_flexor')
    model.Trial(rec, 52, 4, 'max_extensor')
    return experiment