Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def processData(self):
_data = self.getInputData()
data = DataDict()
for k in self.traceNames:
data[k] = _data.get(k)
if self.xaxisName in _data:
data[self.xaxisName] = _data[self.xaxisName]
return data
def two_compatible_noisy_2d_sets(nx=10, ny=10):
x = np.linspace(0, 10, nx)
y = np.arange(ny)
xx, yy = np.meshgrid(x, y, indexing='ij')
dd = np.cos(xx) + (-0.05 + 0.1 * np.random.rand(*yy.shape))
dd2 = np.sin(xx) + (-0.5 + 1 * np.random.rand(*yy.shape))
d = DataDict(
x = dict(values=xx.reshape(-1)),
y = dict(values=yy.reshape(-1)),
cos_data = dict(values=dd.reshape(-1), axes=['x', 'y']),
sin_data = dict(values=dd2.reshape(-1), axes=['x', 'y']),
)
return d
def test_shape_guessing_simple():
"""test whether we can infer shapes correctly"""
a = np.linspace(0, 1, 11)
b = np.arange(5)
aa, bb = np.meshgrid(a, b, indexing='ij')
zz = aa * bb
dd = DataDict(
a=dict(values=aa.reshape(-1)),
b=dict(values=bb.reshape(-1)),
z=dict(values=zz.reshape(-1), axes=['a', 'b'])
)
assert guess_shape_from_datadict(dd) == dict(z=(['a', 'b'], (11, 5)))
dd['a']['values'][5] = None
dd['a']['values'][10] = np.nan
assert guess_shape_from_datadict(dd) == dict(z=(['a', 'b'], (11, 5)))
# non-uniform
# noise on the coordinates should not result in failing as long as it
# keeps monotonicity in the sweep axes
dd['a']['values'] = (aa + np.random.rand(a.size).reshape(a.size, 1)
* 1e-3).reshape(-1)
def three_compatible_3d_sets(nx=3, ny=3, nz=3, rand_factor=1):
x = np.linspace(0, 10, nx)
y = np.linspace(-5, 5, ny)
z = np.arange(nz)
xx, yy, zz = np.meshgrid(x, y, z, indexing='ij')
dd = np.cos(xx) * np.sin(yy) + rand_factor * np.random.rand(*zz.shape)
dd2 = np.sin(xx) * np.cos(yy) + rand_factor * np.random.rand(*zz.shape)
dd3 = np.cos(xx)**2 * np.cos(yy)**2 + rand_factor * np.random.rand(*zz.shape)
d = DataDict(
x = dict(values=xx.reshape(-1), unit='mA'),
y = dict(values=yy.reshape(-1), unit='uC'),
z = dict(values=zz.reshape(-1), unit='nF'),
data = dict(values=dd.reshape(-1), axes=['x', 'y', 'z'], unit='kW'),
more_data = dict(values=dd2.reshape(-1), axes=['x', 'y', 'z'], unit='MV'),
different_data = dict(values=dd3.reshape(-1), axes=['x', 'y', 'z'], unit='TS')
)
return d
def test_basic_storage_and_retrieval():
x = np.arange(3)
y = np.repeat(np.linspace(0, 1, 5).reshape(1, -1), 3, 0)
z = np.arange(y.size).reshape(y.shape)
data = dd.DataDict(
x=dict(values=x, unit='A',
__info__='careful!',
__moreinfo__='more words in here'),
y=dict(values=y, unit='B'),
z=dict(values=z, axes=['x', 'y'], unit='C'),
__desc__='some description',
)
assert data.validate()
dds.datadict_to_hdf5(data, FN, append_mode=dds.AppendMode.none)
datafromfile = dds.datadict_from_hdf5(FN)
# hdf5 saving added a few extra metas that we need to ignore when
# comparing
datafromfile = _clean_from_file(datafromfile)
assert(data == datafromfile)
def one_2d_set(nx=10, ny=10):
x = np.linspace(0, 10, nx)
y = np.arange(ny)
xx, yy = np.meshgrid(x, y, indexing='ij')
dd = np.cos(xx) + (-0.05 + 0.1 * np.random.rand(*yy.shape))
d = DataDict(
x=dict(values=xx.reshape(-1)),
y=dict(values=yy.reshape(-1)),
cos_data=dict(values=dd.reshape(-1), axes=['x', 'y']),
)
d.validate()
return d
DataGridder.uiClass = None
fc = linearFlowchart(('grid', DataGridder))
node = fc.nodes()['grid']
x = np.arange(5.0)
y = np.linspace(0, 1, 5)
z = np.arange(4.0, 6.0, 1.0)
xx, yy, zz = np.meshgrid(x, y, z, indexing='ij')
vv = xx * yy * zz
x1d, y1d, z1d = xx.flatten(), yy.flatten(), zz.flatten()
v1d = vv.flatten()
# construct data dict, with axes for vals not conforming to the
# correct order with which we've generated the data
data = DataDict(
x=dict(values=x1d),
y=dict(values=y1d),
z=dict(values=z1d),
vals=dict(values=v1d, axes=['y', 'z', 'x'])
)
assert data.validate()
# in the 1-d data, nothing unusual should happen
fc.setInput(dataIn=data)
assert num.arrays_equal(
fc.outputValues()['dataOut'].data_vals('vals'),
v1d,
)
# guessing the grid should work, and fix the wrong order
node.grid = GridOption.guessShape, dict()
def process(self, **kw):
data = kw['dataIn']
if data is None:
return None
data = super().process(**kw)
if data is None:
return None
data = data['dataOut'].copy()
self.axesList.emit(data.axes())
dout = None
method, opts = self._grid
if isinstance(data, DataDict):
if method is GridOption.noGrid:
dout = data.expand()
elif method is GridOption.guessShape:
dout = dd.datadict_to_meshgrid(data)
elif method is GridOption.specifyShape:
dout = dd.datadict_to_meshgrid(data, target_shape=opts['shape'])
elif isinstance(data, MeshgridDataDict):
if method is GridOption.noGrid:
dout = dd.meshgrid_to_datadict(data)
elif method is GridOption.guessShape:
dout = data
elif method is GridOption.specifyShape:
self.logger().warning(
f"Data is already on grid. Ignore shape.")
dout = data
def two_compatible_noisy_2d_sets(nx=10, ny=10):
x = np.linspace(0, 10, nx)
y = np.arange(ny)
xx, yy = np.meshgrid(x, y, indexing='ij')
dd = np.cos(xx) + (-0.05 + 0.1 * np.random.rand(*yy.shape))
dd2 = np.sin(xx) + (-0.5 + 1 * np.random.rand(*yy.shape))
d = DataDict(
x = dict(values=xx.reshape(-1)),
y = dict(values=yy.reshape(-1)),
cos_data = dict(values=dd.reshape(-1), axes=['x', 'y']),
sin_data = dict(values=dd2.reshape(-1), axes=['x', 'y']),
)
return d
# * too few or too many independents
if len(data.axes()) < 1 or len(data.axes()) > 2:
return PlotDataType.unknown
# * no data to plot
if len(data.dependents()) == 0:
return PlotDataType.unknown
if isinstance(data, MeshgridDataDict):
if len(data.axes()) == 2:
return PlotDataType.grid2d
else:
return PlotDataType.line1d
elif isinstance(data, DataDict):
if len(data.axes()) == 2:
return PlotDataType.scatter2d
else:
return PlotDataType.scatter1d
return PlotDataType.unknown