1
Automation / Re: Grayvalues for Material ID's using Python
« on: June 13, 2025, 12:35:28 PM »
Hi,
I assume, your gray value image does not have index 1. If only one image is loaded it has always index 0. If more images are loaded, have a look at the Color By combo-box in the Volume Field tab. Counting starts with 0. The fields have the order in which they are loaded to GeoDict.
[attachimg=1]
Another thing I noted in your code example, is that the indentation does not seem to make sense within the loop. In the example in the User Guide it was meant like the following. I marked the significant differences to your code with "!!!".
This will output something like this:
[attachimg=2]
I assume, your gray value image does not have index 1. If only one image is loaded it has always index 0. If more images are loaded, have a look at the Color By combo-box in the Volume Field tab. Counting starts with 0. The fields have the order in which they are loaded to GeoDict.
[attachimg=1]
Another thing I noted in your code example, is that the indentation does not seem to make sense within the loop. In the example in the User Guide it was meant like the following. I marked the significant differences to your code with "!!!".
Code: [Select]
VolumeInfo = gd.getVolumeFieldsInfo() # get list of dictionaries of loaded Volume Fields and assign it to variable VolumeInfo
nx, ny, nz = gd.getVolDimensions() # get the number of voxels in X-, Y- and Z-direction and assign the numbers to variables
Structure = gd.getStructure() # assign 3D numpy array describing the loaded structure to the variable Structure
# !!! In the following command ensure to have the right index for your gray value field !!!
Name = VolumeInfo[0]['name'] # assign the name of the volume field to the variable Name.
VolumeField = gd.getVolumeField(Name) # assign the numpy array describing the volume field to the variable VolumeField
Statistic = [] # Create empty list to store the statistical values
for k in range(nz): # loop over all Z-layers
value_sum = 0 # creating variable value_sum to sum up the result values
value_count = 0 # creating variable value_count to count the summands
for j in range(ny): # loop over all Y-layers
for i in range(nx): # loop over all X-layers
if Structure[i][j][k] == 0: # condition: if the kth Z-value in the jth Y-column of the ith X-layeris pore space (ID 0), the following indented section is executed
value_sum = value_sum + VolumeField[i][j][k] # add all pore space result values of the kth Z-layer to the sum value_sum
value_count = value_count + 1 # count the summands of value_sum
# !!! The following section should have the right indentation according to what should be plotted. Since we want to have one value for each Z-layer it belongs to the outer loop (for k in range nz) and thus needs to have the same indentation level as the head of the second loop, here 1 space. !!!
meanVal = value_sum / value_count # compute mean value of all pore space result values in the kth Z-layer and assign it to the variable meanVal
Statistic.append(meanVal) # append the mean value of the Z-layer to the Statistic list
# !!! The following section needs to be outside of the loop to plot all data in one plot !!!
gDlg = gd.makeGraphDialog() # create a graph dialog object
graph = gDlg.addGraph(Name, "Z layers", Name) # add a graph object with the name of the volume field as title and Y-axis legend and Z-layers as X-axis legend
Z_layers = list(range(1, nz + 1)) # writes the Z-layer numbers 1, 2, …, nz-1, nz into a list named Z-layers
graph.addData(Z_layers, Statistic, Name) # add a single dataset with the Z-layers as X-values, the mean result values as Y-values and the name of the volume field as legend to this graph
gDlg.run() # show graph dialog
This will output something like this:
[attachimg=2]