Externalize property from Python Transform

500 views
Skip to first unread message

Jonathan Newman

unread,
Sep 25, 2018, 12:56:19 PM9/25/18
to Bonsai Users
Lets say I have a Python Transform:

from random import random

home
= 2
rwd_prob
= 0.5

@returns(int)
def process(value):


  origin
= value.Item2
  location
= value.Item1


 
if (origin == home and random() > rwd_prob):
     
return 1 # Correct action, get reward
 
elif (origin == home):
     
return 2 # Correct action but no reward
 
elif (location == home):
     
return 1 # Return home get reward
 
else:
     
return 0 # Incorrect action

Is it possible to externalize `home` and `rwd_prob` the way that other public properties can be externalized in C#-based nodes? I know there is an @property decorator in python, but I think this is for decorating members of classes. I just want to expose these global. I guess it might be required that they are constexpr though or they will have become reactive elements somehow.

Thanks.

Jon

Gonçalo Lopes

unread,
Sep 25, 2018, 1:10:32 PM9/25/18
to Jonathan Newman, Bonsai Users
Hey Jon,

Yeah, unfortunately this is not possible at the moment... actually, this is a big part of the reason why I'm moving to C# scripts more and more.

The best approximation you can do is to actually receive these parameters as input to the node. For example, you can use a CombineLatest before the Python and feed it a couple of Float sources. Inside the python script you can then retrieve the elements from the tuple using home = value.Item3, rwd_prob = value.Item4 and so on.



--
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+unsubscribe@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/c038bc33-b892-45ee-a78f-72341738b88d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonathan Newman

unread,
Sep 26, 2018, 12:46:04 PM9/26/18
to Bonsai Users
OK, that sorta what I figured although Bonsai has certainly surprised me in the past with its polish for stuff like this :).

When you refer to C# scripts, are you referring to the ExpressionTransforms, Sinks, etc? 

If so, do you have any sort of examples of these being used to implement relatively complex logic? Just examples of how you are using them where in the past you might have used the python transform would be enough to get started, I think.

- Jon
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.

Gonçalo Lopes

unread,
Sep 26, 2018, 7:49:50 PM9/26/18
to Jonathan Newman, Bonsai Users
Hey Jon,

Actually, I was referring to the C# scripting capabilities of Bonsai 2.4. They are available for trial now in the preview. Here's a very rough visual guide:

1) Add a new csharp node.



2) Double-click to edit (requires Visual Studio Code to be installed). A bunch of popups with questions will show up. Expect some pain, there are a couple of steps to go through the first time, installing a bunch of stuff. The user experience is definitely not great, but it's only that first time.

Eventually, you should see something like this:


Click on "Restore". To be honest, there is a high probability this will fail because the 2.4 nuget packages are not online yet, so Visual Studio Code will barf. One quick way to fix this is to open Extensions.csproj and change the version of the Bonsai.Core dependency to version 2.3, that will be found online. You may have to restart VS Code. Probably you will have to anyway to install a bunch of other stuff.

Hopefully at some point all the red stuff will go away and you can then edit the C# directly with Intellisense and all the niceties.

3) If you get to this point, the rest is more or less trivial. Just add properties to the class, and they will be added to the operator in Bonsai, after you reload extensions:




Hope this helps. Sorry for the lousy write-up, I will get to do a decent one on the website once the release is ironed-out and finally pushed out the door!

To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.

Jon Newman

unread,
Sep 26, 2018, 8:28:35 PM9/26/18
to Gonçalo Lopes, bonsai...@googlegroups.com
Holy shit that looks awesome! I will try tomorrow first thing.




--
Jonathan Newman
Postdoctoral Fellow, MIT
Reply all
Reply to author
Forward
0 new messages