Skip to content

Commit c79b2a3

Browse files
authored
Creating a Report for the conversion (#50)
* create_report * typo * making report one object * missing state * adjusting report * forgotten +1 * Experimental approaches * returning the report * dealing with list
1 parent 186961c commit c79b2a3

File tree

2 files changed

+134
-8
lines changed

2 files changed

+134
-8
lines changed

Diff for: bids2openminds/converter.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
from warnings import warn
1+
import warnings
22
from bids import BIDSLayout, BIDSValidator
33
from openminds import Collection
44
import os
55
import click
66
from . import main
77
from . import utility
8+
from . import report
89

910

10-
def convert(input_path, save_output=False, output_path=None, multiple_files=False, include_empty_properties=False):
11+
def convert(input_path, save_output=False, output_path=None, multiple_files=False, include_empty_properties=False, quiet=False):
1112
if not (os.path.isdir(input_path)):
1213
raise NotADirectoryError(
1314
f"The input directory is not valid, you have specified {input_path} which is not a directory."
1415
)
16+
# TODO use BIDSValidator to check if input directory is a valid BIDS directory
1517
# if not(BIDSValidator().is_bids(input_path)):
1618
# raise NotADirectoryError(f"The input directory is not valid, you have specified {input_path} which is not a BIDS directory.")
19+
20+
if quiet:
21+
warnings.filterwarnings('ignore')
22+
1723
collection = Collection()
1824
bids_layout = BIDSLayout(input_path)
1925

@@ -53,12 +59,15 @@ def convert(input_path, save_output=False, output_path=None, multiple_files=Fals
5359

5460
collection.save(output_path, individual_files=multiple_files,
5561
include_empty_properties=include_empty_properties)
56-
print(
57-
f"Conversion was successful, the openMINDS file is in {output_path}")
58-
return collection
62+
63+
if not quiet:
64+
print(report.create_report(dataset, dataset_version, collection,
65+
dataset_description, input_path, output_path))
66+
5967
else:
6068
print("Conversion was successful")
61-
return collection
69+
70+
return collection
6271

6372

6473
@click.command()
@@ -67,9 +76,10 @@ def convert(input_path, save_output=False, output_path=None, multiple_files=Fals
6776
@click.option("--single-file", "multiple_files", flag_value=False, default=False, help="Save the entire collection into a single file (default).")
6877
@click.option("--multiple-files", "multiple_files", flag_value=True, help="Each node is saved into a separate file within the specified directory. 'output-path' if specified, must be a directory.")
6978
@click.option("-e", "--include-empty-properties", is_flag=True, default=False, help="Whether to include empty properties in the final file.")
70-
def convert_click(input_path, output_path, multiple_files, include_empty_properties):
79+
@click.option("-q", "--quiet", is_flag=True, default=False, help="Not generate the final report and no warning.")
80+
def convert_click(input_path, output_path, multiple_files, include_empty_properties, quiet):
7181
convert(input_path, save_output=True, output_path=output_path,
72-
multiple_files=multiple_files, include_empty_properties=include_empty_properties)
82+
multiple_files=multiple_files, include_empty_properties=include_empty_properties, quiet=quiet)
7383

7484

7585
if __name__ == "__main__":

Diff for: bids2openminds/report.py

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import os
2+
3+
4+
def create_report(dataset, dataset_version, collection, dataset_description, input_path, output_path):
5+
subject_number = 0
6+
subject_state_numbers = []
7+
file_bundle_number = 0
8+
files_number = 0
9+
behavioral_protocols_numbers = 0
10+
11+
for item in collection:
12+
if item.type_ == "https://openminds.ebrains.eu/core/Subject":
13+
14+
subject_number += 1
15+
subject_state_numbers.append(len(item.studied_states))
16+
17+
if item.type_ == "https://openminds.ebrains.eu/core/File":
18+
19+
files_number += 1
20+
21+
if item.type_ == "https://openminds.ebrains.eu/core/FileBundle":
22+
23+
file_bundle_number += 1
24+
25+
if item.type_ == "https://openminds.ebrains.eu/core/BehavioralProtocol":
26+
27+
behavioral_protocols_numbers += 1
28+
29+
experimental_approaches_list = ""
30+
if dataset_version.experimental_approaches is not None:
31+
for approache in dataset_version.experimental_approaches:
32+
experimental_approaches_list += f"{approache.name}\n"
33+
34+
data_types_list = ""
35+
if dataset_version.data_types is not None:
36+
if isinstance(dataset_version.data_types, list):
37+
for data_type in dataset_version.data_types:
38+
data_types_list += f"{data_type.name}\n"
39+
else:
40+
data_types_list = f"{dataset_version.data_types.name}\n"
41+
42+
author_list = ""
43+
i = 1
44+
if dataset_version.authors is not None:
45+
for author in dataset_version.authors:
46+
if author.family_name is not None:
47+
author_list += f" {i}. {author.family_name}, {author.given_name}\n"
48+
i += 1
49+
else:
50+
author_list += f" {i}. ___, {author.given_name}\n"
51+
i += 1
52+
53+
min_subject_state_numbers = min(subject_state_numbers)
54+
max_subject_state_numbers = max(subject_state_numbers)
55+
if min_subject_state_numbers == max_subject_state_numbers:
56+
text_subject_state_numbers = str(min_subject_state_numbers)
57+
else:
58+
text_subject_state_numbers = f"min={min_subject_state_numbers}, max={max_subject_state_numbers}"
59+
60+
report = f"""
61+
Conversion Report
62+
=================
63+
Conversion was successful, the openMINDS file is in {output_path}
64+
65+
Dataset title : {dataset.full_name}
66+
67+
68+
Experimental approaches detected:
69+
------------------------------------------
70+
{experimental_approaches_list}
71+
72+
Detected data types:
73+
------------------------------------------
74+
{data_types_list}
75+
76+
The following elements were converted:
77+
------------------------------------------
78+
+ number of authors : {len(dataset_version.authors or [])}
79+
+ number of converted subjects: {subject_number}
80+
+ number of states per subject: {text_subject_state_numbers}
81+
+ number of files: {files_number}
82+
+ number of file bundles: {file_bundle_number}
83+
+ number of techniques: {len(dataset_version.techniques or [])}
84+
+ number of behavioral protocols: {behavioral_protocols_numbers}
85+
86+
87+
88+
**Important Notes**
89+
------------------------------------------
90+
91+
Authors:
92+
The conversion of authors is not reliable due to missing source convention.
93+
The converter may fail in detecting family vs given name.
94+
The converter will fail in detecting organizations.
95+
The following persons (family name, given name) were converted, :
96+
{author_list}
97+
98+
Subject States:
99+
There are as many subject states as sessions for each subject.
100+
Please modify to your needs (divide into more or merge into fewer subject states).
101+
102+
Behavioral protocols:
103+
The conversion of behavioral protocols is incomplete.
104+
Only the task-label is extracted as name and internal identifier of a behavioral protocol.
105+
Please adjust to your needs.
106+
107+
"""
108+
if "GeneratedBy" in dataset_description:
109+
report = report+"+Dataset is derivative, derivative data are ignored for now\n"
110+
111+
derivatives_path = os.path.join(
112+
input_path, "derivatives")
113+
if os.path.isdir(derivatives_path):
114+
report = report+"+ Dataset contains derivative, derivative data are ignored for now\n"
115+
116+
return report

0 commit comments

Comments
 (0)