Compare commits

...

3 Commits

2 changed files with 56 additions and 24 deletions

View File

@@ -18,10 +18,24 @@ print("""Axiomatic Design Information Calculator by Joseph. T. Foley<foley AT ru
From https://gitea.cs.ru.is/AxiomaticDesign/adcalc/""") From https://gitea.cs.ru.is/AxiomaticDesign/adcalc/""")
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Axiomatic Design Information Calculator.") description="Axiomatic Design Information Calculator.")
parser.add_argument('csvfile', subparsers = parser.add_subparsers(dest='mode')
subparsers.required = True
### MODE DATA
parser_data = subparsers.add_parser("DATA")
parser_data.add_argument('csvfile',
help="CSV file with data and headers") help="CSV file with data and headers")
parser.add_argument('column', parser_data.add_argument('column',
help='Which column header to take data from') help='Which column header to take data from')
## MODE SIM
parser_sim = subparsers.add_parser("SIM")
parser_sim.add_argument('samplesize', type=int,
help="sample size")
parser_sim.add_argument('mean', type=float,
help="mean(average) value")
parser_sim.add_argument('stddev', type=float,
help="sample standard deviation")
## General Arguments
parser.add_argument('minvalue', type=float, parser.add_argument('minvalue', type=float,
help='Tolerance low limit') help='Tolerance low limit')
parser.add_argument('maxvalue', type=float, parser.add_argument('maxvalue', type=float,
@@ -30,8 +44,9 @@ parser.add_argument('--normalizey', action="store_true",
help='Set y-axis to normalized probability density') help='Set y-axis to normalized probability density')
parser.add_argument('--log', default="INFO", parser.add_argument('--log', default="INFO",
help='Console log level: Number or DEBUG, INFO, WARNING, ERROR') help='Console log level: Number or DEBUG, INFO, WARNING, ERROR')
parser.add_argument('--graphinfo', parser.add_argument('--graphinfo', action="store_true",
help='Put information on the PDF graph') help='Put information on the PDF graph')
args = parser.parse_args() args = parser.parse_args()
## Set up logging ## Set up logging
@@ -42,7 +57,7 @@ if not isinstance(numeric_level, int):
logger = logging.getLogger("app") logger = logging.getLogger("app")
logger.setLevel(numeric_level) logger.setLevel(numeric_level)
# log everything to file # log everything to file
logpath = os.path.splitext(args.csvfile)[0]+".log" logpath = "infocalc.log"
fh = logging.FileHandler(logpath) fh = logging.FileHandler(logpath)
fh.setLevel(logging.DEBUG) fh.setLevel(logging.DEBUG)
# log to console # log to console
@@ -59,18 +74,28 @@ logger.addHandler(fh)
logger.info("Creating infocalc log file %s", logpath) logger.info("Creating infocalc log file %s", logpath)
lowerbound = args.minvalue
upperbound = args.maxvalue
# seed values for variable scoping
mean = 0
stddev = 1
samplesize =1
if args.mode == "DATA":
# filename pre-processing for output # filename pre-processing for output
inpath = PurePath(args.csvfile) inpath = PurePath(args.csvfile)
print(f"Input: {inpath}") print(f"Input: {inpath}")
# grab the data and process # grab the data and process
data = np.array(pd.read_csv(inpath)[args.column]) data = np.array(pd.read_csv(inpath)[args.column])
lowerbound = args.minvalue
upperbound = args.maxvalue
logger.debug(f"data:{data}, lower:{lowerbound}, upper:{upperbound}")
mean = data.mean() mean = data.mean()
stddev = data.std(ddof=1) stddev = data.std(ddof=1)
samplesize = len(data)
elif args.mode == "SIM":
mean = args.mean
stddev = args.stddev
samplesize = args.samplesize
# Delta Degrees of Freedom: ddof=0 for population, ddof=1 for sample std dev # Delta Degrees of Freedom: ddof=0 for population, ddof=1 for sample std dev
prob = norm.cdf(upperbound, mean, stddev) - norm.cdf(lowerbound, mean, stddev) prob = norm.cdf(upperbound, mean, stddev) - norm.cdf(lowerbound, mean, stddev)
#print("probability: %f", prob) #print("probability: %f", prob)
@@ -78,8 +103,10 @@ info = -np.emath.log2(prob)
#print("information content: %f bits", info) #print("information content: %f bits", info)
## place text on plot: https://matplotlib.org/3.3.4/gallery/recipes/placing_text_boxes.html ## place text on plot: https://matplotlib.org/3.3.4/gallery/recipes/placing_text_boxes.html
fig, ax = plt.subplots() fig, ax = plt.subplots()
if args.graphinfo:#put info on corner of graph
textstr = '\n'.join(( textstr = '\n'.join((
r'$n=%d$' % (len(data)), r'$n=%d$' % (samplesize),
r'$\mu=%.2f$' % (mean, ), r'$\mu=%.2f$' % (mean, ),
r'$\sigma=%.2f$' % (stddev, ), r'$\sigma=%.2f$' % (stddev, ),
r'$P=%.2f$' % (prob, ), r'$P=%.2f$' % (prob, ),

5
tests.sh Executable file
View File

@@ -0,0 +1,5 @@
#!/bin/bash
echo "Loading data from file"
./infocalc.py DATA testdata.csv data1 0.9 1.1 --graphinfo
echo "Creating simulated curve from parameters"
./infocalc.py SIM 8 1.0 0.5 0.9 1.1