Mworks - python

Shay Ohayon's Avatar

Shay Ohayon

22 Aug, 2017 03:18 PM

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

  1. Support Staff 1 Posted by Christopher Sta... on 22 Aug, 2017 07:49 PM

    Christopher Stawarz's Avatar

    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

  2. 2 Posted by Shay Ohayon on 22 Aug, 2017 08:25 PM

    Shay Ohayon's Avatar

    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

  3. Support Staff 3 Posted by Christopher Sta... on 22 Aug, 2017 08:36 PM

    Christopher Stawarz's Avatar

    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

  4. 4 Posted by Shay Ohayon on 22 Aug, 2017 08:38 PM

    Shay Ohayon's Avatar

    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?

  5. Support Staff 5 Posted by Christopher Sta... on 23 Aug, 2017 02:17 PM

    Christopher Stawarz's Avatar

    Yes. Here's the equivalent XML.

  6. 6 Posted by Shay Ohayon on 23 Aug, 2017 02:19 PM

    Shay Ohayon's Avatar

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

  7. Support Staff 7 Posted by Christopher Sta... on 23 Aug, 2017 02:25 PM

    Christopher Stawarz's Avatar

    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

  8. Support Staff 8 Posted by Christopher Sta... on 23 Aug, 2017 06:44 PM

    Christopher Stawarz's Avatar

    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

  9. 9 Posted by Shay Ohayon on 23 Aug, 2017 06:48 PM

    Shay Ohayon's Avatar

    OK. Thank you for the update.

  10. Christopher Stawarz closed this discussion on 24 Aug, 2017 08:16 PM.

  11. Shay Ohayon re-opened this discussion on 30 Aug, 2017 03:40 PM

  12. 10 Posted by Shay Ohayon on 30 Aug, 2017 03:40 PM

    Shay Ohayon's Avatar

    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

  13. Support Staff 11 Posted by Christopher Sta... on 30 Aug, 2017 04:40 PM

    Christopher Stawarz's Avatar

    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

  14. 12 Posted by Shay Ohayon on 30 Aug, 2017 06:08 PM

    Shay Ohayon's Avatar

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

  15. 13 Posted by Shay Ohayon on 30 Aug, 2017 06:08 PM

    Shay Ohayon's Avatar

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

  16. 14 Posted by Shay Ohayon on 30 Aug, 2017 06:14 PM

    Shay Ohayon's Avatar

    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

    ---------

    <sounds tag="Sounds">
            <folder tag="sounds">
                <sound tag="acquire_sound" type="wav_file"
    path="/Users/shayo/Desktop/sounds/acquire.wav"></sound>
     ...

    ---------

    *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

  17. Support Staff 15 Posted by Christopher Sta... on 30 Aug, 2017 07:14 PM

    Christopher Stawarz's Avatar

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

    Chris

  18. 16 Posted by Shay Ohayon on 30 Aug, 2017 07:26 PM

    Shay Ohayon's Avatar

    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

  19. Support Staff 17 Posted by Christopher Sta... on 30 Aug, 2017 08:07 PM

    Christopher Stawarz's Avatar

    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

    <resource path="/path/to/sounddir" />

    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

  20. Christopher Stawarz closed this discussion on 06 Sep, 2017 02:37 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac