Combining MagPy-TMS with PsychoPy

OS: Win 10
PsychoPy version: 2020.2.5
Standard Standalone?: Yes, but I have also tried running my code through Anaconda using Spyder IDE, where I also have the latest psychopy package installed.
TMS apparatus: Magstim Rapid2 stimulator v9 connected to the PC via serial cable.

What are you trying to achieve?:
I am trying to build a staircase experiment to estimate a Transcranial Magnetic Stimulation (TMS) threshold for experiencing phosphenes.

What did you try to make it work?:
I have installed the python package MagPy-TMS which was created for controlling magstim stimulators. Running a script with MagPy-TMS works fine and I can control my stimulator and control all parameters.
The PsychoPy staircase works perfectly fine with no errors if I do not include the script for the stimulator.
In summary, both the TMS script and the PsychoPy staircase work fine if run separately.
However, as soon as I combine the two, python stops responding. I do not get any error, the program just crashes and I get the message “python has stopped responding”.

I have made the staircase through the standalone builder version. When that crashed, I extracted the python script and run it through my Anaconda’s Spyder IDE where I have all necessary packages installed. It crashed in the same way.

What specifically went wrong when you tried that?:
Unfortunately, I do not get any error message. What I have noticed by moving the TMS lines up and down through my script, is that as soon as my script reads the TMS lines I get the message that python has stopped responding. I am attaching the python script.

General:
I know that people have used PsychoPy with MagPy-TMS before and I have tried finding a code that previously worked to check it with my equipment. Unfortunately I had no luck. If anyone has any idea, or experienced a similar problem, I would appreciate any tips.

Phosphene_Threshold_v1.py (27.2 KB)

Hi Phivos - I see that the .py file here is an export from builder - would it be possible for you also to please share the .psyexp file you are working from?

Thanks,
Becca

sure, here it is!

Phosphene_Threshold_v1.psyexp (27.4 KB)

SOLVED:

The developer of MagPy-TMS had a looked and explained the issue to me.

The problem was that I was using the command if __name__ == "__main__": before each TMS command. The solution was using this command at the beginning of the script and including everything (except for imports).

1 Like

Apologies for resurrecting an old thread, but it seemed pertinent to reply and keep queries and info together.

In a nutshell, I was having a very similar problem and have managed to partly circumvent it by running via Coder, but I’d really like to be able to use MagPy-TMS via Builder. @phivph, did you ever manage this?

Hello!

No, to be honest I haven’t tried to solve this so it runs through the Builder. I think one solution would be to run the experiment on MAC OS, as MagPy-TMS would not require the if __name__ == "__main__": line.

Many thanks for getting back to me. It’s greatly appreciated. :blush:

Not that it’s an option for me, but what makes you suggest Mac OS and it not requiring if __name__ == '__main__' ?

It’s noted in the MagPy-TMS wiki, that if __name__ == '__main__' is only for windows. Here’s the link.

1 Like

Thanks for reminding me about that! :blush:
I now remember reading that before a long time ago, but (obviously) had completely forgotten about it. I wonder if there are any plans for MagPy to not require this. It’s the first lib/package I’ve encountered that actually needs this (admittedly common and often recommended) practice of using if __name__ == '__main__'.

Using it inside code components in Builder for the MagPy code doesn’t seem to work still over here though. It seems to only get as far as the connect() call before spiralling off and hanging. The only way I’ve gotten it to work as intended so far is to compile from Builder and then wrap all the code (except imports) in if __name__ == '__main__', which isn’t very elegant, but it does work.

I wonder if there’s a way in Builder to essentially do this and run all its code under an if __name__ == '__main__'…

(I’ve typed and written if __name__ == '__main__' too many times now, it doesn’t even mean anything any more. :melting_face:)

Yes, that is exactly how I dealt with it too. Once, the final version was ready it was not a big issue, as I could open the script through the PsychoPy runner and simply run the experiment through there.

One thing that might help, is to have a TMS True/False prompt at the beginning, and in your scripts controlling MagPy-TMS, adjust so commands are run only when the prompt is set to True. This could help when piloting/testing your experiment, as it will allow you to skip the compiling part.

Agreed. This is a very good shout indeed and I do something similar myself. :slight_smile: