|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding:utf-8 -*-
|
|
|
|
'''
|
|
|
|
Step 1
|
|
|
|
'''
|
|
|
|
from __future__ import print_function
|
|
|
|
from base import Lo, RAW_DIR, TARGET_DIR
|
|
|
|
import os
|
|
|
|
import csv
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
los = Lo()
|
|
|
|
|
|
|
|
|
|
|
|
def get_rank_thing(filename, **kwargs):
|
|
|
|
model_count = {
|
|
|
|
'I': {}, 'P': {}, 'C': {}, 'M': {}, 'F': {},
|
|
|
|
'H': {}, 'S': {}, 'D': {}, 'f': {}
|
|
|
|
}
|
|
|
|
output_filename = kwargs.get('output', '%s-output.csv' % filename)
|
|
|
|
count = tot = lo_match = 0
|
|
|
|
with open(os.path.join(TARGET_DIR, output_filename), 'wb') as o:
|
|
|
|
owriter = csv.writer(o, delimiter=',', quotechar='"',
|
|
|
|
quoting=csv.QUOTE_ALL)
|
|
|
|
with open(os.path.join(RAW_DIR, filename)) as f:
|
|
|
|
reader = csv.reader(f)
|
|
|
|
reader.next()
|
|
|
|
for r in reader:
|
|
|
|
mn, rank_no = r[0], r[9]
|
|
|
|
w = [
|
|
|
|
r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], # July 1
|
|
|
|
rank_no, # July 1
|
|
|
|
]
|
|
|
|
## add 24 model's weight
|
|
|
|
for i in xrange(10, 34):
|
|
|
|
w.append(r[i])
|
|
|
|
q = (
|
|
|
|
('A', r[1]), ('B', r[2]), ('C', r[3]), ('D', r[4]),
|
|
|
|
('E', r[5]), ('F', r[6]), ('G', r[7]), ('H', r[8]),
|
|
|
|
)
|
|
|
|
result = los.get_mm_tuple(q)
|
|
|
|
tot += 1
|
|
|
|
lo_match += len(result)
|
|
|
|
if result:
|
|
|
|
# print(u'%s > %s' % (r[0], list(result)))
|
|
|
|
count += 1
|
|
|
|
owriter.writerow([
|
|
|
|
mn,
|
|
|
|
','.join(list(result)),
|
|
|
|
len(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
|
|
|
|
|
|
|
|
|
|
|
|
def main(*argv):
|
|
|
|
fs = ('Case1_LS.csv', 'Case1_Gender.csv',
|
|
|
|
'Case1_Level.csv', 'Case1_SciF.csv')
|
|
|
|
result = ['IPC', 'MF', 'HS', 'fD']
|
|
|
|
for f in xrange(0, len(fs)):
|
|
|
|
mc = get_rank_thing(fs[f])
|
|
|
|
x = {}
|
|
|
|
for i in result[f]:
|
|
|
|
x[i] = mc[i]
|
|
|
|
# print('x: ', x)
|
|
|
|
mx = [set(j) for i, j in x.items()]
|
|
|
|
order = sorted(list(set.union(*mx)))
|
|
|
|
sum_file = '%s-result.csv' % fs[f]
|
|
|
|
with open(os.path.join(TARGET_DIR, sum_file), 'wb') as o:
|
|
|
|
result_writer = csv.writer(o, delimiter=',', quotechar='"',
|
|
|
|
quoting=csv.QUOTE_ALL)
|
|
|
|
m = {}
|
|
|
|
# print(j, ':', order)
|
|
|
|
for i in xrange(0, len(order)):
|
|
|
|
result_writer.writerow(['rank_no', order[i]])
|
|
|
|
# print('count , ', order[i], ' / ', type(order[i]))
|
|
|
|
for j in x.keys():
|
|
|
|
try:
|
|
|
|
# print(j, ', ', s)
|
|
|
|
lo_result = mc[j][order[i]]
|
|
|
|
# print(order[i], ' == ', len(lo_result))
|
|
|
|
# if i == 2 and len(mc[j]) > 3:
|
|
|
|
# for _i in xrange(3, len(mc[j])):
|
|
|
|
# lo_result = lo_result.union(mc[j][order[_i]])
|
|
|
|
# # print(' ==EX== ', len(lo_result))
|
|
|
|
if j not in m:
|
|
|
|
m[j] = lo_result
|
|
|
|
else:
|
|
|
|
m[j] = m[j].union(lo_result)
|
|
|
|
result_writer.writerow([j, ','.join(list(lo_result))])
|
|
|
|
except KeyError:
|
|
|
|
print(' no key ', j, '[', order[i], ']')
|
|
|
|
result_writer.writerow(['All Result', ''])
|
|
|
|
for _m in m.keys():
|
|
|
|
result_writer.writerow([_m, ','.join(list(m[_m]))])
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
main(sys.argv[1:])
|
|
|
|
else:
|
|
|
|
main()
|