Skip to content

Commit

Permalink
Rename fname to base_name.
Browse files Browse the repository at this point in the history
  • Loading branch information
FredLoney committed Jun 2, 2017
1 parent 71504fd commit 3b64936
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 110 deletions.
22 changes: 11 additions & 11 deletions qipipe/interfaces/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class CompressInputSpec(BaseInterfaceInputSpec):
in_file = File(exists=True, mandatory=True, desc='The file to compress')

dest = Directory(desc='The optional directory to write the compressed file'
'(default current directory)')

Expand All @@ -17,25 +17,25 @@ class CompressOutputSpec(TraitedSpec):

class Compress(BaseInterface):
input_spec = CompressInputSpec

output_spec = CompressOutputSpec

def _run_interface(self, runtime):
self.out_file = self._compress(
self.inputs.in_file, dest=self.inputs.dest)

return runtime

def _list_outputs(self):
outputs = self._outputs().get()
outputs['out_file'] = self.out_file

return outputs

def _compress(self, in_file, dest=None):
"""
Compresses the given file.
:param in_file: the path of the file to compress
:param dest: the destination (default is the working directory)
:return: the compressed file path
Expand All @@ -44,12 +44,12 @@ def _compress(self, in_file, dest=None):
dest = os.getcwd()
if not os.path.exists(dest):
os.makedirs(dest)
_, fname = os.path.split(in_file)
out_file = os.path.join(dest, fname + '.gz')
_, base_name = os.path.split(in_file)
out_file = os.path.join(dest, base_name + '.gz')
f = open(in_file, 'rb')
cf = gzip.open(out_file, 'wb')
cf.writelines(f)
f.close()
cf.close()

return os.path.abspath(out_file)
34 changes: 17 additions & 17 deletions qipipe/interfaces/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
class CopyInputSpec(BaseInterfaceInputSpec):
in_file = traits.Either(File, Directory, exists=True, mandatory=True,
desc='The file or directory to copy')

dest = Directory(desc='The destination directory path'
' (default current directory)')
out_fname = traits.Either(File, Directory,

out_base_name = traits.Either(File, Directory,
desc='The destination file name'
' (default is the input file name)')

Expand All @@ -25,28 +25,28 @@ class CopyOutputSpec(TraitedSpec):
class Copy(BaseInterface):
"""The Copy interface copies a file to a destination directory."""
input_spec = CopyInputSpec

output_spec = CopyOutputSpec

def _run_interface(self, runtime):
self._out_file = self._copy(self.inputs.in_file, self.inputs.dest,
self.inputs.out_fname)
self.inputs.out_base_name)
return runtime

def _list_outputs(self):
outputs = self._outputs().get()
outputs['out_file'] = self._out_file

return outputs
def _copy(self, in_file, dest=None, out_fname=None):

def _copy(self, in_file, dest=None, out_base_name=None):
"""
Copies the given file.
:param in_file: the path of the file or directory to copy
:param dest: the destination directory path
(default is the current directory)
:param out_fname: the destination file name
:param out_base_name: the destination file name
(default is the input file name)
:return: the copied file path
"""
Expand All @@ -56,16 +56,16 @@ def _copy(self, in_file, dest=None, out_fname=None):
os.makedirs(dest)
else:
dest = os.getcwd()
if out_fname:
if out_base_name:
# Remove the out file name directory.
_, out_fname = os.path.split(out_fname)
_, out_base_name = os.path.split(out_base_name)
else:
# The default out file name is the input file name.
_, out_fname = os.path.split(in_file)
out_file = os.path.join(dest, out_fname)
_, out_base_name = os.path.split(in_file)
out_file = os.path.join(dest, out_base_name)
if os.path.isdir(in_file):
shutil.copytree(in_file, out_file)
else:
shutil.copy(in_file, out_file)

return out_file
4 changes: 2 additions & 2 deletions qipipe/interfaces/move.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ def _move(self, in_file, dest):
if not os.path.exists(dest):
os.makedirs(dest)
shutil.move(in_file, dest)
_, fname = os.path.split(in_file)
out_file = os.path.join(dest, fname)
_, base_name = os.path.split(in_file)
out_file = os.path.join(dest, base_name)
return out_file
44 changes: 22 additions & 22 deletions qipipe/interfaces/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
class PreviewInputSpec(BaseInterfaceInputSpec):
in_file = traits.File(exists=True, mandatory=True,
desc='The input 3D NIfTI file')

dest = traits.Directory(desc='The destination directory path'
' (default current directory)')

crop = traits.List(traits.List(traits.Int),
desc='The x and y [min, max] bounds')
out_fname = traits.File(desc='The destination file name'

out_base_name = traits.File(desc='The destination file name'
' (default is the input file name)')


Expand All @@ -25,32 +25,32 @@ class PreviewOutputSpec(TraitedSpec):

class Preview(BaseInterface):
"""Preview creates a JPEG image from an input DICOM image."""

input_spec = PreviewInputSpec

output_spec = PreviewOutputSpec

def _run_interface(self, runtime):
self._out_file = self._convert(
self.inputs.in_file, dest=self.inputs.dest,
out_fname=self.inputs.out_fname, crop=self.inputs.crop
out_base_name=self.inputs.out_base_name, crop=self.inputs.crop
)
return runtime

def _list_outputs(self):
outputs = self._outputs().get()
outputs['out_file'] = self._out_file

return outputs
def _convert(self, in_file, dest=None, out_fname=None, crop=None):

def _convert(self, in_file, dest=None, out_base_name=None, crop=None):
"""
Copies the given file.
:param in_file: the path of the file or directory to copy
:param dest: the destination directory path
(default is the current directory)
:param out_fname: the destination file name
:param out_base_name: the destination file name
(default is the input file name)
:param crop: the x and y [min, max] bounds
:return: the preview JPEG file path
Expand All @@ -61,15 +61,15 @@ def _convert(self, in_file, dest=None, out_fname=None, crop=None):
os.makedirs(dest)
else:
dest = os.getcwd()

