# -*- coding:utf-8 -*- #!/usr/bin/env python from __future__ import print_function from base import Lo, create_csv, TARGET_DIR import os import csv import sys los = Lo() xmodels = { 'IMSf': {}, 'IMSD': {}, 'IMHf': {}, 'IMHD': {}, 'IFSf': {}, 'IFSD': {}, 'IFHf': {}, 'IFHD': {}, 'CMSf': {}, 'CMSD': {}, 'CMHf': {}, 'CMHD': {}, 'CFSf': {}, 'CFSD': {}, 'CFHf': {}, 'CFHD': {}, 'PMSf': {}, 'PMSD': {}, 'PMHf': {}, 'PMHD': {}, 'PFSf': {}, 'PFSD': {}, 'PFHf': {}, 'PFHD': {}, } ''' แก้ตรงนี้ >> RANK_ORDER << ถ้าจะเพิ่ม rank ใส่อะไรก็ใส่ไปตามลำดับ ''' RANK_ORDER = ['1', '2', '3', '4'] fs = ('Case1_LS.csv', 'Case1_Gender.csv', 'Case1_Level.csv', 'Case1_SciF.csv') def process_s2_data(): sum_result = {} rank_result = {} step, rank = '', '' for i in fs: f_name = '%s-result.csv' % i # print(f_name) with open(os.path.join(TARGET_DIR, f_name), 'rb') as f: rows = csv.reader(f) for r in rows: if r[0] in ('rank_no', 'All Result'): step = r[0] rank = r[1] continue if step == 'rank_no': if r[0] not in rank_result: rank_result[r[0]] = {} rank_result[r[0]][rank] = r[1].split(',') elif step == 'All Result': if not r[0]: continue if r[0] in sum_result: # print('exists:', r[1], '|', step, '|', rank) pass sum_result[r[0]] = r[1].split(',') # print(rank_result['C'].keys()) return { 'sum': sum_result, 'rank': rank_result } def produce_s2_part1(sum_result): # create xxxx-1.csv for ii in xmodels.keys(): rows = [] rows.append([ii, ]) for i in ii: if i not in sum_result: continue rows.append([i, ','.join(sum_result[i])]) _f = '%s-1.csv' % ii create_csv(_f, rows, directory='part2') def produce_s2_part2(rank_result): # create xxxx-2.csv for ii in xmodels.keys(): rows = [] for rank in RANK_ORDER: # print(ii, ':rank:', rank) rows.append(['Rank#%s' % rank, 'rank', rank]) for i in ii: if i not in rank_result: continue if rank not in rank_result[i]: continue rows.append([i, ','.join(rank_result[i][rank])]) _f = '%s-2.csv' % ii create_csv(_f, rows, directory='part2') def process_lo_weight(): los = {} for i in fs: f_name = '%s-output.csv' % i with open(os.path.join(TARGET_DIR, f_name), 'rb') as f: rows = csv.reader(f) for r in rows: _los = r[1].split(',') for _l in _los: if _l not in los: los[_l] = {} if r[0] in los[_l]: # print('exists:', _l, ':', r[0], ' > ') # los[_l]['%s2' % r[0]] = r[2:] pass los[_l][r[0]] = r[2:] return los def produce_part3(sum_result, lo_weight): # produce part3 for ii in xmodels.keys(): rows = [] lo_xmodels = set() for i in ii: if i in sum_result: lo_xmodels = lo_xmodels.union(set(sum_result[i])) lo_xmodels = sorted(list(lo_xmodels)) for l in lo_xmodels: ## build container to store sum of all weight lo_sum = [0 for k in xrange(0, 25)] if l not in lo_weight: continue for i in ii: if i not in lo_weight[l]: continue lo_sum = [ float(x) + float(y) for x, y in zip(lo_weight[l][i][9:], lo_sum)] rows.append([l] + lo_sum) _f = '%s-3.csv' % ii create_csv(_f, rows, directory='part2') def main(*argv): _r = process_s2_data() _lw = process_lo_weight() produce_s2_part1(_r['sum']) produce_s2_part2(_r['rank']) produce_part3(_r['sum'], _lw) if __name__ == '__main__': if len(sys.argv) > 1: main(sys.argv[1:]) else: main()