|
4 | 4 | """ |
5 | 5 | import sys |
6 | 6 | import os |
7 | | -from os.path import join, exists |
8 | 7 | from contextlib import contextmanager |
9 | 8 | import numpy as np |
10 | | -import tables |
11 | 9 |
|
12 | 10 | if sys.version_info[0] == 2: |
13 | 11 | from cStringIO import StringIO |
@@ -48,89 +46,6 @@ def get_data_dir(): |
48 | 46 | return data_dir |
49 | 47 |
|
50 | 48 |
|
51 | | -def get_h5_data_file(): |
52 | | - """ |
53 | | - return the data file used for holding test data. If the data |
54 | | - directory or file do not exist, they are created. |
55 | | -
|
56 | | - Notes |
57 | | - ----- |
58 | | - This should ideally be called from a context manage as so:: |
59 | | -
|
60 | | - with get_h5_data_file() as f: |
61 | | - # do stuff |
62 | | -
|
63 | | - This way we know the file will be closed and cleaned up properly |
64 | | -
|
65 | | - """ |
66 | | - data_dir = get_data_dir() |
67 | | - |
68 | | - if not exists(data_dir): |
69 | | - os.mkdir(data_dir) |
70 | | - |
71 | | - data_file = join(data_dir, "testing_data.h5") |
72 | | - |
73 | | - return tables.open_file(data_file, "a", "Data for quantecon tests") |
74 | | - |
75 | | - |
76 | | -def get_h5_data_group(grp_name, parent="/", f=get_h5_data_file()): |
77 | | - """ |
78 | | - Try to fetch the group named grp_name from the file f. If it doesn't |
79 | | - yet exist, it is created |
80 | | -
|
81 | | - Parameters |
82 | | - ---------- |
83 | | - grp_name : str |
84 | | - A string specifying the name of the new group. This should be |
85 | | - only the group name, not including any information about the |
86 | | - group's parent (path) |
87 | | -
|
88 | | - parent : str, optional(default="/") |
89 | | - The parent or path for where the group should live. If nothing |
90 | | - is given, the group will be created at the root node `"/"` |
91 | | -
|
92 | | - f : hdf5 file, optional(default=get_h5_data_file()) |
93 | | - The file where this should happen. The default is the data file |
94 | | - for these tests |
95 | | -
|
96 | | - Returns |
97 | | - ------- |
98 | | - existed : bool |
99 | | - A boolean specifying whether the group existed or was created |
100 | | -
|
101 | | - group : tables.Group |
102 | | - The requested group |
103 | | -
|
104 | | - Examples |
105 | | - -------- |
106 | | - with get_h5_data_file() as f: |
107 | | - my_group = get_h5_data_group("jv") # data for jv tests |
108 | | -
|
109 | | - Notes |
110 | | - ----- |
111 | | - As with other code dealing with I/O from files, it is best to call |
112 | | - this function within a context manager as shown in the example. |
113 | | -
|
114 | | - """ |
115 | | - existed = True |
116 | | - try: |
117 | | - group = f.getNode(parent + grp_name) |
118 | | - except: |
119 | | - # doesn't exist |
120 | | - existed = False |
121 | | - msg = "data for {} tests".format(grp_name + ".py") |
122 | | - group = f.create_group(parent, grp_name, msg) |
123 | | - |
124 | | - return existed, group |
125 | | - |
126 | | - |
127 | | -def write_array(f, grp, array, name): |
128 | | - "stores array in into group grp of h5 file f under name name" |
129 | | - atom = tables.Atom.from_dtype(array.dtype) |
130 | | - ds = f.createCArray(grp, name, atom, array.shape) |
131 | | - ds[:] = array |
132 | | - |
133 | | - |
134 | 49 | def max_abs_diff(a1, a2): |
135 | 50 | "return max absolute difference between two arrays" |
136 | 51 | return np.max(np.abs(a1 - a2)) |
0 commit comments