Old 05-10-2018, 05:24 AM   #1
g-roma
Human being with feelings
 
Join Date: May 2018
Posts: 2
Default Using numpy in ReaScript

Hi all. Being able to use numpy in ReaScript python scripts would be great, given the amount of audio signal processing code based on numpy can be found around the Internet.
Problems using numpy were reported in this thread:
https://forum.cockos.com/showthread.php?t=161787

I'm trying to submit a more formal bug report in case it can help fixing the issue.
At the moment, importing numpy in a python script seems to work on OSX with system python (2.7), however support for python 2 will end soon. The issue with other versions is that when running a python script a second time, Reaper crashes. The crash happens at PyArray_Item_INCREF, so the behavior seems consistent with numpy's known problem with multiple initializations:
https://github.com/numpy/numpy/issues/7595
https://stackoverflow.com/questions/...ice-with-numpy

How to reproduce
Create a new python script in Actions with the following. Run it twice.

import numpy as np
x = np.ones((3,3))
a = np.sum(x)
RPR_ShowConsoleMsg('a: ' + str(a))

Expected result
For every execution, Reascript shows the following in the console:
a: 9.0

Observed behaviors
We have observed different behaviors using different python versions and platforms, all in Reaper 5.80/64. For the Anaconda versions, we created an fresh environment with the desired python version, e.g.
conda create -n reaper_python3 python=3.6. Then installed numpy in the environment.

1. OSX 10.12.6, Python 2.7 (system)
Works as expected.
2. OSX 10.12.6, Python 2.7 (Anaconda)
Works for the first time. The second time we get Reascript Error: Script execution error
3. OSX 10.12.6, Python 3.6 (Anaconda)
Works for the first time. The second time Reaper crashes.
4. Windows, Python 2.7 (Anaconda)
Reaper becomes unresponsive.
5. Windows, Python 3.6 (Anaconda)
Works for the first time. The second time Reaper crashes.



Example OSX Crash report (excerpt)

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 multiarray.cpython-36m-darwin.so 0x0000000107f19571 PyArray_Item_INCREF + 65
1 multiarray.cpython-36m-darwin.so 0x0000000107f1cbf2 PyArray_FromScalar + 258
2 multiarray.cpython-36m-darwin.so 0x0000000107f25a2e gentype_nonzero_number + 14
3 libpython3.6m.dylib 0x00000001152551e4 _PyEval_EvalFrameDefault + 33492
4 libpython3.6m.dylib 0x000000011525869b fast_function + 603
5 libpython3.6m.dylib 0x0000000115258341 call_function + 449
6 libpython3.6m.dylib 0x000000011524e7ff _PyEval_EvalFrameDefault + 6383
7 libpython3.6m.dylib 0x00000001152590ef _PyFunction_FastCallDict + 1119
8 libpython3.6m.dylib 0x000000011513f8f9 _PyObject_FastCallDict + 313
9 libpython3.6m.dylib 0x0000000115147a06 _PyObject_CallMethodIdObjArgs + 582
10 libpython3.6m.dylib 0x00000001152792e8 PyImport_ImportModuleLevelObject + 1512
11 libpython3.6m.dylib 0x0000000115246e85 builtin___import__ + 149
(...)
168 com.cockos.reaper 0x000000010086b1a6 PythonAPI::InterpretScript(char const*, char const*) + 1862
169 com.cockos.reaper 0x00000001006b04f9 ReaScript_Instance::editorProc(HWND__*, unsigned int, unsigned long, long) + 3241
170 com.cockos.reaper 0x00000001006af82f ReaScriptEditorProc(HWND__*, unsigned int, unsigned long, long) + 111
171 com.cockos.reaper 0x000000010050edac SwellDialogDefaultWindowProc(HWND__*, unsigned int, unsigned long, long) + 364
172 com.cockos.reaper 0x000000010050b0d1 -[REAPERSwell_hwnd onSwellCommand:] + 1169
173 libsystem_trace.dylib 0x00007fff9f04c3a7 _os_activity_initiate_impl + 53
174 com.apple.AppKit 0x00007fff87909721 -[NSApplication(NSResponder) sendAction:to:from:] + 456
175 com.apple.AppKit 0x00007fff873edcc4 -[NSControl sendAction:to:] + 86
176 com.apple.AppKit 0x00007fff873edbec __26-[NSCell _sendActionFrom:]_block_invoke + 136
177 libsystem_trace.dylib 0x00007fff9f04c3a7 _os_activity_initiate_impl + 53
178 com.apple.AppKit 0x00007fff873edb44 -[NSCell _sendActionFrom:] + 128
179 com.apple.AppKit 0x00007fff87430539 -[NSButtonCell _sendActionFrom:] + 98
180 libsystem_trace.dylib 0x00007fff9f04c3a7 _os_activity_initiate_impl + 53
181 com.apple.AppKit 0x00007fff873ec426 -[NSCell trackMouse:inRectfView:untilMouseUp:] + 2481
182 com.apple.AppKit 0x00007fff87430272 -[NSButtonCell trackMouse:inRectfView:untilMouseUp:] + 798
183 com.apple.AppKit 0x00007fff873eaddb -[NSControl mouseDown:] + 832
184 com.apple.AppKit 0x00007fff87a8524f -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 6341
185 com.apple.AppKit 0x00007fff87a81a6c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 1942
186 com.apple.AppKit 0x00007fff87a80f0a -[NSWindow(NSEventRouting) sendEvent:] + 541
187 com.apple.AppKit 0x00007fff87905681 -[NSApplication(NSEvent) sendEvent:] + 1145
188 com.cockos.reaper 0x000000010036b595 -[REAPERapp sendEvent:] + 789
189 com.apple.AppKit 0x00007fff87180427 -[NSApplication run] + 1002
190 com.apple.AppKit 0x00007fff8714ae0e NSApplicationMain + 1237
191 com.cockos.reaper 0x0000000100011b7c main + 28
192 com.cockos.reaper 0x0000000100011b44 start + 52
g-roma is offline   Reply With Quote
Old 10-11-2018, 07:54 AM   #2
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 40
Default

