Difference between revisions of "ViSUSpy"
From
(→Read from IDX file) |
(→Read from IDX file) |
||
Line 73: | Line 73: | ||
IdxModule.attach() | IdxModule.attach() | ||
+ | #open an IDX dataset | ||
dataset=Dataset_loadDataset(self.filename) | dataset=Dataset_loadDataset(self.filename) | ||
+ | # check it is valid | ||
self.assertIsNotNone(dataset) | 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() | box=dataset.get().getBox() | ||
+ | |||
+ | #get the default field of a dataset | ||
field=dataset.get().getDefaultField() | field=dataset.get().getDefaultField() | ||
+ | |||
+ | #create access | ||
access=dataset.get().createAccess() | access=dataset.get().createAccess() | ||
− | + | ||
sampleid=0 | sampleid=0 | ||
for Z in range(0,16): | for Z in range(0,16): |
Revision as of 16:57, 9 March 2018
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() 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()