mirror of https://github.com/openXC7/prjxray.git
timfuz: sub2csv
Signed-off-by: John McMaster <johndmcmaster@gmail.com>
This commit is contained in:
parent
632fbffc4b
commit
d4d0af9d9f
|
|
@ -0,0 +1,105 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from timfuz import Benchmark, Ar_di2np, loadc_Ads_b, index_names, A_ds2np, simplify_rows
|
||||||
|
import numpy as np
|
||||||
|
import glob
|
||||||
|
import math
|
||||||
|
import json
|
||||||
|
import sympy
|
||||||
|
from collections import OrderedDict
|
||||||
|
from fractions import Fraction
|
||||||
|
|
||||||
|
def mlcm(xs):
|
||||||
|
'''
|
||||||
|
Find the LCM between elements in a group
|
||||||
|
'''
|
||||||
|
l = xs[0]
|
||||||
|
for n in xs:
|
||||||
|
for m in xs:
|
||||||
|
lthis = lcm(n, m)
|
||||||
|
l = int(max(lthis, l))
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def write_state(state, fout):
|
||||||
|
j = {
|
||||||
|
'names': dict([(x, None) for x in state.names]),
|
||||||
|
'drop_names': list(state.drop_names),
|
||||||
|
'base_names': list(state.base_names),
|
||||||
|
'subs': dict([(name, values) for name, values in state.subs.items()]),
|
||||||
|
'pivots': state.pivots,
|
||||||
|
}
|
||||||
|
json.dump(j, fout, sort_keys=True, indent=4, separators=(',', ': '))
|
||||||
|
|
||||||
|
def gen_rows(fn_ins):
|
||||||
|
for fn_in in fn_ins:
|
||||||
|
try:
|
||||||
|
print('Loading %s' % fn_in)
|
||||||
|
j = json.load(open(fn_in, 'r'))
|
||||||
|
|
||||||
|
group0 = list(j['subs'].values())[0]
|
||||||
|
value0 = list(group0.values())[0]
|
||||||
|
if type(value0) is float:
|
||||||
|
print("WARNING: skipping old format JSON")
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
print("Value OK")
|
||||||
|
|
||||||
|
for sub in j['subs'].values():
|
||||||
|
row_ds = {}
|
||||||
|
# TODO: convert to gcd
|
||||||
|
# den may not always be 0
|
||||||
|
# lazy solution...just multiply out all the fractions
|
||||||
|
n = 1
|
||||||
|
for _var, (_num, den) in sub.items():
|
||||||
|
n *= den
|
||||||
|
|
||||||
|
for var, (num, den) in sub.items():
|
||||||
|
num2 = n * num
|
||||||
|
assert num2 % den == 0
|
||||||
|
row_ds[var] = num2 / den
|
||||||
|
yield row_ds
|
||||||
|
except:
|
||||||
|
print("Error processing %s" % fn_in)
|
||||||
|
raise
|
||||||
|
|
||||||
|
def run(fnout, fn_ins, verbose=0):
|
||||||
|
print('Loading data')
|
||||||
|
|
||||||
|
with open(fnout, 'w') as fout:
|
||||||
|
fout.write('ico,fast_max fast_min slow_max slow_min,rows...\n')
|
||||||
|
for row_ds in gen_rows(fn_ins):
|
||||||
|
ico = '1'
|
||||||
|
out_b = [1e9, 1e9, 1e9, 1e9]
|
||||||
|
items = [ico, ' '.join(['%u' % x for x in out_b])]
|
||||||
|
|
||||||
|
for k, v in sorted(row_ds.items()):
|
||||||
|
items.append('%i %s' % (v, k))
|
||||||
|
fout.write(','.join(items) + '\n')
|
||||||
|
|
||||||
|
def main():
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description=
|
||||||
|
'Convert substitution groups into .csv to allow incremental rref results'
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument('--verbose', action='store_true', help='')
|
||||||
|
parser.add_argument('--out', help='Output csv')
|
||||||
|
parser.add_argument(
|
||||||
|
'fns_in',
|
||||||
|
nargs='*',
|
||||||
|
help='sub.json input files')
|
||||||
|
args = parser.parse_args()
|
||||||
|
bench = Benchmark()
|
||||||
|
|
||||||
|
fns_in = args.fns_in
|
||||||
|
|
||||||
|
try:
|
||||||
|
run(fnout=args.out,
|
||||||
|
fn_ins=args.fns_in, verbose=args.verbose)
|
||||||
|
finally:
|
||||||
|
print('Exiting after %s' % bench)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Loading…
Reference in New Issue