ViSUSpy

From
Revision as of 16:11, 9 March 2018 by Admin (talk | contribs) (Library imports)
Jump to: navigation, search

ViSUSpy is a library that allows the user to use the ViSUS framework from python.

In the following sections we provide some examples on how to use it.

Library imports

To import and use the library in your python application you need to use the following imports:

from visuspy import *
  from VisusKernelPy import *
  from VisusIdxPy    import *
  from VisusDbPy     import *

These modules allow the basic interactions with IDX datasets, which can be either local or remote (served by a ViSUS Server).

Important: your code that interacts with IDX datasets has always to attach the IdxModule first, as following:

IdxModule.attach()
  # your program code
  IdxModule.detach()

Write data to an IDX file

   IdxModule.attach()
   dataset_box=NdBox(NdPoint(0,0,0),NdPoint.one(16,16,16))
   
   idxfile=IdxFile();
   idxfile.box=NdBox(dataset_box)
   idxfile.fields.push_back(Field("myfield",DType.fromString("uint32")))
   bSaved=idxfile.save(self.filename)
   self.assertTrue(bSaved)
   
   dataset=Dataset.loadDataset(self.filename)
   self.assertIsNotNone(dataset)
   access=dataset.get().createAccess()
   
   sampleid=0
   
   for Z in range(0,16):
     slice_box=dataset.get().getBox().getZSlab(Z,Z+1)
     
     query=QueryPtr(Query(dataset.get(),ord('w')))
     query.get().position=Position(slice_box)
     
     self.assertTrue(dataset.get().beginQuery(query))
     self.assertEqual(query.get().nsamples.innerProduct(),16*16)
     
     buffer=Array(query.get().nsamples,query.get().field.dtype)
     query.get().buffer=buffer
     
     fill=convertToNumPyArray(buffer)
     for Y in range(16):
       for X in range(16):
         fill[Y,X]=sampleid
         sampleid+=1
     success = dataset.get().executeQuery(access,query)
     IdxModule.detach()

Read from IDX file

   IdxModule.attach()
   dataset=Dataset_loadDataset(self.filename)
   self.assertIsNotNone(dataset)
   box=dataset.get().getBox()
   field=dataset.get().getDefaultField()
   access=dataset.get().createAccess()
   
   sampleid=0
   for Z in range(0,16):
     slice_box=box.getZSlab(Z,Z+1)
     
     query=QueryPtr(Query(dataset.get(),ord('r')))
     query.get().position=Position(slice_box)
     
     self.assertTrue(dataset.get().beginQuery(query))
     self.assertEqual(query.get().nsamples.innerProduct(),16*16)
     self.assertTrue(dataset.get().executeQuery(access,query))
     
     check=convertToNumPyArray(query.get().buffer)
     for Y in range(16):
       for X in range(16):
         self.assertEqual(check[Y,X],sampleid)
         sampleid+=1
    IdxModule.detach()