Skip to content

Commit 5a80116

Browse files
author
Bill Majoros
committed
ConfigFile.py, SummaryStats.py, Translation.py
1 parent 6f056ef commit 5a80116

5 files changed

Lines changed: 325 additions & 0 deletions

File tree

‎ConfigFile.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#====================================================================
2+
# Copyright (C)2016 William H. Majoros (martiandna@gmail.com).
3+
# This is OPEN SOURxCE SOFTWARE governed by the Gnu General Public
4+
# License (GPL) version 3, as described at www.opensource.org.
5+
#====================================================================
6+
from __future__ import (absolute_import, division, print_function,
7+
unicode_literals, generators, nested_scopes, with_statement)
8+
from builtins import (bytes, dict, int, list, object, range, str, ascii,
9+
chr, hex, input, next, oct, open, pow, round, super, filter, map, zip)
10+
import re
11+
12+
######################################################################
13+
# Attributes:
14+
# hash
15+
# Methods:
16+
# configFile=ConfigFile(filename)
17+
# value=configFile.lookup(key)
18+
# value=configFile.lookupOrDie(key)
19+
# Private methods:
20+
# self.load(filename)
21+
######################################################################
22+
23+
class ConfigFile:
24+
"""ConfigFile stores variable assignments as key-value pairs
25+
in a hash
26+
"""
27+
def __init__(self,filename):
28+
self.hash={}
29+
self.load(filename)
30+
31+
def lookup(self,key):
32+
return self.hash[key]
33+
34+
def lookupOrDie(self,key):
35+
if(self.hash[key] is None):
36+
raise Exception("$key not defined in config file\n")
37+
return self.hash[key]
38+
39+
def load(self,filename):
40+
hash=self.hash
41+
with open(filename,"r") as fh:
42+
while(True):
43+
line=fh.readline()
44+
if(not line): break
45+
match=re.search("^(.*)#",line);
46+
if(match): line=match.group(1)
47+
match=re.search("^\s*(\S+)\s*=\s*(\S+)",line);
48+
if(match):
49+
key=match.group(1)
50+
value=match.group(2)
51+
hash[key]=value
52+

‎SummaryStats.py‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#====================================================================
2+
# Copyright (C)2016 William H. Majoros (martiandna@gmail.com).
3+
# This is OPEN SOURxCE SOFTWARE governed by the Gnu General Public
4+
# License (GPL) version 3, as described at www.opensource.org.
5+
#====================================================================
6+
from __future__ import (absolute_import, division, print_function,
7+
unicode_literals, generators, nested_scopes, with_statement)
8+
from builtins import (bytes, dict, int, list, object, range, str, ascii,
9+
chr, hex, input, next, oct, open, pow, round, super, filter, map, zip)
10+
import math
11+
12+
######################################################################
13+
# Attributes:
14+
#
15+
# Methods:
16+
# [mean,SD,min,max]=SummaryStats.summaryStats(array)
17+
# [mean,SD,min,max)=SummaryStats.roundedSummaryStats(array)
18+
# sum=SummaryStats.sum(array)
19+
# r=SummaryStats.correlation(array1,array2)
20+
######################################################################
21+
22+
class SummaryStats:
23+
"""SummaryStats computes simple mean, variance, and correlation
24+
statistics
25+
"""
26+
27+
@classmethod
28+
def summaryStats(self,array):
29+
n=len(array)
30+
minX=None
31+
maxX=None
32+
sumX=0
33+
sumXX=0
34+
for i in range(0,n):
35+
x=array[i]
36+
sumX+=x
37+
sumXX+=x*x
38+
if(i==0): minX=maxX=x
39+
if(x<minX): minX=x
40+
if(x>max): maxX=x
41+
meanX=sumX/n
42+
varX=None if n<2 else (sumXX-sumX*sumX/n)/(n-1)
43+
if(varX<0): varX=0
44+
stddevX=math.sqrt(varX)
45+
return [meanX,stddevX,minX,maxX]
46+
47+
@classmethod
48+
def roundedSummaryStats(self,array):
49+
[mean,stddev,min,max]=SummaryStats.summaryStats(array)
50+
mean=int(100.0*mean+5.0/9.0)/100.0
51+
stddev=int(100.0*stddev+5.0/9.0)/100.0
52+
min=int(100.0*min+5.0/9.0)/100.0
53+
max=int(100.0*max+5.0/9.0)/100.0
54+
return [mean,stddev,min,max]
55+
56+
@classmethod
57+
def sum(self,array):
58+
s=0.0
59+
n=len(array)
60+
for i in range(0,n):
61+
s+=array[i]
62+
return s;
63+
64+
@classmethod
65+
def correlation(self,Xs,Ys):
66+
sumX=0.0
67+
sumY=0.0
68+
sumXY=0.0
69+
sumXX=0.0
70+
sumYY=0.0
71+
n=len(Xs)
72+
for i in range(0,n):
73+
x=Xs[i]
74+
y=Ys[i]
75+
sumX+=x
76+
sumY+=y
77+
sumXY+=x*y
78+
sumXX+=x*x
79+
sumYY+=y*y
80+
r=(sumXY-sumX*sumY/n)/math.sqrt((sumXX-sumX*sumX/n)*(sumYY-sumY*sumY/n))
81+
return r
82+

‎Translation.py‎

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#====================================================================
2+
# Copyright (C)2016 William H. Majoros (martiandna@gmail.com).
3+
# This is OPEN SOURxCE SOFTWARE governed by the Gnu General Public
4+
# License (GPL) version 3, as described at www.opensource.org.
5+
#====================================================================
6+
from __future__ import (absolute_import, division, print_function,
7+
unicode_literals, generators, nested_scopes, with_statement)
8