swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Swarm-Support] Subject: alternaties to inFile and outFile for ascii


From: Paul Johnson
Subject: Re: [Swarm-Support] Subject: alternaties to inFile and outFile for ascii text?
Date: Mon, 09 May 2005 23:41:29 -0500
User-agent: Mozilla Thunderbird 1.0.2-1.3.2 (X11/20050324)

Dear Marcus:

This example is very helpful. Thank you very much.

I can almost get over the hump if I understand the way that R talks to hdf5 and then to Swarm. Can you point me at the syntax on setting variables & arrays, possibly just an explanation of the attr settings here. Is there a way to just set a vector of doubles and have it read in directly? Or, suppose I want to insert an array of integers valued 0 and 1 and want them to come into Objective-C as BOOL.

I can trace through your example like the monkey typing the Bible, adding a dataset:

library(hdf5)

data <- read.table("input.txt",header=TRUE)

attr(data,"type") <- "List"
attr(data,"component-type") <- "MyClass"

#new lines here to create new data.frame
attr(data2,"type") <- "List"
attr(data2,"component-type") <- "MyClass"
newx <- rnorm(100)
newy <- rnorm(1000)
data2 <- data.frame(newx,newy)

hdf5save("test.hdf", "data", "data2")

That makes a new List object that I can read into swarm by iterating over the list. Is this the intended way? With the serialization based on hdf5, it seems to me there is some more automagical way....




Marcus G. Daniels wrote:
Paul Box wrote:

What are the alternatives these days for someone trying to reading and writing ascii files in swarm?

These instructions are for Windows, but it works similarly on other systems.

1. Install R for Windows
   http://cran.cnr.berkeley.edu/bin/windows/base/rw2010.exe

2. Run R and go to the Packages menu and then Install Packages(s). Choose a mirror near you and then select hdf5 from the menu that follows.

3. Save the attached input.R and input.txt in the same folder somewhere

4. Change R's directory to that folder with File/Change dir.

5. Use File/Source R code to load input.R.

6. Compile the attached input.m (this assumes a Swarm installation on Windows) as per the comments at the end. 7. Running ./input from the command line will print out the contents of the file by loading the HDF5 file that R created.

The idea here is to use R to load the data. R has good capabilities for parsing text files and manipulating and summarizing their contents. The input.R code doesn't do any preprocessing in this case, it simply loads the data, declares its type for Swarm (the calls to "attr") and then saves it in a form Swarm can load.

In turn, the Swarm input.m file then doesn't even need to declare classes for the imported data. The classes will be generated on the fly with instance variable names that track the column names in the original text file. (This is why to print out the values I used probes to get the values.)


------------------------------------------------------------------------

library(hdf5)

data <- read.table("input.txt",header=TRUE)

attr(data,"type") <- "List"
attr(data,"component-type") <- "MyClass"

hdf5save("test.hdf", "data")


------------------------------------------------------------------------

#import <simtools.h> // initSwarm
#import <objectbase.h> // VarProbe
#import <defobj.h> // Archiver

int
main (int argc, const char **argv)
{
  id <Archiver> archiver;
  id <VarProbe> vp1, vp2;
  id list;

  initSwarmBatch (argc, argv);

  archiver = [[[HDF5Archiver createBegin: globalZone]
                 setPath: "test.hdf"]
               createEnd];

  list = [archiver getObject: "data"];

  {
    Class class = [[list getFirst] getClass];

    vp1 = [[[[VarProbe createBegin: globalZone]
              setProbedClass: class]
             setProbedVariable: "val1"]
            createEnd];

    vp2 = [[[[VarProbe createBegin: globalZone]
              setProbedClass: class]
             setProbedVariable: "val2"]
            createEnd];
  }
  {
    id <Index> index = [list begin: scratchZone];
    id obj;
for (obj = [index next]; [index getLoc] == Member; obj = [index next])
      printf ("%d %d\n", [vp1 probeAsInt: obj], [vp2 probeAsInt: obj]);
[index drop];
  }
  [vp1 drop];
  [vp2 drop];
  [list drop];
  [archiver drop];
}

/*
Local Variables:
compile-command: "/usr/bin/libtool-swarm --mode=link gcc -I/usr/include/swarm -o 
input -g -Wno-import input.m -L/usr/lib/swarm -lswarm"
End:
*/


------------------------------------------------------------------------

_______________________________________________
Support mailing list
address@hidden
http://www.swarm.org/mailman/listinfo/support


--
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ku.edu/~pauljohn
1541 Lilac Lane, Rm 504
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66044-3177           FAX: (785) 864-5700


reply via email to

[Prev in Thread] Current Thread [Next in Thread]