Back
Why "psychophysics programming with Java"?
I have noticed several problems after I started writing computer programs
for psychophysics experiments.
Foremost, I needed a platform which allowed me to work on my preferred
operating system and share my
experimental designs with my colleagues, I thought that I should
be able to e-mail them a single file and it should run with just a
mouse click whatever operating system, whatever computer architecture
they are using. Moreover, I needed to be able to communicate with myself:
I use a Mac in my lab and office, but Linux at home and on my laptop for
fMRI experiments. I found that lack of a solution to this simple need was
unacceptable. Do I have to prepare two versions of each experiment if
I want to work on it both at home and in the lab? And a third one to
send to my collegue that runs on MS Windows using DirectX libraries?
What about sharing it with others who visit my web page?
After switching
to Java, my collegues and I found solutions to problems in our
experimental designs literally over night, over a few e-mails. I can
now convert my experiments into Java applets by changing just a few
lines in my code and place them on my web page. Advantages of Java are
not limited by ease of communication. Due to its object oriented design
and technologies, Java provides a very effective platform of programming
and development. It boosts the productivity in several other ways as I
will explain in more detail below.
What Java offers to the Psychophysicist
First, the key technologies that concern the computational
psychophysicist.
Full Screen Exclusive Mode and new Java2D API
Full Screen Exclusive Mode (FSEM) is introduced to core Java platform
with version 1.4. Sun probably had the gaming industry in mind when
they introduced it, nevertheless this offered a marvelous opportunity
for psychophysicists. This mode and the new Java2D API, allow capturing
the entire screen, provide tools for double buffering and proper vertical
synchronization, all of which are essential for psychophysics programming.
Depending on the OS/architecture, Java may use different strategies in
FSEM. For instance on MS Windows it will most probably use the DirectX
routines, on Mac OS X it uses native OpenGL libraries, on Linux/Unix it
uses X11 libraries but can be instructed to use OpenGL libraries instead.
Architecture specific details are hidden while low level access is still possible
Engineers at Sun do the hard work of hiding the layer between the
programmer and the low level interaction with the hardware. This way
your code behaves in the exact same way on different platforms even
though the Java interpreter may do completely different jobs. On the
other hand Java doens't lack ways for low level hardware access for
visual displays.
3D graphics
For 3D graphics there are a few options. Among the most popular ones are
Java-3D, which provides a high level scene graphics API, and JOGL,
which stands for Java bindings for OpenGL. These two options offer
different levels of flexibility and level of hardware access. There is
a strong community support behind those open source projects.
Converting experiments into Java Applets
It is a matter of changing a few lines to convert your experiments
into Java applets, or web applications (called as WebStart technology).
Packaging and sharing the work
You can package your experiment in a so called jar file and send to
your collegues. Whatever platform they are on, they can run the
experiment with a mouse click.
Networked experiments
Java platform has excellent support for network programming,
providing all the necessary tools for networked experiments.
Multithreaded programming
In psychophysics or neuroimaging experiments, particularly with
animations and simultaneusly running task trials, it is desirable to
have a program which can run multiple tasks concurrently.
Core Java platform provides an extensive collection of tools for
multitasking programming, also known as multithreaded programming,
or parallel programming.
Matlab and Mathematica interface
It is a fact that many computational psychophysicists rely on Matlab or
Mathematica for programming. Those who like to continue using their
preferred environment can do so,
because both Matlab and Mathematica
offer simple ways to interact with Java objects.
Next, more general advantages of Java platform for improving productivity.
Cross platform portability
Even though it is not as perfect as it was promised, Java is platform
independent (OS and architecture) to a great extent. The behavior of
my experimental programs were always identical on all three systems I
used (Linux, Mac OS X (Tiger), MS Windows).
This improves productivity and communication:
Improved productivity at programming phase - easy team work:
Researchers working on their preferred OS can still work on the same
piece of code together.
Improved productivity at application phase: Researchers can work on their
preferred environments and can still run their experiments on different
systems in their labs. For example, they can prepare an experimental
program to measure the behavioral response to a visual stimulus in their
psychophysics lab on Mac OS X and use the same code on a PC to determine
the behevioral effect to the same stimulus inside the scanner before an
fMRI experiment.
Code sharability: Once written a class (objects are instances of
classes), you can use the same class again and again
(see also Object Oriented Programming below), moreover anybody else can
use the same code on their own platform. This is actually a great asset
because it means that you can get advantage of many existing classes and
build upon them your new tools.
Advantages of object oriented programming
Extendibility and reusability: OOP by design targets re-using and/or
extending existing classes. Mind you that this is not just re-use of
your functions in a more traditional language. You will find many
examples where we take full advantage of OOP in this guide.
Hide the implementation: By hiding the implementation you can write
code which does not break the client's program with every new version.
Strict type checking: Once it compiles a Java program usually runs
without any problem, you will not get memory errors as you would in C.
This is because Java compiler is extremely strict in variable types.
Java won't do the conversions for you, it wants you to do the
"type casting" to see that you know what you are doing.
Exception handling: In languages like C, and Fortran there is no real
exception handling. You invoke a method and if it fails your program
crashes. Or they may return a funny result which indicates that the
method failed to accomplish what you asked from it. There is nothing
worse than a program crashing with no information. Java provides a
much more advanced approach to handling such errors through its
Exception class. This way you can put appropriate mechanisms in
your code to fix the problems during its execution. Even if it is
unfixible, you can instruct your code to gracefully exit rather than
crash.
Automatic memory management: Java has an automatic "garbage collector".
You usually do not have to worry about memory allocations and
deallocations. In case you have special needs you can still manually
manage your memory.
Easier to code
Extensive API (collection of thousands of classes) in the core Java
platform. This eliminates to re-write many classes and their methods,
because as discussed above using existing classes is in the nature of OOP.
Excellent documentation of the core classes. You can easily reach the
most current documentation online at java.sun.com. The documentation
is superior to even older compiler languages such as C.
High level: One line of code does a lot for you when you use existing
classes as building boxes. This makes your code less complicated,
easier to read, and less prone to errors.
New generation of Integrated Development Environments (IDEs) provide very
easy and effective programming environment. One commonly used IDE is
Eclipse. The editor of Eclipse flags the mistakes as you are typing your
code and suggest possible options to fix those mistakes. You do not need
to remember which libraries to import, because Eclipse will do that for
you automatically, and suggest automatic code completions.
Apart from the IDEs, the user can still use Matlab or
Mathemetica as interface to Java objects.
Comparison to other tools, particularly to Psychtoolbox
Java is a programming platform, which is as complete as it gets for the
modern programmer, covering a whole range of areas, from parallel
programming to network tools. It is not fair to compare Matlab to Java in
those areas, because Matlab is not a real programming platform, it is an
advanced tool for numerical analysis of small technical problems. It is
quicker to learn, quicker to implement small programs in Matlab. But it
does not provide the same flexibility and variety of tools as Java.
Psychtoolbox was motivated by the fact that it was hard to learn the
syntax of C and graphics libraries in the absance of development
environments as we have today. It is also hard to code in C because it
leaves too much room for errors. Therefore there emerged a need to place
a layer between C and the novice programmer. Psychtoolbox has been that
layer for many years. But now new programming platforms, including Java
(but also others such as Python) open other doors eliminating the need
to that extra layer, because they offer not only a much advanced
programming platform but also a programming language that is easy to
code with.
Advantages
Java is platform independent, Psychtoolbox is not: Matlab runs on
multiple platforms, but Psychtoolbox does not. Even on the platforms
that Psychtoolbox runs you still have to make some modifications in your
code if you want to port it to another platform (actually the platforms
supported by the latest version are limited by two, MS Windows and
Mac OS X).
You can convert your experiments into Java applets, there is no such
option with Psychtoolbox
You can prepare a single executable file of your experiment and send
to collegues, they can then run it with a mouse click. There is no such
possibility with Psychtoolbox.
Psychtoolbox doesn't provide tools for 3D rendering. Java offers
multiple options, at multiple levels of ease and levels of programming.
Java has extensive coverage in threaded programming and network
programming areas, Matlab runs on a single thread.
Object oriented programming, and other advanced programming techniques
inrease productivity. Matlab/Psychtoolbox doesn't provide such advanced
programming techniques.
New generation IDEs offer much superior programming environments
compared to Matlab's editor. One such IDE is an open source project
called Eclipse.
The underlying graphical details of Psychtoolbox are hidden away
from the researcher. One can in principle download and inspect the C
source code, but it is not such an easy task to fully understand that
code. You would also need to understand the native graphics libraries on
the two OSs that it runs. This is a discomfarting solution for a
researcher. Sooner or later the researcher will need more independence
then that.
Java is freely available (its source is open, as well.) Matlab needs a
licence to run. You either purchase a standalone licence, or use a
network licence if your institute has one. The first option is more
reliable but not cheap. The second one is free for you, but frequently
suffers from number of user limitations and network distruptions. A lab
under tight budget could reduce the costs considerably by using a Java
system (one could further reduce the costs if the system is built on
Linux).
Backward portability: It is unlikely that a program written for an
older version of Java platform breaks when run with a newer version.
You can upgrade your Java platform to the latest version without the
fear of breaking older code. Matlab/Psychtoolbox frequently breaks
older code.
Disadvantages (mispercieved and real)
It is easier to program in Matlab, OOP and Java are difficult.
It is definetaly simpler to program small projects in Matlab as a starter.
But this simplicity doesn't scale to larger projects and it does not
provide any of the productivity benefits that OOP offers in the
longer run. Yes, if it is a highschool intern in your lab one may then
argue that learning Java is going to waste his or her time. But you can
provide a framework for experiments in a specialized class and let your
student use that class, even create objects of those classes from within
Matlab or Mathematica if they prefer that option. psychWithJava is suuch
a framework and it is successfully used by inexperienced students.
Moreover, Java and OOP are taught in courses in many Computer Science
programs both at undergraduate and graduate levels. New generation of
students do come to our psychophysics labs with that knowledge and
are usually more keen on using Java rather then Matlab.
Community support: Many Psychophysicists use Psychtoolbox.
Psyctoolbox/Matlab option is extensively used by our community.
This naturally leads to a strong community support. For the moment
there is no such community for Psychophysics programmer on Java.
Your source of support will probably be the Java gaming community for now.
Integration with analysis programs: I run my experiments with
Psychtoolbox, analyze them with Matlab.
If you are doing your analysis using Matlab, it may seem like it is a
better idea to limit yourself only to that program and use Psychtoolbox.
But you can use Java from within Matlab/Mathematica. As for the pure Java
programmers, I think soon there will be enough numerical computation
tools, if not already, available to perform any important statistical
analysis with Java. The number of such methods may soon exceed the number
that exists in Matlab. The widely used gnu Scientific Library
(by computational physicists particularly) could easily
be ported to Java.
Performance. Java is slower than low level compiler languages such as C.
However the difference is shrinking with introduction of new technologies,
notably the Hot Spot Technology. Nowadays the speed of Java is about
2/3 of C. And certainly Java is not inferior to Matlab in terms of speed.
I have not met a situation where my experimental code suffered from cpu
or gpu speed. Only in numerical computations I found C faster than Java
(less than one fold), and Java is faster than Matlab usually a few folds!
Hardware manufacturers provide interfaces with Psychtoolbox,
not with Java. This is a big real disadvantage of using Java in my
opinion. Many companies provide interfaces for Matlab/Psychtoolbox to
use their devices. For example CRS provides Matlab functions for
Bits++ which integrate with Psyctoolbox (though they don't have their
LightScan program for Mac, why don't they use Java? They could cut costs!)
But as you will see in this guide, it is not all that difficult to
write your own interface if you know how to program independently
and as the number of users increase there will be many classes available
on the internet for such purposes. Since Java allows low level access it
is usually possible to build your own interfaces. Besides one would
expect the manufacturing companies should soon notice the advantage of
building their tools on Java to reduce their costs (Matlab did that).
There is no real viable cross-platform solution to displaying
movies with Java (mpeg etc.) This is a real drawback because more and
more researchers need to use movies as stimuli. I will try my best to
suggest solutions to this problem in the guide.
So, which one to choose?
In summary, Psychtoolbox was a temprory solution. The C compiler
language was hard to learn and hard to code with, there was a need for
the community to find an easier solution. Psychtoolbox provided that
solution using an unlikely ally Matlab, which is actually an advanced
numerical analysis tool, not a fullscale programming environment. But
the compilers constantly advance, the IDEs get better and better with
large community support and the need for such temprorary solutions is
eliminated.
All of the disadvantages that Java suffers from will soon change.
As the number of psychophysicists using Java increases, a community will
form. Besides, there is already a very strong community support behind
Java2D, java3D, and JOGL. Even though they mostly have gaming, not
psychophysics programming in mind one can benefit a lot from them.
However, if you already have library of functions to run your experiments
using Psychtoolbox and rely heavily on Matlab for analysis, it wouldn't
make sense to port all those to Java now, but I would recommend
implementing the new ones in Java. If you are newly creating your
library or faced a situation where Psychtoolbox isn't sufficient any
more, switch as soon as possible to pure Java programming.
Another option is using Java objects from within Matlab or Mathematica.
Even if you like to continue using your preferred platform, Matlab or
Mathematica, you can still create and use Java objects from within them.
Using Java objects has one distinctive advantage: they are platform
independent! Matlab and Mathematica both have multiple OS versions,
for the end user they are platform independent. Whereas Psychtoolbox
fails to be completely platform independent (and built only for Matlab).
When you replace Psychtoolbox with Java objects you would virtually be
completely platform independent even if you are using Matlab or
Mathematica for development.
Back
|