Skip to content

Commit 322416a

Browse files
author
Bill Majoros
committed
update
1 parent 04b179d commit 322416a

File tree

5 files changed

+142
-8
lines changed

5 files changed

+142
-8
lines changed

DataFrame.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ def nrow(self):
172172
return len(self.matrix)
173173

174174
def ncol(self):
175-
return len(self.header)
175+
if(len(self.header)!=0): return len(self.header)
176+
if(len(self.matrix)==0): return 0
177+
return self.matrix[0].length()
176178

177179
def __getitem__(self,i):
178180
return self.matrix[i]

MatrixMarket.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env python
2+
#=========================================================================
3+
# This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
4+
# License (GPL) version 3, as described at www.opensource.org.
5+
# Author: William H. Majoros (bmajoros@alumni.duke.edu)
6+
#=========================================================================
7+
from __future__ import (absolute_import, division, print_function,
8+
unicode_literals, generators, nested_scopes, with_statement)
9+
from builtins import (bytes, dict, int, list, object, range, str, ascii,
10+
chr, hex, input, next, oct, open, pow, round, super, filter, map, zip)
11+
# The above imports should allow this program to run in both Python 2 and
12+
# Python 3. You might need to update your version of module "future".
13+
import sys
14+
import ProgramName
15+
import gzip
16+
from Rex import Rex
17+
rex=Rex()
18+
19+
#=========================================================================
20+
# Attributes:
21+
# FH : file handle
22+
# header : array of string
23+
# nextLine : string
24+
# Instance Methods:
25+
# MatrixMarket(filename)
26+
# nextGroup(self,colIndex)
27+
# getHeader()
28+
# Class Methods:
29+
# allGroups=loadFile(filename,colIndex)
30+
#=========================================================================
31+
class MatrixMarket:
32+
def __init__(self,filename):
33+
if(rex.find("\.gz$",filename)):
34+
self.FH=gzip.open(filename,"rt")
35+
else:
36+
self.FH=open(filename,"rt")
37+
self.header=None
38+
self.nextLine=None
39+
40+
def getHeader(self):
41+
return self.header
42+
43+
def nextGroup(self,colIndex):
44+
line=None
45+
# First, see if the header needs to be parsed
46+
while(True):
47+
line=self.nextLine
48+
if(line is None): line=self.FH.readline()
49+
if(line is None): return None
50+
L=len(line)
51+
if(L>0 and line[0]=="%"): continue
52+
break
53+
# The first non-comment line contains the totals
54+
if(self.header is None):
55+
self.header=line.rstrip().split()
56+
self.header=[int(x) for x in self.header]
57+
line=self.FH.readline()
58+
# Now we can read in the next group of lines
59+
prevID=None
60+
group=[]
61+
if(self.nextLine is not None): # buffered from previous call
62+
prevID=int(self.nextLine.rstrip().split()[colIndex])
63+
while(True):
64+
fields=line.rstrip().split()
65+
if(prevID is None): prevID=int(fields[colIndex])
66+
thisID=int(fields[colIndex])
67+
if(thisID!=prevID):
68+
self.nextLine=line
69+
return group
70+
group.append(fields)
71+
line=self.FH.readline()
72+
if(line is None): return None
73+
74+
@classmethod
75+
def loadFile(self,filename,colIndex):
76+
reader=MatrixMarket(filename)
77+
groups=[]
78+
while(True):
79+
group=reader.nextGroup(colIndex)
80+
if(group is None): break
81+
groups.append(group)
82+
return groups

Pipe.py

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# Instance Methods:
1717
# pipe=Pipe(command)
1818
# line=pipe.readline()
19+
# Class Methods:
1920
# output=Pipe.run(command)
2021
#=========================================================================
2122
class Pipe:

Stan.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@
1717
#
1818
# Methods:
1919
# stan=Stan(model)
20-
# stan.run(numWarmup,numSamples,inputFile,outputFile,stderrFile,initFile=None):
20+
# stan.run(numWarmup,numSamples,inputFile,outputFile,stderrFile,
21+
# initFile=None):
22+
# stan.variational(numSamples,inputFile,outputFile,stderrFile,
23+
# initFile=None):
24+
# cmd=stan.getCmd(numWarmup,numSamples,inputFile,outputFile,
25+
# stderrFile,initFile=None)
26+
# cmd=stan.getVarCmd(numSamples,inputFile,outputFile,
27+
# stderrFile,initFile=None)
2128
# stan.writeOneDimArray(name,array,dim,OUT):
2229
# stan.writeTwoDimArray(name,array,firstDim,secondDim,OUT):
2330
# stan.writeThreeDimArray(name,array,firstDim,secondDim,thirdDim,OUT):
@@ -82,6 +89,10 @@ def run(self,numWarmup,numSamples,inputFile,outputFile,stderrFile,initFile=None)
8289
cmd=self.getCmd(numWarmup,numSamples,inputFile,outputFile,stderrFile,initFile)
8390
os.system(cmd)
8491

