Home > Mobile >  How can I sort datapath in numerical order?
How can I sort datapath in numerical order?

Time:03-18

I have 10000119~10000130 patient 3D ct images I've sliced 3D CT images into 250 slices for each patient data along the z axis.

I want to sort this data path in numerical order for each slice for each patient. What I want to do is the following:

/mnt/intern/code/dataset/test/original2Densenble/10000119slice0.mat
/mnt/intern/code/dataset/test/original2Densenble/10000119slice1.mat
/mnt/intern/code/dataset/test/original2Densenble/10000119slice2.mat
/mnt/intern/code/dataset/test/original2Densenble/10000119slice3.mat
...
/mnt/intern/code/dataset/test/original2Densenble/10000119slice250.mat
/mnt/intern/code/dataset/test/original2Densenble/10000120slice0.mat
/mnt/intern/code/dataset/test/original2Densenble/10000120slice1.mat
/mnt/intern/code/dataset/test/original2Densenble/10000120slice2.mat
...
/mnt/intern/code/dataset/test/original2Densenble/10000120slice250.mat
/mnt/intern/code/dataset/test/original2Densenble/10000121slice0.mat
/mnt/intern/code/dataset/test/original2Densenble/10000121slice1.mat
...
/mnt/intern/code/dataset/test/original2Densenble/10000121slice250.mat
...
/mnt/intern/code/dataset/test/original2Densenble/10000130slice0.mat
...
/mnt/intern/code/dataset/test/original2Densenble/10000130slice250.mat

The reason I'm doing this is because I want to ensemble 2D Unet softmax output with 3D Unet softmax output. So I think I need to sort 2D slices for each slice for each patient to mean over 2D Unet softmax output and 3D Unet softmax output. To do that I think I need to load 2D data in numerical order in the first place. I thought of split function but I think it will eventually be mixed up because of patients' numbers(10000119~10000130) and patients' slices numbers(slice0.mat~slice250.mat for each patient).

CodePudding user response:

I found my own answer..! I hope this will help other deeplearning engineer too! I hope you have a great day! I explicitly coded to get the intuition at hand.

class SliceData2D(Dataset):

    def __init__(self, root_data, root_label, transform):

        self.transform = transform
        self.examples = []
        self.examples2 = []

        data_files = list(pathlib.Path(root_data).iterdir())
        label_files = list(pathlib.Path(root_label).iterdir())
        data_files = list(map(str, data_files))
        label_files = list(map(str, label_files))
        # print(len(data_files))
        # print(len(label_files))
        data_files = sorted(data_files)
        label_files = sorted(label_files)
        del label_files[0] # /mnt/intern/code/dataset/test/label2Densemble/.mat 이상한 파일 1개 제거
        # print(len(label_files))

        data1 = sorted(data_files[0 : 250], key=lambda x: int(           (x.split('slice')[1]).split('.')[0]    ) )
        data2 = sorted(data_files[250 : 500], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        data3 = sorted(data_files[500 : 750], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        data4 = sorted(data_files[750 : 1000], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        data5 = sorted(data_files[1000 : 1250], key=lambda x: int(     (x.split('slice')[1]).split('.')[0]    ) )
        data6 = sorted(data_files[1250 : 1500], key=lambda x: int(      (x.split('slice')[1]).split('.')[0]    ) )
        data7 = sorted(data_files[1500 : 1750], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        data8 = sorted(data_files[1750 : 2000], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        data9 = sorted(data_files[2000 : 2250], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        data10 = sorted(data_files[2250 : 2500], key=lambda x: int(       (x.split('slice')[1]).split('.')[0]    ) )
        data11 = sorted(data_files[2500 : 2750], key=lambda x: int(       (x.split('slice')[1]).split('.')[0]    ) )
        
        # print(data1, '\n', data2, '\n', data3, '\n', data4, '\n', data5, '\n', data6, '\n', data7, '\n', data8, '\n', data9, '\n', data10, '\n', data11, '\n')
        
        data1 = list(map(pathlib.Path, data1))
        data2 = list(map(pathlib.Path, data2))
        data3 = list(map(pathlib.Path, data3))
        data4 = list(map(pathlib.Path, data4))
        data5 = list(map(pathlib.Path, data5))
        data6 = list(map(pathlib.Path, data6))
        data7 = list(map(pathlib.Path, data7))
        data8 = list(map(pathlib.Path, data8))
        data9 = list(map(pathlib.Path, data9))
        data10 = list(map(pathlib.Path, data10))
        data11 = list(map(pathlib.Path, data11))

        label1 = sorted(label_files[0 : 250], key=lambda x: int(           (x.split('slice')[1]).split('.')[0]    ) )
        label2 = sorted(label_files[250 : 500], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        label3 = sorted(label_files[500 : 750], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        label4 = sorted(label_files[750 : 1000], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        label5 = sorted(label_files[1000 : 1250], key=lambda x: int(     (x.split('slice')[1]).split('.')[0]    ) )
        label6 = sorted(label_files[1250 : 1500], key=lambda x: int(      (x.split('slice')[1]).split('.')[0]    ) )
        label7 = sorted(label_files[1500 : 1750], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        label8 = sorted(label_files[1750 : 2000], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        label9 = sorted(label_files[2000 : 2250], key=lambda x: int(        (x.split('slice')[1]).split('.')[0]    ) )
        label10 = sorted(label_files[2250 : 2500], key=lambda x: int(       (x.split('slice')[1]).split('.')[0]    ) )
        label11 = sorted(label_files[2500 : 2750], key=lambda x: int(       (x.split('slice')[1]).split('.')[0]    ) )

        # print(label1, '\n', label2, '\n', label3, '\n', label4, '\n', label5, '\n', label6, '\n', label7, '\n', label8, '\n', label9, '\n', label10, '\n', label11, '\n')
        
        label1 = list(map(pathlib.Path, label1))
        label2 = list(map(pathlib.Path, label2))
        label3 = list(map(pathlib.Path, label3))
        label4 = list(map(pathlib.Path, label4))
        label5 = list(map(pathlib.Path, label5))
        label6 = list(map(pathlib.Path, label6))
        label7 = list(map(pathlib.Path, label7))
        label8 = list(map(pathlib.Path, label8))
        label9 = list(map(pathlib.Path, label9))
        label10 = list(map(pathlib.Path, label10))
        label11 = list(map(pathlib.Path, label11))


        self.examples = data1   data2   data3   data4   data5   data6   data7   data8   data9   data10   data11
        self.examples2 = label1   label2   label3   label4   label5   label6   label7   label8   label9   label10   label11
        print(self.examples)
        
    def __len__(self):
        
        return len(self.examples)

    def __getitem__(self, i):
        
        fname = self.examples[i]
        fname2 = self.examples2[i]
        
        data_name = str(pathlib.Path(fname))
        label_name = str(pathlib.Path(fname2))
        
        d_load = scipy.io.loadmat(data_name); l_load = scipy.io.loadmat(label_name)
        data = d_load['data']; label = l_load['label']
            
        return self.transform(data, label, fname.name)
  • Related