Programming and Poetry

I recently needed to call some synchronous code from asyncio. Thankfully, asyncio provides the run_in_executor function, which runs the specified function in a different thread. Here is an example of using it:

Recursion is awesome, but has the downside of growing the stack, which can limit its usefulness. Some languages like Scheme, however, have Tail-call optimization, which lets programmers write Tail-recursive functions that don't grow the call stack. Python does not have Tail-call optimization (TCO), but with asyncio, we can have something like Tail-call optimization. Basically, this method uses the asyncio event loop like a trampoline function.

Falling alive
Undoing sleep
I awake
At the top of a peak
About to drop straight
Down the wooden track.
And all I can do
Is go with the flow
"You can't fight gravity,
So down you will go!"
And I guess it's not bad
Since falling is life.
We're all racing down
But it makes us alive.

I had to leave -
[or was it "flee"?]
I felt entrapped by the drywall office halls
[unable to breathe]
The sanitized artwork hanging in the pale florescent haze
reminded me what art should not be -
[what life should not be]
I hate the straight lines in man-made buildings
that make everything feel so...concrete,
[so unbending]
But I love the trees with their curvy lines that say,
"reality can bend -
come bend with us."
So that's why I am here -
[to answer the trees]

This is my 2nd painting (painted 2014-01-10). I like how it came out, but almost gave up while painting it because I was comparing it to the sky God painted:

I took this photo of a beautiful "vanilla sky" outside of our home in Milwaukee, WI. It served as my model for the painting (along with other memories of looking at sunsets from beneath the spruce tree).

This year (2014) has been, for me, a year of art exploration. And to end it, I'd just like to share what I believe to be the greatest prelude ever written: the Prelude to The Picture of Dorian Gray by Oscar Wilde:

"The artist is the creator of beautiful things. To reveal art and conceal the artist is art's aim. The critic is he who can translate into another manner or a new material his impression of beautiful things.

The highest as the lowest form of criticism is a mode of autobiography. Those who find ugly meanings in beautiful things are corrupt without being charming. This is a fault.

Those who find beautiful meanings in beautiful things are the cultivated. For these there is hope. They ...

I recently finished reading Phantastes by George MacDonald for the 2nd time. The first time I read it, I really liked it. This second time, it became one of my all-time favorites. I've never experience any other work of art which feels so much like being in a dream.

To give you an idea of what it's about, here is the end of the Prelude:

"In a genuine fairy-story, everything must be miraculous, mysterious, and interrelated; everything must be alive, each in its own way. The whole of Nature must be wondrously blended with the whole world of the Spirit. In fairy-story the time of anarchy, lawlessness, freedom, the natural state of Nature makes itself felt in the world... The wolrd of the fairy-story is that ...

Intro

I first heard of Node.js about 4 years ago. I thought it was a ridiculous idea: using JavaScript as your server-side language! JavaScript always felt to me like an inferior language. Why would you use it unless you absolutely had to!? Now of course, you have to do JavaScript if you are writing code which runs in the browser, but on the server, you can use any language you choose. So why choose JavaScript?

I did choose to use JavaScript/Node.js on my current project. I have now been using it for the last several months, and I am quite impressed! Let me tell you what made me change my mind about Node.js.

When to use an SSH Tunnel?

What is an SSH Tunnel?

An SSH Tunnel is where you tell your local SSH Client to forward your local computer's traffic to a specified SSH Server (over the encrypted SSH protocol). The SSH Server will then act as a proxy for all requests you make on your local computer.

How to set up an SSH Tunnel

