def test_get_sidecar_dict(self):
        column1 = {'a': 3, 'b': 2, 'c': 1}
        column2 = {'a1': 6, 'b1': 22}
        columns_info = {'column1': column1, 'column2': column2}
        [hed_dict, issues] = SidecarMap.get_sidecar_dict(columns_info, {})
        self.assertFalse(hed_dict, "Dictionary is empty of no columns selected")
        self.assertTrue(issues, "Issues is not empty if no columns selected")

        [hed_dict, issues] = SidecarMap.get_sidecar_dict(columns_info, {'banana': False})
        self.assertFalse(hed_dict, " get_sidecar_dict: Dictionary is empty of bad column selected")
        self.assertTrue(issues, " get_sidecar_dict: Issues is not empty if bad columns selected")

        [hed_dict, issues] = SidecarMap.get_sidecar_dict(columns_info,
                                                         {'column1': True, 'banana': False, 'apple': True})
        self.assertTrue(hed_dict, " get_sidecar_dict: Dictionary not empty if at least one good column selected")
        self.assertTrue(issues, " get_sidecar_dict: Issues is not empty if at least one bad column selected")
        self.assertEqual(len(issues), 2, "get_sidecar_dict: Same number of issues as bad columns")

        [hed_dict, issues] = SidecarMap.get_sidecar_dict(columns_info, {'column1': True, 'column2': False})
        self.assertTrue(hed_dict, "Dictionary not empty if at least one good column selected")
        self.assertFalse(issues, "Issues is empty if good data provided")
示例#2
0
def extract(events, columns_selected):
    """Extracts a JSON sidecar template from a BIDS-style events file.

    Parameters
    ----------
    events: EventInput
        An events input object
    columns_selected: dict
        dictionary of columns selected

    Returns
    -------
    dict
        A dictionary pointing to extracted JSON file.
    """

    columns_info = get_columns_info(events.dataframe)
    sr = SidecarMap()
    hed_dict, issues = sr.get_sidecar_dict(columns_info, columns_selected)
    display_name = events.name
    if issues:
        issue_str = get_printable_issue_string(
            issues, f"{display_name} HED validation errors")
        file_name = generate_filename(display_name,
                                      name_suffix='_errors',
                                      extension='.txt')
        return {
            base_constants.COMMAND: base_constants.COMMAND_VALIDATE,
            'data': issue_str,
            "output_display_name": file_name,
            "msg_category": "warning",
            'msg': f"Events file {display_name} had extraction errors"
        }
    else:
        file_name = generate_filename(display_name,
                                      name_suffix='_extracted',
                                      extension='.json')
        return {
            base_constants.COMMAND: base_constants.COMMAND_EXTRACT,
            'data': json.dumps(hed_dict, indent=4),
            'output_display_name': file_name,
            'msg_category': 'success',
            'msg': 'Events extraction to JSON complete'
        }