<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/transfer-data-from-r-to-python-with-pyrserve-and-bio7/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://bio7.org/transfer-data-from-r-to-python-with-pyrserve-and-bio7/"> R – Bio7 Website</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>29.08.2018</strong></p>
<p>Recently I discovered the package <a rel="nofollow" href="https://pypi.org/project/pyRserve/" target="_blank">PyRserve</a> for Python which connects Python with R using Rserve.</p>
<p>This is extremly useful because <a rel="nofollow" href="https://bio7.org/" target="_blank">Bio7</a> already integrates <a rel="nofollow" href="https://github.com/s-u/Rserve" target="_blank">Rserve</a> and has special GUI interfaces available to transfer, e.g.,  data from spreadsheets, ImageJ image and selection data (also georeferenced),  Java simulation data, etc.</p>
<p>With this new Rserve connection this data can now easily transferred from Java and R to a Python workflow.</p>
<p>Python as a language is already supported in Bio7 with an integration of <a rel="nofollow" href="https://en.wikipedia.org/wiki/Jython" target="_blank">Jython</a> (which runs on the Java platform a supported<a rel="nofollow" href="https://en.wikipedia.org/wiki/List_of_JVM_languages" target="_blank"> JVM language</a> like <a rel="nofollow" href="http://www.groovy-lang.org/" target="_blank">Groovy</a>, <a rel="nofollow" href="http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html" target="_blank">JavaScript</a>, <a rel="nofollow" href="https://scala-lang.org/" target="_blank">Scala</a>, etc. and has a direct access to the Bio7 API).<br>
In additon native Python can be executed with the default Bio7 Python editor if configured in the native preferences of Bio7 (running as an external process in a Bio7 Python shell if enabled).</p>
<p>However since Bio7 is based on Eclipse a more powerful editor for Python/Jython can be installed with <a rel="nofollow" href="https://marketplace.eclipse.org/content/pydev-python-ide-eclipse" target="_blank">PyDev</a> which is a complete Python IDE for Eclipse and available as an Eclipse plugin which can be installed with the Bio7 (Eclipse) Update Manager</p>
<p>Here a video <a rel="nofollow" href="https://youtu.be/-kySAN9doXQ" target="_blank">how PyDev can be installed in Bio7</a>:</p>
<div allowfullscreen src="https://www.youtube-nocookie.com/embed/-kySAN9doXQ?feature=oembed" data-embed-frame="&lt;iframe width=">&quot; data-embed-play=&quot;<span>Play Video</span><div><span>This video will be embedded from Youtube. The <a rel="nofollow" href="https://www.google.com/intl/en/policies/privacy/" target="_blank"> apply.&quot;>
<div><span>Please activate JavaScript to view this video.<br>Video-Link: <a rel="nofollow" href="https://youtu.be/-kySAN9doXQ" target="_blank">https://youtu.be/-kySAN9doXQ</a></span></div>

<p><strong>Update Site PyDev:</strong> http://www.pydev.org/updates/</p>
<p>If PyDev is installed you can create Python projects in Bio7 and execute Python and Jython scripts depending on the settings in it’s own process (e.g. with the action editor context menu-> Run As->Python Run) .</p>
<p>In addition it is possible to use a special Bio7 toolbar action (visible if the PyDev editor or Bio7 Python editor is opened) to interpret the content of the PyDev editor in the current Bio7 Jython or Python process (which is of no importance for the following use of PyRserve!).</p>
<p>PyRserve can be installed easily using the <a rel="nofollow" href="https://pip.pypa.io/en/stable/" target="_blank">pip</a> command in the python shell:</p>
<p><strong>pip install PyRserve</strong></p>
<p>or by using PyDev with the <a rel="nofollow" href="http://www.pydev.org/manual_101_interpreter.html" target="_blank">preferences dialog</a> (see screenshot below).</p>
<p><img sizes="(max-width: 981px) 100vw, 981px" src="https://i0.wp.com/bio7.org/wp-content/uploads/2018/08/pydevpip.png?w=450&ssl=1" alt="" class="wp-image-3384" data-recalc-dims="1"></p>
<p>After the installation we can start RServe from within R as usual ,e.g., with:</p>
<p><strong>library(Rserve)</strong><br>
<strong>run.Rserve()</strong></p>
<p>To connect Python to Rserve the following commands are suffcient:</p>
<p><strong>import pyRserve </strong><br>
<strong>conn = pyRserve.connect()</strong></p>
<p>More examples can be found in the <a rel="nofollow" href="https://pythonhosted.org/pyRserve/manual.html" target="_blank">PyRserve documentation</a>.</p>
<p>However since the Bio7 functionality can be easily extended with scripts (which can be added dynamically to a menu copying them to a special scripts location, see screenshot below) I decided to write two short Groovy scripts to start and stop Rserve not controlled by the Bio7 Java connection interface (and if necessary disconnect R from a current Bio7 controlled Java connection).</p>
<p><img sizes="(max-width: 424px) 100vw, 424px" src="https://i1.wp.com/bio7.org/wp-content/uploads/2018/08/pyrservestartup.png?resize=424%2C427&ssl=1" alt="" width="424" class="wp-image-3387" data-recalc-dims="1" height="427"></p>
<p>The ‘Start_PyRserve’ script starts R and Rserve if not alive in the Bio7 R console and also disconnects from a running Java Rserve connection if activated.</p>
<p>Note that in Bio7 only one connection to Rserve is allowed (Rserve cooperative mode).</p>
<p>The ‘Stop_PyRserve’ script reconnects Python (closed in the Python scripts!) to shutdown Rserve and returns to the Bio7 console and R (Pydev has it’s own console).</p>
<p>This shutdown command is excecuted by an external Python process. To get the path for the Python interpreter we also have to set the Bio7 Python path (not PyDev!) in the Bio7 preferences (see Windows screenshot below – normally not necessary on Linux or when the Python interpreter is on the OS PATH!).</p>
<p><img sizes="(max-width: 886px) 100vw, 886px" src="https://i0.wp.com/bio7.org/wp-content/uploads/2018/08/pythonpref.png?w=450&ssl=1" alt="" class="wp-image-3388" data-recalc-dims="1"></p>
<p>With this two scripts it is now (more secure) possible to start Rserve and use the Bio7 Java interface, e.g., to transfer image data to R (ImageJ to R), start the Python connection (which disconnects Java) and use the image data inside a Python workflow (see the second example).</p>
<p>Here a <a rel="nofollow" href="https://sourceforge.net/projects/bio7/files/bio7/scripts/" target="_blank">first example script</a> to transfer random numbers (in PyDev use the context menu action ‘Runs As->Python Run’ of the editor to execute the Python script) with a PyRserve connection.</p>
<pre><strong>import pyRserve</strong>

