Thursday, June 4, 2015

Spaceship Earth - part2


At the moment, mankind is quickly destroying the Earth's natural environment and each other. Spaceship Earth helps us see the Earth as a permanent home, rather than something we are simply going to ditch in a couple billion years when the sun becomes a hostile red giant star. We need to start working together now to clean up the environment, and make sure all races and cultures can first live in peace, and then voluntarily cooperate to create this mega structure. No other species can save the planet, so we better step up and get to work now on a real peace plan.


Spaceship Earth Near Light Speed

After living in peace and harmony for millions of years, humanity and the entire earth reach relativistic speeds.


Blender Scene

Created with Blender 2.74

Monday, April 13, 2015

4D Navigation


This weekend I teamed up with Steve Favis on a nearly nonstop 30 hour hackathon for NASA SpaceApps 2015. We developed a navigation system that takes into account the physics of possible future events and jumping to reach its target goal. Checkout Steve's twitter for more info: @stevefavis

Thursday, February 20, 2014

Fishing in a River of Trash - part1


I had posted earlier this month about the garbage polluting the river that flows into Tarlac City here. 30Km downstream in the center of Tarlac City, things get much worse.

Passing through the city I often see people of all ages fishing in the river, despite its extreme level of pollution. Today I saw four young boys with a battery and two long prods shocking fish to catch them. They were also collecting coconuts from the river.

Monday, February 3, 2014

San Jose - Tarlac Province


San Jose, Tarlac Province, has a network of cool streams flowing from the western mountains, it one of the few places in the Philippines that it is still safe to drink water directly from the river. Many small villages can be found near and along the water ways.

Until recently the people in San Jose had lived in near perfect harmony with their environment; sadly things have recently changed. Along my short 3km hike to the river, I estimate I encountered about twenty small dumping sites composed mainly of baby diapers. All of these sites were relatively new. Each site had about 20-100 diapers.

above: baby diapers torn apart and scattered by wild dogs.

Garbage Dynamics

Garbage accumulates at different rates depending on what type it is and where it was dumped. Often it starts out as a few pieces of discarded plastic, once a place no longer is pristine, people are more likely to litter more. Once some threshold has been reached, someone will dump the first big bag of trash, this quickly leads others to dump more in the same area. There is a powerful feedback effect.

It is routine in the Philippines to burn trash in remote areas, this helps restore the areas to a relatively clean state, and control the feedback effect. Diapers are hard to burn compared to most other trash, so they tend to build up and encourage the growth of dumping sites. The Pinoy style for trash burning is simply to throw some grass on top and set it ablaze. This technique fails with diapers.

How To Safely Burn Plastics

Grass and wood should be placed under the plastic. A ratio of at least 50/50 wood/plastic is the minimum. Less harmful dioxins are released when the fire is hot, so the more dry wood the better.

Anything thing with chlorine (like paper products) when burned with plastics creates more dioxins, so burn them separately. Avoid burning black and dark colored plastics. Obviously, try to recycle as much of the plastic as possible, instead of burning it.

Thursday, January 23, 2014

Spaceship Earth


A new computer model by Eric Wolf, estimates the earth will be destroyed by our sun in 1.5 billion years. [1]

It is a popular idea in science and science fiction that we will one day escape destruction on earth by colonizing other planets. In these scenarios the polluted and spent earth is left behind. But, will there be enough space ships to save us all? What about all the plants and animals we would leave behind? This is a bad way of thinking about the future that can have a negative impact on how we think about the present. We more readily accept pollution and things that harm the planet, when we think that in the distant future, we are going to ditch the earth anyways.

Instead we should plan to save the earth by turning it into a giant space ship, so we can safely move the earth to a higher orbit as the sun becomes a red giant star. Is it too soon to worry about these things? It might take millions of years to safely move the earth to this higher orbit, and transforming the earth into a spaceship may itself take millions of years as well, so we better start now to be sure we have enough time.

