2011-12-30

Firefox plug-in container's problem

by Forrest Sheng Bao http://fsbao.net

Firefox's market share has just been passed by Chrome. If they wanna catch up, Firefox really needs to fix many problems of itself. Its plug-in container on Linux should be on top of their list.

I use two important plug-ins in Firefox, Adobe PDF reader and flash player. But they sometimes do not work after having been used in Firefox for a while, e.g., after you open several tabs of PDF files.

I am not alone. For example, ever since Ubuntu 9.10, the Firefox (around 3.0) shipped with Ubuntu Linux has had problem with Adobe PDF reader: http://ubuntuforums.org/archive/index.php/t-1385589.html  Somebody even created a ticket at Firefox Help: http://support.mozilla.org/en-US/questions/891508


And I also notice that when playing Flash movies, Firefox's plug-in container takes a lot of CPU resources. This does not happen on Chrome.

I assume this plug-in container problem only happens on Linux platform. But since Linux is a growing market, it may not be a good idea to ignore this problem.

2011-12-28

An incompatible problem between python-vtk and pyvtk

I am using python-vtk (official Python binding of VTK) and pyvtk (only for VTK-format file I/O) these days. It's not a pleasant experience. It took me a few hours to debug my code and found out the problem at somewhere I never expected - the incompatibility of the libraries I am using. On top of that, one of them has compatibility issue with Python2.7.

I used python-vtk to read from VTK files (because I couldn't google out examples using pyvtk to do so). And then I used pyvtk to write into VTK files (because I couldn't google out examples using python-vtk to create scalar POINTDATA).

The problem is that in python-vtk, vtk.vtkDataSetReader().GetOutput().GetPoint() returns a 3-tuple, which is the X-, Y- and Z-coordinates of a POINT in DATASET POLYDATA block, whereas in pyvtk, pyvtk.PolyData() takes in coordinates as a list, not a 3-tuple.

I don't understand how this could cause problems, because lists and tuples are very similar in Python. After mandatory type conversion, the problem was gone.

pyvtk also has problem with Python2.7. I haven't debugged out the cause. But I just know it does not work with Python2.7.

PS: There is a great lack of resources on these two libraries. I use one library for reading VTK files and the other for writing VTK files, though each of them has the ability to do both. This "complimentary" combination is because I couldn't google out examples on using the opposite library for the opposite function. I really don't understand this. Without document, a software package is nothing.

I didn't find any documentation on pyvtk's website. For python-vtk, released with VTK (C++ mainly, along with Tcl, Python and Java bindings), I still didn't find docs for Python. I had to use iPython to learn.

Does anyone know why?

2011-12-26

The RIGHT code to enable MathJax in your website

I started using MathJax to display math on my blog (Drupal and Google Blogger) recently. OMG, my life is much easier. LaTeX is easier to type math than MathML and MathJax does not render math into pictures but vector symbols.

But I noticed two problems.
  1. Firefox for Linux has a little problem with the Javascript code on MathJax official instruction. The fonts are odd - no such problem on Chrome for Linux. (See below)
  2. MathJax's official instruction does not work for Blogger's HTML template.

Problem 1: See how t' looks like on the left in Firefox, in contrast to how it looks like on the right in Chrome.



Then I found out this instruction that fixed both of my problems:
http://irrep.blogspot.com/2011/07/mathjax-in-blogger-ii.html Though it says it is for Blogger, it should work for any CMS.

So, this the right code to enable MathJax on your website:

<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js" type="text/javascript">
    MathJax.Hub.Config({
        extensions: ["tex2jax.js","TeX/AmsMath.js","TeX/AMSsymbols.js"],
        jax: ["input/TeX","output/HTML-CSS"],
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
            processEscapes: true,
        },
        "HTML-CSS": { availableFonts: ["TeX"] }
    });
</script>

Please pay great attention to the escape sequences for inlineMath. Maybe you wanna disable the dollar quotations if you have more than one dollar sign in one post.

2011-12-19

Happening sequence for plans with both simple and durative actions in PDDL2.1

by Forrest Sheng Bao http://fsbao.net

I have been struggling with a definition in an important paper about temporal planning in past few weeks. And today i finally understand it with the help of one of the authors. In his words, this definition is sorta counter-intuitive. So I am now writing this blog and hope it will help others struggling with this as well.

Some background: The paper defines a major revision of PDDL, the de facto standard in planning, a sub-field of AI. It's called ``PDDL2. 1: An extension to PDDL for expressing temporal planning domains'' by Maria Fox and Derek Long. The paper is published in Journal of AI Research, one of the few top AI journals, in 2003.

