TUM-Logo

libRASCH

 

Home
 

General

About libRASCH/News
Design
Screen shots
Sample programs (with source code)
License
 

Download

libRASCH
Tools
 

Documentation

User
Developer
 

Resources

Mailing list
Supported Formats
Plugins
Status
Links
 
Contact
About this site
 
Last updated
Tue Mar 27 23:03:50 2007
A Tutorial

Chapter 3. A Tutorial

The handling of evaluations will be changed in the next version of libRASCH. Because we are working already on it, the sections about evaluations, event-sets and event-propertiey may not work with the current version (0.7.0). If you have any questions about the changes, please send them to the mailing list.

3.1. Initialize libRASCH and Basic Usage

This section will show you how to start using libRASCH. We will initialize the library and get some information about it.

So let's start with the first example. We will perform the following steps:

  • init libRASCH

  • get number of plugins

  • find all measurements in a directory

3.1.1. C Version

//
#include <stdio.h>
#include <ra.h> (1)

int main(int argc, char *argv[])
{
    ra_handle ra;
    value_handle vh;
    ra_find_handle f;
    struct ra_find_struct fs;

    /* initialize libRASCH */
    ra = ra_lib_init(); (2)

    /* check if init was successful */
    if ((ra == NULL)
        || (ra_lib_get_error(ra, NULL, 0) != RA_ERR_NONE)) (3)
    {
        if (!ra)
            printf("error initializing libRASCH\n");
        else
        {
            char err_t[200];
            long err_num;

            err_num = ra_lib_get_error(ra, err_t, 200);
            printf("while initializing libRASCH, error #%d "
                   "occured\n  %s\n", err_num, err_t);

            ra_lib_close(ra);
        }
        return -1;
    }

    /* get some infos */
    vh = ra_value_malloc(); (4)
    if (ra_info_get(ra, RA_INFO_NUM_PLUGINS_L, vh) == 0) (5)
    {
        printf("%s (%s): %d\n", ra_value_get_name(vh),
               ra_value_get_desc(vh), ra_value_get_long(vh));
    }
    ra_value_free(vh); (6)

    /* find all measurements in a directory */
    f = ra_meas_find_first(ra, argv[1], &fs); (7)
    if (f)
    {
        int cnt = 1;

        printf("measurements found in %s:\n", argv[1]);
        do
        {
            printf("  %2d: %s\n", cnt, fs.name);
            cnt++;
        }
        while (ra_meas_find_next(f, &fs)); (8)

        ra_meas_close_find(f); (9)
    }

    /* close libRASCH */
    ra_lib_close(ra); (10)

    return 0;
}                               /* main() */

//
(1)
To use libRASCH, the header-file ra.h must be included. In this example the include-directory of libRASCH is in the INCLUDE path of the C compiler. In ra.h you will find all function prototypes of the API of libRASCH. ra.h includes the header-file ra_defines.h, there you will find all define's and structure's needed for the libRASCH API.
(2)
ra_lib_init() initialize libRASCH. The function returns an ra_handle.
(3)
ra_lib_get_error() returns the last error occured in libRASCH. To check if the initialization was successful, check that no error occured. If an error occured, a short description of the error can be retrieved with the function ra_lib_get_error().
(4)
ra_value_malloc() returns an value object. This object will be used in libRASCH to handle data. To set/get the data and to get informations about the stored data, API functions in libRASCH are available. The functions start with 'ra_value_*'.
(5)
ra_info_get() returns information about libRASCH and all objects handled by libRASCH. RA_INFO_NUM_PLUGINS_L asks libRASCH for the the number of loaded plugins. The last character of the info-id ('L') indicates that the returned value will be a long-value. Therefore the number of plugins will be returned by using the function ra_value_get_long(). (See the descriptions of the ra_value_* functions in the reference manual what else can be done with an value object.)
(6)
ra_value_free() frees the memory associated with the value object allocated above.
(7)
ra_meas_find_first() returns a valid handle (not NULL) if at least one supported measurement (this means that at least one measurement in the directory can be handled with one of the loaded access-plugins). The information about the found measurement will be set in the ra_find_struct (for definition of structure see ra_defines.h).
(8)
ra_meas_find_next() returns true (!= 0) if another measurement is available. Again, the info about the measurement will be in the ra_find_struct. This function iterates over all found measurements.
(9)
ra_meas_close_find() frees all memory allocated during ra_meas_find_first() and ra_meas_find_next().
(10)
ra_lib_close() unloads all plugins and frees all allocated memory.