# The default output file name is the input file name
# with a .jpg extension.
if not out_fname:
_, in_fname = os.path.split(in_file)
base, _ = os.path.splitext(in_fname)
out_fname = base + '.jpg'
out_file = os.path.join(dest, out_fname)
if not out_base_name:
_, in_base_name = os.path.split(in_file)
base, _ = os.path.splitext(in_base_name)
out_base_name = base + '.jpg'
out_file = os.path.join(dest, out_base_name)

dcm = dicom.read_file(in_file)
data = dcm.pixel_array
# Crop the data, if necessary.
Expand All @@ -79,5 +79,5 @@ def _convert(self, in_file, dest=None, out_fname=None, crop=None):
crop_ymin, crop_ymax = crop_y
data = data[crop_xmin:crop_xmax, crop_ymin:crop_ymax]
image.imsave(out_file, data, cmap=cm.jet)

return out_file
4 changes: 2 additions & 2 deletions qipipe/interfaces/uncompress.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def _uncompress(self, in_file, dest=None):
dest = os.getcwd()
if not os.path.exists(dest):
os.makedirs(dest)
_, fname = os.path.split(in_file)
out_file = os.path.join(dest, fname[:-3])
_, base_name = os.path.split(in_file)
out_file = os.path.join(dest, base_name[:-3])
cf = gzip.open(in_file, 'rb')
f = open(out_file, 'wb')
f.writelines(cf)
Expand Down
12 changes: 6 additions & 6 deletions qipipe/qiprofile/imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ def _create_rois(self, xnat_scan, **opts):
if not rsc.exists():
return rois
# The file object label is the file base name.
fnames = set(rsc.files().get())
base_names = set(rsc.files().get())
# The mask files do not have _color in the base name.
# Sort the mask files by the lesion prefix.
masks = sorted(f for f in fnames if not '_color' in f)
masks = sorted(f for f in base_names if not '_color' in f)
centroids = opts.get('centroids')
avg_intensities = opts.get('average_intensities')
# The color look-up table.
Expand All @@ -214,7 +214,7 @@ def _create_rois(self, xnat_scan, **opts):
for i, mask in enumerate(masks):
roi_opts = lobel_map_opts.clone()
label_map_name = label_map_basename(mask)
if label_map_name in fnames:
if label_map_name in base_names:
# A label map must have a color table.
if not color_table:
raise ImagingUpdateError(
Expand Down Expand Up @@ -316,11 +316,11 @@ def _create_modeling(self, resource, scans):
xnat_file_labels = {xnat_name(xnat_file) for xnat_file in xnat_files}
result = {}
for output in OUTPUTS:
fname = output + '.nii.gz'
if fname in xnat_file_labels:
base_name = output + '.nii.gz'
if base_name in xnat_file_labels:
# TODO - add the param result average and label map to the
# pipeline and here.
param_result = Modeling.ParameterResult(filename=fname)
param_result = Modeling.ParameterResult(filename=base_name)
result[output] = param_result

# Return the new qiprofile Modeling object.
Expand Down
10 changes: 5 additions & 5 deletions qipipe/staging/fix_dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ def _dest_file_name(in_file, dest):
:param dest: the destination directory
:return: the target output file name
"""
_, fname = os.path.split(in_file)
_, base_name = os.path.split(in_file)
# Replace non-word characters.
fname = re.sub('\W', '_', fname.lower())
base_name = re.sub('\W', '_', base_name.lower())
# Add a .dcm extension, if necessary.
_, ext = os.path.splitext(fname)
_, ext = os.path.splitext(base_name)
if not ext:
fname = fname + '.dcm'
base_name = base_name + '.dcm'

return os.path.join(dest, fname)
return os.path.join(dest, base_name)
4 changes: 2 additions & 2 deletions qipipe/staging/ohsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
slice # The slice subdirectory
(?P<slice_sequence_number>\d+) # The slice index
/ # End of the slice subdirectory
(?P<fname> # The ROI file base name
(?P<base_name> # The ROI file base name
.*\.bqf # The ROI file extension
)$ # End of the ROI file name
""", re.VERBOSE)
Expand All @@ -126,7 +126,7 @@
SARCOMA_ROI_REGEX = re.compile("""
^.* # The slice parent directory
slice(?P<slice_sequence_number>\d+)/ # The slice subdirectory
(?P<fname> # The ROI file base name
(?P<base_name> # The ROI file base name
.*\.bqf # The ROI file extension
)$ # End of the ROI file name
""", re.VERBOSE)
Expand Down
2 changes: 1 addition & 1 deletion test/unit/interfaces/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_copy_file(self):

