-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array_Dataset.txt
371 lines (301 loc) · 9.74 KB
/
Array_Dataset.txt
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
Gumtree Workshop Module 1: Gumpy, Arrays and Datasets
=====================================================
Background
----------
### Gumtree/Python
Gumtree is written in Java. Java is a complex and tedious language to
write scientific code in. Python is a very pleasant language to write
scientific code in, particularly with scientific extensions like Numpy
(see below). However, Python is a scripting language that was
originally written in C, which would preclude its use in
Gumtree. Fortunately, a version of Python written in Java exists,
called Jython. Gumtree can therefore run Python and the Python in
Gumtree has full access to everything that Gumtree can do. 'Python' is used
in the following unless the behaviour is specific to Jython.
### Arrays
Many scientific datasets can be represented as homogeneous *arrays* of elements.
An "array" is a table of elements with an arbitrary number of dimensions -
on our instruments we would usually produce 2,3 or 4-dimensional data.
Many (all?) high-level scientific computing packages provide such array objects together
with many operations that can be performed on these arrays. The array support in Gumtree/Python
is modelled as closely as possible on 'Numpy'. [The Numpy tutorial][numpy tutorial]
is a useful learning resource for Gumpy arrays, and [the Numpy reference][numpy reference]
is a useful reference for many Gumpy array operations.
[numpy tutorial]: http://wiki.scipy.org/Tentative_NumPy_Tutorial
[numpy reference]: http://docs.scipy.org/doc/numpy/reference
### What is Gumpy?
Gumpy (Gumtree/Python) adds Numpy-like support for array objects. It builds on these basic array types
to provide richer objects ("Datasets") that allow automatic error propagation, axis manipulation,
and easy interfacing to our data files.
Arrays
------
The array routines are contained in the module 'array'. As many of the array routines have the same name
as dataset routines (see later), we must prefix them with 'array' to disambiguate.
### Creating arrays
1. arange – to create an array with a given shape and type.
~~~
>> array.arange(12,[3,4])
Array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
~~~
2. From a list
~~~
>> array.Array([1,2,3,4])
Array([1, 2, 3, 4])
~~~
3. As all `zeros` or `ones`, or `fill`ed with an arbitrary value:
~~~
>> array.ones([3,2])
Array([[1.000000, 1.000000],
[1.000000, 1.000000],
[1.000000, 1.000000]])
~~~
4. The same shape as another array (`ones_like`, `zeros_like`):
~~~
>> p = array.arange(6,[3,2])
>> p
Array([[0, 1],
[2, 3],
[4, 5]])
>> array.ones_like(p)
Array([[1, 1],
[1, 1],
[1, 1]])
~~~
5. Also: `rand`,`instance`. `rand` will fill the array with random values between 0 and 1. `instance` creates an array
of the specified shape and type (using the 'dtype' keyword).
### Interrogating arrays
An array object has a number of attributes that provide information:
`shape`, `ndim`, `size`, `dtype`, and `tolist` are the most useful.
~~~
>> p = array.rand([3,2])
>> p.dtype
<type 'float'>
>> p.shape
[3, 2]
>> b = array.instance([2,3],dtype=int)
>> b.fill(100)
>> b
Array([[100, 100, 100],
[100, 100, 100]])
~~~
### Manipulating arrays
The first element in an array is always numbered zero. The dimensions are
numbered from the outside in in a nested list representation; it may help to
read a shape such as `[6,3,2]` as "6 lists of 3 lists of 2 elements". NeXuS
file dimensions are `[frame,TOF,vertical,horizontal]`.
1. Slicing and indexing: `[start:finish:step,<next dimension>,...]`
`finish` and `step` can be omitted, in which case they default to the
end of the array and 1, respectively.
~~~
>> c=array.arange(12,[6,2])
>> c
Array([[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11]])
>> c[1:6:2]
Array([[ 2, 3],
[ 6, 7],
[10, 11]])
>> c[1:6:2,1]
Array([[ 3],
[ 7],
[11]])
~~~
2. Setting values: as above, with equals:
~~~
>> c[1:6:2,1] = 101
>> c
Array([[ 0, 1],
[ 2, 101],
[ 4, 5],
[ 6, 101],
[ 8, 9],
[ 10, 101]])
~~~
3. Joining arrays together: `concatenate`,`append`
~~~
>> a = Array([1,2,3])
>> b = Array([4,5,6])
>> array.concatenate([a,b])
Array([1, 2, 3, 4, 5, 6])
>> array.append(a,b)
Array([1, 2, 3, 4, 5, 6])
~~~
4. Unary mathematical functions: sin, cos, tan, arcsin, arccos, arctan, sqrt,
exp,ln, log10...
~~~
>> c.sqrt()
Array([[0.000000, 1.000000],
[1.414214, 1.732051],
[2.000000, 2.236068],
[2.449490, 2.645751],
[2.828427, 3.000000],
[3.162278, 3.316625]])
~~~
5. Binary mathematical functions: +, - , * , /
~~~
>> c=array.arange(12,[6,2])
>> d = c + 1
>> d
Array([[1.000000, 2.000000],
[3.000000, 4.000000],
[5.000000, 6.000000],
[7.000000, 8.000000],
[9.000000, 10.000000],
[11.000000, 12.000000]])
>> c = array.arange(12,[3,4])
>> d = array.ones_like(c)*2
>> c+d
Array([[ 2, 3, 4, 5],
[ 6, 7, 8, 9],
[10, 11, 12, 13]])
~~~
6. Masking: use of comparison, e.g. < , >=, == etc.
~~~
>> p = array.arange(12)
>> p
Array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>> p > 5
Array([False, False, False, False, False, False, True, True, True, True, True, True])
>> q = array.rand(12)
>> q[p>5] = -0.5
>> q
Array([0.004117, 0.298131, 0.256593, 0.800404, 0.245852, 0.606921, -0.500000, -0.500000, -0.500000, -0.500000, -0.500000, -0.500000])
~~~
7. Reshaping: `transpose`,`reshape`,`get_reduced`. `get_reduced` removes axes of
dimension one.
~~~
>> a = array.arange(12,[3,4])
>> a
Array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>> a.reshape([4,3])
Array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
>> a.transpose()
Array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
~~~
### Aggregate functions: sum(), max(), min(), any(), all(), intg()
Note that sum can take an axis specifier, in which case it will
sum along the given axis, always producing a one-dimensional array. To
sum *only* along one dimension, use `intg`.
~~~
>> c = array.arange(5,[5,1])
>> c.sum()
10.0
>> c = array.arange(12,[6,2])
>> c.sum(0)
Array([1.000000, 5.000000, 9.000000, 13.000000, 17.000000, 21.000000])
>> c.sum(1)
Array([30.000000, 36.000000])
>> c.max()
11.0
~~~
Dataset - Multi-Dimensional Data Container
------------------------------------------
### What is a Dataset?
A Dataset enhances Arrays by adding axes for each dimension, variance and metadata. Look at `arange` for Datasets:
~~~
>> d = arange(12,[3,4])
>> print str(d)
title: 17
storage: [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
error: [[0.000000, 1.000000, 1.414214, 1.732051]
[2.000000, 2.236068, 2.449490, 2.645751]
[2.828427, 3.000000, 3.162278, 3.316625]]
axes:
0. title: dim_0
storage: [0.000000, 1.000000, 2.000000]
1. title: dim_1
storage: [0.000000, 1.000000, 2.000000, 3.000000]
~~~
### Dataset manipulation
All of the above array manipulation routines are applicable to Datasets, and will propagate errors as well.
### Creating datasets
Datasets can be created from Arrays, lists and NeXuS files:
~~~
>> p = Dataset('/home/jrh/programs/echidna/Echidna-Gumtree-Scripts/Data/ECH0010123.nx.hdf')
>> p.shape
[50, 1, 128, 128]
>> p['/entry1/sample/name']
'Ni404-cubane'
>> p['/entry1/experiment/title']
'P2708'
~~~
Additional attributes (i.e. found using `.`) include: `axes` (a list of axes), `storage`
(the array holding the data) and `var` (the variance array). Datasets originating from
files have the `location` attribute as well. You can obtain a list of available metadata
using the standard Python `dir` command.
~~~
>> p.location
u'/home/jrh/programs/echidna/Echidna-Gumtree-Scripts/Data/ECH0010123.nx.hdf'
>> d.axes[0]
SimpleData(Array([0.000000, 1.000000, 2.000000]),
title='dim_0',
units='')
>> len(d.axes)
2
>> d.storage
Array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>> d.var
SimpleData(Array([[0.000000, 1.000000, 2.000000, 3.000000],
[4.000000, 5.000000, 6.000000, 7.000000],
[8.000000, 9.000000, 10.000000, 11.000000]]),
title='variance',
units='')
>> dir(d)
['axes', 'copy_from', 'dtype', 'err', 'error', 'exp', 'fill', 'flatten', 'float_copy', 'get_reduced',
'get_section', 'get_slice', 'item_iter', 'ln', 'location', 'log10', 'max', 'metadata', 'min', 'name',
'ndim', 'positive_float_copy', 'put', 'reshape', 'section_iter', 'set_value', 'shape', 'size', 'sqrt',
'storage', 'sum', 'take', 'title', 'title', 'tolist', 'units', 'view_1d']
~~~
### Saving datasets
Datasets can be saved:
~~~
>> b = Dataset([1,2,3])
>> b.save_copy("/home/jrh/temp/bcopy")
>> Dataset("/home/jrh/temp/bcopy")
Dataset(Array([1, 2, 3]),
title='bcopy',
var=Array([1.000000, 2.000000, 3.000000]),
axes=[SimpleData(Array([0.000000, 1.000000, 2.000000]),
title='dim_0',
units='')])
~~~
### Plotting datasets
Your Gumtree interface has 3 pre-defined plotting areas known as `Plot1`, `Plot2` and `Plot3`. To plot a dataset,
simply call the `set_dataset` method of these objects:
~~~
>> Plot1.set_dataset(p[0][0])
org.gumtree.vis.nexus.dataset.Hist2DNXDataset@a65f55
>> Plot2.set_dataset(Dataset(p.axes[0]))
org.gumtree.vis.dataset.XYErrorDataset@18697cd
~~~
Other useful methods and attributes:
`ds`
: a list of datasets in the plot, or `None`
`add_dataset(ds)`
: add a further dataset
`remove_dataset(ds)`
: remove the specified dataset
`set_title(string)`
: add a title to the plot
`set_x_label(string)`
: add a horizontal axis label to the plot (a plot must be present)
`set_y_label(string)`
: add a vertical axis label (a plot must be present)