-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvmsutilsv043.py
309 lines (264 loc) · 8.08 KB
/
vmsutilsv043.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""vmsutilsv043: Village Market Simulator Utility Funcations.
Original Author: William O. Ruddick
Date: May-18-2018
"""
import numpy as np
from scipy.interpolate import UnivariateSpline
NC = 181
CC = 212
CIRCLE = 4
BANKCIRCLE = 5
RETAILSQUARE = 1
FRETAILSQUARE = 7
MARKETSQUARE = 6
TRIANGLE = 2
DIAMOND = 3
BANK = 66
EXTERNALMARKET = 55
GENERIC = 44
RETAIL = 33
FOREIGNRETAIL = 333
LOCALSERVICE = 22
LOCALPRODUCER = 77
EXPORTSERVICE = 11
LOCALSERVICECOOP = 222
LOCALPRODUCERCOOP = 777
#Trade Types tradeType
#sell/purchase,
LOCALTRADESERVICES = 130
LOCALTRADESTOCK = 230
#makeDeposit/acceptDeposit,
DEPOSIT = 330
FEES = 331
SAVINGS = 332
DIVIDEND = 430
LOAN = 530 #giveLoan/acceptLoan
CLEARING = 630 #giveLoan/acceptLoan
EXTSERVICES = 730 #purchaseImport/theMarket.sellExport
EXTSTOCK = 830#theMarket.purchaseExport/sellExport
yeardays=365/12.0 #dayspermonth
def tradeTypeToString(iType):
tttype = "error"
if iType == LOCALTRADESERVICES:
tttype = "LocalServices"
elif iType == LOCALTRADESTOCK:
tttype = "LocalStock"
elif iType == DEPOSIT:
tttype = "DEPOSIT"
elif iType == SAVINGS:
tttype = "SAVINGS"
elif iType == FEES:
tttype = "FEES"
elif iType == DIVIDEND:
tttype = "DIVIDEND"
elif iType == LOAN:
tttype = "LOAN"
elif iType == CLEARING:
tttype = "CLEARING"
elif iType == EXTSERVICES:
tttype = "ExportServices"
elif iType == EXTSTOCK:
tttype = "ExportStock"
return tttype
def currencyTypeToString(iType):
tttype = "error"
if iType == NC:
tttype = "NC"
elif iType == CC:
tttype = "(cc)"
return tttype
def traderTypeToString(iType):
tttype = "error type not found: ", iType
if iType == BANK:
tttype = "Coop"
elif iType == EXTERNALMARKET:
tttype = "ExternalMarket"
elif iType == GENERIC:
tttype = "GENERIC"
elif iType == RETAIL:
tttype = "RetailShop"
elif iType == FOREIGNRETAIL:
tttype = "ForeignRetail"
elif iType == LOCALSERVICE:
tttype = "LocalService"
elif iType == LOCALPRODUCER:
tttype = "LocalProducer"
elif iType == EXPORTSERVICE:
tttype = "ServiceExporter"
return tttype
#nonRandom Double-Shuffle
def shuffleMix(ztraders):
#ztraders = ['1','2','3','4','5','6','7','8','9','10','11']
A = list(ztraders)
numT = len(ztraders)
B = A[:int(len(A)/2)]
C = A[int(len(A)/2):]
D = []
lenB=len(B)
lenC=len(C)
inc = 0
if lenC>=lenB:
for c in C:
D.append(c)
if inc < lenB:
D.append(B[inc])
inc+=1
else:
for b in B:
D.append(b)
if inc < lenC:
D.append(C[inc])
inc+=1
D = list(reversed(D))
return D
def writeVideo():
#create image array
image_array = []
path = '/home/wor/Projects/Programming/Bangla-Pesa-Python/plotsv4/*.png'
files=glob.glob(path)
for file in files:
print (file)
f=open(file, 'r')
f.readlines()
image_array.append(f)
f.close()
duration = len(image_array)
command = [ FFMPEG_BIN,
'-y', # (optional) overwrite output file if it exists
'-f', 'rawvideo',
'-vcodec','rawvideo',
'-s', '420x360', # size of one frame
'-pix_fmt', 'rgb24',
'-r', '24', # frames per second
'-i', '-', # The imput comes from a pipe
'-an', # Tells FFMPEG not to expect any audio
'-vcodec', 'mpeg',
'my_output_videofile.mp4' ]
#pipe = sp.Popen( command, stdin=sp.PIPE, stderr=sp.PIPE)
#pipe.proc.stdin.write( image_array.tostring() )
fps = 50#24
sp.call(["avconv","-y","-r",str(fps),"-i", "/home/wor/Projects/Programming/Bangla-Pesa-Python/plotsv4/myfile_%05d.png","-vcodec","mpeg4", "-qscale","5", "-r", str(fps), "video.avi"])
def GRLC(values):
'''
Calculate Gini index, Gini coefficient, Robin Hood index, and points of
Lorenz curve
Lorenz curve values as given as lists of x & y points [[x1, x2], [y1, y2]]
@param values: List of values
@return: [Gini index, Gini coefficient, Robin Hood index, [Lorenz curve]]
'''
n = len(values)
assert(n > 0), 'Empty list of values'
sortedValues = sorted(values) #Sort smallest to largest
#Find cumulative totals
cumm = [0]
for i in range(n):
cumm.append(sum(sortedValues[0:(i + 1)]))
#Calculate Lorenz points
LorenzPoints = [[], []]
sumYs = 0 #Some of all y values
robinHoodIdx = -1 #Robin Hood index max(x_i, y_i)
for i in range(1, n + 2):
x = 100.0 * (i - 1)/n
y = 100.0 * (cumm[i - 1]/float(cumm[n]))
LorenzPoints[0].append(x)
LorenzPoints[1].append(y)
sumYs += y
maxX_Y = x - y
if maxX_Y > robinHoodIdx: robinHoodIdx = maxX_Y
giniIdx = 100 + (100 - 2 * sumYs)/n #Gini index
return [giniIdx, giniIdx/100, robinHoodIdx, LorenzPoints]
#create a continuios distribution from discreet data
def smoother(seasonalImportSmooth,seasonalImportOrig,sRounds,rounds,seasonalMarketArray,seasonalImportFlat):
#global seasonalImportSmooth
#global seasonalImportOrig
#global sRounds
seasonalImportSmooth=[]
sRounds=[]
r=0
while r < rounds:
sRounds.append(r)
seasonalImportSmooth.append(0.0)
r = r+1
seasonalImportSmooth[0]=seasonalMarketArray[0]
m1=1
maxrDist = 0
while m1 < rounds:
fMonth = float(m1)/yeardays
xMonth = fMonth % 12.0
theMonth = int(xMonth)
theMonthMult = seasonalMarketArray[theMonth]
nextMonth = int((theMonth +1) %12)
theNextMonthMult = seasonalMarketArray[nextMonth]
z1 = m1 +1
while z1 < rounds*2:
zMonth = float(z1)/yeardays
zMonth = zMonth % 12.0
zheMonth = int(zMonth)
if zheMonth != theMonth:
break
else:
z1=z1+1
rDist = z1-m1
if rDist > maxrDist:
maxrDist = rDist
if rDist == maxrDist :
seasonalImportSmooth[m1]=theMonthMult
else:
seasonalImportSmooth[m1]=np.NaN
m1= m1+1
if rDist <= 1:
maxrDist = 0
yn, tn = tnorm(seasonalImportSmooth, step=-1*rounds, k=3, smooth=0, show=False)
g=0
while g < len(yn):
# sRounds[g]=tn[g]
seasonalImportSmooth[g]=yn[g]
seasonalImportOrig.append(yn[g])
seasonalImportFlat.append(0.5)
g = g+1
return seasonalImportSmooth, seasonalImportOrig, seasonalImportFlat, sRounds
#normalize smoothed data
def tnorm(y, axis=0, step=1, k=3, smooth=0, mask=None, show=False, ax=None):
#y = np.asarray(y)
y = np.asarray(y)
if axis:
y = y.T
if y.ndim == 1:
y = np.reshape(y, (-1, 1))
# turn mask into NaN
if mask is not None:
y[y == mask] = np.NaN
# delete rows with missing values at the extremities
while y.size and np.isnan(np.sum(y[0])):
y = np.delete(y, 0, axis=0)
while y.size and np.isnan(np.sum(y[-1])):
y = np.delete(y, -1, axis=0)
# check if there are still data
if not y.size:
return None, None
if y.size == 1:
return y.flatten(), None
t = np.linspace(0, 100, y.shape[0])
if step == 0:
tn = t
elif step > 0:
tn = np.linspace(0, 100, np.round(100 / step + 1))
else:
tn = np.linspace(0, 100, -step)
yn = np.empty([tn.size, y.shape[1]]) * np.NaN
for col in np.arange(y.shape[1]):
# ignore NaNs inside data for the interpolation
ind = np.isfinite(y[:, col])
if np.sum(ind) > 1: # at least two points for the interpolation
spl = UnivariateSpline(t[ind], y[ind, col], k=k, s=smooth)
yn[:, col] = spl(tn)
if show:
_plot(t, y, ax, tn, yn)
if axis:
y = y.T
if yn.shape[1] == 1:
yn = yn.flatten()
# print len(yn)
# print len(tn)
# print tn
return yn, tn