bagel 0.5.1.dev7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
bagel/logger.py ADDED
@@ -0,0 +1,63 @@
1
+ import logging
2
+ import sys
3
+ from enum import Enum
4
+ from typing import NoReturn
5
+
6
+ import typer
7
+ from rich.logging import RichHandler
8
+
9
+ LOG_FMT = "%(message)s"
10
+ DATETIME_FMT = "[%Y-%m-%d %X]"
11
+
12
+ # Check if code is currently running in a test environment
13
+ IS_TESTING = "pytest" in sys.modules
14
+
15
+ logger = logging.getLogger("bagel.logger")
16
+
17
+
18
+ class VerbosityLevel(str, Enum):
19
+ """Enum for verbosity levels."""
20
+
21
+ ERROR = "0"
22
+ INFO = "1"
23
+ # NOTE: We currently do not have any debug logs, but can add them in the future
24
+ DEBUG = "2"
25
+
26
+
27
+ verbosity_log_levels = {
28
+ VerbosityLevel.ERROR: logging.ERROR,
29
+ VerbosityLevel.INFO: logging.INFO,
30
+ VerbosityLevel.DEBUG: logging.DEBUG,
31
+ }
32
+
33
+
34
+ def configure_logger(verbosity: VerbosityLevel = VerbosityLevel.INFO) -> None:
35
+ """Configure a logger with the specified logging level."""
36
+ level = verbosity_log_levels[verbosity]
37
+
38
+ # Prevent duplicate handlers when updating the logger
39
+ if not logger.handlers:
40
+ handler = RichHandler(
41
+ omit_repeated_times=False, show_path=False, rich_tracebacks=True
42
+ )
43
+ formatter = logging.Formatter(fmt=LOG_FMT, datefmt=DATETIME_FMT)
44
+ handler.setFormatter(formatter)
45
+ logger.addHandler(handler)
46
+
47
+ logger.setLevel(level)
48
+ for handler in logger.handlers:
49
+ handler.setLevel(level)
50
+
51
+ # propagate should be False to avoid duplicate messages from root logger
52
+ # except when testing because otherwise Pytest does not capture the logs
53
+ if not IS_TESTING:
54
+ logger.propagate = False
55
+
56
+
57
+ def log_error(
58
+ logger: logging.Logger,
59
+ message: str,
60
+ ) -> NoReturn:
61
+ """Log an exception with an informative error message, and exit the app."""
62
+ logger.error(message)
63
+ raise typer.Exit(code=1)
bagel/mappings.py ADDED
@@ -0,0 +1,102 @@
1
+ import json
2
+ from collections import namedtuple
3
+ from pathlib import Path
4
+
5
+ import httpx
6
+
7
+ from .logger import log_error, logger
8
+ from .utilities import file_utils
9
+
10
+ Namespace = namedtuple("Namespace", ["pf", "url"])
11
+ COGATLAS = Namespace("cogatlas", "https://www.cognitiveatlas.org/task/id/")
12
+ NB = Namespace("nb", "http://neurobagel.org/vocab/")
13
+ NCIT = Namespace("ncit", "http://ncicb.nci.nih.gov/xml/owl/EVS/Thesaurus.owl#")
14
+ NIDM = Namespace("nidm", "http://purl.org/nidash/nidm#")
15
+ SNOMED = Namespace("snomed", "http://purl.bioontology.org/ontology/SNOMEDCT/")
16
+ NP = Namespace(
17
+ "np", "https://github.com/nipoppy/pipeline-catalog/tree/main/processing/"
18
+ )
19
+
20
+ # Store all supported and deprecated namespaces in a list for easy iteration & testing
21
+ SUPPORTED_NAMESPACES = [NB, NCIT, NIDM, SNOMED, NP]
22
+ SUPPORTED_NAMESPACE_PREFIXES = [ns.pf for ns in SUPPORTED_NAMESPACES]
23
+ # Keep deprecated namespaces for informative user messages
24
+ DEPRECATED_NAMESPACES = [COGATLAS]
25
+ DEPRECATED_NAMESPACE_PREFIXES = [ns.pf for ns in DEPRECATED_NAMESPACES]
26
+
27
+ BIDS = {
28
+ "anat": NIDM.pf + ":Anatomical",
29
+ "func": NIDM.pf + ":Functional",
30
+ "dwi": NIDM.pf + ":DiffusionWeighted",
31
+ "bval": NIDM.pf + ":b-value",
32
+ "bvec": NIDM.pf + ":b-vector",
33
+ "T1w": NIDM.pf + ":T1Weighted",
34
+ "T2w": NIDM.pf + ":T2Weighted",
35
+ "inplaneT2": NIDM.pf + ":T2Weighted",
36
+ "bold": NIDM.pf + ":FlowWeighted",
37
+ "dti": NIDM.pf + ":DiffusionTensor",
38
+ "asl": NIDM.pf + ":ArterialSpinLabeling",
39
+ }
40
+ NEUROBAGEL = {
41
+ "participant": NB.pf + ":ParticipantID",
42
+ "session": NB.pf + ":SessionID",
43
+ "sex": NB.pf + ":Sex",
44
+ "age": NB.pf + ":Age",
45
+ "diagnosis": NB.pf + ":Diagnosis",
46
+ "healthy_control": NCIT.pf + ":C94342",
47
+ "assessment_tool": NB.pf + ":Assessment",
48
+ }
49
+
50
+ # TODO: Use importlib.resources.files(bagel) to get the path to the pipeline-catalog instead?
51
+ PROCESSING_PIPELINE_PATH = (
52
+ Path(__file__).parent / "pipeline-catalog/processing/processing.json"
53
+ )
54
+ PROCESSING_PIPELINE_URL = "https://raw.githubusercontent.com/nipoppy/pipeline-catalog/refs/heads/main/processing/processing.json"
55
+
56
+
57
+ def get_pipeline_catalog(url: str, path: Path) -> list[dict]:
58
+ """
59
+ Load the pipeline catalog from the remote location or, if that fails,
60
+ from the local backup.
61
+ """
62
+ try:
63
+ response = httpx.get(url)
64
+ response.raise_for_status()
65
+ return response.json()
66
+ # The JSONDecodeError should catch the case where the file is empty
67
+ except (httpx.HTTPError, json.JSONDecodeError):
68
+ logger.warning(
69
+ f"Unable to download pipeline catalog from {url}. Will revert to loading backup from {path}."
70
+ )
71
+ try:
72
+ # load_json() will catch JSONDecodeError which should catch when the file is empty
73
+ return file_utils.load_json(path)
74
+ except FileNotFoundError as e:
75
+ log_error(
76
+ logger,
77
+ f"Unable to find a local pipeline-catalog backup. Have you correctly initialized the submodules? {e}",
78
+ )
79
+
80
+
81
+ def parse_pipeline_catalog() -> tuple[dict, dict]:
82
+ """
83
+ Load the pipeline catalog and return a dictionary of pipeline names and their URIs in the Nipoppy namespace,
84
+ and a dictionary of pipeline names and their supported versions in Nipoppy.
85
+ """
86
+ pipeline_catalog_arr = get_pipeline_catalog(
87
+ url=PROCESSING_PIPELINE_URL,
88
+ path=PROCESSING_PIPELINE_PATH,
89
+ )
90
+
91
+ version_dict = {}
92
+ uri_dict = {}
93
+ for pipeline in pipeline_catalog_arr:
94
+ version_dict[pipeline["name"]] = pipeline["versions"]
95
+ uri_dict[pipeline["name"]] = f"{NP.pf}:{pipeline['name']}"
96
+
97
+ return uri_dict, version_dict
98
+
99
+
100
+ # TODO: consider refactoring this into a Mappings class that also
101
+ # handles lazy loading of the remote content, i.e. only when accessed
102
+ KNOWN_PIPELINE_URIS, KNOWN_PIPELINE_VERSIONS = parse_pipeline_catalog()
bagel/models.py ADDED
@@ -0,0 +1,97 @@
1
+ import uuid
2
+ from typing import List, Literal, Optional, Union
3
+
4
+ from pydantic import BaseModel, ConfigDict, Field, HttpUrl
5
+
6
+ from bagel.mappings import NB
7
+
8
+ UUID_PATTERN = r"[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$"
9
+ BAGEL_UUID_PATTERN = rf"^{NB.pf}:{UUID_PATTERN}"
10
+
11
+
12
+ class Bagel(BaseModel):
13
+ """identifier has to be a valid UUID prepended by the Neurobagel namespace
14
+ by default, a random (uuid4) string UUID will be created"""
15
+
16
+ identifier: str = Field(
17
+ pattern=BAGEL_UUID_PATTERN,
18
+ default_factory=lambda: NB.pf + ":" + str(uuid.uuid4()),
19
+ )
20
+
21
+ model_config = ConfigDict(extra="forbid")
22
+
23
+
24
+ class ControlledTerm(BaseModel):
25
+ identifier: Union[str, HttpUrl]
26
+ schemaKey: str
27
+
28
+
29
+ class Sex(ControlledTerm):
30
+ schemaKey: Literal["Sex"] = "Sex"
31
+
32
+
33
+ class Diagnosis(ControlledTerm):
34
+ schemaKey: Literal["Diagnosis"] = "Diagnosis"
35
+
36
+
37
+ class SubjectGroup(ControlledTerm):
38
+ schemaKey: Literal["SubjectGroup"] = "SubjectGroup"
39
+
40
+
41
+ class Assessment(ControlledTerm):
42
+ schemaKey: Literal["Assessment"] = "Assessment"
43
+
44
+
45
+ class Image(ControlledTerm):
46
+ schemaKey: Literal["Image"] = "Image"
47
+
48
+
49
+ class Acquisition(Bagel):
50
+ hasContrastType: Image
51
+ schemaKey: Literal["Acquisition"] = "Acquisition"
52
+
53
+
54
+ class Pipeline(ControlledTerm):
55
+ schemaKey: Literal["Pipeline"] = "Pipeline"
56
+
57
+
58
+ class CompletedPipeline(Bagel):
59
+ hasPipelineVersion: str
60
+ hasPipelineName: Pipeline
61
+ schemaKey: Literal["CompletedPipeline"] = "CompletedPipeline"
62
+
63
+
64
+ class Session(Bagel):
65
+ hasLabel: str
66
+
67
+
68
+ class PhenotypicSession(Session):
69
+ hasAge: Optional[float] = None
70
+ hasSex: Optional[Sex] = None
71
+ isSubjectGroup: Optional[SubjectGroup] = None
72
+ hasDiagnosis: Optional[List[Diagnosis]] = None
73
+ hasAssessment: Optional[List[Assessment]] = None
74
+ schemaKey: Literal["PhenotypicSession"] = "PhenotypicSession"
75
+
76
+
77
+ class ImagingSession(Session):
78
+ # NOTE: Do imaging session have to have at least one acquisition OR at least one completed pipeline to be valid?
79
+ hasFilePath: Optional[str] = None
80
+ hasAcquisition: Optional[List[Acquisition]] = None
81
+ hasCompletedPipeline: Optional[List[CompletedPipeline]] = None
82
+ schemaKey: Literal["ImagingSession"] = "ImagingSession"
83
+
84
+
85
+ class Subject(Bagel):
86
+ hasLabel: str
87
+ hasSession: List[Union[PhenotypicSession, ImagingSession]]
88
+ schemaKey: Literal["Subject"] = "Subject"
89
+
90
+
91
+ class Dataset(Bagel):
92
+ hasLabel: str
93
+ # NOTE: Since Pydantic v2, URL types no longer inherit from `str`
94
+ # (see https://docs.pydantic.dev/latest/migration/#url-and-dsn-types-in-pydanticnetworks-no-longer-inherit-from-str)
95
+ hasPortalURI: Optional[Union[str, HttpUrl]] = None
96
+ hasSamples: List[Subject]
97
+ schemaKey: Literal["Dataset"] = "Dataset"
@@ -0,0 +1 @@
1
+ gitdir: ../../.git/modules/pipeline-catalog
@@ -0,0 +1,16 @@
1
+ # See https://pre-commit.com for more information
2
+ # See https://pre-commit.com/hooks.html for more hooks
3
+ repos:
4
+ - repo: https://github.com/pre-commit/pre-commit-hooks
5
+ rev: v5.0.0
6
+ hooks:
7
+ - id: check-added-large-files
8
+ - id: check-case-conflict
9
+ - id: check-json
10
+ - id: check-merge-conflict
11
+ - id: check-yaml
12
+ - id: end-of-file-fixer
13
+ - id: mixed-line-ending
14
+ - id: pretty-format-json
15
+ args: [--autofix, --indent, "4"]
16
+ - id: trailing-whitespace
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 nipoppy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ # pipeline-catalog
2
+ An index of processing pipelines and versions that are recognized by/are ready to use out-of-the-box with [Nipoppy](https://github.com/nipoppy/nipoppy).
@@ -0,0 +1,20 @@
1
+ [
2
+ {
3
+ "name": "bidscoin",
4
+ "versions": [
5
+ "4.3.2"
6
+ ]
7
+ },
8
+ {
9
+ "name": "dcm2bids",
10
+ "versions": [
11
+ "3.1.0"
12
+ ]
13
+ },
14
+ {
15
+ "name": "heudiconv",
16
+ "versions": [
17
+ "0.12.2"
18
+ ]
19
+ }
20
+ ]
@@ -0,0 +1,36 @@
1
+ [
2
+ {
3
+ "name": "fmriprep",
4
+ "versions": [
5
+ "20.2.0",
6
+ "20.2.7",
7
+ "23.1.3",
8
+ "24.1.1"
9
+ ]
10
+ },
11
+ {
12
+ "name": "freesurfer",
13
+ "versions": [
14
+ "6.0.1",
15
+ "7.3.2"
16
+ ]
17
+ },
18
+ {
19
+ "name": "mriqc",
20
+ "versions": [
21
+ "23.1.0"
22
+ ]
23
+ },
24
+ {
25
+ "name": "qsiprep",
26
+ "versions": [
27
+ "0.23.0"
28
+ ]
29
+ },
30
+ {
31
+ "name": "tractoflow",
32
+ "versions": [
33
+ "2.4.2"
34
+ ]
35
+ }
36
+ ]
File without changes
@@ -0,0 +1,74 @@
1
+ from pathlib import Path
2
+ from typing import Optional
3
+
4
+ from bids import BIDSLayout
5
+ from typer import BadParameter
6
+
7
+ from bagel import mappings, models
8
+
9
+
10
+ def check_absolute_bids_path(bids_path: Path) -> Path:
11
+ """
12
+ Raise an error if the input BIDS path does not look like an absolute path.
13
+ This is a workaround for --source-bids-dir not requiring the path to exist (and thus not being able to resolve the path automatically),
14
+ since it refers to a path on a host machine which may not be mounted as-is when the CLI is running in a container.
15
+ """
16
+ if not bids_path.is_absolute():
17
+ raise BadParameter("BIDS directory path must be an absolute path.")
18
+ return bids_path
19
+
20
+
21
+ def map_term_to_namespace(term: str, namespace: dict) -> str:
22
+ """Returns the mapped namespace term if it exists, or False otherwise."""
23
+ return namespace.get(term, False)
24
+
25
+
26
+ def get_bids_subjects_simple(bids_dir: Path) -> list:
27
+ """Returns list of subject IDs (in format of sub-<SUBJECT>) for a BIDS directory inferred from the names of non-empty subdirectories."""
28
+ bids_subject_list = []
29
+ for path in bids_dir.iterdir():
30
+ if (
31
+ path.name.startswith("sub-")
32
+ and path.is_dir()
33
+ and any(path.iterdir())
34
+ ):
35
+ bids_subject_list.append(path.name)
36
+ return bids_subject_list
37
+
38
+
39
+ def create_acquisitions(
40
+ layout: BIDSLayout,
41
+ bids_sub_id: str,
42
+ session: Optional[str],
43
+ ) -> list:
44
+ """Parses BIDS image files for a specified session/subject to create a list of Acquisition objects."""
45
+ image_list = []
46
+ for bids_file in layout.get(
47
+ subject=bids_sub_id,
48
+ session=session,
49
+ extension=[".nii", ".nii.gz"],
50
+ ):
51
+ # If the suffix of a BIDS file is not recognized, then ignore
52
+ mapped_term = map_term_to_namespace(
53
+ bids_file.get_entities().get("suffix"),
54
+ namespace=mappings.BIDS,
55
+ )
56
+ if mapped_term:
57
+ image_list.append(
58
+ models.Acquisition(
59
+ hasContrastType=models.Image(identifier=mapped_term)
60
+ )
61
+ )
62
+
63
+ return image_list
64
+
65
+
66
+ def get_session_path(
67
+ source_bids_dir: Path,
68
+ bids_sub_id: str,
69
+ session: Optional[str],
70
+ ) -> str:
71
+ """Construct the session directory from the source BIDS directory path if session layer exists, otherwise returns subject directory."""
72
+ subject_path = source_bids_dir / f"sub-{bids_sub_id}"
73
+ session_path = subject_path / f"ses-{session}" if session else subject_path
74
+ return session_path.as_posix()
@@ -0,0 +1,138 @@
1
+ from typing import Iterable
2
+
3
+ import pandas as pd
4
+
5
+ from bagel import mappings, models
6
+ from bagel.logger import log_error, logger
7
+
8
+ # Shorthands for expected column names in a Nipoppy processing status file
9
+ # TODO: While there are multiple session ID columns in a Nipoppy processing status file,
10
+ # we only only look at `bids_session_id` right now. We should revisit this after the schema is finalized,
11
+ # to see if any other logic is needed to avoid issues with session ID discrepancies across columns.
12
+ PROC_STATUS_COLS = {
13
+ "participant": "bids_participant_id",
14
+ "session": "bids_session_id",
15
+ "pipeline_name": "pipeline_name",
16
+ "pipeline_version": "pipeline_version",
17
+ "status": "status",
18
+ }
19
+
20
+
21
+ def get_recognized_pipelines(pipelines: Iterable[str]) -> list:
22
+ """
23
+ Check that all pipelines in the processing status file are supported by Nipoppy.
24
+ Log an error if all pipelines are unrecognized, otherwise warn about unrecognized pipelines.
25
+ """
26
+ recognized_pipelines = list(
27
+ set(pipelines).intersection(mappings.KNOWN_PIPELINE_URIS)
28
+ )
29
+ unrecognized_pipelines = list(
30
+ set(pipelines).difference(mappings.KNOWN_PIPELINE_URIS)
31
+ )
32
+
33
+ unrecognized_pipelines_details = (
34
+ f"Unrecognized processing pipelines: {unrecognized_pipelines}. "
35
+ f"Supported pipelines are found in the Nipoppy pipeline catalog (https://github.com/nipoppy/pipeline-catalog): "
36
+ f"{list(mappings.KNOWN_PIPELINE_URIS.keys())}"
37
+ )
38
+ if not recognized_pipelines:
39
+ log_error(
40
+ logger,
41
+ f"The processing status file contains no recognized pipelines in the column: '{PROC_STATUS_COLS['pipeline_name']}'.\n"
42
+ f"{unrecognized_pipelines_details}",
43
+ )
44
+ if unrecognized_pipelines:
45
+ logger.warning(
46
+ f"The processing status file contains unrecognized pipelines in the column: '{PROC_STATUS_COLS['pipeline_name']}' - "
47
+ "these will be ignored. "
48
+ f"{unrecognized_pipelines_details}"
49
+ )
50
+ return recognized_pipelines
51
+
52
+
53
+ def validate_pipeline_versions(
54
+ pipeline: str, versions: Iterable[str]
55
+ ) -> tuple[list, list]:
56
+ """
57
+ For a given pipeline, return the recognized and unrecognized pipeline versions in the processing status file
58
+ based on the Nipoppy pipeline catalog, and return both as lists.
59
+ """
60
+ recognized_versions = list(
61
+ set(versions).intersection(mappings.KNOWN_PIPELINE_VERSIONS[pipeline])
62
+ )
63
+ unrecognized_versions = list(
64
+ set(versions).difference(mappings.KNOWN_PIPELINE_VERSIONS[pipeline])
65
+ )
66
+
67
+ return recognized_versions, unrecognized_versions
68
+
69
+
70
+ def check_at_least_one_pipeline_version_is_recognized(status_df: pd.DataFrame):
71
+ """
72
+ Check that at least one pipeline name and version combination found in the processing status file is supported by Nipoppy.
73
+ """
74
+ recognized_pipelines = get_recognized_pipelines(
75
+ status_df[PROC_STATUS_COLS["pipeline_name"]].unique()
76
+ )
77
+
78
+ any_recognized_versions = False
79
+ unrecognized_pipeline_versions = {}
80
+ for pipeline in recognized_pipelines:
81
+ versions = status_df[
82
+ status_df[PROC_STATUS_COLS["pipeline_name"]] == pipeline
83
+ ][PROC_STATUS_COLS["pipeline_version"]].unique()
84
+
85
+ recognized_versions, unrecognized_versions = (
86
+ validate_pipeline_versions(pipeline, versions)
87
+ )
88
+ if recognized_versions:
89
+ any_recognized_versions = True
90
+ if unrecognized_versions:
91
+ unrecognized_pipeline_versions[pipeline] = unrecognized_versions
92
+
93
+ unrecognized_versions_details = (
94
+ f"Unrecognized processing pipeline versions: {unrecognized_pipeline_versions}. "
95
+ "Supported pipeline versions are found in the Nipoppy pipeline catalog (https://github.com/nipoppy/pipeline-catalog)."
96
+ )
97
+ if not any_recognized_versions:
98
+ log_error(
99
+ logger,
100
+ f"The processing status file contains no recognized versions of any pipelines in the column '{PROC_STATUS_COLS['pipeline_version']}'.\n"
101
+ f"{unrecognized_versions_details}",
102
+ )
103
+ if unrecognized_pipeline_versions:
104
+ logger.warning(
105
+ f"The processing status file contains unrecognized versions of pipelines in the column '{PROC_STATUS_COLS['pipeline_version']}' - "
106
+ "these will be ignored. "
107
+ f"{unrecognized_versions_details}"
108
+ )
109
+
110
+
111
+ def create_completed_pipelines(session_proc_df: pd.DataFrame) -> list:
112
+ """
113
+ Create a list of CompletedPipeline objects for a single subject-session based on the completion status
114
+ info of pipelines for that session from the processing status dataframe.
115
+ """
116
+ completed_pipelines = []
117
+ for (pipeline, version), session_pipe_df in session_proc_df.groupby(
118
+ [
119
+ PROC_STATUS_COLS["pipeline_name"],
120
+ PROC_STATUS_COLS["pipeline_version"],
121
+ ]
122
+ ):
123
+ if (
124
+ pipeline in mappings.KNOWN_PIPELINE_URIS
125
+ and version in mappings.KNOWN_PIPELINE_VERSIONS[pipeline]
126
+ ) and (
127
+ session_pipe_df[PROC_STATUS_COLS["status"]].str.lower()
128
+ == "success"
129
+ ).all():
130
+ completed_pipeline = models.CompletedPipeline(
131
+ hasPipelineName=models.Pipeline(
132
+ identifier=mappings.KNOWN_PIPELINE_URIS[pipeline]
133
+ ),
134
+ hasPipelineVersion=version,
135
+ )
136
+ completed_pipelines.append(completed_pipeline)
137
+
138
+ return completed_pipelines
@@ -0,0 +1,96 @@
1
+ import json
2
+ from pathlib import Path
3
+ from typing import Any
4
+
5
+ import pandas as pd
6
+ import typer
7
+
8
+ from ..logger import log_error, logger
9
+
10
+
11
+ def file_encoding_error_message(input_p: Path) -> str:
12
+ """Return a message for when a file cannot be read due to encoding issues."""
13
+ return (
14
+ f"Failed to decode the input file {input_p}. "
15
+ "Please ensure that both your phenotypic .tsv file and .json data dictionary have UTF-8 encoding.\n"
16
+ "Tip: Need help converting your file? Try a tool like iconv (http://linux.die.net/man/1/iconv) or https://www.freeformatter.com/convert-file-encoding.html."
17
+ )
18
+
19
+
20
+ def load_tabular(
21
+ input_p: Path, input_type: str = "phenotypic"
22
+ ) -> pd.DataFrame:
23
+ """Load a .tsv pheno file and do some basic validation of the file type."""
24
+ if input_p.suffix == ".tsv":
25
+ try:
26
+ tabular_df = pd.read_csv(
27
+ input_p,
28
+ sep="\t",
29
+ keep_default_na=False,
30
+ dtype=str,
31
+ encoding="utf-8",
32
+ )
33
+ except UnicodeDecodeError:
34
+ log_error(
35
+ logger,
36
+ file_encoding_error_message(input_p),
37
+ )
38
+
39
+ if tabular_df.shape[1] > 1:
40
+ return tabular_df
41
+
42
+ # If we have only one column, but splitting by ',' gives us several elements
43
+ # then there is a good chance the user accidentally renamed a .csv into .tsv
44
+ # and we should give them some extra info with our error message to fix this.
45
+ note_misnamed_csv = (
46
+ f"Note that your {input_type} input file also looks like a .csv file "
47
+ "as it contains several ',' commas. It is possible that "
48
+ "you have accidentally renamed a .csv file as a .tsv."
49
+ )
50
+ log_error(
51
+ logger,
52
+ f"Your {input_type} input file {input_p} has only one column "
53
+ f"and is therefore not valid as a Neurobagel {input_type} file. "
54
+ f"Please provide a valid .tsv {input_type} file!"
55
+ f"\n{note_misnamed_csv if len(tabular_df.columns[0].split(',')) > 1 else ''}",
56
+ )
57
+
58
+ log_error(
59
+ logger,
60
+ f"Your ({input_p}) is not a .tsv file. "
61
+ f"Please provide a valid .tsv {input_type} file!",
62
+ )
63
+
64
+
65
+ def load_json(input_p: Path) -> Any:
66
+ """Load a user-specified json type file."""
67
+ try:
68
+ with open(input_p, "r", encoding="utf-8") as f:
69
+ return json.load(f)
70
+ except UnicodeDecodeError:
71
+ log_error(
72
+ logger,
73
+ file_encoding_error_message(input_p),
74
+ )
75
+ except json.JSONDecodeError:
76
+ log_error(
77
+ logger,
78
+ f"The provided file is not valid JSON: {input_p}. Please provide a valid JSON file.",
79
+ )
80
+
81
+
82
+ def check_overwrite(output: Path, overwrite: bool):
83
+ """Exit program gracefully if an output file already exists but --overwrite has not been set."""
84
+ if output.exists() and not overwrite:
85
+ raise typer.Exit(
86
+ typer.style(
87
+ f"Output file {output} already exists. Use --overwrite or -f to overwrite.",
88
+ fg=typer.colors.RED,
89
+ )
90
+ )
91
+
92
+
93
+ def save_jsonld(data: dict, filename: Path):
94
+ with open(filename, "w") as f:
95
+ f.write(json.dumps(data, indent=2))
96
+ logger.info(f"Saved output to: {filename}")