0

There is a folder with some images(slices) in it that they are sorted. A sample of the file names that are sorted in the folder is:

  0.7.dcm
 -1.1.dcm
  2.5.dcm
 -2.9.dcm
 -4.7.dcm
 -6.5.dcm
 -8.3.dcm
 -10.1.dcm

and some of them are as:

 -10.000000.dcm
 -12.500000.dcm
 -15.000000.dcm
 -17.500000.dcm
 -20.000000.dcm
 -22.500000.dcm
 -25.000000.dcm
 -27.500000.dcm

But when I want to read them, they load as an unsorted list. I tried some methods but the problem isn't solved yet:

for person in range(0, len(dirs1)):
    for root, dirs, files in os.walk(os.path.join(path, dirs1[person])):
        dcmfiles = [_ for _ in files if _.endswith('.dcm')]  # ['-10.000000.dcm', '-22.500000.dcm', '-17.500000.dcm', '-27.500000.dcm', '-25.000000.dcm', '-12.500000.dcm', '-20.000000.dcm', '-15.000000.dcm']
        dcmfilesList = sorted(dcmfiles, key = lambda x: x[:-4]) # ['-10.000000.dcm', '-22.500000.dcm', '-17.500000.dcm', '-27.500000.dcm', '-25.000000.dcm', '-12.500000.dcm', '-20.000000.dcm', '-15.000000.dcm']

and also I checked Sort filenames1, Sort filenames2, Sort filenames3.

How can I read .dcm slices as sorted in python3 such as below?

['0.7.dcm', '-1.1.dcm', '2.5.dcm', '-2.9.dcm', '-4.7.dcm', '-6.5.dcm', -8.3.dcm', '-10.1.dcm'].

and

 ['-10.000000.dcm', '-12.500000.dcm', '-15.000000.dcm', '-17.500000.dcm', '-20.000000.dcm', '-22.500000.dcm', '-25.000000.dcm',  '-27.500000.dcm'].
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Ellie
  • 303
  • 2
  • 16
  • You could build your sorted list without the "-" and the ".dcm" leaving only the float value, and when you read it, concatenate what you took off – Rodolfo Donã Hosp Nov 09 '18 at 11:47

2 Answers2

2

you are not converting them to numbers before sorting, so it's not working.

import os

for root, dirs, files in os.walk('./'):
        dcmfiles = [_ for _ in files if _.endswith('.dcm')]
        dcmFilesList = sorted(dcmfiles, key=lambda x: float(x[:-4]))

To sort ignoring sign, lambda x: abs(float(x[:-4]))

Aravind Voggu
  • 1,491
  • 12
  • 17
  • Yes, it is sorted but an error occurred `FileNotFoundError: [Errno 2] No such file or directory: 'E:\\Test\\LIDC-IDRI-0001\\-10.0.dcm'`. Because there is a file with the name of `-10.000000.dcm` and no `-10.0.dcm` – Ellie Nov 09 '18 at 12:14
  • aaah, I see. Edit on the way. – Aravind Voggu Nov 09 '18 at 12:15
  • @eli I edited it. This now sorts without changing the file names, so it should work. – Aravind Voggu Nov 09 '18 at 12:24
  • Now it is worked, but it is sorted Last to first because of `-` . How to inverse it? In the previous code `key=abs` worked. – Ellie Nov 09 '18 at 12:33
  • @eli Do you want them to be sorted in descending order? sorted(dcmfiles, key=lambda x: float(x[:-4]))[::-1] should give you inverted list. To sort by value, ignoring the -ve sign, I added in the answer. – Aravind Voggu Nov 09 '18 at 12:35
  • 2
    @AravindVoggu you can just add `reverse=True` to `sorted()`. – RoadRunner Nov 09 '18 at 12:36
  • That's much better! @RoadRunner I did a lazy hack :p – Aravind Voggu Nov 09 '18 at 12:37
0

You can first sort the list of files by their float prefixes, only including .dcm file extensions, and open each file indiviudally:

from os import walk
from os.path import splitext

# sort .dcm files by file prefix
sorted_files = sorted(
    (file for _, _, files in walk(".") for file in files if file.endswith(".dcm")),
    key=lambda f: float(splitext(f)[0]),
)

# open each .dcm file and do some processing
for file in sorted_files:
    with open(file) as dcm_file:
        # do reading stuff here
RoadRunner
  • 25,803
  • 6
  • 42
  • 75