Skip to content

More complex examples

Momtchil Momtchev edited this page Nov 21, 2022 · 5 revisions

These examples can be found in the examples directory.

matplotlib

Example from https://matplotlib.org/stable/gallery/lines_bars_and_markers/bar_colors.html#sphx-glr-gallery-lines-bars-and-markers-bar-colors-py

Original Python code:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [40, 100, 30, 55]
bar_labels = ['red', 'blue', '_red', 'orange']
bar_colors = ['tab:red', 'tab:blue', 'tab:red', 'tab:orange']

ax.bar(fruits, counts, label=bar_labels, color=bar_colors)

ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color')

plt.show()

JavaScript code:

const { pymport, proxify } = require('pymport');
const plt = proxify(pymport('matplotlib.pyplot'));

const plots = plt.subplots();
// subplots returns a list, we need the second element
const ax = plots.item(1);

const fruits = ['apple', 'blueberry', 'cherry', 'orange'];
const counts = [40, 100, 30, 55];
const bar_labels = ['red', 'blue', '_red', 'orange'];
const bar_colors = ['tab:red', 'tab:blue', 'tab:red', 'tab:orange'];

// named arguments
ax.bar(fruits, counts, { label: bar_labels, color: bar_colors });

ax.set_ylabel('fruit supply');
ax.set_title('Fruit supply by kind and color');
ax.legend({ title: 'Fruit color' });

plt.show();

graph_tool

One of the official examples from https://graph-tool.skewed.de/static/doc/quickstart.html

const { pymport, proxify, PyObject, pyval } = require('pymport');
const gt = proxify(pymport('graph_tool.all'));
const np = proxify(pymport('numpy'));
const pl = proxify(pymport('pylab'));

const g = gt.Graph();

const v_age = g.new_vertex_property('int');
const e_age = g.new_edge_property('int');

const N = 100000;

let v = g.add_vertex();

// Every sequence/mapping in Python has a __setitem__
// equivalent to an assignment by using the [] operator
v_age.__setitem__(v, 0);

// PyObject returns raw objects, they must be proxified
const vlist = proxify(PyObject.list([v]));

for (let i = 1; i < N; i++) {
  const v = g.add_vertex();
  v_age.__setitem__(v, i);

  const rand = np.random.randint(0, vlist.length);
  const target = vlist.item(rand);

  const e = g.add_edge(v, target);
  e_age.__setitem__(e, i);

  vlist.append(target);
  vlist.append(v);
}

v = g.vertex(np.random.randint(0, g.num_vertices()));

for (; ;) {
  console.log(`vertex: ${pyval('int(v)', { v })}, in-degree: ${v.in_degree()}, ` +
    `out-degree: ${v.out_degree()}, age: ${v_age.item(v)}`);

  if (v.out_degree().toJS() == 0) {
    console.log('Nowhere else to go... We found the main hub!');
    break;
  }

  const n_list = proxify(PyObject.list([]));

  // v.out_neighbors() returns a Python generator object
  const it = v.out_neighbors();
  for (const w of it) {
    n_list.append(w);
  }

  v = n_list.item(np.random.randint(0, n_list.length));
}

g.vertex_properties.__setitem__('age', v_age);
g.edge_properties.__setitem__('age', e_age);

g.save('price.xml.gz');

const in_hist = gt.vertex_hist(g, 'in');

const y = in_hist.item(0);
const err = np.sqrt(y);

// Unlike Python, JavaScript does not have operator overloading meaning that
// we have to unroll all operators to their method equivalents
// including the subtraction which is between a numpy array and a scalar
// In Python we have:
// err[err >= y] = y[err >= y] - 1e-2
// Also, note that we cannot use err.item() because numpy.ndarray.item() will take precedence
err.__setitem__(err.__ge__(y), err.__getitem__(err.__ge__(y)).__sub__(1e2));

pl.figure({ figsize: [6, 4] });
pl.errorbar(in_hist.item(1).__getitem__(PyObject.slice({ stop: -1 })), in_hist.item(0), {
  fmt: 'o', yerr: err, label: 'in'
});

pl.gca().set_yscale('log');
pl.gca().set_xscale('log');
pl.gca().set_ylim(1e-1, 1e5);
pl.gca().set_xlim(0.8, 1e3);
pl.subplots_adjust({ left: 0.2, bottom: 0.2 });
pl.xlabel('$k_{in}$');
pl.ylabel('$NP(k_{in})$');
pl.tight_layout();
pl.savefig('price-deg-dist.svg');