PyExtJS

Python Extension Packages in Javascript and NodeJS

OTHER License

Downloads
569
Stars
51
Committers
2

PyExtJS

(Python Extension Packages in Javascript)

Contents

  • What is PyExtJs?
  • Installation
  • Latest source code
  • Bug reports
  • Getting Started
    • numpy
      • array
      • linspace
      • logspace
      • exp
      • arange
      • power
      • reshape
      • shape
      • size
      • ndim
      • strides
      • dtype
      • ravel
      • T / transpose
      • dot
      • zeros
      • ones
      • random
      • polyfit
    • scipy
    • interpolate
    • linregress

What is PyExtJs?

Python Extension Packages in Javascript is open-source implementation of some common libraries used in the scientific python programming. The main goal of this project is to improve migration of python language to javascript.

License

Copyright 2016 Alvaro Fernandez

License: MIT/X11

Installation

Just include the following libraries in your html.

<script type="text/javascript" src="ss.js"></script>
<script type="text/javascript" src="Numpy.js"></script>
<script type="text/javascript" src="PolySolve.js"></script>
<script type="text/javascript" src="Scipy.js"></script>

Latest source code

https://github.com/fernandezajp/PyExtJs

Bug reports

https://github.com/fernandezajp/PyExtJs/issues

Getting Started

numpy

importing a library

Python

>>> import numpy as np
> np = numpy;

Using array

Python

>>> import numpy
>>> numpy.array([[1,2],[3,4]])
    array([[1, 2], [3, 4]])
> numpy.array([[1,2],[3,4]]);
  [[1, 2], [3, 4]]

Using linspace

Python

>>> import numpy
>>> numpy.linspace(2.0, 3.0, 5)
    array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ])
> numpy.linspace(2.0, 3.0, 5);
  [2, 2.25, 2.5, 2.75, 3]

Using logspace

Python

>>> import numpy
>>> numpy.logspace(2.0, 3.0, 5)
    array([100.,177.827941,316.22776602,562.34132519,1000.])
> numpy.logspace(2.0, 3.0, 5);
  [100, 177.82794100389228, 316.22776601683796, 562.341325190349, 1000]

Using exp

Python

>>> import numpy
>>> numpy.exp(2.4)
    11.023176380641601
>>> numpy.exp([2.4, 3.1])
    array([ 11.02317638,  22.19795128])
> numpy.exp(2.4);
  11.0231763806416
> numpy.exp([2.4, 3.2])
  [11.0231763806416, 24.53253019710935]

Using arange

Python

>>> import numpy
>>> numpy.arange(3)
    array([0, 1, 2])
>>> numpy.arange(3,7)
    array([3, 4, 5, 6])
>>> numpy.arange(3,7,2)
    array([3, 5])
> numpy.arange(3);
  [0, 1, 2]
> numpy.arange(3,7)
  [3, 4, 5, 6]
> numpy.arange(3,7,2)
  [3, 5]

Python

>>> import numpy
>>> x1 = range(6)
>>> numpy.power(x1, 3)
    array([  0,   1,   8,  27,  64, 125])
>>> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
>>> np.power(x1, x2)
    array([  0.,   1.,   8.,  27.,  16.,   5.])
> x1 = numpy.range(6);
  [0, 1, 2, 3, 4, 5]
> numpy.power(x1, 3);
  [0, 1, 8, 27, 64, 125]
> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0];
  [1, 2, 3, 3, 2, 1]
> numpy.power(x1, x2);
  [0, 1, 8, 27, 16, 5]

Using shape

Python

>>> import numpy
>>> a = numpy.arange(6).reshape((3, 2))
>>> a.shape
    (3, 2)
> a = numpy.arange(6).reshape([3, 2]);
[[0, 1], [2, 3], [4, 5]]
> a.shape;
  [3, 2]

Using size

Python

>>> import numpy
>>> a = numpy.arange(6).reshape((3, 2))
>>> a.size
    6
> a = numpy.arange(6).reshape([3, 2]);
[[0, 1], [2, 3], [4, 5]]
> a.size;
  6

Using ndim

Python

>>> import numpy
>>> a = numpy.arange(6).reshape((3, 2))
>>> a.ndim
    2
> a = numpy.arange(6).reshape([3, 2]);
[[0, 1], [2, 3], [4, 5]]
> a.ndim;
  2

Using strides

Python

>>> import numpy
>>> a = numpy.arange(6).reshape((3, 2))
>>> a.strides
    (16, 8)
> a = numpy.arange(6).reshape([3, 2]);
[[0, 1], [2, 3], [4, 5]]
> a.strides;
  [2, 1]
# Very important: in Javascript the element has 1 byte

Using dtype

Python

>>> import numpy
>>> a = numpy.arange(6).reshape((3, 2))
>>> a.dtype
    dtype('int64')