Much of the earth as we know it will be lost in this radical transformation into spaceship. In order to preserve natural habitats and places for cities of the future, we will need to greatly increase the total surface area, control the weather, and globally route the flow of fresh water. I have created this animation in Blender to try to capture some of these ideas. I imagine the disk structure as a place for the new cities, where people can enjoy flying around in the lower gravity. The original earth surface enclosed in a protective biosphere, can be maintained for wild animals and returned to its natural state. I placed the rocket booster at the south pole, I imagine we would have to drill to the earths core and reinforce the entire earth through its center from both poles.

Thursday, January 16, 2014

Tarlac City vs Ace Manalang


No one here can escape the foul smell of burning plastic. Last month I ran into the Mayor of Tarlac City ("Ace" Manalang) and confronted him about the widespread dumping of plastic and other waste burning all around the city. He tried to dodge my questions, and kept repeating that: "nothing can be done to change the people".

Is it time to give up on clean air and water? I took to the streets to see what people think, and the results surprised me. I spoke with many different people of all ages, and there was a clear pattern in every instance. Women are aware and concerned about the situation, and actively clean and recycle plastics. Children under the age of ten enjoyed picking up plastics, it is seen as a game that can make a little money.

Most of the men I spoke with were either drinking beer or gin. They were unaware and not concerned by problems created by burning plastics. When asked to help clean up the community along side the women and children, all quickly refused. This is what surprised me, they clearly had nothing better to do, yet refused to help. What was holding them back? These drunken men were ironically concerned about their public image and status within the community. If they were seen cleaning trash they could be labeled a "scavenger".

Monday, November 25, 2013

Pystone - PythonJS vs PyPy


The following Pystone benchmarks were performed with the default 50,000 iterations, the results are the average of five runs. PythonJS is tested in four different modes with greater levels of optimizations. Test machine is: dual core 2.4GHZ, 4GB RAM, LinuxMint with Google-Chrome v31.

PythonJS vs Brython and Empythoned

Above: Brython, Empythoned, PythonJS in default mode, and PythonJS with inlined optimization. PythonJS outperforms other browser based Python's: Empythoned (3,320 pystones) and Brython (425 pystones). PythonJS in default mode gets 8,150 pystones per-second. See Brython's JavaScript output here

PythonJS vs PyPy

Python2.7 reaches an average of 112,257 Pystones per-second. Pythonium (another Python to JavaScript translator) in its fastest veloce mode reaches an average of pystones 291,480 per-second. PythonJS in javascript mode, reaches an average of 850,120 Pystones per-second. PyPy-1.9 is the winner with an average score of 1,813,461 Pystones per-second.


Brython PythonJS Integration

I am also working on integrating Brython's tokenizer and parser into PythonJS so that PythonJS can be fully self hosted. The file in Brython's source code is py2js.js. It transforms Python code into its own abstract syntax tree that is very similar to the standard Python AST. I am making code to walk this tree and convert it into a standard AST that is required by the translator in PythonJS. See these files, and contact me if you would like to help.

Sunday, November 10, 2013

Brython vs PythonJS


If you have not heard of Brython, it is one of the most popular in-browser Python implementations. From the top of Brython's documentation: "Brython's goal is to replace Javascript with Python, as the scripting language for web browsers." Brython is a long way from reaching this goal, because as we will see below, it has serious performance issues. Brython is slow because of its conformant implementation of the Python standard, which is very dynamic and is generally hard to map to JavaScript.

The following tests compare the performance of Brython 1.1, Python 2.7, PyPy 1.9 and PythonJS 0.8.3. Note that PyPy tests were done with a warmed up JIT. These tests were performed with N=100,000 on a dual core 2.4GHZ machine with 4GB of ram. (test scores below are given in seconds)

Test1 - for loop:

 a = 0
 for x in range( N ):
  a += 1
  • Brython: 6.8249
  • Python: 0.0064
  • PyPy: 0.00267
  • PythonJS: 0.0009

