Skip to content

Module dcm2bids.dcm2niix⚓︎

Dcm2niix class

None

View Source
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-

"""Dcm2niix class"""

import logging

import os

from pathlib import Path

import shlex

import shutil

from glob import glob

from .utils import DEFAULT, run_shell_command

class Dcm2niix(object):

    """ Object to handle dcm2niix execution

    Args:

        dicomDirs (list): A list of folder with dicoms to convert

        bidsDir (str): A path to the root BIDS directory

        participant: Optional Participant object

        options (str): Optional arguments for dcm2niix

    Properties:

        sidecars (list): A list of sidecar path created by dcm2niix

    """

    def __init__(

        self, dicomDirs, bidsDir, participant=None, options=DEFAULT.dcm2niixOptions

    ):

        self.logger = logging.getLogger(__name__)

        self.sidecarsFiles = []

        self.dicomDirs = dicomDirs

        self.bidsDir = bidsDir

        self.participant = participant

        self.options = options

    @property

    def outputDir(self):

        """

        Returns:

            A directory to save all the output files of dcm2niix

        """

        tmpDir = self.participant.prefix if self.participant else DEFAULT.helperDir

        return self.bidsDir / DEFAULT.tmpDirName / tmpDir

    def run(self, force=False):

        """ Run dcm2niix if necessary

        Args:

            force (boolean): Forces a cleaning of a previous execution of

                             dcm2niix

        Sets:

            sidecarsFiles (list): A list of sidecar path created by dcm2niix

        """

        try:

            oldOutput = os.listdir(self.outputDir) != []

        except:

            oldOutput = False

        if oldOutput and force:

            self.logger.warning("Previous dcm2niix directory output found:")

            self.logger.warning(self.outputDir)

            self.logger.warning("'force' argument is set to True")

            self.logger.warning("Cleaning the previous directory and running dcm2niix")

            shutil.rmtree(self.outputDir, ignore_errors=True)

            # os.makedirs(self.outputDir, exist_ok=True)

            # python2 compatibility

            if not os.path.exists(self.outputDir):

                os.makedirs(self.outputDir)

            self.execute()

        elif oldOutput:

            self.logger.warning("Previous dcm2niix directory output found:")

            self.logger.warning(self.outputDir)

            self.logger.warning("Use --forceDcm2niix to rerun dcm2niix")

        else:

            # os.makedirs(self.outputDir, exist_ok=True)

            # python2 compatibility

            if not os.path.exists(self.outputDir):

                os.makedirs(self.outputDir)

            self.execute()

        self.sidecarFiles = glob(os.path.join(self.outputDir, "*.json"))

    def execute(self):

        """ Execute dcm2niix for each directory in dicomDirs

        """

        for dicomDir in self.dicomDirs:

            cmd = ['dcm2niix', *shlex.split(self.options),

                   '-o', self.outputDir, dicomDir]

            output = run_shell_command(cmd)

            try:

                output = output.decode()

            except:

                pass

            self.logger.debug("\n%s", output)

            self.logger.info("Check log file for dcm2niix output")

Classes⚓︎

Dcm2niix⚓︎

1
2
3
4
5
6
class Dcm2niix(
    dicomDirs,
    bidsDir,
    participant=None,
    options="-b y -ba y -z y -f '%3s_%f_%p_%t'"
)

Attributes⚓︎

