From beb4e7d26835544dd2d94e3e74375c966761cb5f Mon Sep 17 00:00:00 2001 From: Steve Pieper Date: Wed, 15 Nov 2023 17:23:51 -0500 Subject: [PATCH] ENH: Moves some code to DICOMPlugin base class Make these methods available for us in other classes. --- Modules/Scripted/DICOMLib/DICOMPlugin.py | 28 +++++++++++++++++++ .../DICOMPlugins/DICOMScalarVolumePlugin.py | 25 ----------------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/Modules/Scripted/DICOMLib/DICOMPlugin.py b/Modules/Scripted/DICOMLib/DICOMPlugin.py index 64277329859..fc249bf34dc 100644 --- a/Modules/Scripted/DICOMLib/DICOMPlugin.py +++ b/Modules/Scripted/DICOMLib/DICOMPlugin.py @@ -139,6 +139,34 @@ def cacheLoadables(self, files, loadables): key = self.hashFiles(files) self.loadableCache[key] = loadables + def cleanNodeName(self, value): + cleanValue = value + cleanValue = cleanValue.replace("|", "-") + cleanValue = cleanValue.replace("/", "-") + cleanValue = cleanValue.replace("\\", "-") + cleanValue = cleanValue.replace("*", "(star)") + cleanValue = cleanValue.replace("\\", "-") + return cleanValue + + def seriesSorter(self, x, y): + """ returns -1, 0, 1 for sorting of strings like: "400: series description" + Works for DICOMLoadable or other objects with name attribute + Use like: + from functools import cmp_to_key + loadables.sort(key=cmp_to_key(lambda x, y: self.seriesSorter(x, y))) + """ + if not (hasattr(x, 'name') and hasattr(y, 'name')): + return 0 + xName = x.name + yName = y.name + try: + xNumber = int(xName[:xName.index(':')]) + yNumber = int(yName[:yName.index(':')]) + except ValueError: + return 0 + cmp = xNumber - yNumber + return cmp + def examineForImport(self, fileList): """Look at the list of lists of filenames and return a list of DICOMLoadables that are options for loading diff --git a/Modules/Scripted/DICOMPlugins/DICOMScalarVolumePlugin.py b/Modules/Scripted/DICOMPlugins/DICOMScalarVolumePlugin.py index 68b210b03a2..72f82300948 100644 --- a/Modules/Scripted/DICOMPlugins/DICOMScalarVolumePlugin.py +++ b/Modules/Scripted/DICOMPlugins/DICOMScalarVolumePlugin.py @@ -171,15 +171,6 @@ def examineForImport(self, fileLists): return loadables - def cleanNodeName(self, value): - cleanValue = value - cleanValue = cleanValue.replace("|", "-") - cleanValue = cleanValue.replace("/", "-") - cleanValue = cleanValue.replace("\\", "-") - cleanValue = cleanValue.replace("*", "(star)") - cleanValue = cleanValue.replace("\\", "-") - return cleanValue - def examineFiles(self, files): """ Returns a list of DICOMLoadable instances corresponding to ways of interpreting the @@ -345,22 +336,6 @@ def examineFiles(self, files): return loadables - def seriesSorter(self, x, y): - """ returns -1, 0, 1 for sorting of strings like: "400: series description" - Works for DICOMLoadable or other objects with name attribute - """ - if not (hasattr(x, 'name') and hasattr(y, 'name')): - return 0 - xName = x.name - yName = y.name - try: - xNumber = int(xName[:xName.index(':')]) - yNumber = int(yName[:yName.index(':')]) - except ValueError: - return 0 - cmp = xNumber - yNumber - return cmp - # # different ways to load a set of dicom files: # - Logic: relies on the same loading mechanism used