SciRuby

Tools for Scientific Computing in Ruby

The Nyaplot Internal Event Handler

Nyaplot multi-pane example

Introduction

Last week I implemented the multiple panes interactivity to the front-end of Nyaplot. You can see the demo on this notebook.

Skip over single pane demo, and have a look at the paragraph ‘Multiple panes demo’ at the bottom. In this example, the histogram and bar chart are created individually, and then thrown into one instance of Nyaplot::Frame. After running frame.show, both of the diagrams appear in one out-put box on the notebook. When you click the histogram, bar chart refrects that and change its height.

In this article I will explain about the event handler of Nyaplot, which allows for interactivity among panes.

The Trick

The trick is actually hidden in Nyaplot::DataFrame. In this example, both of the two diagrams are generated from columns in one DataFrame. When Nyaplot receives a column object (instance of Nyaplot::Series, defined here), it internally find its source data frame and remembers its location.

Consequently, Nyaplot can find that histogram and bar chart are sharing their data source, and allows them to interact with each other.

Nyaplot::DataFrame Example

Implementation

Before explaining the event handler, I want to outline Nyaplot’s diagram rendering procedure. In the back-end JavaScript library, diagrams, such as histograms, fetch column data from data frames and generate their SVG DOM nodes dynamically (source code).

The key is a data frame written in JavaScript. After mouse events coming from a filter (such as the gray box on the plot area) are handled, the histogram registers its filter function to the data frame and instructs all diagrams to update their SVG DOM objects. The bar chart updates its SVG from data filtered by the new function.

As you can see, Nyaplot’s inner workings are fairly simple; and these workings are common to all diagrams supported by Nyaplot. So not only the histogram and bar chart, but other diagrams as well, can behave interactively (e.g., histogram, bar, and Venn example).

Conclusion

Despite, or perhaps because of, the relative simplicity of the structure, the event handler functions well. The actual interactivity in Nyaplot is based on the data frame object, both in the Ruby front-end and the JavaScript back-end. As a result, handling events in DataFrame is both natural and efficient for Nyaplot.

Additional Information

Nyaplot and its back-end library Nyaplotjs are being developed on GitHub.

If you are interesed, feel free to try it and raise issues or send pull-requests.

Usage: Integration Gem Improvements

Numerical Integration

Integration is the process of finding the summation of the value of a function at small intervals, when the width of the intervals is infinitesimally small. As there is no such thing as “infinitesimally small interval”, there are several other methods which help us to approximate the values of integrals.

Numerical integration is one such technique which helps us to estimate the value of the definite integral of a function over an interval. Numerical integration is based on the principle of evaluating the values of a function at specific points in the interval and then taking the product of those values with a corresponding weight, which is a standard constant specific to the method being used.

Integration Gem

The Integration gem is now equipped with a host of additional algorithms for approximating the integral of a function ranging from the simple ones like Simpson’s method to the more complex ones like Gauss–Kronrod and Adaptive Quadrature method.

Example

Let us say you need to find out the integral of a function 5*x**2 -7*Math.cos(x) over the interval (0,1). We can solve this using the following snippet using the Integration gem:

1
2
Integration.integrate(0,1,{:method=>:simpson}) {|x| 5*x**2 -7*Math.cos(x)}
# => -4.223630227110514

We see that the actual value of this integral is -4.2236302269886088799000849584, which is pretty close to the value estimated by the integration gem. Instead of {:method=>:simpson} in the above code, you can use any one of these implemented methods:

1
rectangle,:trapezoid,:simpson, :adaptive_quadrature , :gauss, :romberg, :monte_carlo, :gauss_kronrod, :simpson3by8, :boole, :open_trapezoid, :milne

Each of these algorithms gives similar results — except if the function has rapid changes in the interval of integration, as is the nature of numerical integration.

The Progress of the GSoC D3/Visualization Project

This week is the half-way point of this year’s Google Summer of Code. Let me report the progress during the term.

Example Nyaplot chart