<strong>conn = pyRserve.connect()</strong>
<strong>result = conn.eval(&quot;result&lt;-runif(100)&quot;)</strong>
<strong>conn.close()</strong>
<strong>print (result)</strong></pre>
<p>Here <a rel="nofollow" href="https://sourceforge.net/projects/bio7/files/bio7/scripts/" target="_blank">another example script</a> to plot a transferred image matrix in Python with <a rel="nofollow" href="https://matplotlib.org/" target="_blank">matplotlib</a> (8-bit, grayscale as double matrix) if available in the R workspace: (<a rel="nofollow" href="https://www.youtube.com/watch?v=CV0v_xL-dxU" target="_blank">see this old video how to do this with Bio7</a>):</p>
<pre><strong>import pyRserve </strong>
<strong>import matplotlib.pylab as plt</strong>

<strong>conn = pyRserve.connect()</strong>
<strong>matrix = conn.eval(&quot;tryCatch({matrix&lt;-imageMatrix}, error = function(e) {return ('NULL')})&quot;)</strong>
<strong>conn.close()</strong>
<strong>if matrix != 'NULL':</strong>
<strong>    # we have to transpose the image for a correct view!</strong>
<strong>    matrix = matrix.T</strong>
<strong>    plt.imshow(matrix)</strong>
<strong>    plt.colorbar()</strong>
<strong>    plt.show()</strong></pre>
<p>Here a summary of the installation and configuration steps necessary for Bio7 to use PyRserve and execute Python scripts with the powerful PyDev editor:</p>
<ol>
<li>Install the PyDev editor and configure the Python path</li>
<li>Install PyRserve (pip install PyRserve)</li>
<li>Configure the Python path of Bio7 (Preferences Bio7->Preferences Native)</li>
<li>Install the Groovy Start/Stop scripts for PyRserve (simply unzip <span><a rel="nofollow" href="https://sourceforge.net/projects/bio7/files/bio7/scripts/" target="_blank">Examples_PyRserve.zip </a></span>in one scripts location – Open ‘Scripts->General->Open_Script_Location’ from the main Bio7 menu or in the context menu of the R-Shell view open General->Open_Script_Location’. Put them in an extra folder for a submenu!</li>
<li>Start the ‘Start_PyRserve’ script to start Rserve (not Bio7 controlled!)</li>
<li>Execute a Python script which connects and disconnects PyRserve</li>
<li>Stop the server and shutdown Rserve with the ‘Stop_PyRserve’ script</li>
</ol>
<p><strong><a rel="nofollow" href="https://sourceforge.net/projects/bio7/files/bio7/scripts/" target="_blank">Download Start/Stop PyRserve scripts here</a><br>
</strong></p>
<p> </p>
<p> </p>
<p> </p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://bio7.org/transfer-data-from-r-to-python-with-pyrserve-and-bio7/"> R – Bio7 Website</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/Xm1W4z6fg_s" height="1"></a></div></a></span></div></div>