Browse Source

[s11] alpha

master
sipp11 10 years ago
parent
commit
1a812e23a8
  1. 117
      s11.py

117
s11.py

@ -1,35 +1,135 @@
# -*- coding: utf-8 -*-
#!/usr/bin/env python
from __future__ import absolute_import, print_function
import os
import sys
import csv
import getopt
import time
from base import RAW_DIR, TARGET_DIR, create_csv
OCT_RAW_DIR = os.path.join(RAW_DIR, '..', 'oct')
verbose = False
writefile = False
def get_keyword_level(ksearch=None):
'''
if ksearch is none will return dict with all K_search
'''
groupy = {}
with open(os.path.join(OCT_RAW_DIR, 'keyword_level.csv'), 'rU') as f:
rows = csv.reader(f)
for row in rows:
if row[1] not in groupy:
groupy[row[0]] = row
if ksearch and ksearch in groupy:
return groupy[ksearch]
else:
return groupy
def get_keyword_weight(ksearch=None):
'''
if ksearch is none will return dict with all K_search
'''
groupy = {}
with open(os.path.join(OCT_RAW_DIR, 'keyword_weight.csv'), 'rU') as f:
rows = csv.reader(f)
for row in rows:
if row[1] not in groupy:
groupy[row[0]] = row
if ksearch and ksearch in groupy:
return groupy[ksearch]
else:
return groupy
def get_s10_result(model, rtype):
s10_fname = '%s-%s.csv' % (model, rtype)
filepath = os.path.join(TARGET_DIR, 'part10', s10_fname)
if not os.path.exists(filepath):
print("S10's output file not found")
sys.exit(2)
## TODO: this should force getting live data instead of throw error
new_data = []
with open(filepath, 'rb') as f:
new_data = [row for row in csv.reader(f)]
return new_data
def processingS11(model, rtype, ksearch):
s10_data = get_s10_result(model, rtype)
if not s10_data:
print('[s10] zero record. Thus, no output for s11.')
sys.exit()
## get keyword data
level = get_keyword_level(ksearch)
weight = get_keyword_weight(ksearch)
a2s = [(s[:1] + s[35:]) for s in s10_data]
# * by kw_weight and + with kw_level
result = []
for i in a2s:
### replace '' (blank) with 0 zero too
data_only = [_ or 0 for _ in i[1:]]
w_only = [_ or 0 for _ in weight[1:]]
l_only = [_ or 0 for _ in level[1:]]
### [s11] (2) & (3)
zipped_list = zip(data_only, w_only, l_only)
multiply_n_add = [(float(_[0])*float(_[1]))+float(_[2]) for _ in zipped_list]
result.append(i[:1]+multiply_n_add)
## concat with s10_data
finale = []
for i in xrange(0, len(result)):
if verbose:
print(','.join([('%s' % k if k != 0 else '-') for k in result[i]]))
finale.append(
s10_data[i]+result[i][1:]
)
if writefile:
output_name = '%s-%s-%s.csv' % (model, rtype, ksearch)
create_csv(output_name, finale, directory='part11')
def usage():
print('Help:')
print(
'''Help:
[opts]
-m, --model Model Name [4-char]
-r, --rtype R_Type [1-4]
-k, --ksearch K_search [a-s]
-v verbose
-w write output to file
Usage:
$ python s11.py [opts] [value]
Example:
$ python s11.py -mCFHD -r3 -kA
$ python s11.py -mCFHD -r3 -kB -v << show more information
$ python s11.py -m CFHD -r 3 -k B -v << same as above
$ python s11.py -m CFHD -r 3 -k F -w << write output file too.
$ python s11.py -mCFHD -r3 -k C -vw << show more information & output file.
''')
def main():
override = False
try:
opts, args = getopt.getopt(sys.argv[1:], 'm:r:k:v', ['model=', 'rtype=', 'ksearch='])
opts, args = getopt.getopt(sys.argv[1:], 'm:r:k:vw', ['model=', 'rtype=', 'ksearch='])
except getopt.GetoptError:
usage()
sys.exit(2)
verbose = False
model_value = rtype_value = k_value = None
for opt, arg in opts:
if opt == "-v":
global verbose
verbose = True
elif opt == "-w":
global writefile
writefile = True
elif opt in ('-m', '--model'):
## TODO: filter model availabel and throw exception if invalid
if len(arg) != 4:
@ -51,8 +151,11 @@ def main():
usage()
sys.exit(2)
print('model: %s | r_type: %s | k_search: %s | verbose: %s' % (
model_value, rtype_value, k_value, verbose))
print('[s11] model: %s | r_type: %s | k_search: %s' % (
model_value, rtype_value, k_value))
processingS11(model_value, rtype_value, k_value)
if __name__ == '__main__':
main()

Loading…
Cancel
Save