From ef5eabed2b50e794894d984b8147afb2c9343c98 Mon Sep 17 00:00:00 2001 From: sipp11 Date: Wed, 25 Jun 2014 05:41:28 +0700 Subject: [PATCH] Update algorithm by request --- s1.py | 14 +++++++------- s2.py | 62 +++++++++++++++-------------------------------------------- s3.py | 38 ++++++++++++++++++++---------------- 3 files changed, 43 insertions(+), 71 deletions(-) diff --git a/s1.py b/s1.py index f7f31c1..6bfc36c 100755 --- a/s1.py +++ b/s1.py @@ -24,7 +24,7 @@ def get_rank_thing(filename, **kwargs): reader = csv.reader(f) reader.next() for r in reader: - mn, cnt = r[0], r[9] + mn, rank_no = r[0], r[9] w = [r[10], r[11], r[12], r[13], r[14]] q = ( ('A', r[1]), ('B', r[2]), ('C', r[3]), ('D', r[4]), @@ -36,10 +36,10 @@ def get_rank_thing(filename, **kwargs): if result: # print(u'%s > %s' % (r[0], list(result))) count += 1 - owriter.writerow([mn, ','.join(list(result)), cnt] + w) - if cnt not in model_count[mn]: - model_count[mn][cnt] = set() - model_count[mn][cnt] = model_count[mn][cnt].union(result) + owriter.writerow([mn, ','.join(list(result)), rank_no] + w) + if rank_no not in model_count[mn]: + model_count[mn][rank_no] = set() + model_count[mn][rank_no] = model_count[mn][rank_no].union(result) print(u'[%s]\n Count = %s | Total = %s | Lo_w_match = %s' % ( filename, count, tot, lo_match)) return model_count @@ -56,7 +56,7 @@ def main(*argv): x[i] = mc[i] # print('x: ', x) mx = [set(j) for i, j in x.items()] - order = sorted(list(set.union(*mx)), reverse=True) + order = sorted(list(set.union(*mx))) sum_file = '%s-result.csv' % fs[f] with open(os.path.join(os.getcwd(), 'build', sum_file), 'wb') as o: result_writer = csv.writer(o, delimiter=',', quotechar='"', @@ -64,7 +64,7 @@ def main(*argv): m = {} # print(j, ':', order) for i in xrange(0, len(order)): - result_writer.writerow(['count', order[i]]) + result_writer.writerow(['rank_no', order[i]]) # print('count , ', order[i], ' / ', type(order[i])) for j in x.keys(): try: diff --git a/s2.py b/s2.py index f28d858..b818288 100755 --- a/s2.py +++ b/s2.py @@ -14,17 +14,11 @@ xmodels = { 'CFSf': {}, 'CFSD': {}, 'CFHf': {}, 'CFHD': {}, 'PMSf': {}, 'PMSD': {}, 'PMHf': {}, 'PMHD': {}, 'PFSf': {}, 'PFSD': {}, 'PFHf': {}, 'PFHD': {}, } -rk_conv = { - 'I': ['4', '3', '2', ''], - 'P': ['4', '3', '2', ''], - 'C': ['4', '3', '2', ''], - 'F': ['5', '4', '3', '2', ''], - 'M': ['5', '4', '3', '2', ''], - 'H': ['4', '3', '2', ''], - 'S': ['4', '3', '2', ''], - 'f': ['4', '3', '2', ''], - 'D': ['4', '3', '2', ''], -} +''' +แก้ตรงนี้ >> RANK_ORDER << ถ้าจะเพิ่ม rank ใส่อะไรก็ใส่ไปตามลำดับ +''' +RANK_ORDER = ['1', '2', '3', '4'] + fs = ('Case1_LS.csv', 'Case1_Gender.csv', 'Case1_Level.csv', 'Case1_SciF.csv') @@ -38,11 +32,11 @@ def process_s2_data(): with open(os.path.join(os.getcwd(), 'build', f_name), 'rb') as f: rows = csv.reader(f) for r in rows: - if r[0] in ('count', 'All Result'): + if r[0] in ('rank_no', 'All Result'): step = r[0] - rank = r[1] if r[0] == 'count' else '' + rank = r[1] continue - if step == 'count': + if step == 'rank_no': if r[0] not in rank_result: rank_result[r[0]] = {} rank_result[r[0]][rank] = r[1].split(',') @@ -53,6 +47,7 @@ def process_s2_data(): # 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 } @@ -71,46 +66,19 @@ def produce_s2_part1(sum_result): create_csv(_f, rows, directory='part2') -def produce_s2_part2_old(rank_result): +def produce_s2_part2(rank_result): # create xxxx-2.csv for ii in xmodels.keys(): rows = [] - # get rank count first - ro = set() - for rk in xrange(0, 4): - for i in ii: - if i not in rank_result: - continue - ro = ro.union(set(rank_result[i].keys())) - - rank_no = 1 - for rk in sorted(list(ro), reverse=True): - # print(ii, ':', rk) - rows.append(['Rank#%s' % rank_no, 'rank', rank_no, 'count', rk]) - rank_no += 1 + for rank in RANK_ORDER: + # print(ii, ':rank:', rank) + rows.append(['Rank#%s' % rank, 'rank', rank]) for i in ii: - # find order if i not in rank_result: continue - if rk in rank_result[i]: - rows.append([i, ','.join(rank_result[i][rk])]) - _f = '%s-2.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 rk in xrange(0, 4): - rank_no = rk + 1 - # print(ii, ':rank:', rank_no) - rows.append(['Rank#%s' % rank_no, 'rank', rank_no]) - for i in ii: - if not rk_conv[i][rk]: + if rank not in rank_result[i]: continue - # print(i, '::', rk_conv[i][rk]) - rows.append([i, ','.join(rank_result[i][rk_conv[i][rk]])]) + rows.append([i, ','.join(rank_result[i][rank])]) _f = '%s-2.csv' % ii create_csv(_f, rows, directory='part2') diff --git a/s3.py b/s3.py index 6319e78..8aab51b 100755 --- a/s3.py +++ b/s3.py @@ -8,6 +8,7 @@ from base import Lo, create_csv import os import csv import sys +from s2 import RANK_ORDER los = Lo() @@ -66,20 +67,21 @@ def produce_match_all_mra(xdata): if j not in lo_count: lo_count[j] = 0 for i in ii: - for rank in '1234': - if i in xdata[ii][rank] and j in xdata[ii][rank][i]: + for rank in RANK_ORDER: + if rank in xdata[ii] and i in xdata[ii][rank] \ + and j in xdata[ii][rank][i]: lo_count[j] += 1 - rows = [ - ['4/4', ','.join([k for (k, v) in lo_count.items() if v == 4])], - ['3/4', ','.join([k for (k, v) in lo_count.items() if v == 3])], - ['2/4', ','.join([k for (k, v) in lo_count.items() if v == 2])], - ['1/4', ','.join([k for (k, v) in lo_count.items() if v == 1])], - ] + rows = [] + max_count = len(RANK_ORDER) + for n in xrange(max_count, 0, -1): + rows.append([ + '%s/%s' % (n, max_count), + ','.join([k for (k, v) in lo_count.items() if v == n]) + ]) _f = '%s-all.csv' % (ii,) create_csv(_f, rows, directory='part3') - def produce_match_rank_mra(xdata, rank): for ii in xmodels.keys(): lo_count = {} @@ -87,14 +89,16 @@ def produce_match_rank_mra(xdata, rank): if j not in lo_count: lo_count[j] = 0 for i in ii: - if i in xdata[ii][rank] and j in xdata[ii][rank][i]: + if rank in xdata[ii] and i in xdata[ii][rank] and \ + j in xdata[ii][rank][i]: lo_count[j] += 1 - rows = [ - ['4/4', ','.join([k for (k, v) in lo_count.items() if v == 4])], - ['3/4', ','.join([k for (k, v) in lo_count.items() if v == 3])], - ['2/4', ','.join([k for (k, v) in lo_count.items() if v == 2])], - ['1/4', ','.join([k for (k, v) in lo_count.items() if v == 1])], - ] + rows = [] + max_count = len(RANK_ORDER) + for n in xrange(max_count, 0, -1): + rows.append([ + '%s/%s' % (n, max_count), + ','.join([k for (k, v) in lo_count.items() if v == n]) + ]) _f = '%s-rank-%s.csv' % (ii, rank) create_csv(_f, rows, directory='part3') @@ -103,7 +107,7 @@ def main(*argv): xmm = process_s2_data() produce_match_all(xmm) produce_match_all_mra(xmm) - for i in '1234': + for i in RANK_ORDER: produce_match_rank_mra(xmm, i)