> a = numpy.arange(6).reshape([3, 2]);
[[0, 1], [2, 3], [4, 5]]
> a.dtype;
  "Number"

Using ravel

Python

>>> import numpy
>>> a = numpy.arange(6).reshape((3, 2))
>>> a.ravel()
    array([0, 1, 2, 3, 4, 5])
> a = numpy.arange(6).reshape([3, 2]);
  [[0, 1], [2, 3], [4, 5]]
> a.ravel();
  [0, 1, 2, 3, 4, 5]

Using T or transpose

Python

>>> import numpy
>>> a = numpy.arange(6).reshape((3, 2))
>>> a.T
    array([[0, 2, 4],
           [1, 3, 5]])
>>> a.transpose()
    array([[0, 2, 4],
           [1, 3, 5]])
> a = numpy.arange(6).reshape([3, 2]);
  [[0, 1], [2, 3], [4, 5]]
> a.T;
  [[0, 2, 4], [1, 3, 5]]
> a.transpose();
  [[0, 2, 4], [1, 3, 5]]

Using dot

Python

>>> import numpy
>>> a = numpy.arange(6).reshape(3, 2)
>>> b = numpy.arange(6,12).reshape(2, 3)
>>> a.dot(b)
    array([[ 9, 10, 11],
           [39, 44, 49],
           [69, 78, 87]])
> a = numpy.arange(6).reshape(3, 2);
  [[0, 1], [2, 3], [4, 5]]
> b = numpy.arange(6,12).reshape(2, 3);
  [[6, 7, 8], [9, 10, 11]]
> a.dot(b);
  [[9, 10, 11], [39, 44, 49], [69, 78, 87]]

Using zeros

Python

>>> import numpy
>>> numpy.zeros(5)
    array([ 0.,  0.,  0.,  0.,  0.])
>>> numpy.zeros((3, 2))
    array([[ 0.,  0.],
          [ 0.,  0.],
          [ 0.,  0.]])
> numpy.zeros(5);
  [0, 0, 0, 0, 0]
> numpy.zeros([3, 2]);
  [[0, 0],[0, 0],[0, 0]]

Using ones

Python

>>> import numpy
>>> numpy.ones(5)
    array([ 1.,  1.,  1.,  1.,  1.])
>>> numpy.ones((3, 2))
    array([[ 1.,  1.],
          [ 1.,  1.],
          [ 1.,  1.]])
> numpy.ones(5);
  [1, 1, 1, 1, 1]
> numpy.ones([3, 2]);
  [[1, 1],[1, 1],[1, 1]]

Using random

Python

>>> import numpy
>>> numpy.random.random()
    0.298740136734731
>>> numpy.random.random(2)
    array([ 0.05538307,  0.74942997])
>>> numpy.random.random([2,2])
    array([[ 0.51655267,  0.57323634],
           [ 0.82552349,  0.10818737]])
> numpy.random.random();
  0.298740136734731
> numpy.random.random(2);
  [0.05538307, 0.74942997]
> numpy.random.random([2,2]);
  [[0.51655267, 0.57323634], [0.82552349, 0.10818737]]

Using polyfit

Python

>>> import numpy
>>> x = numpy.array([0.0, 1.0, 2.0, 3.0,  4.0,  5.0])
>>> y = numpy.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
>>> numpy.polyfit(x, y, 3)
    array([ 0.08703704, -0.81349206,  1.69312169, -0.03968254])
> x = [0.0, 1.0, 2.0, 3.0,  4.0,  5.0];
  [0, 1, 2, 3, 4, 5]
> y = [0.0, 0.8, 0.9, 0.1, -0.8, -1.0];
  [0, 0.8, 0.9, 0.1, -0.8, -1]
> numpy.polyfit(x, y, 3);
  [0.0870370370370341, -0.8134920634920405, 1.6931216931216477, -0.039682539682528106]

scipy

Using interpolate

Python

>>> import numpy
>>> from scipy import interpolate
>>> x = numpy.arange(0, 10)
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> y = numpy.exp(-x/3.0)
    array([ 1.        ,  0.71653131,  0.51341712,  0.36787944,  0.26359714,
    0.1888756 ,  0.13533528,  0.09697197,  0.06948345,  0.04978707])
>>> f = interpolate.interp1d(x, y)
>>> f(2.4)
    array(0.4552020478881322)  
> x = numpy.arange(0, 10);
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> tmp = x.map(function(v) {
    return -v/3.0;
  });
  [-0, -0.3333333333333333, -0.6666666666666666, -1, -1.3333333333333333, -1.6666666666666667, -2, -2.3333333333333335, -2.6666666666666665, -3]
> y = numpy.exp(tmp);
  [1, 0.7165313105737892, 0.513417119032592, 0.3678794411714424, 0.26359713811572677, 0.1888756028375618, 0.1353352832366127, 0.09697196786440504, 0.06948345122280153, 0.04978706836786395]
> var interpolation = new interpolate();
  undefined
> interpolation.interp1d(x, y);
  undefined
> interpolation.eval(2.4);
  0.4552020478881322
> interpolation.eval([3.1, 2.4]);
  [0.35745121086587084, 0.4552020478881322]

Using linregress

Python

>>> from scipy import stats
>>> x = [0.6,0.1,0.7,0.7,0.3,0.6,0.1,0.6,0.7,0.8]
>>> y = [0.8,0.8,0.2,0.1,0.8,0.3,0.5,0.9,0.1,0.2]
>>> slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
>>> slope
    -0.72818791946308714
>>> intercept
    0.84865771812080526  
> var Stats = new stats();
  undefined
> x = [ 0.6, 0.1, 0.7, 0.7, 0.3, 0.6, 0.1, 0.6, 0.7, 0.8 ];
  [0.6, 0.1, 0.7, 0.7, 0.3, 0.6, 0.1, 0.6, 0.7, 0.8]
> y = [ 0.8, 0.8, 0.2, 0.1, 0.8, 0.3, 0.5, 0.9, 0.1, 0.2 ];
  [0.8, 0.8, 0.2, 0.1, 0.8, 0.3, 0.5, 0.9, 0.1, 0.2]
> Stats.linregress(x, y);
  undefined
> Stats.get_slope();
  -0.7281879194630861
> Stats.get_intercept();
  0.8486577181208048

Performance

This is very important, the test was executed in a MacBookPro i5

The python Code:

import time
import numpy

def test():
    x = numpy.array([0.0, 1.0, 2.0, 3.0,  4.0,  5.0])
    y = numpy.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])

    start = time.time()
    for num in range(1,10000):
        numpy.polyfit(x, y, 3)
    end = time.time()

    microsecs = end - start
    print microsecs * 1000

test()
function test() {
    x = [0.0, 1.0, 2.0, 3.0,  4.0,  5.0];
    y = [0.0, 0.8, 0.9, 0.1, -0.8, -1.0];

    var start = +new Date();
    for (var i=0;i<10000;i++)
        numpy.polyfit(x, y, 3)
    var end =  +new Date();
    var diff = end - start;
    alert(diff);
}

test();

Python: 1604 milliseconds Javascript: 14 milliseconds

Javascript! Very fast!!!
PyExtJS

(Python Extension Packages in Javascript)

Contents

  • What is PyExtJs?
  • Installation
  • Latest source code
  • Bug reports
  • Wiki
    • Array creation routines
    • Array manipulation routines
    • Mathematical functions
  • Performance

What is PyExtJs?

Python Extension Packages in Javascript is open-source implementation of some common libraries used in the scientific python programming. The main goal of this project is to improve migration of python language to javascript.

License

Copyright 2016 Alvaro Fernandez

License: MIT/X11

Installation

on node.js

	$ npm install pyextjs  
	> require('pyextjs');

	> numpy.linspace(2.0,3.0,5);

on the browser

Just include the following libraries in your html.

<!doctype html>
<html>
  <head>
    <script type="text/javascript" src="../js/ss.js"></script>
    <script type="text/javascript" src="../js/Numpy.js"></script>
    <script type="text/javascript" src="../js/PolySolve.js"></script>
    <script type="text/javascript" src="../js/Scipy.js"></script>
    <script type="text/javascript">

       // Use Numpy & Scipy like python in javascript

       function ready() {
           var ls = numpy.linspace(2.0,3.0,5);
       }

    </script>
  </head>
</html>

Latest source code

https://github.com/fernandezajp/PyExtJs

Bug reports

https://github.com/fernandezajp/PyExtJs/issues

##Performance

This is very important, the test was executed in a MacBookPro i5

The python Code:

import time
import numpy

def test():
    x = numpy.array([0.0, 1.0, 2.0, 3.0,  4.0,  5.0])
    y = numpy.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])

    start = time.time()
    for num in range(1,10000):
        numpy.polyfit(x, y, 3)
    end = time.time()

    microsecs = end - start
    print microsecs * 1000

test()
function test() {
    x = [0.0, 1.0, 2.0, 3.0,  4.0,  5.0];
    y = [0.0, 0.8, 0.9, 0.1, -0.8, -1.0];

    var start = +new Date();
    for (var i=0;i<10000;i++)
        numpy.polyfit(x, y, 3)
    var end =  +new Date();
    var diff = end - start;
    alert(diff);
}

test();

Python: 1604 milliseconds Javascript: 14 milliseconds

Javascript! Very fast!!!