Forum Navigation
You need to log in to create posts and topics.

Bin and Reduce Attributes

Hey All,

In 2.X versions the BinAndReduceModifier had an assigned attribute bin_data, which could be used in the scripting environment to output the matrix of data to file. For example;

localavg = ovito.modifiers.BinAndReduceModifier(...)

np.savetxt("%d_averages.txt" %frame, localavg.bin_data)

This no longer works, the error reports "AttributeError: 'ovito.plugins.GridPython.SpatialBinningModifier' object has no attribute 'bin_data'"
I looked at the current 3.X analogs of export_file but could not find a documentation on BinAndReduce (or SpatialBinning) for their output attributes. Any help here is appreciated, thanks.


-Mitch Wood

Hey Mitch,

this is because the SpatialBinningModifier now let's you perform spatial binning in different dimensions. If you perform a 1D binning, then the resulting histogram will be saved as Data Series object
and can be exported using OVITO's file_export() function with the "txt/series" output format option.

If you use 2 or 3 binning directions, then the result will be stored in a Voxel Grid.
Currently, there is no file export function for Voxel Grids yet, but you can easily do it with your own python modifier function, e.g. like this:

from import *
import numpy as np 

def modify(frame, data):
    grid = data.grids['binning[MyProperty]']
    array = grid['MyProperty']
    array_3d = np.reshape(array, grid.shape)

    with open("voxel_grid_{}.txt".format(frame), "w") as out:
        out.write("#3d grid: x \t  y \t  z \t my_property\n")
        for i in range(0,array_3d.shape[0]):
            for j in range(0,array_3d.shape[1]):
                for k in range(0,array_3d.shape[2]):
                    out.write( "{:f} \t {:f} \t {:f} \t {:.4f} \n".format(i, j, k, array_3d[i][j][k]))

Let me know if that works for you.


New for our users in China: OVITO on WeChat 

Official OVITO WeChat channel operated by Foshan Diesi Technology Co., Ltd.