In a simple for loop over a range, with a single variable incremented: Brython is 1,000 times slower than Python, and 7,500 times slower the PythonJS. Lets examine the output of Brython to see why it is so slow.

Brython Output

a=$globals["a"]=Number(0)
var $iter61=iter( range.__call__(N) )
var $no_break61=true;
while(true){
    try{
        x=$globals["x"]=$iter61.__next__()
    }
    catch($err){
      if($err.__name__=="StopIteration"){break}
      else{throw($err)}
    }
    document.$line_info=[21,"__main__"];None;
    $temp=Number(1)
    if(!hasattr(a,"__iadd__")){
        a=$globals["a"]=a.__add__($temp)
    }
    else{
        a=a.__iadd__($temp)
    }
}

As we can see above, Brython generates alot of code for such a simple for loop. Instead of using JavaScript's native number type, Brython has its own Number object. It has a try/catch to catch the StopIteration exception, and also it testing each loop if the number has a method __iadd__, and if not it falls back to using __add__. Lets take a look at how PythonJS translates the same code, and why it is 7,500 times faster in this case.

PythonJS Output

a = 0;
var x;
x = 0;
while(x < N) {
 a += 1
 x += 1
}

The PythonJS compiler knows that a for loop over a range is actually just a simple loop that can be translated to fast JavaScript.

Test2 - while loop:

 a = 0
 i = 0
 while i < N:
  a += 1
  i += 1
  • Brython: 13.5709
  • Python: 0.01008
  • PyPy: 0.00144
  • PythonJS: 0.001

Things get worse for Brython when the loop gets more complicated, this tests increments two values. In this test Brython is 1,300 times slower than Python.

Test3 - calling simple function:

 a = 0
 for x in range(N):
  a += no_args()
  • Brython: 7.717
  • Python: 0.0137
  • PyPy: 0.0025169
  • PythonJS: 0.0019

Test4 - function call with normal arguments:

 a = 0
 for x in range(N):
  a += no_kwargs(1,2,3)
  • Brython: 15.3209
  • Python: 0.0250
  • PyPy: 0.00279
  • PythonJS: 0.3729
  • PythonJS + with fastdef: 0.0039

In this test PythonJS is 14 times slower than Python, this is because by default PythonJS functions that take arguments, have logic to check those arguments at runtime, and to also check if the function was called from JavaScript and if so - adapt the arguments. This can be bypassed using the @fastdef function decorator, or fastdef with statement `with fastdef:`.

In the above test, PythonJS with fastdef is 6.4 times faster than Python. Note that you can still call fastdef function from external JavaScript, you only need to pack arguments into an array as the first argument, and pack keyword args into an Object as the second argument. Example: func( [x,y,z], {a:1, b:2, c:3} )

Test5 - function call with keyword arguments:

 a = 0
 for x in range(N):
  a += call_kwargs(1,2,3, x=1, y=2, z=3)
  • Brython: 14.75
  • Python: 0.0370
  • PyPy: 0.00417
  • PythonJS: 0.631
  • PythonJS + with fastdef: 0.006

Conclusion

Brython is slow and heavy, written almost entirely in JavaScript, and closely follows the Python language standards. Its runtime brython.js is 227KB. Its tokenizer and compiler, py2js.js is 3,500 lines of JavaScript code. It lacks static type analysis and optimizations. Brython is able to translate Python code to JavaScript on the client side. Its performance is much slower than Python.

PythonJS is fast and light weight, written in pure Python, and tries to balance performance concerns with the Python language standards (read more here). Its runtime pythonjs.js is 98KB. The PythonJS translator is 1,900 lines of Python code. The translator includes static type analysis, and optimizes operator overloading and attribute access. It lacks a tokenizer and currently can only translate code on the server side using Python2 as its host. Its performance can surpass Python and approach PyPy in some cases, because it tries to output JavaScript JIT friendly code.

PythonJS 0.8.3 Released

Today I have released PythonJS 0.8.3, this includes the new fastdef and fast for loop optimizations. The performance test above is also included under tests/test_performance.html. The next release of PythonJS will feature integration with NodeJS.