Now let's begin. In Definition 16, the author defines the happening sequence of a plan, which implicitly includes both simple and durative actions as follows.

If P is a plan, then the happening sequence for P is \( \{t_i\}_{t=0...k}, \) the ordered sequence of the time points formed from the set of times

$$\{t| (t,a)\in P~or~ (t, a[t'])\in P~or~(t-t', a[t'])\in P\}$$

I don't understand why the 3rd disjunction (maybe in British English it's called ``disjunct.'') in the formula has $$t-t'$$ instead of $$t+t'$$ as the time point.

Dr. Derek Long, the 2nd and last author of this paper, gave me two email replies and finally made me understood.

Let's add something into the formula to see whether it helps.

$$\{t| (t,a)\in P~or~ (t, a[t'])\in P~or~(t-t', a[t'])\in P, t\in \mathbb{R}\}$$

So, for the 3rd disjunction, \( t \) is a real number, such that \( t-t' \) is the start of a durative action (by Definition 17) \( a \) whose duration is \( t' \). Since \( t-t' \) is the start, and \( t' \) is the duration of the action \( a \), \( t \) is the end of the action \( a \). In this way, the finishing time points of durative actions are included in the set of \( t \) 's.

Does it still sound ``counter-intuitive?'' If so, please write to me.

2011-12-18

Indexing matrix elements of different indexes in different rows in Octave or MATLAB

Here is a tricky programming problem in Octave or MATLAB. Given a matrix A, and another array Y, how to build a new array Z such that Z(i)=A(i, Y(i)) without using loops?

In other words I need to pick one (and only one) element per row while the indexes of elements vary in rows. This is easy if using loops but I am a ``vectorization freak.''

Below is a solution I found out. A is the given matrix. Y is the array specifying the element of each row to be picked out.

Please note I call trace() function in the end, because I don't really need the array Z but only its sum. So I simply need to sum the diagonal of the last square matrix.

Comments are welcomed. Do you know any name for this operation? And does Octave or MATLAB already has a solution for this?

octave:2> A=magic(10)(:,1:4)
A =

    92    99     1     8
    98    80     7    14
     4    81    88    20
    85    87    19    21
    86    93    25     2
    17    24    76    83
    23     5    82    89
    79     6    13    95
    10    12    94    96
    11    18   100    77

octave:3> Y=ceil(rand(10,1)*4)
Y =

   2
   1
   1
   3
   2
   1
   3
   4
   2
   3

octave:4> A(:,Y)
ans =

    99    92    92     1    99    92     1     8    99     1
    80    98    98     7    80    98     7    14    80     7
    81     4     4    88    81     4    88    20    81    88
    87    85    85    19    87    85    19    21    87    19
    93    86    86    25    93    86    25     2    93    25
    24    17    17    76    24    17    76    83    24    76
     5    23    23    82     5    23    82    89     5    82
     6    79    79    13     6    79    13    95     6    13
    12    10    10    94    12    10    94    96    12    94
    18    11    11   100    18    11   100    77    18   100

octave:5> Z=diag(A(:,Y))
Z =

    99
    98
     4
    19
    93
    17
    82
    95
    12
   100

octave:6> trace(A(:,Y))
ans =  619
octave:7> sum(diag(A(:,Y)))
ans =  619

2011-12-17

Adding model library in Cadence spectre2

To selecting device simulation model in Cadence Spectre for Cadence Affirma, here is how.

2011-12-05

A Chinese that do not speak Chinese

I just realized a very pathetic thing about myself minutes ago.

I was text chatting with a friend (which later you will believe is a new supernova of AI) on Google Talks. We talked about some research stuff. We went thru some history and new techniques in that field. Of course, again, I complained the slow development of symbolic AI now.

Just at that moment, that very moment, I realized that I felt much comfortable to discuss research topics with him in English than in Chinese, though we are both native Chinese speakers.

A few days ago, we did the similar thing. We talked about GPU computing, ensemble learning, academic career and academia vs. industry.

But we did in Chinese. And it bugged me - coz 1) I type Chinese slowly 2) half of the words we used in chat are English words. 3) I don't understand or express well in that Pidgin Chinese.

So, a feeling, a pathetic feeling, came up my mind. I am a Chinese guy who cannot discuss research topics in Chinese! Now my Chinese is only used for daily life, like, "I need an egg roll." I cannot express or understand well when talking about research - i even end up meeting Chinese faculty members in my university in English.

But, this is not my fault. I will write a new blog post later to explain why.

Oh, my friend. In 2010, his paper won the best paper award of AAAI, the best AI conference in the world. But he cannot find a faculty position all the time. So he ended up working for Google. If you are a member of faculty searching committee of a university, please do contact my talented friend Dr. Ruo-Yun Huang. Here is his homepage: http://www.cse.wustl.edu/~huangr/

2011-12-04

Reading VTK files in Python via python-vtk

by Forrest Sheng Bao http://fsbao.net

Update 2012-06-29: I just figured out a way to access polygons/cell using VTK's python binding. Check here: http://forrestbao.blogspot.com/2012/06/vtk-polygons-and-other-cells-as.html

Surprisingly, I noticed that there isn't a good document covering frequently-used functions of the Python module vtk (provided via python-vtk on Debian/Ubuntu Linux systems). So I decide to write a very simple one here, covering all functions that I have used.

Note:
  • This tutorial is for Python 2.X though slight changes can make it work with Python 3.X.
  • I assume you are familiar with VTK data format, thus you know what header, DATASET and POINT_DATA are.
  • Notations like In [123] or Out[123] are prompts in iPython (an interactive Python shell). They show a line of code and its output/effect, respectively. They should NOT appear in your code. And, when you use other Python interpreter/shell, you may not see it.
  • I did not use print function in iPython when showing something. But when you write a Python program, you need print to display.
Step 1: Set up the reader.
import vtk
reader = vtk.vtkDataSetReader()
reader.SetFileName("lh.sulc.fundi.from.pits.pial.vtk")
reader.ReadAllScalarsOn()  # Activate the reading of all scalars
reader.Update()

The reader is the top level object we use to access a VTK file. For example, you can know the header of a VTK file by
In [119]: reader.GetHeader()
Out[119]: 'Created by Mindboggle'

Step 2: Get data in DATASET block.

Again, reader is the top level. To get data in DATASET block, we need to call a method on reader.
data=reader.GetOutput()

We can know how many (not ``much'' here) data of each type are in the DATASET by the function GetNumberOf{VTK_Data_Type}(). For example, to know how may Points are there, we use:
In [11]: data.GetNumberOfPoints()
Out[11]: 128895

I used tab-completion function of iPython to find out all GetNumberOf{VTK_Data_Type}() for data:
data.GetNumberOfCells   data.GetNumberOfPolys
data.GetNumberOfLines   data.GetNumberOfStrips
data.GetNumberOfPieces  data.GetNumberOfVerts
data.GetNumberOfPoints  

After knowing the size of data of each type, we can access them. For example, to get the first point's coordinate, we can do this:
In [136]: data.GetPoint(0)
Out[136]: (-11.605026245117188, -97.47259521484375, 3.8222298622131348)

To access vertexes in the VERTICES block, it is a little different.
In [15]: data.GetNumberOfVerts()
Out[15]: 1L
In [18]: vt = data.GetVerts()
In [20]: vt.GetSize()
Out[20]: 56094L
In [23]: vt.GetData().GetValue(2)
Out[23]: 1091L
In [24]: vt.GetData().GetValue(3)
Out[24]: 1108L
Please note that some people, like myself, prefer to put all vertexes in one line, so the result of data.GetNumberOfVerts() and data.GetVerts().GetSize() look different. But it shouldn't bother you from using data.GetVerts().GetData().GetValue(x) to access them.

Step 3: get data in POINT_DATA/CELL_DATA block.

Data in POINT_DATA and CELL_DATA can be loaded via function GetPointData() and GetCellData(). For example,
d=data.GetPointData()

We will use loading a scalar array from POINT_DATA as an example below.
Vectors and tensors can be accessed in similar way by different functions.

All scalar arrays in a file can be viewed by:
In [140]: reader.GetNumberOfScalarsInFile() # get number of scalars
Out[140]: 5
In [141]: reader.GetScalarsNameInFile(1) # get scalar name string
Out[140]: curv

A scalar can be accessed by its name:
In [38]: array=d.GetArray('curv') # 'curv' is the scalar name
In [50]: array.GetValue(260957-260761+1)
Out[50]: 0.043181419372558594

In the end, you should see the following variables:
Variable   Type         Data/Info
---------------------------------
curv       vtkobject    vtkFloatArray (0x9e5cd38)<...>)\n  Array: 0x9f1a9a0\n\n
d          vtkobject    vtkPointData (0x9789b50)\<...>  PedigreeIds: (none)\n\n
data       vtkobject    vtkPolyData (0x9e6c650)\n<...>: 0\n  Ghost Level: 0\n\n
reader     vtkobject    vtkDataSetReader (0x9c431<...> InputStringLength: 0\n\n
vtk        module       < module 'vtk' from '/usr/<...>hon2.6/vtk/__init__.pyc'>

The end. Comments and questions are welcomed.

References: