From fc2ae78ba776e3a46a7142b50e29a230bf8347dc Mon Sep 17 00:00:00 2001 From: Samuel Guay Date: Tue, 13 Jun 2023 15:30:26 -0400 Subject: [PATCH 1/6] fix README URL --- dcm2bids/cli/dcm2bids_scaffold.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dcm2bids/cli/dcm2bids_scaffold.py b/dcm2bids/cli/dcm2bids_scaffold.py index 58824e4e..d0a42c1e 100644 --- a/dcm2bids/cli/dcm2bids_scaffold.py +++ b/dcm2bids/cli/dcm2bids_scaffold.py @@ -66,7 +66,7 @@ def main(): # README try: run_shell_command(['wget', '-q', '-O', opj(args.output_dir, "README"), - 'https://github.com/bids-standard/bids-starter-kit/blob/main/templates/README.MD']) + 'https://raw.githubusercontent.com/bids-standard/bids-starter-kit/main/templates/README.MD']) except: write_txt(opj(args.output_dir, "README"), bids_starter_kit.README) From eed5980b6d4910afa3f401476fa06f69dddb279f Mon Sep 17 00:00:00 2001 From: Samuel Guay Date: Tue, 13 Jun 2023 15:42:42 -0400 Subject: [PATCH 2/6] fix write instead of append --- dcm2bids/utils/io.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dcm2bids/utils/io.py b/dcm2bids/utils/io.py index 8e694955..6b5ea767 100644 --- a/dcm2bids/utils/io.py +++ b/dcm2bids/utils/io.py @@ -30,10 +30,9 @@ def save_json(filename, data): def write_txt(filename, lines): - with open(filename, "a+") as f: + with open(filename, "w") as f: f.write(f"{lines}\n") - def valid_path(in_path, type="folder"): """Assert that file exists. From f2275dd9539ea73227df2591d0db8941eaf224b1 Mon Sep 17 00:00:00 2001 From: Samuel Guay Date: Thu, 15 Jun 2023 15:12:04 -0400 Subject: [PATCH 3/6] =?UTF-8?q?improve=20log=20of=20scaffold,=20including?= =?UTF-8?q?=20a=20=F0=9F=8C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dcm2bids/cli/dcm2bids_scaffold.py | 23 ++++++++---- dcm2bids/utils/utils.py | 60 ++++++++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 11 deletions(-) diff --git a/dcm2bids/cli/dcm2bids_scaffold.py b/dcm2bids/cli/dcm2bids_scaffold.py index d0a42c1e..036879a5 100644 --- a/dcm2bids/cli/dcm2bids_scaffold.py +++ b/dcm2bids/cli/dcm2bids_scaffold.py @@ -13,12 +13,14 @@ import datetime import logging import os +import sys from os.path import join as opj - +from pathlib import Path from dcm2bids.utils.io import write_txt from dcm2bids.utils.args import add_overwrite_arg, assert_dirs_empty -from dcm2bids.utils.utils import DEFAULT, run_shell_command +from dcm2bids.utils.utils import DEFAULT, run_shell_command, TreePrinter from dcm2bids.utils.scaffold import bids_starter_kit +from dcm2bids.utils.logger import setup_logging def _build_arg_parser(): @@ -38,18 +40,22 @@ def main(): parser = _build_arg_parser() args = parser.parse_args() + setup_logging("info") + logger = logging.getLogger(__name__) + assert_dirs_empty(parser, args, args.output_dir) for _ in ["code", "derivatives", "sourcedata"]: os.makedirs(opj(args.output_dir, _), exist_ok=True) - logging.info("The files used to create your BIDS directory comes from" - "https://github.com/bids-standard/bids-starter-kit") + logger.info("Currently running the following command: \n" + " ".join(sys.argv) + "\n") + logger.info("The files used to create your BIDS directory were taken from " + "https://github.com/bids-standard/bids-starter-kit. \n") + # CHANGES write_txt(opj(args.output_dir, "CHANGES"), bids_starter_kit.CHANGES.replace('DATE', datetime.date.today().strftime("%Y-%m-%d"))) - # dataset_description write_txt(opj(args.output_dir, "dataset_description"), bids_starter_kit.dataset_description.replace("BIDS_VERSION", @@ -66,11 +72,14 @@ def main(): # README try: run_shell_command(['wget', '-q', '-O', opj(args.output_dir, "README"), - 'https://raw.githubusercontent.com/bids-standard/bids-starter-kit/main/templates/README.MD']) - except: + 'https://raw.githubusercontent.com/bids-standard/bids-starter-kit/main/templates/README.MD'], log = False) + except Exception: write_txt(opj(args.output_dir, "README"), bids_starter_kit.README) + # output tree representation of where the scaffold was built. + + TreePrinter(args.output_dir).print_tree() if __name__ == "__main__": main() diff --git a/dcm2bids/utils/utils.py b/dcm2bids/utils/utils.py index b4aed687..ab08f7a5 100644 --- a/dcm2bids/utils/utils.py +++ b/dcm2bids/utils/utils.py @@ -112,16 +112,16 @@ def splitext_(path, extensions=None): return os.path.splitext(path) -def run_shell_command(commandLine): +def run_shell_command(commandLine, log = True): """ Wrapper of subprocess.check_output Returns: Run command with arguments and return its output """ - logger = logging.getLogger(__name__) - logger.info("Running %s", commandLine) + if log: + logger = logging.getLogger(__name__) + logger.info("Running: %s", " ".join(commandLine)) return check_output(commandLine) - def convert_dir(dir): """ Convert Direction Args: @@ -131,3 +131,55 @@ def convert_dir(dir): str: direction - bids format """ return DEFAULT.entity_dir[dir] +class TreePrinter: + """ + Generates and prints a tree representation of a given a directory. + """ + BRANCH = "│" + LAST = "└──" + JUNCTION = "├──" + BRANCH_PREFIX = "│ " + SPACE = " " + + def __init__(self, root_dir): + self.root_dir = Path(root_dir) + + def print_tree(self): + """ + Prints the tree representation of the root directory and + its subdirectories and files. + """ + tree = self._generate_tree(self.root_dir) + logger = logging.getLogger(__name__) + logger.info(f"Tree representation of {self.root_dir}{os.sep}") + logger.info(f"{self.root_dir}{os.sep}") + for item in tree: + logger.info(item) + + def _generate_tree(self, directory, prefix=""): + """ + Generates the tree representation of the recursively. + + Parameters: + - directory: Path + The directory for which a tree representation is needed. + - prefix: str + The prefix to be added to each entry in the tree. + + Returns a list of strings representing the tree. + """ + tree = [] + entries = sorted(directory.iterdir(), key = lambda path: str(path).lower()) + entries = sorted(entries, key=lambda entry: entry.is_file()) + entries_count = len(entries) + + for index, entry in enumerate(entries): + connector = self.LAST if index == entries_count - 1 else self.JUNCTION + if entry.is_dir(): + sub_tree = self._generate_tree(entry, prefix=prefix + + (self.BRANCH_PREFIX if index != entries_count - 1 else self.SPACE)) + tree.append(f"{prefix}{connector} {entry.name}{os.sep}") + tree.extend(sub_tree) + else: + tree.append(f"{prefix}{connector} {entry.name}") + return tree From 9cbd2809939a9e8686f78ce492e18b1b8b289762 Mon Sep 17 00:00:00 2001 From: Samuel Guay Date: Wed, 21 Jun 2023 00:25:54 -0400 Subject: [PATCH 4/6] add .bidsignore with tmp_dcm2bids --- dcm2bids/cli/dcm2bids_scaffold.py | 4 ++++ funfun/.bidsignore | 1 + 2 files changed, 5 insertions(+) create mode 100644 funfun/.bidsignore diff --git a/dcm2bids/cli/dcm2bids_scaffold.py b/dcm2bids/cli/dcm2bids_scaffold.py index 036879a5..96e087bd 100644 --- a/dcm2bids/cli/dcm2bids_scaffold.py +++ b/dcm2bids/cli/dcm2bids_scaffold.py @@ -69,6 +69,10 @@ def main(): write_txt(opj(args.output_dir, "participants.tsv"), bids_starter_kit.participants_tsv) + # .bidsignore + write_txt(opj(args.output_dir, ".bidsignore"), + "tmp_dcm2bids") + # README try: run_shell_command(['wget', '-q', '-O', opj(args.output_dir, "README"), diff --git a/funfun/.bidsignore b/funfun/.bidsignore new file mode 100644 index 00000000..02646fba --- /dev/null +++ b/funfun/.bidsignore @@ -0,0 +1 @@ +tmp_dcm2bids From 7737e367afd10a4e0bc09113520bf6350f927e96 Mon Sep 17 00:00:00 2001 From: Samuel Guay Date: Fri, 23 Jun 2023 05:20:46 -0400 Subject: [PATCH 5/6] pep8ed --- dcm2bids/cli/dcm2bids_scaffold.py | 13 +++++++++---- dcm2bids/utils/io.py | 6 +----- dcm2bids/utils/utils.py | 17 +++++++++++++---- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/dcm2bids/cli/dcm2bids_scaffold.py b/dcm2bids/cli/dcm2bids_scaffold.py index 96e087bd..4c5609c3 100644 --- a/dcm2bids/cli/dcm2bids_scaffold.py +++ b/dcm2bids/cli/dcm2bids_scaffold.py @@ -15,7 +15,6 @@ import os import sys from os.path import join as opj -from pathlib import Path from dcm2bids.utils.io import write_txt from dcm2bids.utils.args import add_overwrite_arg, assert_dirs_empty from dcm2bids.utils.utils import DEFAULT, run_shell_command, TreePrinter @@ -48,14 +47,18 @@ def main(): for _ in ["code", "derivatives", "sourcedata"]: os.makedirs(opj(args.output_dir, _), exist_ok=True) - logger.info("Currently running the following command: \n" + " ".join(sys.argv) + "\n") + logger.info("Currently running the following command: \n" + + " ".join(sys.argv) + "\n") logger.info("The files used to create your BIDS directory were taken from " "https://github.com/bids-standard/bids-starter-kit. \n") # CHANGES write_txt(opj(args.output_dir, "CHANGES"), bids_starter_kit.CHANGES.replace('DATE', - datetime.date.today().strftime("%Y-%m-%d"))) + datetime.date.today().strftime( + "%Y-%m-%d") + ) + ) # dataset_description write_txt(opj(args.output_dir, "dataset_description"), bids_starter_kit.dataset_description.replace("BIDS_VERSION", @@ -76,7 +79,8 @@ def main(): # README try: run_shell_command(['wget', '-q', '-O', opj(args.output_dir, "README"), - 'https://raw.githubusercontent.com/bids-standard/bids-starter-kit/main/templates/README.MD'], log = False) + 'https://raw.githubusercontent.com/bids-standard/bids-starter-kit/main/templates/README.MD'], + log=False) except Exception: write_txt(opj(args.output_dir, "README"), bids_starter_kit.README) @@ -85,5 +89,6 @@ def main(): TreePrinter(args.output_dir).print_tree() + if __name__ == "__main__": main() diff --git a/dcm2bids/utils/io.py b/dcm2bids/utils/io.py index 6b5ea767..9e180761 100644 --- a/dcm2bids/utils/io.py +++ b/dcm2bids/utils/io.py @@ -1,14 +1,9 @@ # -*- coding: utf-8 -*- -import inspect import json -import os -import os.path as opj from pathlib import Path from collections import OrderedDict -import dcm2bids - def load_json(filename): """ Load a JSON file @@ -33,6 +28,7 @@ def write_txt(filename, lines): with open(filename, "w") as f: f.write(f"{lines}\n") + def valid_path(in_path, type="folder"): """Assert that file exists. diff --git a/dcm2bids/utils/utils.py b/dcm2bids/utils/utils.py index ab08f7a5..815ef8b4 100644 --- a/dcm2bids/utils/utils.py +++ b/dcm2bids/utils/utils.py @@ -112,7 +112,7 @@ def splitext_(path, extensions=None): return os.path.splitext(path) -def run_shell_command(commandLine, log = True): +def run_shell_command(commandLine, log=True): """ Wrapper of subprocess.check_output Returns: Run command with arguments and return its output @@ -122,6 +122,7 @@ def run_shell_command(commandLine, log = True): logger.info("Running: %s", " ".join(commandLine)) return check_output(commandLine) + def convert_dir(dir): """ Convert Direction Args: @@ -131,10 +132,13 @@ def convert_dir(dir): str: direction - bids format """ return DEFAULT.entity_dir[dir] + + class TreePrinter: """ Generates and prints a tree representation of a given a directory. """ + BRANCH = "│" LAST = "└──" JUNCTION = "├──" @@ -169,15 +173,20 @@ def _generate_tree(self, directory, prefix=""): Returns a list of strings representing the tree. """ tree = [] - entries = sorted(directory.iterdir(), key = lambda path: str(path).lower()) + entries = sorted(directory.iterdir(), key=lambda path: str(path).lower()) entries = sorted(entries, key=lambda entry: entry.is_file()) entries_count = len(entries) for index, entry in enumerate(entries): connector = self.LAST if index == entries_count - 1 else self.JUNCTION if entry.is_dir(): - sub_tree = self._generate_tree(entry, prefix=prefix + - (self.BRANCH_PREFIX if index != entries_count - 1 else self.SPACE)) + sub_tree = self._generate_tree( + entry, + prefix=prefix + + ( + self.BRANCH_PREFIX if index != entries_count - 1 else self.SPACE + ), + ) tree.append(f"{prefix}{connector} {entry.name}{os.sep}") tree.extend(sub_tree) else: From 62d5e44fe446a720e8fc8f55697ab4e41ba5286c Mon Sep 17 00:00:00 2001 From: Samuel Guay Date: Fri, 23 Jun 2023 06:35:57 -0400 Subject: [PATCH 6/6] remove test file that slipped into previous commit --- funfun/.bidsignore | 1 - 1 file changed, 1 deletion(-) delete mode 100644 funfun/.bidsignore diff --git a/funfun/.bidsignore b/funfun/.bidsignore deleted file mode 100644 index 02646fba..00000000 --- a/funfun/.bidsignore +++ /dev/null @@ -1 +0,0 @@ -tmp_dcm2bids