def test_copy_file_with_output_filename(self):
# Copy the file.
copy = Copy(in_file=SOURCE, dest=TARGET, out_fname='target.txt')
copy = Copy(in_file=SOURCE, dest=TARGET, out_base_name='target.txt')
result = copy.run()

# Verify the result.
Expand Down
28 changes: 14 additions & 14 deletions test/unit/interfaces/test_xnat_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ def test_scan(self):
scan_obj = xnat.find_one(PROJECT, SUBJECT, SESSION, scan=SCAN)
assert_is_not_none(scan_obj, "Upload did not create the %s %s scan:"
" %s" % (SUBJECT, SESSION, SCAN))
_, fname = os.path.split(FIXTURE)
assert_in(fname, xnat_files,
_, base_name = os.path.split(FIXTURE)
assert_in(base_name, xnat_files,
"The XNATCopy result does not include the %s %s scan"
" %d file %s" % (SUBJECT, SESSION, SCAN, fname))
file_obj = scan_obj.resource('NIFTI').file(fname)
" %d file %s" % (SUBJECT, SESSION, SCAN, base_name))
file_obj = scan_obj.resource('NIFTI').file(base_name)
assert_true(file_obj.exists(),
"XNATCopy did not create the %s %s scan %d file: %s" %
(SUBJECT, SESSION, SCAN, fname))
(SUBJECT, SESSION, SCAN, base_name))

def test_registration(self):
logger(__name__).debug("Testing the XNATCopy interface on %s %s"
Expand All @@ -87,11 +87,11 @@ def test_registration(self):
assert_true(rsc_obj.exists(),
"XNATCopy did not create the %s %s resource: %s" %
(SUBJECT, SESSION, REGISTRATION))
_, fname = os.path.split(FIXTURE)
file_obj = rsc_obj.file(fname)
_, base_name = os.path.split(FIXTURE)
file_obj = rsc_obj.file(base_name)
assert_true(file_obj.exists(),
"XNATCopy did not create the %s %s %s file: %s" %
(SUBJECT, SESSION, REGISTRATION, fname))
(SUBJECT, SESSION, REGISTRATION, base_name))

def test_reconstruction(self):
logger(__name__).debug("Testing the XNATCopy interface on %s %s"
Expand All @@ -110,11 +110,11 @@ def test_reconstruction(self):
assert_is_not_none(recon_obj,
"Upload did not create the %s %s reconstruction: %s" %
(SUBJECT, SESSION, RECON))
_, fname = os.path.split(FIXTURE)
file_obj = recon_obj.out_resource('NIFTI').file(fname)
_, base_name = os.path.split(FIXTURE)
file_obj = recon_obj.out_resource('NIFTI').file(base_name)
assert_true(file_obj.exists(),
"XNATCopy did not create the %s %s %s file: %s" %
(SUBJECT, SESSION, REGISTRATION, fname))
(SUBJECT, SESSION, REGISTRATION, base_name))

def test_assessor(self):
logger(__name__).debug("Testing the XNATCopy interface on %s %s"
Expand All @@ -134,11 +134,11 @@ def test_assessor(self):
assert_is_not_none(anl_obj, "XNATCopy did not create the %s %s"
" analysis: %s" %
(SUBJECT, SESSION, ANALYSIS))
_, fname = os.path.split(FIXTURE)
file_obj = anl_obj.out_resource('params').file(fname)
_, base_name = os.path.split(FIXTURE)
file_obj = anl_obj.out_resource('params').file(base_name)
assert_true(file_obj.exists(), "XNATCopy did not create the %s %s"
" %s file: %s" %
(SUBJECT, SESSION, ANALYSIS, fname))
(SUBJECT, SESSION, ANALYSIS, base_name))


if __name__ == "__main__":
Expand Down

0 comments on commit 3b64936

Please sign in to comment.