Progress on the front-end library

I am developing Nyaplot as the front-end library for my work.

One topic on Nyaplot is API design. That is a difficult problem since each plotting library has very different methods for creating plots. For example, matplotlib and Bokeh are both written in Python, but their demo code is written in very different styles. After discussing with my mentor, I decided to implement the function-based API.

As an exmaple of that API, here is a minimal working example for generating bar charts with Nyaplot:

1
2
3
4
require 'nyaplot'
plot = Nyaplot::Plot.new
plot.add(:bar, ['nya1', 'nya2', 'nya3'],[10,20,30])
plot.show

If you want to change options (e.g., color or title), put the return value of Nyaplot::Plot.add into a variable and call its methods.

1
2
bar = plot.add(:bar, ['nya1', 'nya2', 'nya3'],[10,20,30])
bar.title("the number of cats") # name the bar chart

IRuby integration

Interaction with IRuby is a priority for Nyaplot. IRuby is a web-based Ruby lab notebook-like environment, based on IPython, which is also useful for fast prototyping.

The image on top of this blog post is a hyperbolic spiral which I generated with Nyaplot in IRuby. Have a look at the notebook on nbviewer, an IRuby and IPython notebook hosting services.

Progress on the back-end library

Nyaplot uses Nyaplotjs as its back-end library. I spent a lot of time to implement interactivity among multiple panes to Nyaplotjs. See an example.

alt text

The input data source is a TSV file of 2,044 lines. Multiple-pane interactivity is especially important when visualizing such a large dataset.

Have a look at the radio buttons beside the Venn diagram. The left three panels decide which set to put into each of 3 circles (VENN1, VENN2, VENN3). The right panel decides which data in each area (overlapping area, non-overlapping area, and all) to use in the other two panes.

The gray box on the histogram decides the range of PNR values. If you select the range 0.3 to 0.7 with it, the right bar chart will reflect that and show how many of the selected data are in that range.

Nyaplotjs provides this interactivity using an event handler connected to a dataframe object. That allows us to handle unidirectional update method (e.g., histogram updates bar chart, but bar chart does not update histogram).

Conclusion

I finished the first half term of Google Summer of Code 2014, but I still have a lot of things to do. I would like to continue to add interactivity to both the front-end and back-end libraries.

I am developing those two libraries in separate repositories on GitHub. If you are interested, feel free to raise issues or send pull-requests, even during the GSoC term.

Updates: Minimization and Integration

Minimization

The Minimization gem now supports the following unidimensional function minimizations provided by GSL. The supported methods include the pure Ruby implementations of:

  1. Newton–Raphson
  2. Golden Section
  3. Brent
  4. Quad Golden

Of these, the Golden Section, Brent, and Quad Golden are also available via Minimization’s GSL interface (and are thus faster). Everything is organized in such a way that the faster C code (i.e., GSL) will be executed when GSL is available, but that otherwise the Ruby implementation will be used. I still have to beautify the code and add documentation.

Integration

The Integration gem has been transitioned from Hoe to Bundler. For Gauss–Kronrod Quadrature, I have hard-coded the values of nodes and weights (for 15, 21, 31, 41, and 61 points) — which were already hardcoded in the case of the Gauss quadrature.

Additionally, I added basic methods like Simpson’s Three-Eighths Method, Milne’s Method, Boole’s Quadrature and Open Trapezoid.

This week, I will be reviewing a pull request which aims to change the structure of the whole Integration gem.

After that I plan to implement more adaptive methods and incorporate the non-adaptive methods under a single Newton–Cotes function.

Lastly, I am brainstorming designs for symbolic integration using JScience and JRuby.

Progress on Minimization Methods

Current Progress on Minimization Gem

In the first half of the summer, I plan to introduce some new numerical minimization methods to SciRuby’s Minimization gem. As per my proposal, I began by implementing the Powell’s multidimensional minimization method. Powell’s method has a better convergence in most cases than the Nelder–Mead algorithm, and is also a multidimensional minimization method which doesn’t use any derivative of the function.

