EESM23-raw / code /dataset_preparation /ntlab_create_dataset_description_json.py
Zachary1150's picture
Add files using upload-large-folder tool
7229b86 verified
Raw
History Blame Contribute Delete
4.24 kB
import json
from collections import OrderedDict
import warnings
def create_dataset_description_json(path,
name,
bids_version,
hed_version=None,
dataset_links=None,
dataset_type=None,
data_license=None,
authors=None,
acknowledgements=None,
how_to_acknowledge=None,
funding=None,
ethics_approvals=None,
references_and_links=None,
doi=None,
generated_by=None,
source_datasets=None):
"""Create a dataset_description.json file for a BIDS dataset.
Parameters
----------
path : str
Path to the dataset_description.json file.
name : str
Name of the dataset.
bids_version : str
The BIDS version that was used.
Recommended Parameters
----------
hed_version : str or array of strings, recommended
The HED version that was used.
dataset_links : list, required if BIDS URIs are used
List of links to other datasets.
dataset_type : str, recommended
Type of the dataset. Can be "raw" or "derivative".
data_license : str, recommended
License of the dataset.
authors : list, recommended
List of individuals who contributed to the creation/curation of the dataset.
generated_by : list, recommended
Used to specify provenance of the dataset.
source_datasets : list, recommended
Used to specify the locations and relevant attributes of all source datasets. Valid values are "URL", "DOI" or "Version".
Optional Parameters
----------
acknowledgements : str, optional
Text acknowledging contributions of individuals or institutions beyond those listed in Authors or Funding.
how_to_acknowledge : str, optional
How to acknowledge the dataset.
funding : list, optional
List of funding sources.
ethics_approvals : list, optional
List of ethics approvals.
references_and_links : list, optional
List of references to publication that contain information on the dataset, or links.
doi : str, optional
DOI of the dataset (not the paper).
"""
# Check if values are of correct type
if (dataset_type not in ['raw', 'derivative']):
raise ValueError('dataset_type must be either "raw" or "derivative"')
if (source_datasets not in ['URL', 'DOI', 'Version', None]):
raise ValueError('source_datasets must be either "URL", "DOI" or "Version"')
if isinstance(doi, str):
if not doi.startswith('doi:'):
warnings.warn('DOI should start with "doi:"')
# Check if required fields are filled in
if (name is None):
raise ValueError('name is required')
if (bids_version is None):
raise ValueError('bids_version is required')
# Prepare dataset_description.json
description = OrderedDict([
('Name', name),
('BIDSVersion', bids_version),
('HEDVersion', hed_version),
('DatasetType', dataset_type),
('DatasetLinks', dataset_links),
('License', data_license),
('Authors', authors),
('Acknowledgements', acknowledgements),
('HowToAcknowledge', how_to_acknowledge),
('Funding', funding),
('EthicsApprovals', ethics_approvals),
('ReferencesAndLinks', references_and_links),
('DatasetDOI', doi),
('GeneratedBy', generated_by),
('SourceDatasets', source_datasets)])
# Remove None values
pop_keys = [key for key, val in description.items() if val is None]
for key in pop_keys:
description.pop(key)
# Only write data that is not None
with open(path, 'w') as outfile:
json.dump(description, outfile, indent=4)