arbitrary function

301 views
Skip to first unread message

Tomaso Muzzu

unread,
Oct 16, 2018, 7:50:42 AM10/16/18
to Bonsai Users
Hello,

I am trying to generate an arbitrary function by which I can modify some parameters of my stimulus. At the moment this is done by reading a CSV file which contains the sequence of data points for such function. However, I would like to move this into Bonsai.

To start with, I would like to make a ramp function whose parameters can be modified within the workflow. This function would then be used to dynamically modify, for example, the contrast of a visual stimulus.

I am stuck because I tried to use Expression Transform with a for loop that simply adds one to the input for 100 iterations. Unfortunately, I can't implement this function as I am getting syntax errors.

Any help would be much appreciated.

Thanks,
Tomaso

Gonçalo Lopes

unread,
Oct 16, 2018, 8:51:15 AM10/16/18
to muzzu...@gmail.com, Bonsai Users
Hi Tomaso and welcome to the forums!

There are basically three ways to do this (in ascending order of complexity):

1) Use the Range operator. This source will generate a sequence of values between Start and Count, which you can parameterize in the workflow. You can rescale the ramp to be between any values by using the Rescale operator. This is probably the easiest way.

2) Use the Scan operator. This operator allows you to implement output feedback in bonsai. It's a nested operator, which means you have to write the workflow inside. The way it works is that every value you place in the WorkflowOutput will be fedback to the input Source1. This feedback is combined with the actual input to the node. For example, to create an accumulator in this way:

image.png

And inside Scan:

image.png

This method can be useful for building non-linear, or conditional, accumulators, since you can use all the power of Bonsai workflows to decide how your accumulation is computed.

3) Use a CSharpSource and write your own custom script operator (2.4-preview only). If you are using the preview, you can try the new C# script support. Insert a CSharpSource operator into the workflow, and double-click on it to launch the editor. Any parameters you include in the source will be directly configurable in the workflow. For example, here is a source that outputs a sequence of elements generated from a for loop:

using Bonsai;
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;

[Combinator]
[Description("")]
[WorkflowElementCategory(ElementCategory.Source)]
public class Loop
{
public int Iterations { get; set; }

public IObservable<int> Process()
{
return Observable.Defer(() =>
{
var values = new int[Iterations];
for (int i = 0; i < values.Length; i++)
{
values[i] = i;
}

return values.ToObservable();
});
}
}


This last method has maximum flexibility as you can use all Bonsai features, all of reactive, and all of C# to generate your values.

There are lots of variants of each of these methods that you could use to generate elements depending on what you need, but hope this helps!

--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/c98ce125-d678-4c23-aba2-dcf34213e283%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tomaso Muzzu

unread,
Oct 16, 2018, 11:08:55 AM10/16/18
to Bonsai Users
Hi,

first, thank you very much for the very quick reply.

I am trying to use the 3rd option but I get this error when I try to save a .cs file right after the insertion of a 'CSharpSource': "Access to the path C:/WINDOWS/system32/Extensions.csproj is denied". Any idea why I am getting this?

Thanks again,
Tomaso

Gonçalo Lopes

unread,
Oct 16, 2018, 11:12:35 AM10/16/18
to Tomaso Muzzu, Bonsai Users
Interesting, it looks like for some reason the current working directory for Bonsai is being set to the Windows system32 folder. This might be a bug with the 2.4-preview version. Are you trying to place the CSharpScript on an empty workflow?

If this is the case, can you try restarting Bonsai, creating a workflow from scratch, saving it first to a folder with write permissions (e.g. Documents or Desktop) and trying to insert the script node after that?



Tomaso Muzzu

unread,
Oct 17, 2018, 7:17:11 AM10/17/18
to goncal...@gmail.com, bonsai...@googlegroups.com
Hi,

I figured out the problem. I was using the wrong version of Bonsai, the 32bit instead of the 64 one. So that is solved now.

However I can't implement the C# code no matter what I do.

I tried to use the code you suggested but I am getting this error message: "This is a proxy for an unknown type. Please install any missing packages or replace the module."

What I did is the following:
- added a CSharpSource to the workflow without connecting it to anything.
- double-clicked it and pasted the code you wrote, saved and closed my C# compiler (Visual Studio Code);
- reloaded the extension from the button in the top bar.
- at this point I get the error.

Am I missing some packages? I thought that if the C# modules can be added they can also be used straight away.

Thank you again,
Tomaso

Gonçalo Lopes

unread,
Oct 17, 2018, 9:08:32 AM10/17/18
to Tomaso Muzzu, Bonsai Users
Interesting, it looks like the extensions might not be correctly compiled. Do you get any error message in the black command window? The one that pops up behind the Bonsai editor? Any script compile errors should be displayed there.

Tomaso Muzzu

unread,
Oct 17, 2018, 9:30:48 AM10/17/18
to Bonsai Users
I basically only save the script as .cs file and this is all I can see:
screenshotVSC.JPG

Gonçalo Lopes

unread,
Oct 17, 2018, 9:54:06 AM10/17/18
to Tomaso Muzzu, Bonsai Users
Hi Tomaso,

I meant the black command window that hangs behind the Bonsai editor, not the one from VS Code. This one:
image.png

If there are any warning or error messages, they will usually show up there. If the window has nothing, then it means there is a problem somewhere else.

Tomaso Muzzu

unread,
Oct 17, 2018, 10:12:40 AM10/17/18
to Gonçalo Lopes, bonsai...@googlegroups.com
Hi Gonçalo,

thank you very much for your help. The problem was in the module naming. I have to name it as the class name that is inside the CS script.

I didn't remember/know but know I do and I won't forget ;)

Best Wishes,
Tomaso

Tomaso Muzzu

unread,
Oct 22, 2018, 1:53:22 PM10/22/18
to Bonsai Users
Hi,

thank you very much for your help so far.

I am now trying to implement a random inter trial interval, though unsuccessfully.

I have created an arbitrary function in C# that is generated at the beginning of the programme. Its values are stored in an array. The elements of the array are read at each frame update and a parameter of the visual stimulus is updated dynamically. My reasoning is that after reading the last element of the array I would like to have a random time interval. During this, the last value of the array is kept. At the end of the interval, the array is to be read again from the beginning.

Please let me know if this makes sense.

I would be grateful if you could direct me towards the best solution for this. 

Best,
Tomaso

Gonçalo Lopes

unread,
Nov 11, 2018, 12:40:14 PM11/11/18
to Tomaso Muzzu, bonsai...@googlegroups.com
Hi Tomaso,

You might want to store the generated array into an AsyncSubject or BehaviorSubject, to make sure that the value is stored in memory for any subsequent subscribers.

If you do this, you can have another workflow elsewhere that specifies your trial structure (with Repeat). At the beginning of each sequence you can use SubscribeSubject to access the array. This way, everytime the sequence is repeated you will have access to the array.

Regarding the random inter-trial interval, you could use the Numerics package and the CreateExponential operator to sample random intervals that you can feed to a Timer node.
Hope this helps.

--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
Reply all
Reply to author
Forward
0 new messages