Jovian
⭐️
Sign In
In [ ]:
jovian.commit()
[jovian] Saving notebook..
In [64]:
import csv
import glob
import jovian
import numpy
import pandas as pd

root_path = 'E:/segmentation_pupil/'

csv_paths = glob.glob(root_path + '*/*.csv')
csv_paths = csv_paths[:16]

csv_column = ['ori_Area', 'remove_blink', 'derivative',  'change_rate', 
              'nom_Area', 'Center_x', 'Center_y', 'Blink', 
              't_aver_change', 'c_aver_change', 'r_aver_change', 'Blink_cnt', 'Blink_rate', 'Close_cnt', 'Close_rate']
            # contraction, relaxation
for csv_path in csv_paths:
    file_name = csv_path.split('\\')[-1][:-4]
    
    csv = pd.read_csv(csv_path, error_bad_lines=False)
    ori_csv = list(csv['Area'])
    
    if file_name.split('_')[-1] == '1':
        mean_bright = 0
        for idx in range(csv.shape[0]):
            mean_bright += csv['Area'][idx]
        mean_bright /= csv.shape[0]
    else:
        s_csv_list = [] 
        csv['Area'] -= mean_bright
        
        blink = False
        blink_cnt = 0
        close_cnt = 0
        bf_lists = []
        m_change = []
        p_change = []
        change_cnt = 0
        total_change = 0
        for idx in range(csv.shape[0] - 1):
            c_x = csv['Center'][idx].strip('()').split(',')[0]
            c_y = csv['Center'][idx].strip('()').split(',')[1].lstrip()
             
            change_rate = ''
            if blink == False:
                if csv['Area'][idx] == -mean_bright:
                    blink = True
                    bf_list = [idx]
                    blink_start_idx = idx
                    blink_cnt += 1  
                    
                    before_area = ori_csv[idx-1]
        
                derivative = ori_csv[idx + 1] - ori_csv[idx]
                
                if idx == 0:
                    s_idx = idx
                    if derivative >= 0:
                        sign_check = True
                    else:
                        sign_check = False
                else:
                    change_f = -1
                    change_a = 0
                    if sign_check == True:
                        if derivative < 0:
                            change_f = idx - s_idx
                            change_a = ori_csv[idx] - ori_csv[s_idx]
                            sign_check = False
                            s_idx = idx
                            
                    else:
                        if derivative > 0:
                            change_f = idx - s_idx
                            change_a = ori_csv[idx] - ori_csv[s_idx]
                            sign_check = True
                            s_idx = idx
                    
                    if change_f != -1:
                        change_cnt += 1
                        
                        change_rate = change_a / change_f
                        
                        total_change += abs(change_rate)
                        
                        if change_rate > 0 :
                            p_change.append(change_rate)
                        else:
                            m_change.append(change_rate)
                        
                row = [ori_csv[idx], ori_csv[idx], derivative, change_rate, csv['Area'][idx], c_x, c_y, int(blink)]
            else:
                derivative = 0
                
                row = [ori_csv[idx], before_area, derivative,  change_rate, csv['Area'][idx], c_x, c_y, int(blink)]
            s_csv_list.append(row)
            
            if blink == True:
                close_cnt += 1
                if idx < csv.shape[0] - 1:
                    diff = csv['Area'][idx+1] - csv['Area'][idx] 
                    if diff < 0:
                        blink = False
                        bf_list.append(idx)
                        bf_lists.append(bf_list)
                        
                if idx == csv.shape[0] - 1:
                    bf_list.append(idx)
                    bf_lists.append(bf_list)
        
        s_csv_list[0].append(total_change / change_cnt)
        s_csv_list[0].append(sum(m_change) / len(m_change))
        s_csv_list[0].append(sum(p_change) / len(p_change))
        s_csv_list[0].append(blink_cnt)
        s_csv_list[0].append(blink_cnt / csv.shape[0])
        s_csv_list[0].append(close_cnt)
        s_csv_list[0].append(close_cnt / csv.shape[0])
        s_csv = pd.DataFrame(s_csv_list, columns=csv_column)
        csv_name = './' + file_name + '.csv'
        s_csv.to_csv(csv_name)
