Showing posts with label NumPy. Show all posts
Showing posts with label NumPy. Show all posts

Saturday, May 30, 2015

Moire with Spyre (a Python web framework)

By Vasudev Ram




The image above is of Uppsala Cathedral, Sweden, with its spires.

From the Spyre site:

Spyre is a Web Application Framework for providing a simple user interface for Python data projects. It is by Adam Hajari

I had come across Spyre recently via this post by Ian Oszvald:

Data Science Deployed – Opening Keynote for PyConSE 2015. More about Ian Oszvald here.

So I installed Spyre with the command:
pip install dataspyre
which in turn installed NumPy, pandas and matplotlib, all of which went through okay, though I saw some compiler / linker error messages scroll by as some compilation of C modules was happening (as part of the installation).

Then I tried it out a little, this time just using one of the simple applications on the Spyre site, without any modifications, like I sometimes do.
from spyre import server

import matplotlib.pyplot as plt
import numpy as np

class SimpleSineApp(server.App):
    title = "Simple Sine App"
    inputs = [{ "input_type":"text",
                "variable_name":"freq",
                "value":5,
                "action_id":"sine_wave_plot"}]

    outputs = [{"output_type":"plot",
                "output_id":"sine_wave_plot",
                "on_page_load":True }]

    def getPlot(self, params):
        f = float(params['freq'])
        x = np.arange(0,2*np.pi,np.pi/150)
        y = np.sin(f*x)
        fig = plt.figure()
        splt1 = fig.add_subplot(1,1,1)
        splt1.plot(x,y)
        return fig

app = SimpleSineApp()
app.launch()
As you can see, the structure / model of the code for a Spyre app does not seem to be very similar to that of, say, a Flask or a Bottle app. But then, there is no reason that it should be.
Anyway, I ran this app with the command:

python simple_sine_example.py

and then browsed to:

http://127.0.0.1:8080/

where I then gave different values for the input parameter freq, to see what would happen. I found that running it with certain values of freq created interesting Moire patterns in the output; hence the title of this post.

Here are a few screenshots below that show those moire patterns:

With freq = 764:


With freq = 472:


With freq = 475:


With freq = 479:


Notice that in some cases, such as with the values of 472, 475 and 479 for freq, the small changes between those values results in significant changes in the output image. Not only that: for 472 and 479, the images are similar (look sine-wave-y), but for 475 (which is between the other two values), the image looks almost like straight vertical lines. (It isn't, of course; it's still a sine wave, but more compressed along the horizontal axis.) I've not analyzed the reason for this in detail; something to do with the periodicity of the sine function, is my rough guess, but I have seen similar phenomena when drawing other kinds of computer graphics on the screen; those also involved trigonometric functions such as sine and cosine.

Spyre away :)

P.S. In other news, I was recently profiled in PyDev of the Week.

- Vasudev Ram - Online Python training and programming

Dancing Bison Enterprises

Signup to hear about new products or services that I create.

Posts about Python  Posts about xtopdf

Contact Page

Sunday, April 28, 2013

PySynth, a pure Python music synthesizer


By Vasudev Ram

PySynth is a music synthesizer library written in Python. (*)

Excerpt from the site:

[ There are three variants: PySynth A is faster, only needs Python itself, and sounds more like a cross between a flute and organ. PySynth B is more complex in sound and needs NumPy. It's supposed to be a little closer to a piano. (No competition for Pianoteq of course, but a reasonable fit for keyboard music.) Finally, PySynth S is more comparable to a guitar, banjo, or harpsichord, depending on note length and pitch.

The current release of the synthesizer is monophonic, i.e. it can only play one note at a time. (Although successive notes can overlap in PySynth B and S, but not A.) However, two output files can be mixed together as in the case of the stereo files below. ]

(*) Interestingly, the Changes section of the above linked PySynth page seems to indicate that PySynth uses both Pyglet and PyAudio, both of which I had blogged about some time ago:

Playing an MP3 with pyglet and Python is easy

PyAudio and PortAudio - like ODBC for sound

Here is PySynth on Github

PySynth supports ABC notation and can generate WAV audio files.

Here is an example tune:

D D C C B A G

and here is a PySynth program to play that tune as a WAV file:
# pysynth-ddccbag.py

import pysynth

test = ( ('d', 4), ('d', 4), ('c', 4), ('c', 4), ('b', 4), ('a', 4), ('g', 3) )
pysynth.make_wav(test, fn = "ddccbag.wav")
For some reason the last note seems to get partially cut off on my PC. Not sure whether that is a bug or a feature :-) or something to do with my hardware. Maybe the latter, since I'm using my laptop's built-in speakers.

Run that program as:
python pysynth-ddccbag.py 

Then play the generated WAV file ddccbag.wav in a music player, such as VLC or some other one.

Here are a few better examples of sound synthesis by PySynth - the links are to MP4 files, which you can download and play without needing Python or PySynth:

The Sailor’s Hornpipe — PySynth S

Bach: Jesu, Joy of Man’s Desiring — PySynth S (treble) and B (bass)

Also check my recent post: Play the piano on your computer with Python.

If you want to try it out, note that the program has a couple of bugs, related to the frequencies of notes, since I am not musically trained; I just wrote it for fun and as an experiment. Though some of the post comments gave some background and suggested corrections, they did not seem to be sure, and corrected themselves, so I have not implemented any of those suggestions yet.


Enjoy.

- Vasudev Ram - Dancing Bison Enterprises

Wednesday, April 24, 2013

Plot me some randomness


By Vasudev Ram

In this recent post about plot.ly,

plot.ly, fast plotting in the browser,

I mentioned that plotting by pasting data worked, but plotting using Python had an issue, and that they said they were fixing it.

The plot.ly people got back to me saying that the Python issue is fixed.

I tried it out again with a couple of simple plotting examples involving random numbers. They worked.

Here are the examples:

from numpy import *
from random import randint

x = range(1, 10)
y = [ random.randint(1, 10) for i in x ]
plot(x, y)

from numpy import *
from random import randint

x = range(1, 20)
y = [ random.randint(1, 10) for i in x ]
plot(x, y)

(The only difference in the above two scripts is 10 vs 20.)

You can try out that code, or your own, in plot.ly.

Go to https://plot.ly/plot.

Click on the "tab" labeled "+" near the top left of the screen, then click "Script" from the drop-down menu.

Enter your script (no need to Save) and then click Run.

If your code has no errors, your plot will appear in a new tab (not a new browser tab, but a new "tab" within the plot.ly browser tab).

The plot may take 2 to 3 seconds to appear.

plot.ly is beta software, but looks useful. They have many more complex examples in their demos.


- Vasudev Ram - Dancing Bison Enterprises


Saturday, November 10, 2012

Wakari, Scientific Python in the cloud

Introducing Wakari | Continuum Analytics

Wakari seems to be something like PythonAnywhere, which I blogged about some months ago, but Wakari has an emphasis on scientific Python. It is an MVP (Minimum Viable Product) and is in beta at present. Anyone  can sign up for it. Though the emphasis is on scientific Python, it also has support for financial uses, and can be used for general Python work as well.

The Wakari introduction linked above explains the different uses to which it can be put.

Wakari is from Continuum Analytics, a company co-founded by Travis Oliphant, a main developer of NumPy; he was also President of Enthought earlier.

Vasudev Ram
www.dancingbison.com