Browse Source

Update step 1 completed

master
sipp11 11 years ago
parent
commit
6e4fb4d776
  1. 14
      base.py
  2. 108
      s1.py

14
base.py

@ -32,14 +32,15 @@ class Lo:
'H': '%s%s%s' % (r[10], r[22], r[27]), 'H': '%s%s%s' % (r[10], r[22], r[27]),
} }
for k, v in t.items(): for k, v in t.items():
if v not in self.model[k]: self.model[k][v] = [] if v not in self.model[k]:
self.model[k][v] = []
self.model[k][v].append(lo_id) self.model[k][v].append(lo_id)
self.los[lo_id] = t.copy() self.los[lo_id] = t.copy()
def get(self, lo_id): def get(self, lo_id):
try: try:
return self.los[lo_id] return self.los[lo_id]
except (IndexError, KeyError) as e: except (IndexError, KeyError):
return {} return {}
def get_lo_list(self): def get_lo_list(self):
@ -61,10 +62,13 @@ class Lo:
return [] return []
def get_mm_tuple(self, *args): def get_mm_tuple(self, *args):
if len(args) > 1 or type(args) != type(()): return [] if len(args) > 1 or not isinstance(args, tuple):
return []
r = [] r = []
for i in args[0]: for i in args[0]:
if len(i) != 2 or not i[1]: continue if len(i) != 2 or not i[1]:
continue
m = self.get_mm(i[0], i[1]) m = self.get_mm(i[0], i[1])
if m: r.append(m) if m:
r.append(m)
return set.intersection(*[set(k) for k in r]) return set.intersection(*[set(k) for k in r])

108
s1.py

@ -1,50 +1,84 @@
# -*- coding:utf-8 -*-
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function from __future__ import print_function
from base import Lo from base import Lo
import os, csv import os
import csv
import sys
los = Lo() los = Lo()
model_count = {
'I': {}, 'P': {}, 'C': {}, 'M': {}, 'F': {},
'H': {}, 'S': {}, 'D': {}, 'F': {}
}
def get_rank_thing(filename):
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 count = tot = lo_match = 0
with open(os.path.join(os.getcwd(), 'raw', filename)) as f: with open(os.path.join(os.getcwd(), 'build', output_filename), 'wb') as o:
reader = csv.reader(f) owriter = csv.writer(o, delimiter=',', quotechar='"',
reader.next() quoting=csv.QUOTE_ALL)
for r in reader: with open(os.path.join(os.getcwd(), 'raw', filename)) as f:
mn, cnt = r[0], r[9] reader = csv.reader(f)
q = ( reader.next()
('A', r[1]), ('B', r[2]), ('C', r[3]), ('D', r[4]), for r in reader:
('E', r[5]), ('F', r[6]), ('G', r[7]), ('H', r[8]), mn, cnt = r[0], r[9]
) w = [r[10], r[11], r[12], r[13], r[14]]
result = los.get_mm_tuple(q) q = (
tot += 1 ('A', r[1]), ('B', r[2]), ('C', r[3]), ('D', r[4]),
lo_match += len(result) ('E', r[5]), ('F', r[6]), ('G', r[7]), ('H', r[8]),
if result: )
# print(u'%s > %s' % (r[0], list(result))) result = los.get_mm_tuple(q)
count += 1 tot += 1
if cnt not in model_count[mn]: model_count[mn][cnt] = set() lo_match += len(result)
model_count[mn][cnt] = model_count[mn][cnt].union(result) if result:
# print(u'%s > %s' % (r[0], list(result)))
count += 1
owriter.writerow([','.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)
print(u'[%s]\n Count = %s | Total = %s | Lo_w_match = %s' % ( print(u'[%s]\n Count = %s | Total = %s | Lo_w_match = %s' % (
filename, count, tot, lo_match)) filename, count, tot, lo_match))
return model_count
def main():
fs = ('Case1_LS.csv', 'Case1_Gender.csv', 'Case1_Level.csv', 'Case1_SciF.csv')
for f in fs:
get_rank_thing(f)
for i, j in model_count.items():
print('\n===== %s =====' % i)
for k, v in j.items():
print('%s: \n' \
' %s' % (k, list(v)))
def main(*argv):
fs = ('Case1_LS.csv', 'Case1_Gender.csv',
'Case1_Level.csv', 'Case1_SciF.csv')
result = ['IPC', 'MF', 'HS', 'FS']
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)), reverse=True)
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='"',
quoting=csv.QUOTE_ALL)
for i in xrange(0, 3):
result_writer.writerow(['count', 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))
result_writer.writerow([j, ','.join(list(lo_result))])
except KeyError:
print(' no key ', j, '[', order[i], ']')
if __name__ == '__main__': if __name__ == '__main__':
main() if len(sys.argv) > 1:
main(sys.argv[1:])
else:
main()

Loading…
Cancel
Save