Loading [MathJax]/extensions/tex2jax.js
DM_numpy_null_value.py
1 ## @package python.demo.DM_numpy_null_value
2 # "
3 # example script showing how to handle null values with numpy dictionary
4 #
5 
6 from __future__ import print_function
7 import sys, random
8 
9 import numpy
10 from opals import pyDM
11 
12 # create an attribute layout for generating points with the corresponding attributes
13 lf = pyDM.AddInfoLayoutFactory()
14 lf.addColumn(pyDM.ColumnType.uint16, "_attr1")
15 shortLayout = lf.getLayout(clear=False) # shortLayout only contains _attr1
16 lf.addColumn(pyDM.ColumnType.float_, "_attr2")
17 layout = lf.getLayout() # layout contains _attr1 and _attr2
18 
19 def create_test_odm(filename):
20  # create 10 random points with two attributes:
21  # _attr1 (type: uint16) also set with random values for all points
22  # _attr2 (type: float_) filled with random values for half of the points
23  pcount = 10
24  print(f"Generate {pcount} random points")
25  pts = []
26  for i in range(pcount):
27  pt = pyDM.Point(layout)
28  pt.x = random.uniform(-10, 10)
29  pt.y = random.uniform(-10, 10)
30  pt.z = random.uniform(-10, 10)
31  pt.info().set(0, random.randint(0,5000)) # _attr1 (type: uint16)
32  if i % 2:
33  pt.info().set(1, random.uniform(0.5, 20)) # _attr2 (type: float_) is filled every second point only
34  pts.append(pt)
35 
36 
37  # write points to pyDM 'test.pyDM'
38  print("Add points into manager '" + filename + "'")
39 
40  dm = pyDM.Datamanager.create(filename, False)
41  if not dm:
42  print("Unable to create ODM '" + odm + "'")
43  sys.exit(1)
44 
45  for pt in pts:
46  dm.addPoint(pt)
47 
48  return dm
49 
50 
51 def DM_numpy_null_value(dm):
52  queryWin = pyDM.Window(-10, -10, 10, 10) # window
53 
54  print("case 1: no null values -----------------------------------")
55  # [no null values]
56  # We know that shortLayout, which only contains _attr1, doesn't have any null values.
57  # Hence, there is no need to provide noDataObj
58  resultNoNulls = pyDM.NumpyConverter.searchPoint(dm, queryWin, shortLayout, withCoordinates=True)
59  resultNoNulls2 = None
60  try:
61  # since _attr2 is not set for all points an exception will occur. catch it and continue
62  resultNoNulls2 = pyDM.NumpyConverter.searchPoint(dm, queryWin, layout, withCoordinates=True)
63  except Exception as e:
64  print("Exception occurred as expected:", e)
65  assert resultNoNulls2 == None # just to make sure that resultNoNulls2 is still none
66  print("\tno nulls=", resultNoNulls)
67  # [no null values]
68 
69 
70  print("\ncase 2: use no data value ------------------------------")
71  # [use no data value]
72  # use the minimum of the corresponding data type for all attributes as no data value
73  resultNoData1 = pyDM.NumpyConverter.searchPoint(dm, queryWin, layout, withCoordinates=True, noDataObj='min')
74  # use 0 as no data value for attr1 and NaN (=Not a Number) for attr2 (only possible for float and double attributes)
75  resultNoData2 = pyDM.NumpyConverter.searchPoint(dm, queryWin, layout, withCoordinates=True, noDataObj=[0, numpy.nan])
76  print("\tnodata1=", resultNoData1)
77  print("\tnodata2=", resultNoData2)
78  # [use no data value]
79 
80 
81  print("\ncase 3: use masked arrays ------------------------------")
82  # [use masked arrays]
83  # use masked arrays for all attributes
84  resultMasked1 = pyDM.NumpyConverter.searchPoint(dm, queryWin, layout, withCoordinates=True, noDataObj='mask')
85  # use the maximum value as no data for attr1 and a MaskedArray for attr2
86  resultMasked2 = pyDM.NumpyConverter.searchPoint(dm, queryWin, layout, withCoordinates=True, noDataObj=['max', 'mask'])
87  print("\tmasked1=", resultMasked1)
88  print("\tmasked2=", resultMasked2)
89  # [use masked arrays]
90 
91  print("\ncase 4: define value type of output arrays -------------")
92  # [define output value type]
93  # returns float64/double arrays, as defined by NumPy dtype
94  resultType1 = pyDM.NumpyConverter.searchPoint(dm, queryWin, layout, withCoordinates=True, noDataObj=numpy.nan,
95  valueType=numpy.dtype(float))
96  # returns float64/double arrays, as defined by pyDM.ColumnType
97  resultType2 = pyDM.NumpyConverter.searchPoint(dm, queryWin, layout, withCoordinates=True, noDataObj=numpy.nan,
98  valueType=pyDM.ColumnType.double_)
99  print("\tvalueType1=", resultType1)
100  print("\tvalueType2=", resultType2)
101  # [define output value type]
102 
103 
104 
105 dm = create_test_odm("test.odm")
106 
107 DM_numpy_null_value(dm)