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