Script CHOP how to write data quickly?

If I have an array of values, can I write it to the Script CHOP’s output in one line rather than a for-loop?

This code is fairly slow (2.5 ms), but everything before the for-loop only takes .18 ms. I hope it could be faster somehow.

import numpy as np
scriptOp.clear()

num_points = 1000
deg = np.linspace(0, math.pi*2, num=num_points)
#print(deg.shape)
data = np.stack([np.cos(deg), np.sin(deg), np.zeros(num_points)], axis=-1)
#print(data.shape)
scriptOp.appendChan('x')
scriptOp.appendChan('y')
scriptOp.appendChan('z')
scriptOp.numSamples = data.shape[0]
	
## I want this to work as a faster alternative to the for-loop below
#x_values = data[:, 0]	
#scriptOp['x'] = x_values

for i, (x, y, z) in enumerate(data):
	scriptOp['x'][i] = x.item()
	scriptOp['y'][i] = y.item()
	scriptOp['z'][i] = z.item()

hey @DavidBraun ,

this is the reason I made an RFE for writing a numpy array at once to the CHOP, and this feature landed in stable a few months ago! (thanks again Rob :grinning:)
So now the fastest way is to build a numpy array and write all your values to that. Your np array must in the form shape(numChannels, numSamples). All values must be float32.
Then write the whole array in a single step to the CHOP using copyNumpyArray method.
This is quite fast - on a recent project I wrote 66 channels with each 12_000 samples in ~0.3 msec.
Also see scriptCHOP Class - Derivative

2 Likes

Woops I missed that in the docs. Thank you.