There are 2 steps to set up an SSH Tunnel, and they both happen on your local computer (no configuration is needed on the SSH server which will be acting as ...

Data

I cry to the night
To the one in the sky
My echoes fly
With the birds on high.
My soul is one
With the trees and the sun
For we are all sung
From the same divine lung.
And He is near
And the whisper is clear
"My love, my dear
I am with you here."

"We all must die."
"That's beautiful!", she cried.
But for now I will hold you in my embrace
Though fleeting be this time and place.
Fear not my love, there is more!
Just wait till we reach that other shore.
Beauty will become us - you and I,
And "together we will live forever" in the sky.

Author: Caleb Madrigal

Date written: 4/14/2014

Inspiration: Clint Mansell's "Together We Will Live Forever", Darren Aronofsky's "The Fountain", The Flaming Lips' "Do You Realize?". Every person you know will die. So let's not waste our life - let's live to the fullest, and hope for what is to come.

Inspiration: I've found that taking in good art makes me want to write.

Note: This isn't so much a poem as a truth put into a rhythmic format. It's a bit too "teachy" to be real poetry, in my opinion. Real poetry should not try to teach directly, but rather, should convey feelings (which may indirectly teach something).

Gray
I feel gray
Cannot stay
In this daze.
Even pain -
Let it rain!
Anything…
but this haze.
I need light
Or even night.
These gray skies
Glaze my eyes.
Stimulation!
Exhumation!
Must awaken
To elation.

Author: Caleb Madrigal

Date written: 3/17/2014

Inspiration: I was in a gray mood today, and when I looked outside and saw the gray skies, I thought, “the weather matches my mood… or does my mood match the weather?” I didn’t feel like staying in this gray mood.

Carried along
On the words of a song,
We let ourselves go
In the waves as they roll.
This ebb and flow
Entraps our souls.
Enraptured so,
As our feelings grow.
And into this torrent,
Reason forfeit,
Our hearts implore us
To join the chorus.

Author: Caleb Madrigal

Date written: 3/10/2014

Inspiration: Music is universal. We enjoy it; we cannot help ourselves. And we want to join with it.

Stay away! Stay away!
Keep away from the bay!
You know that at night,
Will in come the tide,
Sweeping out the mind
That is near the seaside.
And into this sky
You'll sink and you'll fly,
Fish floating by,
Enchanting your eyes,
No more to rise
From the deep ocean skies.

Author: Caleb Madrigal

Date written: 3/10/2014

Inspiration: Sometimes, when I need to get work done, I have to stay away from things which will get me into an artistic, or I'll be swept up in it for hours.

I think Ali Jardine was going for the same idea in this beautiful picture:

Oft on cold and starry nights,
He raises arms, and the battle fights.
And storms the castle of my mind,
Removing from the throne my logical side.
And having the ship commandeered,
Into the stormy winds he steers,
And brings us into crashing waves -
Deep oceans are within his gaze.
And rejoicing on the captain's deck,
Tiring of appointments to respect,
He does not care if we should wreck
But only in beauty does he invest.
Careless and wild - this side of mine.
Roaring through life, not glancing behind
"Behold! the sails are filled with wind!
Onward towards the moon again!"

The stars as they shine
Will begin to divide
And in the sky
The Morning’s alive
Which never will die,
And then we ride
To eternal life
And to find our time
Was not lived a lie,
And to meet His smile
And to see His face
To bask in His grace
And to in love, embrace.

Author: Caleb Madrigal

Date written: 3/9/2014

Inspiration: Riding on a bus, looking at the beautiful sky, and thinking about meeting God.

Life is beautiful
I feel it inside
But how can I express
The inner mind?
I suppose I can try
Let’s take a ride…
Heart overflowing
No need to hide
Love is a real thing
And there’s a spiritual side.
There is magic
Hidden from our eyes
Yet felt with the soul
That romantic flow.
And to this goal
I continue to stroll
Seeking that beacon
My spirit to strengthen.

Author: Caleb Madrigal

Date written: 3/9/2014

Inspiration: Looking at the sunrise in Chicago over the lake, after a night casually dressed and deep in conversation.

What's my great fear?
I'll tell you; come near.
To lay down in death
with so much left.
Passion not spent -
Oh cowardly regret!
For fear of others?
The thousand deaths.
I'm afraid to die
With no twinkle in my eye
To pass meagerly by
Yet hidden inside.
To walk through life
Not truly alive
And to leave in the night
With an unfelt "goodbye".

Author: Caleb Madrigal

Date written: 3/7/2014

Inspiration: An explanation of why living from my heart is so important to me.

In the last week or so (starting Monday, March 3, 2014) I've been in a very poetic mood. It has been so interesting - it was light a light switch flipping on. I haven't been sitting down and trying to write poetry, they've just been coming to me. Often, as soon as one ends, another begins. It has been so pleasurable. I hope people enjoy them, but even if they don't, it has been so worth it to me!

I've recently tried both Heroku and Node.js for the first time. Both are very simple, and work very nicely together. This is a quick guide to deploying a Node.js app to Heroku. To make it simple, I'll walk through deploying a Node.js sample project I created. It is a very simple message board.

Here are the steps to getting this simple app up and running on Heroku...

Deploy your app to heroku

IPython Notebook provides hook methods like _repr_html_ which can be defined by Python objects to allow richer representations. Here's an example:

In [1]:

classListTable(list):""" Overridden list class which takes a 2-dimensional list of the form [[1,2,3],[4,5,6]], and renders an HTML Table in IPython Notebook. """def_repr_html_(self):html=["<table>"]forrowinself:html.append("<tr>")forcolinrow:html.append("<td>{0}</td>".format(col))html.append("</tr>")html.append("</table>")return''.join(html)

Simple example to show how to draw lines with Matplotlib (in IPython Notebook).

ipython notebook --pylab inline

In [5]:

importmatplotlib.linesaslinesfig,ax=plt.subplots()fig.set_size_inches(6,6)# Make graph squarescatter([-0.1],[-0.1],s=0.01)# Move graph window a little left and downline1=[(0,0),(1,0)]line2=[(0,0),(0,1)]# Note that the Line2D takes a list of x values and a list of y values,# not 2 points as one might expect. So we have to convert our points# an x-list and a y-list.(line1_xs,line1_ys)=zip(*line1)(line2_xs,line2_ys)=zip(*line2)ax.add_line(Line2D(line1_xs,line1_ys,linewidth=2,color='blue'))ax.add_line(Line2D(line2_xs,line2_ys,linewidth=2,color='red'))plot()show()

Example of usefulness

Monte Carlo Estimation

Monte Carlo Estimation is a method of numerically estimating things which we don't (or can't) calculate numerically by randomly generating samples. In this IPython Notebook, I'm going to use Monte Carlo Estimation to estimate:

I recently migrated my blog from Wordpress to Pelican. Pelican is a static-site generating blog system which is written in Python and uses Jinja2 for templating. I'll probably do a post about the migration process later, but for now, I'll just give my reasons for moving to Pelican...

Data Longevity

I didn't want my blog data stored in a database; I vastly prefer it being stored in version-controlled Markdown format.

Markdown

I wanted to write blog posts in Markdown (which is possible in Wordpress, but Wordpress isn't designed to use Markdown).

I also get to use vim to write my blog posts now, which is much nicer than the Wordpress editor.

Language

Overview

This is a guide to set up IPython Notebook on a Server - specifically, on a DigitalOcean VPS. This will allow you to access your iPython Notebooks from anywhere.

Overview of Steps:

Set up a domain name

Get a VPS

Install IPython Notebook (and all dependencies)

Configure IPython Notebook to run in a server mode

Add SSL

Make IPython Notebook start automatically

Create a domain

Go to http://freedns.afraid.org and click "Setup an account here" Go through the signup form Click on the activation link they send to your email This will bring you back to their site; Click the link you see there called "Add a subdomain" Here is how I filled out the form:

Notes:

I've been doing a good bit of graphing in IPython Notebook recently, and I often wanted to make the graphs larger. I also often wanted to label the graph axes. So I wrote this simple function and have been using it a lot.

Without setup_graph()

I've recently been playing with the pandas Python data analysis framework while reading the Python for Data Analysis book (which is written by the author of the pandas library). Here are 2 of my IPython Notebooks that demonstrate some of the basics:

I'm mostly a Mac user and I also really like Linux (especially Ubuntu), but I hate Windows. However, I'm occasionally required to use Windows at work, so I've found a way to make using Windows more enjoyable: to use Ubuntu (installed on VirtualBox inside of Windows) instead of Windows for everything except for the few tasks which require Windows.

So I have made this little How to Install Ubuntu on VirtualBox in Windows setup guide:

Contents

Install VirtualBox

Download Ubuntu 12.04 ISO file

Create and configure a Virtual Machine

Tweak Virtual Machine hardware settings

Install Ubuntu in the Virtual Machine

Make Ubuntu nicer to use in VirtualBox

Using your new Virtual Ubuntu system

Install VirtualBox

I've continued on my (hopefully) short-term fascination with music+math (it's fun, but really bad for productivity). So I found this great library for generating music in Python called Pyknon. And it can be installed using pip: pip install pyknon.

