조현아

get aligned shape targets

import pandas as pd
import os
from natsort import natsorted
csv = 'ce_train_targets.csv'
data_path = 'ce_train'
df = pd.read_csv(csv)
#idx = df.index[df.iloc[:,0]=='BraTS19_CBICA_BHB_1_seg_flair_8.png'].tolist()
# df = df.loc[df.iloc[:,0]=='BraTS19_CBICA_BHB_1_seg_flair_8.png']
# print(df.iloc[0, 1])
imgs = natsorted(os.listdir(data_path)) # img file list
targets = []
for fname in imgs:
row = df.loc[df['filename'] == fname]
targets.append(row.iloc[0, 1])
#print(targets, len(targets))
#BraTS19_2013_10_1_seg_flair_0.png -> class 0
#BraTS19_CBICA_AYU_1_seg_flair_2.png -> class 1
\ No newline at end of file
data_path = '..\data\MICCAI_BraTS_2019_Data_Training\HGG_seg_flair\BraTS19_CBICA_AVG_1_seg_flair.nii';
data = niftiread(data_path);
cp_data = flipud(rot90(mat2gray(double(data(:,:,82)))));
type = '.png';
filename = strcat('BraTS19_CBICA_AVG_1_seg_flair_8', type); % BraTS19_2013_2_1_seg_flair_c.png
outpath = strcat('..\data\MICCAI_BraTS_2019_Data_Training\zero to valid\', filename);
imwrite(cp_data, outpath);
% st, end 10
%% (circle 0, ellipse 0)
% BraTS19_CBICA_AOP_1_seg_flair_0
% BraTS19_CBICA_ASF_1_seg_flair_9 -> 8
% BraTS19_CBICA_ASR_1_seg_flair_8
% BraTS19_CBICA_ASR_1_seg_flair_9
% BraTS19_CBICA_AVG_1_seg_flair_9 -> 8
%
%
%
%
%
%
%
%
%
%
%
%
\ No newline at end of file
getname_path = '..\data\MICCAI_BraTS_2019_Data_Training\zero2\';
inputheader = '..\data\MICCAI_BraTS_2019_Data_Training\HGG_seg_flair\';
outfolder = '..\data\MICCAI_BraTS_2019_Data_Training\zero to valid2\';
files = dir(getname_path);
id = {files.name};
% files + dir file
flag = ~strcmp(id, '.') & ~strcmp(id, '..');
files = files(flag);
% get filenames in getname_path folder
for i = 1 : length(files)
%id = split(files(i).name, '.nii');
fname = files(i).name;
id = reverse(extractBetween(reverse(fname), 7, strlength(fname)));
data_path = convertCharsToStrings(strcat(inputheader,'\', id, '.nii'));
data = niftiread(data_path);
fprintf('ID #%d = %s\n', i, data_path);
idx = 1;
frames = zeros(1,1);
for j = 1 : z
n_nonblack = numel(find(data(:,:,j) > 0));
if(n_nonblack > 70)
frames(idx,1) = j;
idx = idx + 1;
end
end
% for j = z : -1 : 1
% n_nonblack = numel(find(data(:,:,j) > 0));
% if(n_nonblack > 50)
% fprintf('%s: %d\n ','number: ', n_nonblack);
% en = j;
% break;
% end
% end
c = 0;
[nrow, ncol] = size(frames);
step = round(nrow/11);
% fprintf('%s: %d \n', 'nrow', nrow);
% fprintf('%s: %d\n ','st: ', frames(1, 1));
% fprintf('%s: %d\n ','end: ', frames(nrow, 1));
%disp(frames);
for k = 1 : step : step*10
%fprintf('%d ', k);
fprintf('%d ', frames(k, 1));
type = '.png';
filename = strcat(id, '_', int2str(c), type); % BraTS19_2013_2_1_seg_flair_c.png
outpath = convertCharsToStrings(strcat(outfolder, filename));
% typecase int16 to double, range[0, 1], rotate 90 and filp updown
% range [0, 1]
cp_data = flipud(rot90(mat2gray(double(data(:,:,frames(k, 1))))));
% M = max(cp_data(:));
% disp(M);
imwrite(cp_data, outpath);
c = c+ 1;
end
end
import pandas as pd
import os
import csv
class0_path = "../../data/MICCAI_BraTS_2019_Data_Training/circle_val"
class1_path = "../../data/MICCAI_BraTS_2019_Data_Training/ellipse_val"
target_csv = "../../data/MICCAI_BraTS_2019_Data_Training/ce_valid_targets.csv"
f=open(target_csv, 'w', newline='')
w=csv.writer(f)
for path, dirs, files in os.walk(class0_path):
for filename in files:
print(filename)
n_class = 0
w.writerow([filename, n_class])
for path, dirs, files in os.walk(class1_path):
for filename in files:
print(filename)
n_class = 1
w.writerow([filename, n_class])
\ No newline at end of file