ViSUSpy

From
Revision as of 17:16, 9 March 2018 by Admin (talk | contribs) (Read from IDX file)
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()

# open an IDX dataset
dataset=Dataset_loadDataset(self.filename)
# check it is valid
self.assertIsNotNone(dataset)

# get the bounding box of the dataset 
# the box is defined by a set of 2 points (p1 and p2)
# which define the two corners of a bounding box
box=dataset.get().getBox()

# get the default field of a dataset
field=dataset.get().getDefaultField()

#create access
access=dataset.get().createAccess()

# here we read the datasets slice by slice
# making box queries 
sampleid=0
for Z in range(0,16):
  slice_box=box.getZSlab(Z,Z+1)
      
  # define a read query
  query=QueryPtr(Query(dataset.get(),ord('r')))
  # set the box for the query
  query.get().position=Position(slice_box)
      
  self.assertTrue(dataset.get().beginQuery(query))
  # check the size of the output of the query 
  print("query size", query.get().nsamples.innerProduct())
  # execute query
  self.assertTrue(dataset.get().executeQuery(access,query))
  
  # convert output buffer to a numpy array
  my_numpy_array=convertToNumPyArray(query.get().buffer)

IdxModule.detach()