Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 58 additions & 11 deletions BIDSTools/BidsDatasetBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

from BIDSTools.BIDS_PROJECT_CONFIG.BIDS_modality_custom import ModalityCustomBuilder

def generate_top_level_file(outpout_dir):
def generate_empty_top_level_bids_files(outpout_dir):
"""
Generate a top-level bid dataset structure file

Expand Down Expand Up @@ -174,7 +174,7 @@ def extract_primary_key(template):
return primary_keys


def writeheader_tsv_json_files(output_dir):
def initiate_top_level_bids_files_tsv(output_dir):
"""

Writes headers to TSV files in the BIDS top-level directory based on JSON template files.
Expand All @@ -196,15 +196,15 @@ def writeheader_tsv_json_files(output_dir):
os.path.isfile(os.path.join(output_dir, f))]

script_dir = os.path.dirname(__file__)
agnostic_template_dir = os.path.join(script_dir, 'template_agnotic_file')
agnostic_template_dir = os.path.join(script_dir, 'template_agnostic_files')

list_template_files = os.listdir(agnostic_template_dir)
list_template_files = [f for f in list_template_files if
os.path.isfile(os.path.join(agnostic_template_dir, f)) and f.endswith(
".json")]
"_tsv.json")]

for template_name in list_template_files:
template_base_name, template_ext = os.path.splitext(template_name)
template_base_name = template_name.removesuffix("_tsv.json")

template_path = os.path.join(agnostic_template_dir, template_name)
with open(template_path, 'r') as template_file:
Expand Down Expand Up @@ -251,6 +251,50 @@ def writeheader(template_content, file_name, output_dir):
f.write(header)


def initiate_top_level_bids_files_json(output_dir):
"""

Initiate json files with default values in the BIDS top-level directory based on JSON template files.

Parameters
----------
output_dir: str
Path to the directory where the BIDS top-level TSV files are located.

Returns
-------
None

"""
json_files_list = [f for f in os.listdir(output_dir) if
f.endswith(".json")]

json_files_list = [f for f in json_files_list if
os.path.isfile(os.path.join(output_dir, f))]

script_dir = os.path.dirname(__file__)
agnostic_template_dir = os.path.join(script_dir, 'template_agnostic_files')

list_template_files = os.listdir(agnostic_template_dir)
list_template_files = [f for f in list_template_files if
os.path.isfile(os.path.join(agnostic_template_dir, f)) and f.endswith(
"_json.json")]

for template_name in list_template_files:
template_base_name = template_name.removesuffix("_json.json")

template_path = os.path.join(agnostic_template_dir, template_name)
with open(template_path, 'r') as template_file:
template_content = json.load(template_file)

if template_content:
for file_name in json_files_list:
if file_name.startswith(template_base_name):
#writeheader(template_content, file_name, output_dir)
file_path = os.path.join(output_dir, file_name)
write_static_files(template_path, file_path)