I wrote a little python script just to help me understand some concepts in music theory like intervals and chords. It is meant to be read top to bottom (which is why I intersperse functions and variables throughout). It is NOT written in good modular form (in general, I don't recommend writing python like this). This code can also be used as an intro to the pyknon library.

I was able to get some basic facial detection working in OpenCV with Python. Here's what it looks like:

And here is the 34-line python script to draw boxes around all detected faces in live video:

importcvHAAR_CASCADE_PATH="/opt/local/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml"CAMERA_INDEX=0defdetect_faces(image):faces=[]detected=cv.HaarDetectObjects(image,cascade,storage,1.2,2,cv.CV_HAAR_DO_CANNY_PRUNING,(100,100))ifdetected:for(x,y,w,h),nindetected:faces.append((x,y,w,h))returnfacesif__name__=="__main__":cv.NamedWindow("Video",cv.CV_WINDOW_AUTOSIZE)capture=cv.CaptureFromCAM(CAMERA_INDEX)storage=cv.CreateMemStorage()cascade=cv.Load(HAAR_CASCADE_PATH)faces=[]i=0whileTrue:image=cv.QueryFrame(capture)# Only run the Detection algorithm every 5 frames to improve performanceifi%5==0:faces ...

In the last few weeks, I've used the [ALAssetsLibrary loadImages:callback:] method to load a list of images. For what I was doing, I wanted to wait till all the images were loaded before proceeding. I found that I could solve this problem very elegantly using a recursive solution:

When using ARC (automatic reference counting) in Objective-C, you need to be careful to avoid causing retain cycles. One place where a retain cycle can occur is where self is referenced in a block. To avoid a retain cycle, you can use the __unsafe_unretained modifier as such:

A co-worker was just showing me a situation where another developer had written a validation framework which was only used on 2 pages. I've seen this pattern again and again, and I've put a phrase to the thought:

Today, I had this XML document which I was wanting to make viewable in a browser. My plan was to run the XML document through an XSLT engine to transform it into an HTML document (which could then be opened in a browser).

Instead, I found that I could just embed the XSLT directly in the XML document. Then, when this file is opened in a browser, the XSLT executes and spits out HTML, which is then rendered into the browser window. This is great because I now have the original XML file (which we wanted to keep further analysis purposes) which can also be rendered nicely in a browser.

I've been taking the Udacity CS373 (Robotic Car) class, and this week the topic was Particle filters. Particle filters are basically a localization algorithm that accounts for error in measurements and sensors.

Anyway, part of the Particle Filter algorithm requires the generation of a new set of these things called "particles" based on the particles' weights. So to accomplish this task, the Resample Wheel algorithm was presented in class. It is a particularly elegant method of generating a new set of particles by randomly drawing from an old set of particles (with replacement). The particle weight determines the likelihood of it being picked.

Here is the algorithm (with print statements - I used these to help me understand how the algorithm works):

I was recently wanting to enable a webpage to utilize some server-side functionality which was on a different server than the one serving the original page. Traditionally, this has been problematic due to browsers' Same origin policy. JSONP is a solution to this problem. Here is how to do it with jQuery...

This webpage can be saved locally to any machine, opened in a browser, and can then load data from my web server:

Since I've been getting into functional programming more recently, the fact that Python doesn't do tail-call optimization has been bothering me. So I did a bit of searching, and found this gem: Tail Call Optimization Decorator.

Here is a snippet from it:

importsysclassTailRecurseException:def__init__(self,args,kwargs):self.args=argsself.kwargs=kwargsdeftail_call_optimized(g):""" This function decorates a function with tail call optimization. It does this by throwing an exception if it is it's own grandparent, and catching such exceptions to fake the tail call optimization. This function fails if the decorated function recurses in a non-tail context. """deffunc(*args,**kwargs):f=sys._getframe()iff.f_backandf.f_back.f_back \
andf.f_back.f_back.f_code==f.f_code ...

I just finished going through the process of installing and configuring Django (with FastCGI) on Hostmonster's Shared hosting. It was more painful than I expected, so I decided write a post about how I got it working...

First, I wanted to create a subdomain which would host my django stuff. In order to do this, I created a subdomain using the cPanel (the item is called "Subdomains"). This created the directory and a basic .htaccess file (along with a few error pages).

Today, I needed to convert an ASCII-encoded NSString to a base64 NSString. I found a number of methods that convert from an NSString to a Base64 NSData, or from NSData to a Base64 string, but couldn't find one that did exactly what I needed. So here is a method that simply converts an NSString to a Base64 NSString:

In the process of learning haskell, I've been trying to become comfortable with the function applicator ($) and function composition (.). Here is some code to show how they relate (and how they relate to using parenthesis to enforce order of operations):

Today, I was faced with an issue several times where I had an array of items of one type, and had to convert this into an array of items of a different type. This made me really want a map() function. So I added one to NSArray like so:

Just started looking into the Haskell programming language by going through the first few chapters of the awesome book: Learn You a Haskell which is free to read online. Here are some things I really like about what I have seen of Haskell so far:

Type inference allows you to have static typing but not all the verbosity of most other statically-typed languages.

Lazy evaluation is huge in Haskell, and it lets you do things like this: let factorial n = product (take n [1..]) So here, we have an infinite list ([1..] creates a infinite list from 1 to infinity!) which from which the first n items are taken and passed to the product function. Awesomeness...

For running most commands, powershell and CMD work the same. One big difference I've found is that, when you use periods (.) in a command, they must be surrounded by quotation marks. For instance, this won't work: