#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Jan 31 18:22:05 2018 @author: andche """ from nipype.interfaces.base import TraitedSpec, BaseInterface, BaseInterfaceInputSpec, File, traits, isdefined, CommandLine, CommandLineInputSpec import os from nipype.utils.filemanip import split_filename class DecodingMatShellInputSpec(CommandLineInputSpec): matlab_path = traits.String('/opt/matlab/R2017b/', desc='Path to MATLAB', useDefault=True, mandatory=True, position = 0, argstr="%s") beh_file = File(exists=True, mandatory=True, desc = 'Behavioral data file', position = 1, argstr="%s") samples_file = File(exists=True, mandatory=True, desc = 'Samples file', position = 2, argstr="%s") out_file = traits.File(desc = 'Output file', position = 3, genfile = True, argstr="%s", keep_extension=False) test_run = traits.Int(desc = 'Test run to use for testing', mandatory = False, position = 4, argstr="%s") pre_init_n = traits.Int(desc = 'n voxels to use for pre-initialization', position = 5, mandatory = False, argstr="%s") feature_space = traits.String(desc = 'type of the feature space: 180 or 360', position = 6, mandatory = False, argstr="%s") log_file = traits.File(desc = 'Log file', position = 7, genfile = True, argstr="%s", keep_extension=False) nsamples = traits.String(desc = 'number of samples to draw', position = 8, mandatory = False, argstr="%s") #out_dir = traits.Directory(desc = 'Output directory') class DecodingMatShellOutputSpec(TraitedSpec): out_file = File(exists=True) log_file = File(exists=True) class DecodingMatShell(CommandLine): input_spec = DecodingMatShellInputSpec output_spec = DecodingMatShellOutputSpec _cmd = '/home/visual/andche/DecodeSNC_sample/decodeSNC_sample_2017b/run_runDecoding.sh' #_cmd = '/home/visual/andche/DecodeSNC_sample/run_runDecoding.sh' def _gen_filename(self, name): if hasattr(self.inputs,name) and isdefined(getattr(self.inputs, name)): return getattr(self.inputs, name) if name == 'out_file': path, base, _ = split_filename(self.inputs.samples_file) return os.path.abspath(os.path.join(os.getcwd(), '%s_testrun_%d_out.mat' % (base, self.inputs.test_run))) elif name == 'log_file': path, base, _ = split_filename(self.inputs.samples_file) return os.path.abspath(os.path.join(os.getcwd(), '%s_testrun_%d_log.txt' % (base, self.inputs.test_run))) def _list_outputs(self): outputs = self._outputs().get() print(outputs) import os.path out_file = self._gen_filename('out_file') log_file = self._gen_filename('log_file') # check if file could be loaded from scipy.io import loadmat try: loadmat(out_file) except: raise ValueError('Decoding failed, output file exists but cannot be loaded. Filename: %s' % out_file) outputs['out_file'] = out_file outputs['log_file'] = log_file print(os.path.isfile(out_file) ) return outputs