Skip to content

Commit

Permalink
API: Add complex.DataArray.npview() function
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Jackson <[email protected]>
  • Loading branch information
imikejackson committed Oct 17, 2023
1 parent 677ddc6 commit 031a04b
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 47 deletions.
13 changes: 13 additions & 0 deletions src/Plugins/ComplexCore/wrapping/python/complexpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ auto BindDataArray(py::handle scope, const char* name)
{
py::class_<DataArray<T>, IDataArray, std::shared_ptr<DataArray<T>>> dataArray(scope, name);
dataArray.def_property_readonly_static("dtype", []([[maybe_unused]] py::object self) { return py::dtype::of<T>(); });
dataArray.def(
"npview",
[](DataArray<T>& dataArray) {
using DataArrayType = DataArray<T>;
using DataStoreType = DataStore<T>;
const typename DataArrayType::store_type& abstractDataStore = dataArray.getDataStoreRef();
const DataStoreType& dataStore = dynamic_cast<const DataStoreType&>(abstractDataStore);
IDataStore::ShapeType shape = dataStore.getTupleShape();
IDataStore::ShapeType componentShape = dataStore.getComponentShape();
shape.insert(shape.end(), componentShape.cbegin(), componentShape.cend());
return py::array_t<T, py::array::c_style>(shape, dataStore.data(), py::cast(dataStore));
},
py::return_value_policy::reference_internal);
return dataArray;
}

Expand Down
9 changes: 1 addition & 8 deletions wrapping/python/docs/source/Python_Introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,8 @@ The next code section was take from `basic_arrays.py <https://github.com/BlueQua
output_data_array=output_array_path,
tuple_dimensions=tuple_dims)
# First get the array from the DataStructure
data_array = data_structure[output_array_path]
# Get the underlying complex.DataStore object
data_store = data_array.store
# Get the raw data as an Numpy View
npdata = data_store.npview()
# The developer could also just do this in a single line
npdata = data_structure[output_array_path].store.npview()
npdata = data_structure[output_array_path].npview()
The next code section was take from `basic_arrays.py <https://github.com/BlueQuartzSoftware/complex/tree/develop/wrapping/python/examples/angle_conversion.py>`__

Expand Down
6 changes: 1 addition & 5 deletions wrapping/python/examples/angle_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
15 changes: 3 additions & 12 deletions wrapping/python/examples/basic_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

#------------------------------------------------------------------------------
Expand All @@ -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)


Expand All @@ -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,
Expand Down
7 changes: 1 addition & 6 deletions wrapping/python/examples/basic_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions wrapping/python/examples/geometry_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down
8 changes: 2 additions & 6 deletions wrapping/python/examples/import_d3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions wrapping/python/examples/output_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 031a04b

Please sign in to comment.