-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdata.py
More file actions
82 lines (63 loc) · 2.23 KB
/
data.py
File metadata and controls
82 lines (63 loc) · 2.23 KB
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
import numpy
class Point():
def __init__(self, x, y, err=0.0):
self.x = x
self.y = y
self.err = err
class DataSet():
# mint01 / mdcs01 / mdor01 / mgor01 / dcs
def __init__(self, path, exists, lim=None):
if not exists:
self.dataSet = None
else:
self.dataSet = self.constructDataSet(path, lim)
def constructDataSet(self, path, lim=None):
dataSet = []
with open(path, "r", encoding="utf-8") as fp:
for dataLine in fp.readlines():
# Ignore commented lines.
if dataLine[0] == "#":
continue
splitLine = [float(n) for n in dataLine.split()]
if len(splitLine) > 2:
x, y, err, *__ = splitLine
if lim and x > lim:
return dataSet
dataSet.append(Point(x, y, err))
else:
x, y = splitLine
if lim and x > lim:
return dataSet
dataSet.append(Point(x, y))
return dataSet
class NpDataSet():
# mint01 / mdcs01 / mdor01 / mgor01 / dcs
def __init__(self, path, lim=None):
self.LIMIT = lim
self.x = []
self.y = []
with open(path, "r", encoding="utf-8") as fp:
for dataLine in fp.readlines():
# Ignore commented lines.
if dataLine[0] == "#":
continue
splitLine = [float(n) for n in dataLine.split()]
x, y, *__ = splitLine
if lim and x > lim:
break
self.x.append(x)
self.y.append(y)
if not self.LIMIT:
self.LIMIT = self.x[-1]
self.x = numpy.array(self.x)
self.y = numpy.array(self.y)
self.interpolate()
def interpolate(self):
xnew = numpy.round(numpy.arange(0, self.LIMIT, 0.015), 4)
ynew = numpy.interp(xnew, self.x, self.y)
self.x = xnew
self.y = ynew
def meanSquaredError(d1: NpDataSet, d2: NpDataSet) -> float:
return sum((y1 - y2)**2
for y1, y2 in zip(d1.y, d2.y)
) / len(d1.y)