From 031a04bbf169c2e26137a39893d6a3c6223c8c4c Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Tue, 10 Oct 2023 15:26:35 -0400 Subject: [PATCH] API: Add complex.DataArray.npview() function Signed-off-by: Michael Jackson --- .../ComplexCore/wrapping/python/complexpy.cpp | 13 +++++++++++++ .../python/docs/source/Python_Introduction.rst | 9 +-------- wrapping/python/examples/angle_conversion.py | 6 +----- wrapping/python/examples/basic_arrays.py | 15 +++------------ wrapping/python/examples/basic_numpy.py | 7 +------ wrapping/python/examples/geometry_examples.py | 14 +++++++------- wrapping/python/examples/import_d3d.py | 8 ++------ wrapping/python/examples/output_file.py | 4 +--- 8 files changed, 29 insertions(+), 47 deletions(-) diff --git a/src/Plugins/ComplexCore/wrapping/python/complexpy.cpp b/src/Plugins/ComplexCore/wrapping/python/complexpy.cpp index 78de6fea30..6254d876f1 100644 --- a/src/Plugins/ComplexCore/wrapping/python/complexpy.cpp +++ b/src/Plugins/ComplexCore/wrapping/python/complexpy.cpp @@ -169,6 +169,19 @@ auto BindDataArray(py::handle scope, const char* name) { py::class_, IDataArray, std::shared_ptr>> dataArray(scope, name); dataArray.def_property_readonly_static("dtype", []([[maybe_unused]] py::object self) { return py::dtype::of(); }); + dataArray.def( + "npview", + [](DataArray& dataArray) { + using DataArrayType = DataArray; + using DataStoreType = DataStore; + const typename DataArrayType::store_type& abstractDataStore = dataArray.getDataStoreRef(); + const DataStoreType& dataStore = dynamic_cast(abstractDataStore); + IDataStore::ShapeType shape = dataStore.getTupleShape(); + IDataStore::ShapeType componentShape = dataStore.getComponentShape(); + shape.insert(shape.end(), componentShape.cbegin(), componentShape.cend()); + return py::array_t(shape, dataStore.data(), py::cast(dataStore)); + }, + py::return_value_policy::reference_internal); return dataArray; } diff --git a/wrapping/python/docs/source/Python_Introduction.rst b/wrapping/python/docs/source/Python_Introduction.rst index 6bfd7bb83c..4ec04450e7 100644 --- a/wrapping/python/docs/source/Python_Introduction.rst +++ b/wrapping/python/docs/source/Python_Introduction.rst @@ -307,15 +307,8 @@ The next code section was take from `basic_arrays.py `__ diff --git a/wrapping/python/examples/angle_conversion.py b/wrapping/python/examples/angle_conversion.py index d95d9029f2..baa601a8ca 100644 --- a/wrapping/python/examples/angle_conversion.py +++ b/wrapping/python/examples/angle_conversion.py @@ -26,11 +26,7 @@ print("No errors running the CreateDataArray") # Get a numpy.view into the newly created DataArray -data_array = data_structure[array_path] -# Get the underlying DataStore object -data_store = data_array.store -# Get a Numpy View into the data -npdata = data_store.npview() +npdata = data_structure[array_path].npview() # Read the CSV file into the DataArray using the numpy view file_path = 'angles.csv' diff --git a/wrapping/python/examples/basic_arrays.py b/wrapping/python/examples/basic_arrays.py index 1823da1ed2..dbc73bb0ac 100644 --- a/wrapping/python/examples/basic_arrays.py +++ b/wrapping/python/examples/basic_arrays.py @@ -37,12 +37,7 @@ print("No errors running the filter") # We can check the output of the filter by simply printing the array -# First get the array from the DataStructure -data_array = data_structure[output_array_path] -# Get the underlying DataStore object -data_store = data_array.store -# Get the raw data as an Numpy View -npdata = data_store.npview() +npdata = data_structure[output_array_path].npview() print(npdata) #------------------------------------------------------------------------------ @@ -68,9 +63,7 @@ else: print("No errors running the filter") -data_array = data_structure[output_array_path] -data_store = data_array.store -npdata = data_store.npview() +npdata = data_structure[output_array_path].npview() print(npdata) @@ -97,9 +90,7 @@ else: print("No errors running the filter") -data_array = data_structure[output_array_path] -data_store = data_array.store -npdata = data_store.npview() +npdata = data_structure[output_array_path].npview() print(npdata) result = cx.CreateAttributeMatrixFilter.execute(data_structure=data_structure, diff --git a/wrapping/python/examples/basic_numpy.py b/wrapping/python/examples/basic_numpy.py index 54072c1b19..406c435d52 100644 --- a/wrapping/python/examples/basic_numpy.py +++ b/wrapping/python/examples/basic_numpy.py @@ -23,12 +23,7 @@ output_data_array=array_path, initialization_value='0') - -data_array = data_structure[array_path] -# Get the underlying DataStore object -data_store = data_array.store -# Get a Numpy View into the data -npdata = data_store.npview() +npdata = data_structure[array_path].npview() # Manipulate the underlying array npdata += 42.0 diff --git a/wrapping/python/examples/geometry_examples.py b/wrapping/python/examples/geometry_examples.py index 9bb63e6623..2763380f9e 100644 --- a/wrapping/python/examples/geometry_examples.py +++ b/wrapping/python/examples/geometry_examples.py @@ -65,7 +65,7 @@ output_data_array=output_array_path, tuple_dimensions=tuple_dims) -x_coords = data_structure[output_array_path].store.npview() +x_coords = data_structure[output_array_path].npview() x_coords = np.squeeze(x_coords, axis=1) x_coords[:] = np.arange(0, 10, 1) @@ -81,7 +81,7 @@ output_data_array=output_array_path, tuple_dimensions=tuple_dims) -y_coords = data_structure[output_array_path].store.npview() +y_coords = data_structure[output_array_path].npview() y_coords = np.squeeze(y_coords, axis=1) y_coords[:] = np.arange(10, 20, 1) @@ -97,7 +97,7 @@ output_data_array=output_array_path, tuple_dimensions=tuple_dims) -z_coords = data_structure[output_array_path].store.npview() +z_coords = data_structure[output_array_path].npview() z_coords = np.squeeze(z_coords, axis=1) z_coords[:] = np.arange(20, 30, 1) @@ -131,7 +131,7 @@ initialization_value='0') # Read the CSV file into the DataArray using the numpy view -vertex_coords = data_structure[array_path].store.npview() +vertex_coords = data_structure[array_path].npview() file_path = 'complex/test/Data/VertexCoordinates.csv' vertex_coords[:] = np.loadtxt(file_path, delimiter=',', skiprows=1) @@ -145,7 +145,7 @@ initialization_value='0') # Read the CSV file into the DataArray using the numpy view -triangles = data_structure[array_path].store.npview() +triangles = data_structure[array_path].npview() file_path = 'complex/test/Data/TriangleConnectivity.csv' triangles[:] = np.loadtxt(file_path, delimiter=',', skiprows=1) @@ -179,7 +179,7 @@ initialization_value='0') # Read the CSV file into the DataArray using the numpy view -vertex_coords = data_structure[array_path].store.npview() +vertex_coords = data_structure[array_path].npview() file_path = 'complex/test/Data/VertexCoordinates.csv' vertex_coords[:] = np.loadtxt(file_path, delimiter=',', skiprows=1) @@ -193,7 +193,7 @@ initialization_value='0') # Read the CSV file into the DataArray using the numpy view -edges = data_structure[array_path].store.npview() +edges = data_structure[array_path].npview() file_path = 'complex/test/Data/EdgeConnectivity.csv' edges[:] = np.loadtxt(file_path, delimiter=',', skiprows=1) diff --git a/wrapping/python/examples/import_d3d.py b/wrapping/python/examples/import_d3d.py index 72cd598654..82c8d16561 100644 --- a/wrapping/python/examples/import_d3d.py +++ b/wrapping/python/examples/import_d3d.py @@ -26,12 +26,8 @@ #------------------------------------------------------------------------------ # Get the underlying data from the DataStructure #------------------------------------------------------------------------------ -data_array = data_structure[cx.DataPath(["Small IN100", "Scan Data", "Image Quality"])] -# Get the underlying DataStore object -data_store = data_array.store +npview = data_structure[["Small IN100", "Scan Data", "Image Quality"]].npview() -# Get a View into the DataArray through a Numpy.View -npview = data_store.npview() # Change the underlying data based on some criteria using Numpy npview[npview < 120] = 0 @@ -53,7 +49,7 @@ # View with MatPlotLib #------------------------------------------------------------------------------ # Make a copy to that we can use MatPlotLib -npdata = data_store.npview().copy() +npdata = data_structure[["Small IN100", "Scan Data", "Image Quality"]].npview().copy() # Remove any dimension with '1' for MatPlotLib? npdata = np.squeeze(npdata, axis=0) diff --git a/wrapping/python/examples/output_file.py b/wrapping/python/examples/output_file.py index da922eea50..ab2a55a4e0 100644 --- a/wrapping/python/examples/output_file.py +++ b/wrapping/python/examples/output_file.py @@ -25,9 +25,7 @@ else: print("No errors running the filter") -data_array = data_structure[output_array_path] -data_store = data_array.store -npdata = data_store.npview() +npdata = data_structure[output_array_path].npview() print(npdata) output_file_path = "output_file_example.dream3d"