I started by studying SciPy and Apache Commons library’s Powell’s optimizer. I decided to base my implementation on the method from the Apache Commons Mathematics Library. Powell’s method requires a line minimum searching algorithm, for which I used Brent minimizer (already available in SciRuby).

Having finished with Powell’s method, I am now working on the Fletcher–Reeves minimization method — a gradient method which uses the first derivative of the integrating function.

Introduction: Minimization and Integration (Lahiru)

Editor’s Note: We have two students working on numerical minimization and integration this summer, Rajat and Lahiru. Rajat’s introductory post appeared two weeks ago.

Introduction

I’m Lahiru Lasandun and I’m an undergraduate of University of Moratuwa, Sri Lanka. I’ve been selected for Google Summer of Code 2014 for SciRuby’s Minimization and Integration projects.

I was working with SciRuby about a month before GSOC started and did some tests on how to enhance the performance of these numerical computations. My first idea was to use multi-threading. With the instuctions and guidance of mentors, I tested more methods such as Erlang multi-processing, the AKKA package of multi-threading, and finally OpenCL. The final decision was to use OpenCL to enhance computation power of these mathematical computations with the support of multi-cores and GPUs.

Minimization Gem

After GSOC started, I began working on SciRuby’s Minimization gem. I proposed multidimensional minimization methods for the Minimization gem, which already had plenty of unidimensional minimization methods. I chose two non-gradient and two gradient minimization methods as well as simulated annealing.

Integration Gem

For Integration, I proposed to replicate some unidimensional integration methods from the GNU Scientific Library, GSL. Additionally, I proposed to add OpenCL support to enhance performance of integration methods.

Current Progress

Currently, I am working on Nelder–Mead multidimensional minimization method which is a non-gradient method, including working on the relevant test cases.

Introduction to the Minimization and Integration Project (Rajat)

Editor’s Note: We have two students working on numerical minimization and integration this summer, Rajat and Lahiru. Lahiru will be writing a separate post about his work.

Introduction to the Minimization and Integration Project

Hi. My name is Rajat Kapoor and I have been selected to work with SciRuby for Google Summer of Code 2014.

Minimization and Integration are two of the many available gems in the SciRuby suite. My project this year aims to improve these gems to replicate the functionality provided by GNU’s GSL. I will be trying to implement all the minimization and integration algorithms present in GSL in pure Ruby, with improvements as needed, so that these functions are easily accesible to all Ruby users, while the users which have GSL already installed will have an advantage in terms of speedy computations.

What Minimization and Integration actually mean

Minimization refers to the process of finding out the minimum of a mathematical function whose values might depend on multiple variables. Unidimensional minimization restricts these problems to functions of one single variable. Integration is the same as the very widely used concept in calculus which basically boils down to finding the summation of the value of a function at small intervals, when the width of the intervals in infinitesimally small. I can bet that you knowingly or unknowingly use both these things on a daily basis.

The plan

The project can be broken into two major parts: Minimization and Integration, as these are two seperate gems.

The Minimization gem can be broken in two parts: unidimensional (or univariate) and multidimensional (multivariate). With respect to coding, these two can again be broken down into sub-parts: pure Ruby implementations and GSL support. The Integration gem will include the pure Ruby implementations as well as GSL support. Along with this, some support for symbolic integration will be added for JRuby users by way of the JScience library.

Progress

The pure Ruby implementations of the unidimensional minimization part are almost finished. I am also working on the GSL support for the same along with it. I plan to finish up any unidimensional minimization work by the end of this week and start the work with multidimensional minimization methods.

Keep watching this blog for more updates regarding my project.

Introducing the FFTW SciRuby GSoC Project

My name is Magdalen Berns and I am a physics student with a technical background in live audio. I am particularly interested in using science and technology to improve access for all.

This summer, I will be working on implementing the external library appropriately named “Fastest Fourier Transform in the West” version 3 (FFTW3) C and Fortran API in Ruby for this year’s Google Summer of Code (GSoC).