Running the example in the examples directory with the command init_lib ./database produced the following output.

#plugins (): 30
measurements found in ./database:
   1: ./database/JesusOlivan2003-12-EMG2.edf
   2: ./database/100s.hea

3.1.2. Perl Version

The Perl script shown below produces exactly the same output as the C version, therefore the output is not shown.

#
use strict;
use RASCH; (1)

# initialize libRASCH
my $ra = new RASCH or die "error initializing libRASCH\n"; (2)
my ($err_num, $err_text) = $ra->get_error ();
if ($err_num != 1)
{
    print "while initializing libRASCH, error # $err_num " .
        "occured:\n $err_text\n";
    exit -1;
}

# get some infos
my $value = $ra->get_info (info =>'num_plugins');                 (3)
if ($value->is_ok())
{
    print $value->name() . ' (' . $value->desc() . '): ' . $value->value() . "\n";
}

# find all measurements in a directory
my $meas = $ra->find_meas($ARGV[0]);     (4)
print "measurements found in $ARGV[0]:\n";
my $cnt = 1;
for (@$meas)
{
    print $cnt . ': ' . $_->filename() . "\n";
    $cnt++;
}

# ra_close() will be called when $ra is being destroyed

exit 0;
#
(1)
After installing the Perl support for libRASCH, the package RASCH.pm is available.
(2)
Create a new RASCH object.
(3)
The function get_info() returns an array including (1) the value, (2) the name and (3) a short description of the wanted information.
(4)
get_all_meas() returns an array with all the measurements found.

3.1.3. Python Version

The Python script shown below produces exactly the same output as the C and Perl version, therefore the output is not shown.

#
import sys
from RASCH import * (1)

# initialize libRASCH
ra = RASCH() (2)
if not ra:
    print "can't initialize libRASCH"
    sys.exit()
[err_num, err_text] = ra.get_error()
if err_num != 1:
    print "while initializing libRASCH, error #%d occured:\n "           "%s\n" % err_num, err_text
    sys.exit()
    
# get some infos
[value, name, desc] = ra.get_info(info='num_plugins') (3)
if (value):
    print name, "("+desc+"):", value

# find all measurements in a directory
meas = ra.get_all_meas(sys.argv[1]) (4)
print "measurements found in " + sys.argv[1] + ":\n"
cnt = 1
for item in meas:
    print "%d: %s" % (cnt, item)
    cnt = cnt + 1

#
(1)
After installing the Python support for libRASCH, the module 'RASCH' is available.
(2)
Create a new RASCH object.
(3)
The function get_info() returns an array including (1) the value, (2) the name and (3) a short description of the wanted information.
(4)
get_all_meas() returns an array with all the measurements found.

3.1.4. Matlab/Octave Version

Here an Octave session using the libRASCH support is shown. The same tasks are performed as in the previous examples. Most of the functions have in Matlab and Octave the same behaviour. Differences are listed in the function reference section for Matlab and Octave.

rasch@rasch:~> octave
GNU Octave, version 2.1.50 (i686-pc-linux-gnu).
...

octave:1> ra=ra_lib_init (1)
ra = 145016336
octave:2> [err_num, err_text]=ra_lib_get_error(ra)
err_num = 1
err_text = libRASCH: no error
octave:3> [value, name, desc]=ra_lib_get_info(ra, 'num_plugins') (2)
value = 25
name = #plugins
desc =
octave:4> meas=ra_meas_find(ra, './database') (3)
meas =
{
  [1,1] = ./database/JesusOlivan2003-12-EMG2.edf
  [2,1] = ./database/100s.hea
}
octave:5>
(1)
Initialize libRASCH. If function call is successfull, a value not 0 will be returned.
(2)
The function returns an array including (1) the value, (2) the name and (3) a short description of the wanted information.
(3)
The function returns a cell-array with all the measurements found.