Hi g-roma.

Have you since found a solution to run numpy in reaper, with python 3?
(using windows here and it's crashing as described...)

Thank you.

C
Cedrik0s is offline   Reply With Quote
Old 10-11-2018, 09:12 AM   #3
g-roma
Human being with feelings
 
Join Date: May 2018
Posts: 2
Default

Hi Cedrik0s, I think I did some attempts with the subprocess module, but I did not find a suitable workaround. I guess you could try something like starting a new script, but for me if it gets too complicated it outweights the benefits...
g-roma is offline   Reply With Quote
Old 10-11-2018, 09:57 AM   #4
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 40
Default

Quote:
Originally Posted by g-roma View Post
Hi Cedrik0s, I think I did some attempts with the subprocess module, but I did not find a suitable workaround. I guess you could try something like starting a new script, but for me if it gets too complicated it outweights the benefits...
Thanks g-roma.
Subprocess? Are you referring to beyond.Reaper stuff mentioned in other threads?
And not sure what you mean by "I guess you could try something like starting a new script".
I agree that easier is better, but the benefits of using Numpy are very hard to match in another way, so some effort in the setup could be worth it.

Cheers
Cedrik0s is offline   Reply With Quote
Old 10-15-2018, 02:33 AM   #5
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 40
Default

Kind of found a solution, but it's not a general solution. It just works for the task I had to perform in my case.

From my python script parsing selected items (on which I want to perform some numpy calculations) I launch an external script that's using numpy with a call to subprocess.Popen(...)
I pass the files to process as a command line argument.

Note that the external script is initializing numpy in its own process each time you call it with Popen(...) so the problem of multiple initialization doesn't occur, and reaper won't crash.

Also, numpy initialization takes much more time (something like 1s here) than the calculation itself (in this case). So I ended up calling the external script once only, to process all N files, rather than calling it N times on a single file each time. On a large number of file this makes a huge difference of performance.

This is a workaround that's OK in my case this time, but not a general solution to the numpy incompatibility with Reaper.

Cheers.

C
Cedrik0s is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 10:37 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2018, vBulletin Solutions Inc.