Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Python Example use_case_02.py with additional example codes #423

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 53 additions & 7 deletions Wrapping/Python/Examples/use_case_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@
import dream3d.simpl_helpers as simpl_helpers




def import_csv_xyz_vertices_2(file_path: str):
#Generates a csv file with floats 0-(numVerts-1) and ints 1-numVerts
def make_random_data(num_elements, file_path: str):
with open(file_path + 'new_data.csv', 'w') as f:
writer = csv.writer(f, lineterminator='\n',)
writer.writerow(["Float", "Int"])
i = 0
while i < num_elements:
row = [i, i+1]
writer.writerow(row)
i = i+1

def import_csv_xyz_vertices_2(args):
"""
This version of the sample code uses slightly different CSV python APIs the difference being the use of
the numpy.loadtxt() function.

"""
file_path = args.input_file
file_path: str
verts_np = np.loadtxt(file_path, dtype={'names': ('x', 'y', 'z'), 'formats': ('f4', 'f4', 'f4')}, usecols=(0, 1, 2), delimiter=',', skiprows=1)
verts_flat = rfn.structured_to_unstructured(verts_np, dtype=np.float32, copy=False, casting='safe')

Expand All @@ -34,9 +44,44 @@ def import_csv_xyz_vertices_2(file_path: str):
dc.setGeometry(vertGeom)
# Add the data container to the DataContainerArray
dca.addOrReplaceDataContainer(dc)
# Write the Vertex Geometry out to a .dream3d file.
err = simpl_helpers.WriteDREAM3DFile('vert_geom_2.dream3d', dca, True)

file_path=args.output_dir
file_path: str
numVerts = len(verts_np)

#Creates csv with same amount of elements as vertices from input file
make_random_data(numVerts, file_path=args.output_dir)

# Create Cell AttributeMatrix
tupleDims = simpl.VectorSizeT([1, numVerts])
am = simpl_helpers.CreateAttributeMatrix(tupleDims, 'CellAttributeMatrix', simpl.AttributeMatrix.Type.Vertex)

imikejackson marked this conversation as resolved.
Show resolved Hide resolved
# Create Data Arrays
#Loads in csv file and gets data for each type
uints = np.genfromtxt(file_path + 'new_data.csv', dtype=np.uint32, skip_header=1, usecols=1, delimiter=',')
f32s = np.genfromtxt(file_path + 'new_data.csv', dtype=np.float32, usecols=0, skip_header=1, delimiter=',')

#Creates arrays from data with shape of (# of vertices x 1)
uintarr = np.ndarray(shape=(numVerts, 1), dtype=np.uint32, buffer=uints)
floatarr = np.ndarray(shape=(numVerts, 1), dtype=np.float32, buffer=f32s)

#Makes simpl arrays using numpy arrays
uint32arr = simpl.UInt32Array(uintarr, 'UInt32 Arr', True)
f32arr = simpl.FloatArray(floatarr, 'Float32 Arr', True)

# Number of tuples must match the number of vertices
# Create 2 Data Arrays: UInt32 and Float32
# Populate data ararys with some random data
# or
# for bonus points read the data from another text file
# Add Data Arrays to AttributeMatrix
am.addOrReplaceAttributeArray(uint32arr)
am.addOrReplaceAttributeArray(f32arr)

# Add AttributeMatirx to DataContainer
dc.addOrReplaceAttributeMatrix(am)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See if you can add the DREAM3D filter "apply_transformation_to_geometry" which is in the DREAM3DReview plugin. Take a look at https://github.com/dream3d/DREAM3DReview/blob/develop/Test/Python/Apply_Transformation_To_Geometry.py file for inspiration.

In that example we are using a transformation matrix but see if you can figure out how to use just a scaling factor in each direction. Try out a scaling factor of 10x in each direction just to make it obvious when the data is viewed.

Write out the dream3d file before the transformation and after then use ParaView to view the results and verify that you have successfully scaled the vertices.

err = simpl_helpers.WriteDREAM3DFile(file_path + 'vert_geom_2.dream3d', dca, True)

def import_csv_xyz_vertices_1(file_path: str):
"""
Expand Down Expand Up @@ -78,7 +123,8 @@ def import_csv_xyz_vertices_1(file_path: str):
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--input_file')
parser.add_argument('--output_dir')
args = parser.parse_args()

import_csv_xyz_vertices_1(file_path=args.input_file)
import_csv_xyz_vertices_2(file_path=args.input_file)
import_csv_xyz_vertices_2(args)