-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataGetter.py
66 lines (54 loc) · 2.1 KB
/
dataGetter.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
import string
import numpy
def isNum(testNum):
try:
return float(testNum)
except:
return testNum
def getTableData(filename, skiprows=1, delimiter=',', senseUnits=True):
f = open(filename, 'r')
# the first line is the header information naming the columns of data
firstLine = f.readline()
columnNames = string.split(firstLine.strip(), delimiter)
if senseUnits:
unitFactorsList = []
secondLine = f.readline()
columnUnits = string.split(secondLine.strip(), delimiter)
for unit in columnUnits:
unit = string.replace(string.replace(unit,')', ''), '(', '')
unit = string.replace(string.replace(unit,']', ''), '[', '')
if unit.lower() in ['v', 'a', 'm', 'kg, w', 's']:
unitFactorsList.append(float(1))
elif unit.lower() in ['mv', 'ma', 'mm', 'g', 'mw', 'ms']:
unitFactorsList.append(float(1e-3))
elif unit.lower() in ['uv', 'ua', 'um', 'mg', 'uw', 'us']:
unitFactorsList.append(float(1e-6))
elif unit.lower() in ['nv', 'na', 'nm', 'ng', 'nw', 'ns']:
unitFactorsList.append(float(1e-9))
else:
unitFactorsList.append(float(1))
f.close()
tableData = numpy.loadtxt(filename, skiprows=skiprows, delimiter=delimiter)
tableDict = {}
for (n, columnName) in list(enumerate(columnNames)):
if senseUnits:
tableDict[columnName] = tableData[:,n] * unitFactorsList[n]
else:
tableDict[columnName] = tableData[:,n]
return tableDict
def getTableRowData(filename, delimiter=','):
tableDict = {}
f = open(filename, 'r')
for line in f:
rowInfo = line.split(delimiter)
rowHeader = rowInfo[0].strip()
rowDataList = [isNum(datum) for datum in rowInfo[1:]]
dataSize = len(rowDataList)
if dataSize > 1:
tableDict[rowHeader] = rowDataList
elif dataSize == 1:
tableDict[rowHeader] = rowDataList[0]
else:
tableDict[rowHeader] = None
f.close()
return tableDict