import os
import sys
import glob
import numpy as np
import pandas as pd
from tqdm import tqdm
import logging

from ClsFormProcessor import ClsFormProcessor

logger = logging.getLogger()
fh = logging.FileHandler('./log/RibFormProcessor.log',encoding='utf-8')
sh = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
fh.setFormatter(formatter)  
sh.setFormatter(formatter)

logger.addHandler(fh)   
logger.addHandler(sh)

logger.setLevel(10) 

class RibFormProcessor(ClsFormProcessor):
    def __init__(self,**kwargs):
        ClsFormProcessor.__init__(self,**kwargs)
        self.position_type_kw = u'骨折部位'    ##### target val is 3
        self.frac_flag_kw = u'是否骨折' ##### target val is 1
        self.keep_flag_kw = u'是否保留' ####### target val is 0
        self.target_organs = [3]

        
    def _getInfoMapDict(self):
        '''
        需要子类重写
        '''
        type_map_dict = {}
        for kw_before,kw_after in zip(self.type_kws,self.type_cor_kws):
            type_map_dict[kw_before] = kw_after
                     
        
        self.rename_dict = {
        self.task_id_kw:'task_id',
        self.uid_kw:'uid',
        self.data_kw:'data',
        self.position_type_kw:'organ',
        self.frac_flag_kw:'frac_flag',
        self.keep_flag_kw:'keep_flag'
        } 
        self.rename_dict.update(type_map_dict)
        ################# update kw related info
        
    def _FilterForm(self):
        print ('='*60)
        print (self.df.columns)
        self.df = self.df[(self.df[self.frac_flag_kw]==1)&(self.df[self.keep_flag_kw]==0)]
        self.df.reset_index(drop=True,inplace=True)
        
        logger.info('Number of records before filter %d '%(len(self.df)))
        self.df = self.df[self.df[self.position_type_kw].isin(self.target_organs)]
        self.df.reset_index(drop=True,inplace=True)
        logger.info('Number of records after filter %d '%(len(self.df)))
        

if __name__ == "__main__":

    paths = ['/fileser/zrx/Rib/infos/20201026/Rib_result_TASK_3813_3814_3815.csv']
    type_kws = [u'骨痂(v2)',u'骨折类型']
    type_cor_kws = ['poroma','frac_type']
    form_save_path = '/fileser/xupl/cls_data_preprocess/RibCls/label'
    uid_save_path = '/fileser/xupl/cls_data_preprocess/RibCls/info/uid'
    data_base_path = '/fileser/CT_RIB/data/image/res0/'
    folder_name = '20210107_test'
    iou_th = 0.0

    param_list = {
        'paths':paths,
        'type_kws':type_kws,
        'type_cor_kws':type_cor_kws,
        'form_save_path':form_save_path,
        'uid_save_path':uid_save_path,
        'data_base_path':data_base_path,
        'folder_name':folder_name,
        'iou_th':iou_th
    }

    processor = RibFormProcessor(**param_list)
    processor()
