-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_utils.pyx
More file actions
211 lines (175 loc) · 5.18 KB
/
_utils.pyx
File metadata and controls
211 lines (175 loc) · 5.18 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
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
"""Cython-based utils to be imported into utils."""
from __future__ import division, unicode_literals
from libc.stdlib cimport malloc, free
from libc.stdlib cimport atof
from libc.string cimport strtok, strcpy, strncpy
cimport numpy as np
cimport pyne.cpp_utils
from cython.operator cimport dereference as deref
import numpy as np
def fromstring_split(s, sep=None, dtype=float):
"""A replacement for numpy.fromstring() using the Python str.split()
and np.array().
Parameters
----------
s : str
String of data.
sep : str or None
String of separator characters, has the same meaning as in
str.split().
dtype : np.dtype
Numpy dtype to cast elements enough.
Returns
-------
data : ndarray, 1d
Will always return a 1d array of dtype. You must reshape to the
appropriate shape.
See Also
--------
fromstring_token : May faster depending on the data.
"""
cdef list rawdata
rawdata = s.split(sep)
return np.array(rawdata, dtype=dtype)
def fromstring_token(s, sep=" ", bint inplace=False, int maxsize=-1):
"""A replacement for numpy.fromstring() using the C standard
library atof() and strtok() functions.
Parameters
----------
s : str
String of data.
sep : str
String of separator characters. Unlike numpy.fromstring(),
all characters are separated on independently.
inplace : bool
Whether s should tokenized in-place or whether a copy should
be made. If done in-place, the first instance of sep between
any tokens will replaced with the NULL character.
maxsize : int
Specifies the size of the array to pre-allocate. If negative,
this will be set to the maximum possible number of elements,
ie len(s)/2 + 1.
Returns
-------
data : ndarray, 1d, float64
Will always return a 1d float64 array. You must cast and reshape
to the appropriate type and shape.
See Also
--------
fromstring_split : May faster depending on the data.
"""
cdef char* cstring
cdef char* cs
cdef char* csep
cdef int i, I
cdef np.ndarray[np.float64_t, ndim=1] cdata
s_bytes = s.encode()
I = len(s_bytes)
sep_bytes = sep.encode()
csep = sep_bytes
if inplace:
cs = s_bytes
else:
cs = <char *> malloc(I * sizeof(char))
strcpy(cs, s_bytes)
if maxsize < 0:
maxsize = (I // 2) + 1
data = np.empty(maxsize, dtype=np.float64)
cdata = data
i = 0
cstring = strtok(cs, csep)
while cstring != NULL:
cdata[i] = atof(cstring)
cstring = strtok(NULL, csep)
i += 1
if not inplace:
free(cs)
data = data[:i].copy()
return data
def endftod(s):
"""Converts a string from ENDF number format to float64.
Parameters
----------
s : char *
Plain string to convert.
Returns
-------
float64
"""
cdef char * cs
if isinstance(s, str):
s = s.encode()
cs = s
return pyne.cpp_utils.endftod(cs)
def use_fast_endftod():
""" Switches to fast ENDF string parser"""
pyne.cpp_utils.use_fast_endftod()
def fromendf_tok(s):
"""A replacement for numpy.fromstring().
Parameters:
-----------
s : str
String of data, consisting of complete lines of ENDF data.
Returns:
--------
data : ndarray, 1d, float64
Will always return a 1d float64 array. You must reshape to the
appropriate shape.
"""
cdef char * cs
if isinstance(s, str):
s = s.encode()
cs = s
cdef int i, num_entries
cdef char entry[12]
cdef long pos = 0
cdef np.ndarray[np.float64_t, ndim=1] cdata
i = 0
num_entries = len(cs)//81 * 6
cdata = np.empty(num_entries, dtype=np.float64)
while i < num_entries:
pos = i*11 + i//6 * 15
strncpy(entry, cs+pos, 11)
cdata[i] = pyne.cpp_utils.endftod(entry)
i += 1
return cdata
def fromendl_tok(s, num_fields):
"""A replacement for numpy.fromstring().
Parameters:
-----------
s : str
String of data, consisting of complete lines of ENDL data.
num_fields : int
Number of fields in each line of the ENDL data
Returns:
--------
data : ndarray, float64
Will return a num_fields-dimensional float64 array.
"""
cdef char * cs
if isinstance(s, str):
s = s.encode()
cs = s
cdef int i, num_entries, num_lines, line_length
cdef char entry[12]
cdef long pos = 0
cdef np.ndarray[np.float64_t, ndim=1] cdata
i = 0
line_length = num_fields*11+1
num_lines = len(cs)//line_length
num_entries = num_fields*num_lines
cdata = np.empty(num_entries, dtype=np.float64)
while i < num_entries:
pos = (i%num_fields)*11 + i//num_fields * line_length
strncpy(entry, cs+pos, 11)
cdata[i] = pyne.cpp_utils.endftod(entry)
i += 1
return cdata.reshape(num_lines, num_fields)
def use_warnings():
"""Displays if warnings are off or on
"""
return pyne.cpp_utils.USE_WARNINGS
def toggle_warnings():
"""Toggles warnings on and off
"""
return pyne.cpp_utils.toggle_warnings()