-
Notifications
You must be signed in to change notification settings - Fork 1
/
Populations.py
221 lines (200 loc) · 6.52 KB
/
Populations.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import numpy as np
import numpy.random as npr
import scipy.stats as stats
from collections import deque
import copy
class PointError(ValueError):
pass
class Point:
def __init__(self, data, time=0.0):
self.point = (data, time)
self.value = data
self.t = time
self.nxt = None
def __repr__(self):
return repr(self.value)
def __add__(self, other):
try:
return self.value+other.value
except:
return self.value+other
def __iadd__(self, other):
## replace current with temp
tmp1=Point(self.value,self.t)
tmp2=Point(self.value,self.t+other.t)
tmp1.nxt=self.nxt
tmp2.nxt=tmp1
self.value=self.value+other.value
self.t=self.t+other.t
self.nxt=tmp2
return self
def push_back(self,data,t):
self.__iadd__(Point(data,t))
def __radd__(self, other):
return self.__add__(other)
def __getitem__(self, key):
if key == 't' or key == 'time' or key == 'Time' or key == 'T':
return self.t
if key == 'value' or key == 'val' or key == 'Val' or key == 'Value':
return self.value
def __setitem__(self, key, val):
if key == 't' or key == 'time' or key == 'Time' or key == 'T' or key == 1:
self.t = val
if key == 'value' or key == 'val' or key == 'Val' or key == 'Value' or key == 0:
self.value = val
def __iter__(self):
curr=self
while curr is not None:
yield curr
curr=curr.nxt
def get_all(self):
return [(i, i.t) for i in self]
class PointUpdate(Point):
def __init__(self,data,t):
super().__init__(data,t)
def __getitem__(self, key):
if key == 't' or key == 'time' or key == 'Time' or key == 'T':
return self.t
elif key == 'value' or key == 'val' or key == 'Val' or key == 'Value':
return self.value
elif key == 'key':
return 'vt'
# class Population:
# _ID = 0
# _counter = 0
# _archived = []
# @classmethod
# def archive(cls, population):
# cls._archived.append(population)
# @classmethod
# def increment(cls):
# cls._counter += 1
# cls._ID += 1
# def __init__(self, point=None):
# self.head = point
# self._ID = Population._ID
# self._index = Population._counter
# Population._ID_index_map[self._index] = self._ID
# Population.increment()
# def __iter__(self):
# curr = self.head
# while curr is not None:
# yield curr
# curr = curr.nxt
# def get_ID(self):
# return self._ID
# def push_back(self, point):
# try:
# assert(isinstance(point, Point))
# point.nxt = self.head
# self.head = point
# except:
# try:
# tmp_next = Point(point[0], point[1])
# tmp_next.nxt = self.head
# self.head = point
# except:
# print("input to push_back must be Point or container of two elements")
# raise PointError
# def get_all(self):
# return [(i, i.t) for i in self]
# def __repr__(self):
# return repr(self.head.value)
# def __add__(self, other):
# return self.head.__add__(other)
# def __radd__(self, other):
# return self.head.__add__(other)
# def __getitem__(self, key):
# if key == 't' or key == 'time' or key == 'Time' or key == 'T':
# return self.head['t']
# if key == 'value' or key == 'val' or key == 'Val' or key == 'Value':
# return self.head['value']
# def __setitem__(self, key, val):
# cur_val=self.head['val']
# cur_t=self.head['t']
# try:
# pnt1=Point(cur_val,cur_t+val[1])
# pnt2=Point(cur_val+val[0],cur_t+val[1])
# self.push_back(pnt1)
# self.push_back(pnt2)
# except:
# if key == 't' or key == 'time' or key == 'Time' or key == 'T' or key == 1:
# newpnt=Point(cur_val,cur_t+val)
# self.push_back(newpnt)
# elif key == 'value' or key == 'val' or key == 'Val' or key == 'Value' or key == 0:
# newpnt=Point(cur_val+val,cur_t)
# def __iadd__(self, point):
class CompositionVector:
_archive=[]
@classmethod
def archive(cls,point):
cls._archive.append(point)
def __init__(self, point=None, nc=0):
self.__methods={'sum':self.__sum,'average':self.__avg}
if point is not None:
self.initialized=True
try:
self.state=np.array(point)
except:
print('gotta fit into a numpy array')
raise
else:
self.initialized=False
def add_variable(self,point):
if not self.initialized:
try:
self.state=np.array(point)
for i in self.state:
print(i.get_ID())
except:
print('gotta finnarray')
raise
else:
self.state=np.append(self.state,point)
def __getitem__(self, key):
if isinstance(key,integer):
return self.state[index]
elif key=='average':
return sum(self.state)/len(self.state)
elif key=='sum':
return sum(self.state)
elif key=='length'
elif isinstance(key,tuple):
try:
outp=self.state[key[0]]
for i in range(key[1]):
outp=outp.next
return outp
except:
print("that time step probably doesnt exist")
def __setitem__(self,index,key):
pass
def __iter__(self):
self.it=0
return self
def length(self):
return len(self.state)
def __next__(self):
index=self.it
self.it+=1
while self.it<=len(self.state):
return self.state[index]
raise StopIteration
def __sum(self):
return sum(self.state)
def __avg(self):
return sum(self.state)/len(self.state)
def __len(self):
return len(self.state)
def main():
lst=[0,1,2,3,4,5]
tlst=[0.0,0.1,0.2,0.3,0.4,0.5]
points=[Point(i,j) for i,j in zip(lst,tlst)]
state=CompositionVector(points)
state[2]+=Point(11,11)
state[4]+=Point(100,1000)
for i in state:
print(i.get_all())
print(sum(state))
if __name__ == '__main__':
main()