b'Skipping line 260: expected 3 fields, saw 5\nSkipping line 1583: expected 3 fields, saw 5\nSkipping line 6147: expected 3 fields, saw 5\n' b'Skipping line 9: expected 3 fields, saw 7\nSkipping line 142: expected 3 fields, saw 5\n' b'Skipping line 10794: expected 3 fields, saw 5\nSkipping line 11584: expected 3 fields, saw 5\nSkipping line 14503: expected 3 fields, saw 5\nSkipping line 19828: expected 3 fields, saw 5\nSkipping line 20270: expected 3 fields, saw 5\n' b'Skipping line 2352: expected 3 fields, saw 5\nSkipping line 2895: expected 3 fields, saw 5\nSkipping line 7709: expected 3 fields, saw 5\nSkipping line 9006: expected 3 fields, saw 5\nSkipping line 9892: expected 3 fields, saw 5\nSkipping line 15145: expected 3 fields, saw 5\nSkipping line 15261: expected 3 fields, saw 5\nSkipping line 15601: expected 3 fields, saw 5\nSkipping line 15828: expected 3 fields, saw 5\nSkipping line 15853: expected 3 fields, saw 5\nSkipping line 15893: expected 3 fields, saw 5\nSkipping line 20393: expected 3 fields, saw 5\nSkipping line 21258: expected 3 fields, saw 5\nSkipping line 21457: expected 3 fields, saw 5\nSkipping line 21671: expected 3 fields, saw 5\nSkipping line 21672: expected 3 fields, saw 5\nSkipping line 21709: expected 3 fields, saw 5\nSkipping line 21710: expected 3 fields, saw 5\nSkipping line 21757: expected 3 fields, saw 5\nSkipping line 21866: expected 3 fields, saw 5\nSkipping line 22771: expected 3 fields, saw 5\n'
In [65]:
csv_paths
Out[65]:
['E:/segmentation_pupil\\1_after_b\\1_after_b_1.csv',
 'E:/segmentation_pupil\\1_after_b\\1_after_b_15.csv',
 'E:/segmentation_pupil\\1_after_g\\1_after_g_1.csv',
 'E:/segmentation_pupil\\1_after_g\\1_after_g_15.csv',
 'E:/segmentation_pupil\\1_after_t\\1_after_t_1.csv',
 'E:/segmentation_pupil\\1_after_t\\1_after_t_15.csv',
 'E:/segmentation_pupil\\1_before\\1_before_1.csv',
 'E:/segmentation_pupil\\1_before\\1_before_15.csv',
 'E:/segmentation_pupil\\2_after_b\\2_after_b_1.csv',
 'E:/segmentation_pupil\\2_after_b\\2_after_b_15.csv',
 'E:/segmentation_pupil\\2_after_g\\2_after_g_1.csv',
 'E:/segmentation_pupil\\2_after_g\\2_after_g_15.csv',
 'E:/segmentation_pupil\\2_after_t\\2_after_t_1.csv',
 'E:/segmentation_pupil\\2_after_t\\2_after_t_15.csv',
 'E:/segmentation_pupil\\2_before\\2_before_1.csv',
 'E:/segmentation_pupil\\2_before\\2_before_15.csv']

Version 2

In [18]:
import csv
import glob
import jovian
import numpy
import pandas as pd

root_path = 'E:/segmentation_pupil/'

csv_paths = glob.glob(root_path + '*/*.csv')
csv_paths = csv_paths[:2]

