Mworks - python

Chris,

I would like to run a short python script to load images into Mworks (i.e., the python script parse some json file and reads the location of those images).
How do I then instruct Mworks to load those files?

Thanks,

Shay

Hi Shay,

I’ve attached an example with explanatory comments. To run it, you’ll need a recent nightly build (which I believe you’re using already). The experiment is written in MWEL, but the techniques it demonstrates can be implemented in XML, too.

If you have any questions, please let me know.

Chris

Attachment: python_image_loading.zip (1.62 KB)

Chris,

Do I need to rewrite all my experiment protocol in this mwel format, or can
I incorporate pieces of mwel code into existing mworks protocols?
– Shay

At the moment, you can’t mix them. If you want to use MWEL, all of your experiment must be written in it. But it might be worth changing that, to help folks who want to transition old experiment code to the new language. Let me think about it.

Chris

Hi Chris,

Long term - yes.
Short term - I’d like to get this up & running as soon as possible. Can I
load images from a python script without using mwel?

Yes. Here’s the equivalent XML.

Attachment: python_image_loading.xml (4.17 KB)

Great. Thank you Chris.
Was this the automatic output generated by “compiling” the mwel ?

No, I created it from scratch in MWEditor. The automatic conversion from MWEL produces XML that is less readable, with a fair amount of metadata. If you want to see it:

/Library/Application\ Support/MWorks/MWEL/mwel2xml python_image_loading.mwel

Chris

Note that, at present, the path to the image directory (both here and in the Python script) must be absolute. This limitation will be removed shortly.

This is done. As of tonight’s nightly build, the path to the image directory can be relative to the experiment file.

Chris

OK. Thank you for the update.

Hi Chris,

Sorry to bother you during your vacation, but this example doesn’t seem to
work for me.
I’ve tried it with the latest nightly build.
The problem seems to be related to defining the image path as a variable
for deferred loading.
It keeps trying to actually search for a file called $variablename

Do you have an idea how to fix this?

Thanks,

Shay

Did you include one or more “resource” declarations that identify the images you want to use? My examples include them. They won’t work without them.

Chris

Yes. Not sure what is going on. It is not finding some of the images.
Also complains about missing sound files.

I think it’s a relative/absolute path problem. I’ll try to dig into things
further.

Hi.
I can’t figure out what is going on. I have a absolute path stated for the
sound files, but I still get an error during run time:


BRAIN-AND-COG-SIX-SEVENTY-NINE:sounds shayo$ ls -la
/Users/shayo/Desktop/sounds/acquire.wav

-rw-------@ 1 shayo staff 11468 Jul 13 2004
/Users/shayo/Desktop/sounds/acquire.wav


...

22:08:27: ERROR: Failed to create object.

Extended information:

reason: Path does not exist:
/var/folders/ch/j7r8cncd6ql35y2p34dx1f_00000gn/T/MWorks/Experiment
Cache/_Users_shayo_Desktop_shay_protocols/tmp/Users/shayo/Desktop/sounds/acquire.wav

location: at line 265

object_type: sound/wav_file

ref_id: idm34632846946112

component: acquire_sound
parser_context: mw_create

You need to declare the sound files as resources, too. See https://mworks.github.io/documentation/0.8/components/resource.html .

Chris

I don’t fully understand. Do I need to completely remove all my previous
declarations of images and sound files and put all under resources?
How do I declare a sound resource vs an image resource?
Will the dynamic path work with resources?

– Shay

You don’t need to change or remove anything. You just need to add a resource declaration for any external files your experiment needs. If multiple files are in the same directory, you can just declare the directory path. Something like

The paths used in the resource declarations must match the paths used in the image and sound declarations (e.g. if using absolute paths, they must be absolute in both places).

Chris