python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns =...

17
python @ Strand python @ Strand

Transcript of python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns =...

Page 1: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

python @ Strandpython @ Strand

Page 2: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 2

Overview

• Strand’s avadisTM platform

• Used in several verticals:

�Microarray expression (GeneSpring)

�Chemical structure descriptor (Sarchitect)

�Next gen sequencing (faNGS)

� stock market, semiconductor (potential)

• Data analysis and visualization:

� Import tabular data

�Perform visualizations and preprocessing

�Execute analysis algorithms

�Visualize results leading to discovery

Page 3: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 3

Examples

Page 4: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 4

Architecture

• Python:

�Rapid development

�Mix and match features

� Fast debugging

• JAVA:

�Core pluggable framework

�User interface

� Several algorithms

• C++:

�Core/Legacy algorithms

Python

JAVA

C++

JNI

Jython

Page 5: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 5

avadis in action

# generic python code …

url = “http://in.pycon.org/2009/delegates/”

# python code to download and parse URL

# say using sgmllib.py

# to get lists of ids, names, occupation, city, etc.

tableData = extractTableData (url)

# avadis code starts here …

# create the dataset

from script.dataset import createStringColumn, createDataset

columns = []

for (name, data) in tableData:

columns.append (createStringColumn (name, data)

d = script.dataset.createDataset (“delegates”, columns)

# launch a view on the dataset

view = script.view.Histogram (dataset=d, xLabelOrientation=“Slanted”)

view.show()

Page 6: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 6

avadis in action

But that’s already available at http://in.pycon.org/2009/statistics/

So, lets do something a little more interesting.

Page 7: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 7

avadis in action

view = script.view.Histogram (dataset=d, xLabelOrientation=“Slanted”)

view.colorBy.columnIndex = 4

view.show()

Page 8: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 8

The Law of Choices

• Give a man a single choice, and he will gladly take it.

• Give a man two choices, and he will be confused.

• Give a man three choices, and he will run to his wife.

• Give a man multiple choices, and he will be doomed.

• Give a man infinite choices, and YOU are doomed.

Page 9: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 9

python - JAVA interface

• Jython : used for the python – JAVA interface.

• Jython is Python.

• Install Jython from http://www.jython.org/

• Use jython command line tool to execute Jythonscripts.

• All JAVA classes are instantly accessible from within the Jython script.

• Additional JAVA classes are also accessible once the CLASSPATH variable is set.

Page 10: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 10

Example

import javax.swing.JFrame;

import javax.swing.JLabel;

import java.awt.Dimension;

public class Test {

public static void main (String[] args)

{

JFrame f = new JFrame ("Hello");

String s = "From within JAVA : Hello pycon.in";

JLabel l = new JLabel (s, JLabel.CENTER);

f.getContentPane().add (l);

f.setSize (new Dimension (300, 50));

f.setDefaultCloseOperation (f.EXIT_ON_CLOSE);

f.setVisible (true);

}

}

javac Test.java

java –cp . Test

moksha:jython2.5.1rc2$ ./jython

>>> from javax.swing import JFrame, JLabel

>>> f = JFrame ('Hello')

>>> t = 'From within Jython : Hello pycon.in'

>>> l = JLabel (t, JLabel.CENTER)

>>> f.contentPane.add (l)

>>> f.size = (300, 50)

>>> f.defaultCloseOperation = f.EXIT_ON_CLOSE

>>> f.visible = 1

Page 11: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 11

Charming Jython

• Can nicely mix Python and JAVA code:moksha:jython2.5.1rc2$ ./jython

>>> import random

>>> l = [random.randint (0, 100) for i in xrange (50)]

>>> from java.util import Collections

>>> Collections.sort (l)

• or, extend JAVA classes in Python:moksha:jython2.5.1rc2$ ./jython

>>> from java.io import FileOutputStream

>>> class UppercaseFileOutputStream (FileOutputStream):

... def write (self, text):

... text = text.upper()

... FileOutputStream.write (self, text)

...

>>> fos = UppercaseFileOutputStream ('out.txt')

>>> [fos.write ('This is line number ' + str(i) + '\n') for i in xrange(10)]

>>> fos.close()

• and more http://wiki.python.org/jython/LearningJython

Page 12: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 12

Jython from JAVA

• PyInterpreter class (org.python.util package)

� interp.exec (code)

� interp.set (name, value)

� interp.setOut (outstream)

� interp.setErr (outstream)

• avadis has a thin layer of JAVA on top of Jython,

which essentially does the above (JAVA6 has a better

way of doing this – JAVA Scripting API).

• most of the user interaction with the application

begins with python scripts.

Page 13: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 13

An example

<object type="spring.resource.menu.menuItem" version="1.0">

<key>name</key>

<string>K-Means</string>

<key>mnemonic</key>

<string>K</string>

<key>accelerator</key>

<string></string>

<key>tooltip</key>

<string>K-means</string>

<key>action</key>

<string>script.algorithm.KMeans().execute()</string>

</object>

<object type="spring.resource.menu.menuItem" version="1.0">

<key>name</key>

<string>Exit</string>

<key>mnemonic</key>

<string>X</string>

<key>accelerator</key>

<string>X</string>

<key>tooltip</key>

<string>Exit</string>

<key>action</key>

<string>java.lang.System.exit(0)</string>

</object>

Page 14: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 14

Another example

from script.omega import createComponent, showDialog

c = script.project.getActiveDataset().getColumn ('Occupation')

occupations = [c.getCategoryValue(i) for i in xrange (c.getCategoryCount())]

c1 = createComponent (id='rid', type='int', description='pycon Registration ID')

c2 = createComponent (id='name', type='string', description='Name')

c3 = createComponent (id='occ', type='enum', description='Occupation', options=occupations)

c4 = createComponent (id='place', type='string', description='Town/City')

c5 = createComponent (id='talk', type='string', description='Talk', value='pycon@strand')

c6 = createComponent (id='fb', type='enum', description='Talk feedback', options=['', 'Stinks'])

c = createComponent (id='x', type='group', description='', components=[c1,c2,c3,c4,c5,c6])

v = showDialog (c)

print v

Page 15: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 15

Debugging – differently

• The script editor and its beauty for debugging,

and quickly trying out code.

Page 16: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 16

Issues

• Started as a light wrapper, going on to become the heavyweight in the code base.

• Jython uses reflection internally => efficiency issues in making large number of JAVA calls from Jython – say within a for loop.

• Unlike JAVA, OOPS is not enforced => issues when programming in a larger software group.

• Compilation doesn’t capture JAVA compile errors, only syntax errors.

Page 17: python @ Strand - PyCon Indiafrom script.dataset import createStringColumn, createDataset columns = [] for (name, data) in tableData: columns.append (createStringColumn (name, data)

© Strand Life Sciences 2006Sep-09 17

Take homes

• The Law of Choices ☺

• A Scripting Engine for JAVA applications.

• Script Editor.

• Moderation ☺

Thank you