92+
def variational(self,numSamples,inputFile,outputFile,stderrFile,initFile=None):
93+
cmd=self.getVarCmd(numSamples,inputFile,outputFile,stderrFile,initFile)
94+
os.system(cmd)
95+
8596
def getCmd(self,numWarmup,numSamples,inputFile,outputFile,stderrFile,initFile=None):
8697
init=" init="+initFile if initFile is not None else ""
8798
cmd=self.model+" sample thin=1"+\
@@ -91,4 +102,13 @@ def getCmd(self,numWarmup,numSamples,inputFile,outputFile,stderrFile,initFile=No
91102
init+\
92103
" output file="+outputFile+" refresh=0 > "+stderrFile
93104
return cmd
105+
106+
def getVarCmd(self,numSamples,inputFile,outputFile,stderrFile,initFile=None):
107+
init=" init="+initFile if initFile is not None else ""
108+
cmd=self.model+" variational "+\
109+
" output_samples="+str(numSamples)+\
110+
" data file="+inputFile+\
111+
init+\
112+
" output file="+outputFile+" refresh=0 > "+stderrFile
113+
return cmd
94114

StanParser.py

+35-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
# (median,mean,SD,min,max)=parser.getSummary(var)
2626
# (CI_left,CI_right)=parser.getCredibleInterval(0.95,variableName)
2727
# (median,CI_left,CI_right)=parser.getMedianAndCI(0.95,variableName)
28+
# P=parser.getLeftTail(variableName,value)
29+
# P=parser.getRightTail(variableName,value)
2830
######################################################################
2931

3032
class StanParser:
@@ -43,6 +45,20 @@ def getCredibleInterval(self,percent,name):
4345
CI_right=samples[n-int(half*n)]
4446
return (CI_left,CI_right)
4547

48+
def getLeftTail(self,name,value):
49+
samples=self.getVariable(name)
50+
count=0
51+
for x in samples:
52+
if(x<value): count+=1
53+
return float(count)/float(len(samples))
54+
55+
def getRightTail(self,name,value):
56+
samples=self.getVariable(name)
57+
count=0
58+
for x in samples:
59+
if(x>value): count+=1
60+
return float(count)/float(len(samples))
61+
4662
def getMedianAndCI(self,percent,name):
4763
samples=self.getVariable(name)
4864
n=len(samples)
@@ -58,23 +74,36 @@ def parse(self,filename):
5874
return self.parseFile(IN)
5975

6076
def parseFile(self,IN):
77+
firstIndex=None
6178
for line in IN:
6279
if(len(line)<1): continue
6380
if(line[0]=="#"): continue
6481
fields=line.rstrip().split(",")
6582
if(len(fields)<1): continue
66-
if(fields[0]=="lp__"): self.parseVarNames(fields)
67-
else: self.parseSample(fields)
83+
if(fields[0]=="lp__"):
84+
firstIndex=self.getFirstVariableIndex(fields)
85+
self.parseVarNames(fields,firstIndex)
86+
else: self.parseSample(fields,firstIndex)
87+
88+
def getFirstVariableIndex(self,fields):
89+
for i in range(len(fields)):
90+
field=fields[i]
91+
L=len(field)
92+
lastChar=field[L-1]
93+
if(lastChar!="_"): return i
94+
return -1
6895

69-
def parseVarNames(self,fields):
70-
self.varNames=fields[7:]
96+
def parseVarNames(self,fields,firstIndex):
97+
self.varNames=fields[firstIndex:]
98+
#print("firstIndex=",firstIndex,"names=",self.varNames)
7199
for i in range(len(self.varNames)):
72100
self.varIndex[self.varNames[i]]=i
73101

74-
def parseSample(self,fields):
75-
sample=fields[7:]
102+
def parseSample(self,fields,firstIndex):
103+
sample=fields[firstIndex:]
76104
for i in range(len(sample)):
77105
sample[i]=float(sample[i])
106+
#print("sample=",sample[i],"firstIndex=",firstIndex)
78107
self.samples.append(sample)
79108

80109
def getSamples(self):

0 commit comments

Comments
 (0)