조현아

get aligned shape targets

1 +import pandas as pd
2 +import os
3 +from natsort import natsorted
4 +
5 +csv = 'ce_train_targets.csv'
6 +data_path = 'ce_train'
7 +
8 +df = pd.read_csv(csv)
9 +
10 +#idx = df.index[df.iloc[:,0]=='BraTS19_CBICA_BHB_1_seg_flair_8.png'].tolist()
11 +
12 +# df = df.loc[df.iloc[:,0]=='BraTS19_CBICA_BHB_1_seg_flair_8.png']
13 +# print(df.iloc[0, 1])
14 +
15 +
16 +imgs = natsorted(os.listdir(data_path)) # img file list
17 +targets = []
18 +
19 +for fname in imgs:
20 + row = df.loc[df['filename'] == fname]
21 + targets.append(row.iloc[0, 1])
22 +
23 +#print(targets, len(targets))
24 +
25 +#BraTS19_2013_10_1_seg_flair_0.png -> class 0
26 +#BraTS19_CBICA_AYU_1_seg_flair_2.png -> class 1
...\ No newline at end of file ...\ No newline at end of file
1 +data_path = '..\data\MICCAI_BraTS_2019_Data_Training\HGG_seg_flair\BraTS19_CBICA_AVG_1_seg_flair.nii';
2 +data = niftiread(data_path);
3 +
4 +cp_data = flipud(rot90(mat2gray(double(data(:,:,82)))));
5 +
6 +type = '.png';
7 +filename = strcat('BraTS19_CBICA_AVG_1_seg_flair_8', type); % BraTS19_2013_2_1_seg_flair_c.png
8 +outpath = strcat('..\data\MICCAI_BraTS_2019_Data_Training\zero to valid\', filename);
9 +imwrite(cp_data, outpath);
10 +
11 +
12 +% st, end 10
13 +
14 +%% (circle 0, ellipse 0)
15 +% BraTS19_CBICA_AOP_1_seg_flair_0
16 +% BraTS19_CBICA_ASF_1_seg_flair_9 -> 8
17 +% BraTS19_CBICA_ASR_1_seg_flair_8
18 +% BraTS19_CBICA_ASR_1_seg_flair_9
19 +% BraTS19_CBICA_AVG_1_seg_flair_9 -> 8
20 +%
21 +%
22 +%
23 +%
24 +%
25 +%
26 +%
27 +%
28 +%
29 +%
30 +%
31 +%
...\ No newline at end of file ...\ No newline at end of file
1 +getname_path = '..\data\MICCAI_BraTS_2019_Data_Training\zero2\';
2 +inputheader = '..\data\MICCAI_BraTS_2019_Data_Training\HGG_seg_flair\';
3 +outfolder = '..\data\MICCAI_BraTS_2019_Data_Training\zero to valid2\';
4 +
5 +files = dir(getname_path);
6 +id = {files.name};
7 +% files + dir file
8 +flag = ~strcmp(id, '.') & ~strcmp(id, '..');
9 +files = files(flag);
10 +
11 +% get filenames in getname_path folder
12 +for i = 1 : length(files)
13 + %id = split(files(i).name, '.nii');
14 + fname = files(i).name;
15 + id = reverse(extractBetween(reverse(fname), 7, strlength(fname)));
16 +
17 + data_path = convertCharsToStrings(strcat(inputheader,'\', id, '.nii'));
18 + data = niftiread(data_path);
19 +
20 + fprintf('ID #%d = %s\n', i, data_path);
21 +
22 + idx = 1;
23 + frames = zeros(1,1);
24 + for j = 1 : z
25 + n_nonblack = numel(find(data(:,:,j) > 0));
26 + if(n_nonblack > 70)
27 + frames(idx,1) = j;
28 + idx = idx + 1;
29 + end
30 + end
31 +
32 +% for j = z : -1 : 1
33 +% n_nonblack = numel(find(data(:,:,j) > 0));
34 +% if(n_nonblack > 50)
35 +% fprintf('%s: %d\n ','number: ', n_nonblack);
36 +% en = j;
37 +% break;
38 +% end
39 +% end
40 +
41 +
42 + c = 0;
43 + [nrow, ncol] = size(frames);
44 + step = round(nrow/11);
45 +% fprintf('%s: %d \n', 'nrow', nrow);
46 +% fprintf('%s: %d\n ','st: ', frames(1, 1));
47 +% fprintf('%s: %d\n ','end: ', frames(nrow, 1));
48 + %disp(frames);
49 +
50 + for k = 1 : step : step*10
51 + %fprintf('%d ', k);
52 + fprintf('%d ', frames(k, 1));
53 + type = '.png';
54 + filename = strcat(id, '_', int2str(c), type); % BraTS19_2013_2_1_seg_flair_c.png
55 + outpath = convertCharsToStrings(strcat(outfolder, filename));
56 + % typecase int16 to double, range[0, 1], rotate 90 and filp updown
57 + % range [0, 1]
58 + cp_data = flipud(rot90(mat2gray(double(data(:,:,frames(k, 1))))));
59 +% M = max(cp_data(:));
60 +% disp(M);
61 + imwrite(cp_data, outpath);
62 +
63 + c = c+ 1;
64 + end
65 +
66 +
67 +end
68 +
1 +import pandas as pd
2 +import os
3 +import csv
4 +
5 +class0_path = "../../data/MICCAI_BraTS_2019_Data_Training/circle_val"
6 +class1_path = "../../data/MICCAI_BraTS_2019_Data_Training/ellipse_val"
7 +target_csv = "../../data/MICCAI_BraTS_2019_Data_Training/ce_valid_targets.csv"
8 +
9 +f=open(target_csv, 'w', newline='')
10 +w=csv.writer(f)
11 +
12 +for path, dirs, files in os.walk(class0_path):
13 + for filename in files:
14 + print(filename)
15 + n_class = 0
16 + w.writerow([filename, n_class])
17 +
18 +for path, dirs, files in os.walk(class1_path):
19 + for filename in files:
20 + print(filename)
21 + n_class = 1
22 + w.writerow([filename, n_class])
...\ No newline at end of file ...\ No newline at end of file