Friday, November 8, 2013

Pixi.js and Blockly Animation System


I am currently working on a way to drive animations in Pixi.js using Tween.js and GoogleBlockly. The new SpriteController Block directly parses the conditional blocks it contains, if the selected Event is True, then the animation will play. In this example the Bee moves left and right when the left and right keys are pressed.

Next I will be working on improving the event system by adding more event types, branching logic, relative animation, and variables.

Wednesday, November 6, 2013

Pixi.js and GoogleBlockly


Pixi.js

Pixi.js is a fast 2D rendering engine that is optimized for WebGL, and is able to use HTML5 Canvas as a fallback. Using WebGL Pixi is able to render high resolution graphics very quickly. The Pixi API is well designed, and easily wrapped in PythonJS, see my bindings here.

Google Blockly Integration

The officially documented way to integrate Google Blockly with an external API is using Blockly.addChangeListener( my_callback ), your callback will be called each time the UI is updated. Your callback can then call Blockly.JavaScript.workspaceToCode() and then use JavaScript's eval to execute the string that it returns. This only allows for weak integration, here are the following problems:

  1. Your callback will be called for every single UI event, like moving blocks, or typing in an input field. This is slow and makes it very hard to catch just the events you are interested in.
  2. You have no way to send signals back to the blocks that generated the code. This makes it impossible to update fields on Blockly's blocks, like number fields. For example, your custom block creates a Sprite, and then in the game the sprite is moved, now your code will need to update the position field on the block in Blockly's workspace, but the Sprite has no reference to the block to do so.
  3. Each time your callback is triggered, you might have to tear down and reinitialize alot of state.
  4. This is generally a bad fit for an Actor model, where entities carry out their own behaviors, and interact with their environment using dynamic rules.

The above problems show us that the official way to integrate Blockly is pretty much useless with a dynamic system that requires bi-directional updates. The workaround to these problems requires us to modify Blockly's prototypes and functions so that blocks can be created from an external API, and UI events can be caught directly for each field. My solution is implemented in the binding layer between Blockly and PythonJS. Blockly's source code can remain intact - because prototypes and functions can be changed at runtime in JavaScript using special syntax in PythonJS. You can see my binding here.

Wrapper Decorators

One of my goals when starting the integration between Google Blockly and Pixi was to write as little wrapper code as possible, but not so little that it was unclear how things are tied together. To achive this I implemented two special class decorators: @pythonjs.init_callbacks and @pythonjs.property_callbacks. These are different from normal Python class decorators because they inject some code and attributes into the class and it's methods at compile time. The "Block Generator" in the Blockly binding can then use the extra attributes to hook in it's own callbacks for when instances of a class are created, and when values are set on property setters. In my first attempts to integrate Blockly with Three.js I had written alot of code to generate a custom block for each Three.js class. The new Block Generator can wrap a class and generate a custom block with a single call: block.bind_class( my_class ).

To run this demo you need the checkout the latest source code for: Pixi.js, GoogleBlockly, and Tween.js. Then pull the latest source for PythonJS. The source code for the demo is here.

Friday, October 25, 2013

Blockly Three.js and Tween.js


Tween.js by Soledad Penades is a fast and simple animation library that can interpolate "tween" values. I have created a binding for Tween.js for PythonJS, here, and integrated it with the GoogleBlockly THREE.js editor, the new animation blocks are efficiently driven by Tween.js and can animate translation and color. see my commit here.

Last night I was chatting with Erik de Bruijn (creator of The UltiMaker 3D printer) and he completely blew me away with his work he has been pioneering with GoogleBlockly and THREE.js to make a constructive solid geometry editor called "UltiShaper". Do not miss these youtubes he has posted!

Tuesday, July 16, 2013

We live in the forest


This is Miran and I. I am coding, while he is training for a fight. Thomas Backlund is another coder living in the forest, but not like Miran and I. No tent, no sleeping bags required - just dig a hole.