The primary aim of the project is to give SciRuby the capability to handle signal analysis, processing and synthesis by performing discrete fast Fourier transform operations on NMatrix objects.

After some investigation during the preparation stages of GSoC, it was determined that implementing FFTW3 is more desirable than starting from scratch in pure Ruby because the FFTW3 API is already extensively used, developed, and optimised far beyond what would be achievable in just three months. So, putting FFTW3 in the driving seat allows the SciRuby project to take advantage of the good work of the FFTW3 developers by bringing it to Ruby.

Putting NMatrix to the test with FFTW3 should give users the opportunity to test drive NMatrix — and SciRuby’s NMatrix developers a chance to root out bugs.

Since a gem called ruby-fftw3 already existed to perform FFTW3 operations on NArray objects, I forked that repository as a starting point. Things are progressing on my Github fork right now.

My mentor for this project is Colin Fuller who is an exceptionally talented programmer — and he really knows his git too. He has been a great help as I adapt to the learning curve of working in C and Ruby (languages which I am less familiar with than say, Java or JavaScript).

As I work, I intend to share useful gems of information I gather. Those, in addition to my weekly project updates, will appear right here in this blog so others can hopefully benefit.

I have already posted a few useful bits and bobs on thismagpie.com which relate to my work so far. I hope to add those to the SciRuby blog, too, provided the readers are interested in that and time permits. Of course, readers here can feel free to have a browse of the keywords sciruby, ruby and git on there for the time being. I sometimes add posts, manuals and tutorials from external sites where I find useful ones on the web too, so watch out for these too.

Please, feel free to watch or follow along as the project comes together and those inclined are welcome to share constructive comments and advice or raise bugs on the fftw3 issue tracker. Input about my work is very welcome as the project progresses. This gem is being written for the community, after all!

You can find me on Twitter (Facebook) or GitHub under the username @thisMagpie.

Introducing the GSoC 2014 D3 Project

Hello. I am Naoki, one of four Google Summer of Code (GSoC) 2014 students in SciRuby. Let me introduce my project. The goal of the GSoC 2014 D3 Project is to create a new plotting library for SciRuby. D3.js is the most suitable JavaScript library to achieve this goal.

There are several non-Ruby plotting software libraries in the wild, like ggplot, matplotlib, and ggplot2. Actually, SciRuby already has its own plotting libraries named Plotrb and Rubyvis. The main feature of my project compared with those software packages is interactivity. Interactivity has various meanings here: interactivity when generating plots, interactivity when viewing them, and server–client interactivity. My project includes all of those.

My project can be divided into two components, one JavaScript and the other Ruby. JavaScript serves as a back-end, and Ruby as a front-end. I’m currently working on the former part. Have a look at a few examples I’ve assembled:

This project involves a number of challenges, but I believe it to be achievable during this Summer of Code. Thank you for reading!

Ruby Science Foundation Selected for GSoC 2014

We’re excited to announce that the Ruby Science Foundation has been selected as a mentoring organization for Google Summer of Code 2014!

Last year was our first year as a mentoring organization, and we had a great group of students working with us on machine learning, timeseries statistics, the semantic web, and scientific plotting.

This year we’ve got a super set of possible projects including more flexible matrix computations, automatic Ruby interface generation for scientific libraries, a dataframe library for structuring and manipulating datasets, interactive plotting, a scientific notebook, high-performance minimization and integration libraries, and a semantic web datastore backend for scientific computing.

If you’re interested in applying as a student, learning more, or even contributing independent of GSoC, head over to our GSoC 2014 ideas page to see what projects we think are great. Don’t hesitate to tell us if you’ve got an amazing idea for a different project, too! If you’re still left wondering where to start, check out the issue tracker for NMatrix, the matrix computation library used as the basis for a number of our projects, and our top priority at the moment.

Good luck to all the GSoC applicants out there, and happy coding!