Name Type Description Default
dicomDirs list A list of folder with dicoms to convert None
bidsDir str A path to the root BIDS directory None
participant None Optional Participant object None
options str Optional arguments for dcm2niix
Properties: None
sidecars list A list of sidecar path created by dcm2niix None
View Source
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class Dcm2niix(object):

    """ Object to handle dcm2niix execution

    Args:

        dicomDirs (list): A list of folder with dicoms to convert

        bidsDir (str): A path to the root BIDS directory

        participant: Optional Participant object

        options (str): Optional arguments for dcm2niix

    Properties:

        sidecars (list): A list of sidecar path created by dcm2niix

    """

    def __init__(

        self, dicomDirs, bidsDir, participant=None, options=DEFAULT.dcm2niixOptions

    ):

        self.logger = logging.getLogger(__name__)

        self.sidecarsFiles = []

        self.dicomDirs = dicomDirs

        self.bidsDir = bidsDir

        self.participant = participant

        self.options = options

    @property

    def outputDir(self):

        """

        Returns:

            A directory to save all the output files of dcm2niix

        """

        tmpDir = self.participant.prefix if self.participant else DEFAULT.helperDir

        return self.bidsDir / DEFAULT.tmpDirName / tmpDir

    def run(self, force=False):

        """ Run dcm2niix if necessary

        Args:

            force (boolean): Forces a cleaning of a previous execution of

                             dcm2niix

        Sets:

            sidecarsFiles (list): A list of sidecar path created by dcm2niix

        """

        try:

            oldOutput = os.listdir(self.outputDir) != []

        except:

            oldOutput = False

        if oldOutput and force:

            self.logger.warning("Previous dcm2niix directory output found:")

            self.logger.warning(self.outputDir)

            self.logger.warning("'force' argument is set to True")

            self.logger.warning("Cleaning the previous directory and running dcm2niix")

            shutil.rmtree(self.outputDir, ignore_errors=True)

            # os.makedirs(self.outputDir, exist_ok=True)

            # python2 compatibility

            if not os.path.exists(self.outputDir):

                os.makedirs(self.outputDir)

            self.execute()

        elif oldOutput:

            self.logger.warning("Previous dcm2niix directory output found:")

            self.logger.warning(self.outputDir)

            self.logger.warning("Use --forceDcm2niix to rerun dcm2niix")

        else:

            # os.makedirs(self.outputDir, exist_ok=True)

            # python2 compatibility

            if not os.path.exists(self.outputDir):

                os.makedirs(self.outputDir)

            self.execute()

        self.sidecarFiles = glob(os.path.join(self.outputDir, "*.json"))

    def execute(self):

        """ Execute dcm2niix for each directory in dicomDirs

        """

        for dicomDir in self.dicomDirs:

            cmd = ['dcm2niix', *shlex.split(self.options),

                   '-o', self.outputDir, dicomDir]

            output = run_shell_command(cmd)

            try:

                output = output.decode()

            except:

                pass

            self.logger.debug("\n%s", output)

            self.logger.info("Check log file for dcm2niix output")

Instance variables⚓︎

1
outputDir

Returns:

A directory to save all the output files of dcm2niix

Methods⚓︎

execute⚓︎

1
2
3
def execute(
    self
)

Execute dcm2niix for each directory in dicomDirs

View Source
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    def execute(self):

        """ Execute dcm2niix for each directory in dicomDirs

        """

        for dicomDir in self.dicomDirs:

            cmd = ['dcm2niix', *shlex.split(self.options),

                   '-o', self.outputDir, dicomDir]

            output = run_shell_command(cmd)

            try:

                output = output.decode()

            except:

                pass

            self.logger.debug("\n%s", output)

            self.logger.info("Check log file for dcm2niix output")

run⚓︎

1
2
3
4
def run(
    self,
    force=False
)

Run dcm2niix if necessary

Parameters:

Name Type Description Default
force boolean Forces a cleaning of a previous execution of
dcm2niix

Sets: | None | | sidecarsFiles | list | A list of sidecar path created by dcm2niix | None |

View Source
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    def run(self, force=False):

        """ Run dcm2niix if necessary

        Args:

            force (boolean): Forces a cleaning of a previous execution of

                             dcm2niix

        Sets:

            sidecarsFiles (list): A list of sidecar path created by dcm2niix

        """

        try:

            oldOutput = os.listdir(self.outputDir) != []

        except:

            oldOutput = False

        if oldOutput and force:

            self.logger.warning("Previous dcm2niix directory output found:")

            self.logger.warning(self.outputDir)

            self.logger.warning("'force' argument is set to True")

            self.logger.warning("Cleaning the previous directory and running dcm2niix")

            shutil.rmtree(self.outputDir, ignore_errors=True)

            # os.makedirs(self.outputDir, exist_ok=True)

            # python2 compatibility

            if not os.path.exists(self.outputDir):

                os.makedirs(self.outputDir)

            self.execute()

        elif oldOutput:

            self.logger.warning("Previous dcm2niix directory output found:")

            self.logger.warning(self.outputDir)

            self.logger.warning("Use --forceDcm2niix to rerun dcm2niix")

        else:

            # os.makedirs(self.outputDir, exist_ok=True)

            # python2 compatibility

            if not os.path.exists(self.outputDir):

                os.makedirs(self.outputDir)

            self.execute()

        self.sidecarFiles = glob(os.path.join(self.outputDir, "*.json"))

Last update: 2022-09-30
Back to top