def construct_bids_folders(output_dir, experiment, project_config):
"""
Creates all necessary directories for each experiment (each row in the metadata file).
Expand Down Expand Up @@ -548,7 +592,7 @@ def write_static_files(template_path, file_path):
f"Written static content from {template_path} to {file_path} with primary key: {primary_key}")


def fill_static_files(output_dir):
def initiate_top_level_bids_files_other(output_dir):
"""
Processes static files in the specified directory. For each file that does not have a .json or
.tsv extension,
Expand All @@ -566,7 +610,7 @@ def fill_static_files(output_dir):
"""
script_dir = os.path.dirname(__file__)

agnostic_template_dir = os.path.join(script_dir, "template_agnotic_file")
agnostic_template_dir = os.path.join(script_dir, "template_agnostic_files")

list_template_files = os.listdir(agnostic_template_dir)

Expand All @@ -575,6 +619,7 @@ def fill_static_files(output_dir):

for file_name in all_files:
if file_name in tsv_json_files_list:
# skip json and tsv files to process only the others
continue

file_path = os.path.join(output_dir, file_name)
Expand Down Expand Up @@ -636,17 +681,19 @@ def main(config_file_path, metada_file_path, output_dir, tag, project_config_yml
project_config = ProjectConfig(project_config_yml_path)

# Generate top-level file structure
generate_top_level_file(output_dir)
generate_empty_top_level_bids_files(output_dir)



# Generate participant folder structure


# Write headers to TSV files
writeheader_tsv_json_files(output_dir)
# Write header (first line) of tsv files
initiate_top_level_bids_files_tsv(output_dir)
# Temporary: fill json files (as static ones)
initiate_top_level_bids_files_json(output_dir)
# Fill static files
fill_static_files(output_dir)
initiate_top_level_bids_files_other(output_dir)

# Fill metadata files

Expand Down
6 changes: 4 additions & 2 deletions BIDSTools/Createfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ def layout_file(self):
info = self.filestructure.get_detail_for_file(filename)

if 'path' in info:
self.file_name.append(info['path'])
if info['level']=='required':
self.file_name.append(info['path'])

elif 'stem' in info:

Expand All @@ -148,7 +149,8 @@ def layout_file(self):

for extension in info['extensions']:
path = path + extension
self.file_name.append(path)
if info['level']=='required':
self.file_name.append(path)
if extension != '':
path = path[:-len(extension)]

Expand Down
39 changes: 0 additions & 39 deletions BIDSTools/ressources/schema/rules/files/common/core.yaml

This file was deleted.

1 change: 1 addition & 0 deletions BIDSTools/ressources/schema/rules/files/common/core.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
# Core files are files and directories that can be found at the top level
# of a dataset.
# The keys of this file must match keys in objects/files.yaml
dataset_description:
level: required
path: dataset_description.json
CITATION:
level: optional
path: CITATION.cff
README:
level: required
stem: README
extensions:
- ''
CHANGES:
level: optional
path: CHANGES
LICENSE:
level: optional
path: LICENSE
genetic_info:
level: optional
path: genetic_info.json
code:
level: optional
path: code
derivatives:
level: optional
path: derivatives
sourcedata:
level: optional
path: sourcedata
stimuli:
level: optional
path: stimuli
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
# Core files are files and directories that can be found at the top level
# of a dataset.
# The keys of this file must match keys in objects/files.yaml
dataset_description:
level: required
path: dataset_description.json
CITATION:
level: optional
path: CITATION.cff
README:
level: recommended
stem: README
extensions:
- ''
- .md
- .rst
- .txt
CHANGES:
level: optional
path: CHANGES
LICENSE:
level: optional
path: LICENSE
genetic_info:
level: optional
path: genetic_info.json
code:
level: optional
path: code
derivatives:
level: optional
path: derivatives
sourcedata:
level: optional
path: sourcedata
stimuli:
level: optional
path: stimuli
32 changes: 0 additions & 32 deletions BIDSTools/ressources/schema/rules/files/common/tables.yaml

This file was deleted.

1 change: 1 addition & 0 deletions BIDSTools/ressources/schema/rules/files/common/tables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
participants:
level: required
stem: participants
extensions:
- .tsv
- .json
samples:
level: required
stem: samples
extensions:
- .tsv
- .json
scans:
level: optional
suffixes:
- scans
extensions:
- .tsv
- .json
entities:
subject: required
session: optional # session is required if session is present in the dataset.
sessions: # This file may only exist if session is present in the dataset.
level: optional
suffixes:
- sessions
extensions:
- .tsv
- .json
entities:
subject: required
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
participants:
level: optional
stem: participants
extensions:
- .tsv
- .json
samples:
level: optional
stem: samples
extensions:
- .tsv
- .json
scans:
level: optional
suffixes:
- scans
extensions:
- .tsv
- .json
entities:
subject: required
session: optional # session is required if session is present in the dataset.
sessions: # This file may only exist if session is present in the dataset.
level: optional
suffixes:
- sessions
extensions:
- .tsv
- .json
entities:
subject: required
6 changes: 6 additions & 0 deletions BIDSTools/template_agnostic_files/README.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"fake-readme-field": {
"Requirement Level": "RECOMMENDED",
"Default value": "This is the default content for the README file that will be located at the top-level of the BIDS directory"
}
}