def __convertDwi(self, sequence, session, target): """ Convert a dwi dicom images into nifti the name of the resulting image will be: prefix_subjectName.nii.gz """ dwi = util.buildName(self.__configParser, target, sequence.getPrefix().getValue(), session.getName(),'nii.gz') bEnc = util.buildName(self.__configParser, target, dwi, None, "b") bvals = util.buildName(self.__configParser, target, dwi, None, "bvals") bvecs = util.buildName(self.__configParser, target, dwi, None, "bvecs") cmd = "mrconvert {} {} -force -export_grad_mrtrix {} -export_grad_fsl {} {}"\ .format(sequence.getEscapedDirectory(), dwi, bEnc, bvecs, bvals) if not self.__arguments.noStride: cmd += " -stride 1,2,3,4 " print cmd util.launchCommand(cmd) if not self.__arguments.noConfig: dicoms = glob.glob("{}/*.dcm".format(sequence.getDirectory())) if len(dicoms) > 0: toadinfo = Toadinfo(dicoms.pop()) toadinfo.writeToadConfig( self.__configFilename) util.launchCommand(cmd)
def __convertDwi(self, sequence, session, target): """ Convert a dwi dicomparser images into nifti the name of the resulting image will be: prefix_subjectName.nii.gz """ dwi = util.buildName(self.__configParser, target, sequence.getPrefix().getValue(), session.getName(), 'nii.gz') bEnc = util.buildName(self.__configParser, target, dwi, None, "b") bvals = util.buildName(self.__configParser, target, dwi, None, "bvals") bvecs = util.buildName(self.__configParser, target, dwi, None, "bvecs") cmd = "mrconvert {} {} -force -export_grad_mrtrix {} -export_grad_fsl {} {}"\ .format(sequence.getEscapedDirectory(), dwi, bEnc, bvecs, bvals) if not self.__arguments.noStride: cmd += " -stride 1,2,3,4 " print cmd util.launchCommand(cmd) if not self.__arguments.noConfig: dicoms = glob.glob("{}/*.dcm".format(sequence.getDirectory())) if len(dicoms) > 0: toadinfo = Toadinfo(dicoms.pop()) toadinfo.writeToadConfig(self.__configFilename)
def __convert(self, sequence, session, target): filename = util.buildName(self.__configParser, target, sequence.getPrefix().getValue(), session.getName(), 'nii.gz') cmd = "mrconvert {0} {1} -force ".format(sequence.getEscapedDirectory(), filename) if not self.__arguments.noStride: cmd += " -stride 1,2,3 " if sequence.getPrefix().getName() == 'phase': cmd += " -datatype float32 " print cmd util.launchCommand(cmd)
def __convertMagnitude(self, sequence, session, target): """ Convert a magnitude fieldmap dicomparser images into nifti the name of the resulting image will be: prefix_subjectName.nii.gz """ def __setMagnitudeFieldmapInConfigFiles(echo1, echo2): """ write magnitude image properties into a config file Args: configFile: a config file echo1: the echo time of the first magnitude map echo2: the echo time of the secong magnitude map """ if os.path.exists(self.__configFilename): self.__configParser.read(self.__configFilename) if not self.__configParser.has_section("correction"): self.__configParser.add_section('correction') self.__configParser.set('correction', "echo_time_mag1", echo1) self.__configParser.set('correction', "echo_time_mag2", echo2) with open(self.__configFilename, 'w') as w: self.__configParser.write(w) values = [] pattern = os.path.join(session.getDirectory(), os.path.dirname(sequence.getName()), 'echo_*') for directory in glob.glob(pattern): values.append( (os.path.basename(directory).strip('echo_'), directory)) try: echo1 = float(values[0][0]) echo2 = float(values[1][0]) if not self.__arguments.noConfig: if echo1 > echo2: __setMagnitudeFieldmapInConfigFiles(echo2, echo1) else: __setMagnitudeFieldmapInConfigFiles(echo1, echo2) except IndexError: return filename = util.buildName(self.__configParser, target, sequence.getPrefix().getValue(), session.getName(), 'nii.gz') cmd = "mrconvert {0} {1} -force ".format( sequence.getEscapedDirectory(), filename) if not self.__arguments.noStride: cmd += " -stride 1,2,3 " print cmd util.launchCommand(cmd)
def __convertMagnitude(self, sequence, session, target): """ Convert a magnitude fieldmap dicom images into nifti the name of the resulting image will be: prefix_subjectName.nii.gz """ def __setMagnitudeFieldmapInConfigFiles(echo1, echo2): """ write magnitude image properties into a config file Args: configFile: a config file echo1: the echo time of the first magnitude map echo2: the echo time of the secong magnitude map """ if os.path.exists(self.__configFilename): self.__configParser.read(self.__configFilename) if not self.__configParser.has_section("correction"): self.__configParser.add_section('correction') self.__configParser.set('correction', "echo_time_mag1", echo1) self.__configParser.set('correction', "echo_time_mag2", echo2) with open(self.__configFilename,'w') as w: self.__configParser.write(w) values = [] pattern = os.path.join(session.getDirectory(), os.path.dirname(sequence.getName()),'echo_*') for directory in glob.glob(pattern): values.append((os.path.basename(directory).strip('echo_'), directory)) try: echo1 = float(values[0][0]) echo2 = float(values[1][0]) if not self.__arguments.noConfig: if echo1 > echo2: __setMagnitudeFieldmapInConfigFiles(echo2, echo1) else: __setMagnitudeFieldmapInConfigFiles(echo1, echo2) except IndexError: return filename = util.buildName(self.__configParser, target, sequence.getPrefix().getValue(), session.getName(), 'nii.gz') cmd = "mrconvert {0} {1} -force ".format(sequence.getEscapedDirectory(), filename) if not self.__arguments.noStride: cmd += " -stride 1,2,3 " print cmd util.launchCommand(cmd)
def launchCommand(self, cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=None, nice=0): """Execute a program in a new process Args: command: a string representing a unix command to execute stdout: this attribute is a file object that provides output from the child process stderr: this attribute is a file object that provides error from the child process timeout: Number of seconds before a process is consider inactive, usefull against deadlock nice: run cmd with an adjusted niceness, which affects process scheduling Returns return a 3 elements tuples representing the command execute, the standards output and the standard error message Raises OSError: the function trying to execute a non-existent file. ValueError : the command line is called with invalid arguments """ binary = cmd.split(" ").pop(0) if util.which(binary) is None: self.error("Command {} not found".format(binary)) self.info("Launch {} command line...".format(binary)) self.info("Command line submit: {}".format(cmd)) (executedCmd, output, error)= util.launchCommand(cmd, stdout, stderr, timeout, nice) if not (output is "" or output is "None" or output is None): self.info("Output produce by {}: {} \n".format(binary, output)) if not (error is '' or error is "None" or error is None): self.info("Error produce by {}: {}\n".format(binary, error)) self.info("------------------------\n")
def __convert(self, sequence, session, target): filename = util.buildName(self.__configParser, target, sequence.getPrefix().getValue(), session.getName(), 'nii.gz') cmd = "mrconvert {0} {1} -force ".format(sequence.getEscapedDirectory(), filename) if not self.__arguments.noStride: cmd += " -stride 1,2,3 " if sequence.getPrefix().getName() == 'phase': cmd += " -datatype float32 " print cmd util.launchCommand(cmd) if not self.__arguments.noConfig: dicoms = glob.glob("{}/*.dcm".format(sequence.getDirectory())) if len(dicoms) > 0: toadinfo = Toadinfo(dicoms.pop()) toadinfo.writeToadConfig(self.__configFilename)
def __convert(self, sequence, session, target): filename = util.buildName(self.__configParser, target, sequence.getPrefix().getValue(), session.getName(), 'nii.gz') cmd = "mrconvert {0} {1} -force ".format( sequence.getEscapedDirectory(), filename) if not self.__arguments.noStride: cmd += " -stride 1,2,3 " if sequence.getPrefix().getName() == 'phase': cmd += " -datatype float32 " print cmd util.launchCommand(cmd) if not self.__arguments.noConfig: dicoms = glob.glob("{}/*.dcm".format(sequence.getDirectory())) if len(dicoms) > 0: toadinfo = Toadinfo(dicoms.pop()) toadinfo.writeToadConfig(self.__configFilename)
def launchCommand(self, cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, nice=0): """Execute a program in a new process Args: command: a string representing a unix command to execute stdout: this attribute is a file object that provides output from the child process stderr: this attribute is a file object that provides error from the child process nice: run cmd with an adjusted niceness, which affects process scheduling Returns return a 3 elements tuples representing the command execute, the standards output and the standard error message Raises OSError: the function trying to execute a non-existent file. ValueError : the command line is called with invalid arguments """ binary = cmd.split(" ").pop(0) if util.which(binary) is None: self.error("Command {} not found".format(binary)) self.info("Launch {} command line...\n".format(binary)) self.info("Command line submit: {}\n".format(cmd)) out = None err = None if stdout == 'log': out = self.getLog() self.info("Output will be log in {} \n".format(out.name)) if stderr == 'log': err = self.getLog() self.info("Error will be log in {} \n".format(err.name)) (output, error) = util.launchCommand(cmd, out, err, nice) if stdout is not "None": self.info("Output produce by {}: {} \n".format(binary, output)) if error != '' or error != "None": self.info("Error produce by {}: {}\n".format(binary, error)) self.info("------------------------\n")
def launchCommand(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=None, nice=0): import sys sys.path.append('/home/grg/toad') from lib import util """Execute a program in a new process Args: command: a string representing a unix command to execute stdout: this attribute is a file object that provides output from the child process stderr: this attribute is a file object that provides error from the child process timeout: Number of seconds before a process is consider inactive, usefull against deadlock nice: run cmd with an adjusted niceness, which affects process scheduling Returns return a 3 elements tuples representing the command execute, the standards output and the standard error message Raises OSError: the function trying to execute a non-existent file. ValueError : the command line is called with invalid arguments """ binary = cmd.split(" ").pop(0) if util.which(binary) is None: print("Command {} not found".format(binary)) print("Launch {} command line...".format(binary)) print("Command line submit: {}".format(cmd)) (executedCmd, output, error) = util.launchCommand(cmd, stdout, stderr, timeout, nice) if not (output is "" or output is "None" or output is None): print("Output produce by {}: {} \n".format(binary, output)) if not (error is '' or error is "None" or error is None): print("Error produce by {}: {}\n".format(binary, error))
def frames2Gif(frames, target, gifSpeed): cmd = 'convert -delay {} '.format(str(gifSpeed)) for frame in frames: cmd += '{} '.format(frame.name) cmd += target util.launchCommand(cmd)