|
| 1 | +from nipype.interfaces.r import RCommand |
| 2 | +from nipype.interfaces.base import ( |
| 3 | + TraitedSpec, |
| 4 | + BaseInterface, |
| 5 | + BaseInterfaceInputSpec, |
| 6 | + File, |
| 7 | + traits, |
| 8 | +) |
| 9 | +import os |
| 10 | +import tempfile |
| 11 | +from string import Template |
| 12 | + |
| 13 | + |
| 14 | +class WhiteStripeInputSpec(BaseInterfaceInputSpec): |
| 15 | + in_file = File(exists=True, mandatory=True) |
| 16 | + out_file = File("out.nii.gz", usedefault=True) |
| 17 | + indices = traits.Array(desc="WhiteStripe indices", mandatory=False) |
| 18 | + img_type = traits.String( |
| 19 | + desc="WhiteStripe image type", mandatory=False, default="T1" |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +class WhiteStripeOutputSpec(TraitedSpec): |
| 24 | + out_file = File(exists=True) |
| 25 | + |
| 26 | + |
| 27 | +class WhiteStripe(BaseInterface): |
| 28 | + input_spec = WhiteStripeInputSpec |
| 29 | + output_spec = WhiteStripeOutputSpec |
| 30 | + |
| 31 | + def _run_interface(self, runtime): |
| 32 | + d = dict( |
| 33 | + in_file=self.inputs.in_file, |
| 34 | + out_file=self.inputs.out_file, |
| 35 | + img_type=self.inputs.img_type, |
| 36 | + ) |
| 37 | + if len(self.inputs.indices) == 0: |
| 38 | + tmpfile = False |
| 39 | + script = Template( |
| 40 | + """ |
| 41 | + library(neurobase) |
| 42 | + library(WhiteStripe) |
| 43 | + in_file = readnii('$in_file') |
| 44 | + ind = whitestripe(in_file, "$img_type")$$whitestripe.ind |
| 45 | + norm = whitestripe_norm(in_file, ind) |
| 46 | + out_file = '$out_file' |
| 47 | + writenii(norm, out_file) |
| 48 | + """ |
| 49 | + ).substitute(d) |
| 50 | + else: |
| 51 | + # d['indices'] = ",".join(map(str, self.inputs.indices)) |
| 52 | + tmpfile = tempfile.mkstemp()[1] |
| 53 | + self._write_indices(tmpfile, self.inputs.indices) |
| 54 | + d["indices"] = tmpfile |
| 55 | + script = Template( |
| 56 | + """ |
| 57 | + library(neurobase) |
| 58 | + library(WhiteStripe) |
| 59 | + in_file = readnii('$in_file') |
| 60 | + # ind = c($indices) |
| 61 | + ind = as.vector(read.table("$indices")[[1]], mode='numeric') |
| 62 | + norm = whitestripe_norm(in_file, ind) |
| 63 | + out_file = '$out_file' |
| 64 | + writenii(norm, out_file) |
| 65 | + """ |
| 66 | + ).substitute(d) |
| 67 | + |
| 68 | + # rfile = True will create a .R file with your script and executed. |
| 69 | + # Alternatively |
| 70 | + # rfile can be set to False which will cause the R code to be |
| 71 | + # passed |
| 72 | + # as a commandline argument to the R executable |
| 73 | + # (without creating any files). |
| 74 | + # This, however, is less reliable and harder to debug |
| 75 | + # (code will be reduced to |
| 76 | + # a single line and stripped of any comments). |
| 77 | + rcmd = RCommand(script=script, rfile=False) |
| 78 | + result = rcmd.run() |
| 79 | + if tmpfile: |
| 80 | + os.remove(tmpfile) |
| 81 | + return result.runtime |
| 82 | + |
| 83 | + def _list_outputs(self): |
| 84 | + outputs = self._outputs().get() |
| 85 | + outputs["out_file"] = os.path.abspath(self.inputs.out_file) |
| 86 | + return outputs |
| 87 | + |
| 88 | + def gen_indices(self): |
| 89 | + path = tempfile.mkstemp()[1] |
| 90 | + d = dict( |
| 91 | + in_file=self.inputs.in_file, out_file=path, img_type=self.inputs.img_type |
| 92 | + ) |
| 93 | + script = Template( |
| 94 | + """ |
| 95 | + library(neurobase) |
| 96 | + library(WhiteStripe) |
| 97 | + in_file = readnii('$in_file') |
| 98 | + t1_ind = whitestripe(in_file, "$img_type")$$whitestripe.ind |
| 99 | + write.table(t1_ind, file = "$out_file", row.names = F, col.names = F) |
| 100 | + """ |
| 101 | + ).substitute(d) |
| 102 | + RCommand(script=script, rfile=False).run() |
| 103 | + ret = self._read_indices(path) |
| 104 | + os.remove(path) |
| 105 | + return ret |
| 106 | + |
| 107 | + def _read_indices(self, fn): |
| 108 | + with open(fn) as f: |
| 109 | + # read lines as ints |
| 110 | + return list(map(int, f)) |
| 111 | + |
| 112 | + def _write_indices(self, fn, indices): |
| 113 | + with open(fn, "w") as f: |
| 114 | + for idx in indices: |
| 115 | + f.write("{}\n".format(idx)) |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + w = WhiteStripe() |
| 120 | + w.inputs.img_type = "T1" |
| 121 | + w.inputs.in_file = "T1W.nii.gz" |
| 122 | + # w.inputs.indices = [1,2,3] |
| 123 | + w.inputs.indices = w.gen_indices() |
| 124 | + w.inputs.out_file = "T1W_ws.nii.gz" |
| 125 | + w.run() |
0 commit comments