Skip to content

Commit 24472a2

Browse files
committed
update
1 parent 0b0b486 commit 24472a2

26 files changed

+738
-102
lines changed

.coveragerc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[run]
2+
plugins = Cython.Coverage

.gitignore

-21
Original file line numberDiff line numberDiff line change
@@ -1,22 +1 @@
1-
build/*
2-
htmlcov/*
31
*.pyc
4-
.idea/*
5-
pyfin/.idea/*
6-
doc/_build/*
7-
*.coverage
8-
*.html
9-
*.js
10-
*.png
11-
*.dat
12-
*.css
13-
*.c
14-
*.pyd
15-
*.pyproj
16-
*.sln
17-
.vs/*
18-
Finance_Python.egg-info/*
19-
dist/*
20-
*.pyd
21-
*.c
22-
setup.cfg

.idea/workspace.xml

+544
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.travis.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ install:
3737
- pip install coveralls
3838
# command to run tests
3939
script:
40-
- python setup.py build_ext --inplace
41-
- coverage run PyFin/tests/testSuite.py
42-
- coverage report
43-
- coverage html
40+
- python setup.py build_ext --line_trace --inplace
41+
- coverage run --rcfile=./.coveragerc PyFin/tests/testSuite.py
42+
- coverage report --rcfile=./.coveragerc -i
43+
- coverage html --rcfile=./.coveragerc -i
4444
after_success:
4545
- coveralls

PyFin/Analysis/CrossSectionValueHolders.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
u"""
43
Created on 2017-2-10
54

PyFin/Analysis/SecurityValueHolders.pyx

+28-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
u"""
43
Created on 2015-8-7
54
@@ -14,6 +13,7 @@ import operator
1413
import numpy as np
1514
cimport numpy as np
1615
import pandas as pd
16+
cimport cython
1717
from PyFin.Analysis.SecurityValues cimport SecurityValues
1818
from PyFin.Utilities import to_dict
1919
from PyFin.Math.Accumulators.StatefulAccumulators cimport Shift
@@ -98,37 +98,49 @@ cdef class SecurityValueHolder(object):
9898
self._innerHolders[name] = holder
9999

100100
@property
101+
@cython.boundscheck(False)
102+
@cython.wraparound(False)
101103
def value(self):
102104

103-
cdef list values
105+
cdef np.ndarray[double, ndim=1] values
104106
cdef Accumulator holder
107+
cdef int n
108+
cdef int i
105109

106110
if self.updated:
107111
return SecurityValues(self.cached.values, self.cached.name_mapping)
108112
else:
109113
keys = self._innerHolders.keys()
110-
values = []
111-
for name in keys:
114+
n = len(keys)
115+
values = np.zeros(n)
116+
for i, name in enumerate(keys):
112117
try:
113118
holder = self._innerHolders[name]
114-
values.append(holder.result())
119+
values[i] = holder.result()
115120
except ArithmeticError:
116-
values.append(np.nan)
117-
self.cached = SecurityValues(np.array(values), index=OrderedDict(zip(keys, range(len(keys)))))
121+
values[i] = np.nan
122+
self.cached = SecurityValues(values, index=OrderedDict(zip(keys, range(n))))
118123
self.updated = 1
119124
return self.cached
120125

126+
@cython.boundscheck(False)
127+
@cython.wraparound(False)
121128
cpdef value_by_names(self, list names):
122129
cdef Accumulator holder
130+
cdef np.ndarray[double, ndim=1] res
131+
cdef int i
132+
cdef int n
133+
123134
if self.updated:
124135
return self.cached[names]
125136
else:
126-
res = []
127-
for name in names:
137+
n = len(names)
138+
res = np.zeros(n)
139+
for i, name in enumerate(names):
128140
holder = self._innerHolders[name]
129-
res.append(holder.result())
130-
return SecurityValues(np.array(res),
131-
index=OrderedDict(zip(names, range(len(names)))))
141+
res[i] = holder.result()
142+
return SecurityValues(res,
143+
index=OrderedDict(zip(names, range(n))))
132144

133145
cpdef value_by_name(self, name):
134146
cdef Accumulator holder
@@ -296,11 +308,13 @@ cdef class FilteredSecurityValueHolder(SecurityValueHolder):
296308

297309
@property
298310
def value(self):
311+
cdef SecurityValues filter_value
312+
299313
if self.updated:
300314
return self.cached
301315
else:
302-
filter_value = self._filter.value.values
303-
self.cached = self._computer.value.mask(filter_value)
316+
filter_value = self._filter.value
317+
self.cached = self._computer.value.mask(filter_value.values)
304318
self.updated = 1
305319
return self.cached
306320

PyFin/Analysis/SecurityValues.pyx

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
u"""
43
Created on 2017-2-7
54
@@ -20,16 +19,27 @@ cdef class SecurityValues(object):
2019
data = np.array(list(data.values()))
2120

2221
self.values = data
23-
self.name_mapping = index
22+
if isinstance(index, OrderedDict):
23+
self.name_mapping = index
24+
else:
25+
raise ValueError("name mapping {0} is not Ordered dict".format(index))
2426
self.name_array = None
2527

2628
@cython.boundscheck(False)
2729
@cython.wraparound(False)
2830
def __getitem__(self, name):
31+
32+
cdef np.ndarray data
33+
cdef np.ndarray values
34+
2935
if not isinstance(name, list):
3036
return self.values[self.name_mapping[name]]
3137
else:
32-
data = np.array([self.values[self.name_mapping[n]] for n in name])
38+
39+
values = self.values
40+
name_mapping = self.name_mapping
41+
42+
data = np.array([values[name_mapping[n]] for n in name])
3343
return SecurityValues(data, OrderedDict(zip(name, range(len(name)))))
3444

3545
@cython.boundscheck(False)

PyFin/Analysis/TechnicalAnalysis/StatefulTechnicalAnalysers.pyx

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
u"""
43
Created on 2015-8-8
54
65
@author: cheng.li
76
"""
87

98
import copy
9+
from collections import OrderedDict
10+
import numpy as np
1011
cimport numpy as np
12+
cimport cython
13+
from PyFin.Math.Accumulators.IAccumulators cimport Accumulator
1114
from PyFin.Analysis.SecurityValues cimport SecurityValues
1215
from PyFin.Analysis.SecurityValueHolders cimport SecurityValueHolder
1316
from PyFin.Math.Accumulators.StatefulAccumulators cimport MovingAverage
@@ -396,6 +399,59 @@ cdef class SecurityMovingHistoricalWindow(SecuritySingleValueHolder):
396399
else:
397400
raise ValueError("{0} is not recognized as valid int or string".format(item))
398401

402+
@property
403+
@cython.boundscheck(False)
404+
@cython.wraparound(False)
405+
def value(self):
406+
407+
cdef list values
408+
cdef Accumulator holder
409+
cdef int n
410+
cdef int i
411+
412+
if self.updated:
413+
return SecurityValues(self.cached.values, self.cached.name_mapping)
414+
else:
415+
keys = self._innerHolders.keys()
416+
n = len(keys)
417+
values = [None] * n
418+
for i, name in enumerate(keys):
419+
try:
420+
holder = self._innerHolders[name]
421+
values[i] = holder.result()
422+
except ArithmeticError:
423+
values[i] = np.nan
424+
self.cached = SecurityValues(np.array(values), index=OrderedDict(zip(keys, range(n))))
425+
self.updated = 1
426+
return self.cached
427+
428+
@cython.boundscheck(False)
429+
@cython.wraparound(False)
430+
cpdef value_by_names(self, list names):
431+
cdef Accumulator holder
432+
cdef list res
433+
cdef int i
434+
cdef int n
435+
436+
if self.updated:
437+
return self.cached[names]
438+
else:
439+
n = len(names)
440+
res = [None] * n
441+
for i, name in enumerate(names):
442+
holder = self._innerHolders[name]
443+
res[i] = holder.result()
444+
return SecurityValues(np.array(res),
445+
index=OrderedDict(zip(names, range(n))))
446+
447+
cpdef value_by_name(self, name):
448+
cdef Accumulator holder
449+
if self.updated:
450+
return self.cached[name]
451+
else:
452+
holder = self._innerHolders[name]
453+
return holder.result()
454+
399455
def __deepcopy__(self, memo):
400456
if self._compHolder:
401457
return SecurityMovingHistoricalWindow(self._window - self._compHolder._window, self._compHolder)

PyFin/Analysis/TechnicalAnalysis/StatelessTechnicalAnalysers.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
u"""
43
Created on 2015-2-12
54

PyFin/DateUtilities/Calendar.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
u"""
43
Created on 2017-2-1
54

PyFin/DateUtilities/Date.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
u"""
43
Created on 2017-1-3
54

PyFin/DateUtilities/Period.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
u"""
43
Created on 2017-1-30
54

PyFin/Math/Accumulators/IAccumulators.pyx

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
u"""
43
Created on 2017-2-8
54
@@ -101,6 +100,7 @@ cdef class Accumulator(IAccumulator):
101100

102101
cdef extract(self, dict data):
103102
cdef str p
103+
cdef Accumulator comp
104104

105105
if not self._isValueHolderContained:
106106
try:
@@ -111,8 +111,9 @@ cdef class Accumulator(IAccumulator):
111111
except KeyError:
112112
return np.nan
113113
else:
114-
self._dependency.push(data)
115-
return self._dependency.result()
114+
comp = self._dependency
115+
comp.push(data)
116+
return comp.result()
116117

117118
cpdef push(self, dict data):
118119
pass
@@ -603,14 +604,18 @@ cdef class StatelessSingleValueAccumulator(Accumulator):
603604
self._window = 0
604605

605606
cdef _push(self, dict data):
607+
608+
cdef Accumulator comp
609+
606610
if not self._isValueHolderContained:
607611
try:
608612
value = data[self._dependency]
609613
except KeyError:
610614
value = np.nan
611615
else:
612-
self._dependency.push(data)
613-
value = self._dependency.result()
616+
comp = self._dependency
617+
comp.push(data)
618+
value = comp.result()
614619
return value
615620

616621
def __deepcopy__(self, memo):

PyFin/Math/Accumulators/StatefulAccumulators.pyx

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
u"""
43
Created on 2017-2-8
54
@@ -103,14 +102,17 @@ cdef class SingleValuedValueHolder(StatefulValueHolder):
103102

104103
cdef double _push(self, dict data):
105104

105+
cdef Accumulator comp
106+
106107
cdef double value
107108
cdef int isValueHolder = self._isValueHolderContained
108109

109110
if not isValueHolder and self._dependency in data:
110111
return data[self._dependency]
111112
elif isValueHolder:
112-
self._dependency.push(data)
113-
return self._dependency.result()
113+
comp = self._dependency
114+
comp.push(data)
115+
return comp.result()
114116
else:
115117
return np.nan
116118

PyFin/Math/Accumulators/StatelessAccumulators.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
u"""
43
Created on 2015-7-25
54

PyFin/Math/Accumulators/impl.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
u"""
43
Created on 2017-1-1
54

PyFin/Math/Distributions/NormalDistribution.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
cimport cython
43
from PyFin.Math.Distributions.norm cimport pdf
54
from PyFin.Math.Distributions.norm cimport cdf

PyFin/Math/Distributions/norm.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
u"""
43
Created on 2017-2-4
54

PyFin/Math/ErrorFunction.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
#cython: embedsignature=True
32
u"""
43
Created on 2017-1-3
54
@@ -8,6 +7,7 @@ Created on 2017-1-3
87

98
cimport cython
109
from libc.math cimport exp
10+
from libc.math cimport fabs
1111

1212
cdef double _DBL_MIN = 2.2250738585072014e-308
1313

@@ -87,7 +87,7 @@ cdef double errorFunction(double x):
8787
cdef double R
8888
cdef double S
8989

90-
ax = abs(x)
90+
ax = fabs(x)
9191

9292
if ax < 0.84375:
9393
if ax < 3.7252902984e-09:

0 commit comments

Comments
 (0)