for csv_path in csv_paths:
    file_name = csv_path.split('\\')[-1][:-4]

    csv = pd.read_csv(csv_path)
    ori_csv = list(csv['Area'])
    
    if file_name.split('_')[-1] == '1':
        mean_bright = 0
        for idx in range(csv.shape[0]):
            mean_bright += csv['Area'][idx]
        mean_bright /= csv.shape[0]
    else:
        s_csv_list = [] 
        csv['Area'] -= mean_bright
        
        blink = False
        blink_cnt = 0
        close_cnt = 0
        bf_lists = []
        for idx in range(csv.shape[0]):
            c_x = csv['Center'][idx].strip('()').split(',')[0]
            c_y = csv['Center'][idx].strip('()').split(',')[1].lstrip()
            
            if blink == False:
                if csv['Area'][idx] == -mean_bright:
                    blink = True
                    bf_list = [idx]
                    blink_start_idx = idx
                    blink_cnt += 1  
                    
                    before_area = ori_csv[idx-1]
                    
            if blink == False:
                s_csv_list.append([ori_csv[idx], ori_csv[idx], csv['Area'][idx], c_x, c_y, int(blink)])
            else:
                s_csv_list.append([ori_csv[idx], before_area, csv['Area'][idx], c_x, c_y, int(blink)])
            
            if blink == True:
                close_cnt += 1
                if idx < csv.shape[0] - 1:
                    diff = csv['Area'][idx+1] - csv['Area'][idx] 
                    if diff < 0:
                        blink = False
                        bf_list.append(idx)
                        bf_lists.append(bf_list)
                        
                if idx == csv.shape[0] - 1:
                    bf_list.append(idx)
                    bf_lists.append(bf_list)
        
        s_csv_list[0].append(blink_cnt)
        s_csv_list[0].append(blink_cnt / csv.shape[0])
        s_csv_list[0].append(close_cnt)
        s_csv_list[0].append(close_cnt / csv.shape[0])
        s_csv = pd.DataFrame(s_csv_list, columns=['ori_Area', 'remove_blink', 'nom_Area', 'Center_x', 'Center_y', 'Blink', 'Blink_cnt', 'Blink_rate', 'Close_cnt', 'Close_rate'])
        s_csv.to_csv('./result.csv')
In [ ]:
str.lstrip() # 왼쪽 공백 삭제
str.strip('()') # 양쪽의 특정 문자 삭제하기

Version 1

In [ ]:
import csv
import glob
import jovian
import numpy
import pandas as pd

root_path = 'E:/segmentation_pupil/'

csv_paths = glob.glob(root_path + '*/*.csv')
csv_paths = csv_paths[:2]

for csv_path in csv_paths:
    file_name = csv_path.split('\\')[-1][:-4]

    csv = pd.read_csv(csv_path)
    
    if file_name.split('_')[-1] == '1':
        mean_bright = 0
        for idx in range(csv.shape[0]):
            mean_bright += csv['Area'][idx]
        mean_bright /= csv.shape[0]
    else:
        s_csv_list = []
        
        csv['Area'] -= mean_bright
        
        blink = False
        blink_cnt = 0
        close_cnt = 0
        bf_lists = []
        for idx in range(csv.shape[0]):
            c_x = csv['Center'][idx].strip('()').split(',')[0]
            c_y = csv['Center'][idx].strip('()').split(',')[1].lstrip()
            
            if blink == False:
                if csv['Area'][idx] == -mean_bright:
                    blink = True
                    bf_list = [idx]
                    blink_start_idx = idx
                    blink_cnt += 1
                
            s_csv_list.append([csv['Area'][idx], c_x, c_y, int(blink)])
                
            if blink == True:
                close_cnt += 1
                if idx < csv.shape[0] - 1:
                    diff = csv['Area'][idx+1] - csv['Area'][idx] 
                    if diff < 0:
                        blink = False
                        bf_list.append(idx)
                        bf_lists.append(bf_list)
                        
                if idx == csv.shape[0] - 1:
                    bf_list.append(idx)
                    bf_lists.append(bf_list)
        
        s_csv_list[0].append(blink_cnt)
        s_csv_list[0].append(blink_cnt / csv.shape[0])
        s_csv_list[0].append(close_cnt)
        s_csv_list[0].append(close_cnt / csv.shape[0])
        s_csv = pd.DataFrame(s_csv_list, columns=['Area', 'Center_x', 'Center_y', 'Blink', 'Blink_cnt', 'Blink_rate', 'Close_cnt', 'Close_rate'])
        s_csv.to_csv('./result.csv')