lannybroo / numpyIO
Implementation of scipy.io.numpyio.fread and scipy.io.numpyio.fwrite without depending on scipy. Uses numpy's tofile/fromfile instead.
Clone this repository (size: 28.0 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/lannybroo/numpyio/
| commit 3: | d2415a5d8104 |
| parent 2: | 72cc27bedc9b |
| branch: | default |
Update to more recent numpy conventions:
* type2size dict created once instead of big conditional in fread()
* totalSize doesn't need a loop
* call .ravel() method instead of numpy.ravel()
Changed (Δ212 bytes):
raw changeset »
numpyIO.py (12 lines added, 19 lines removed)
| … | … | @@ -34,6 +34,15 @@ 2003-03-31: Coded and tested. |
34 |
34 |
|
35 |
35 |
import numpy |
36 |
36 |
|
37 |
type2size = dict( |
|
38 |
b = 1, B = 1, c = 1, |
|
39 |
s = 2, w = 2, h = 2, H = 2, |
|
40 |
f = 4, i = 4, I = 4, l = 4, u = 4, |
|
41 |
d = 8, F = 8, |
|
42 |
D = 16, |
|
43 |
) |
|
44 |
type2size['1'] = 1 |
|
45 |
||
37 |
46 |
def fread(fid, num=None, readType='f', mem_type=None, byteswap=0): |
38 |
47 |
'''g = numpyIO.fread(fid, num, read_type, {mem_type, byteswap}) |
39 |
48 |
|
| … | … | @@ -48,19 +57,7 @@ OPTIONAL |
48 |
57 |
different endianness). Default = 0''' |
49 |
58 |
|
50 |
59 |
# how many bytes per number |
51 |
if readType in '1bcB': |
|
52 |
byteSize = 1 |
|
53 |
elif readType in 'swhH': |
|
54 |
byteSize = 2 |
|
55 |
elif readType in 'fiIlu': |
|
56 |
byteSize = 4 |
|
57 |
elif readType in 'dF': |
|
58 |
byteSize = 8 |
|
59 |
elif readType=='D': |
|
60 |
byteSize = 16 |
|
61 |
else: |
|
62 |
print readType |
|
63 |
|
|
60 |
byteSize = type2size[readType] |
|
64 |
61 |
|
65 |
62 |
# figure out num |
66 |
63 |
if not num: |
| … | … | @@ -106,18 +103,14 @@ OPTIONAL |
106 |
103 |
if write_type==0 or write_type==None: |
107 |
104 |
write_type = mem_type |
108 |
105 |
|
109 |
# figure out total size of myArray |
|
110 |
temp1 = myArray.shape |
|
111 |
totalSize = 1 |
|
112 |
for i in temp1: |
|
113 |
|
|
106 |
totalSize = myArray.size |
|
114 |
107 |
|
115 |
108 |
if num==None: |
116 |
109 |
num = totalSize |
117 |
110 |
|
118 |
111 |
# if not all elements of myArray are written |
119 |
112 |
if num!=totalSize: |
120 |
myArray1 = |
|
113 |
myArray1 = myArray.ravel()[:num] |
|
121 |
114 |
else: |
122 |
115 |
myArray1 = myArray |
123 |
116 |
