Difference between revisions of "ViSUSpy"

From
Jump to: navigation, search
(Read from IDX file)
(NumPy utils)
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
ViSUSpy is a library that allows the user to use the ViSUS framework from python.
+
The OpenVisus library is also available in python through a series of packages.
 +
 
 +
You can install the library using python pip:
 +
<syntaxhighlight lang="python">
 +
python -m pip install OpenVisus
 +
python -m OpenVisus configure
 +
</syntaxhighlight>
  
 
In the following sections we provide some examples on how to use it.
 
In the following sections we provide some examples on how to use it.
Line 8: Line 14:
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
from visuspy import *
+
from OpenVisus import *
from VisusKernelPy import *
 
from VisusIdxPy    import *
 
from VisusDbPy    import *
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
These modules allow the basic interactions with IDX datasets, which can be either local or remote (served by a [[ViSUS Server]]).
 
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:
+
'''Important''': your code that interacts with IDX datasets has always to attach the ''DbModule'' first, as following:
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
IdxModule.attach()
+
DbModule.attach()
 
 
 
# your program code
 
# your program code
 
+
DbModule.detach()
IdxModule.detach()
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Write data to an IDX file ==
+
=== Read data into a NumPy array ===
  
<syntaxhighlight lang="python" line='line'>
+
In order to facilitate interoperation with existing python scientific libraries as NumPy we provide two function to convert data arrays from/to ViSUS Array an NumPy array.
IdxModule.attach()
 
  
dataset_box=NdBox(NdPoint(0,0,0),NdPoint.one(16,16,16))
+
<syntaxhighlight lang="python">
   
+
import numpy
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)
+
# load a remote dataset
 +
dataset=LoadDataset("http://atlantis.sci.utah.edu/mod_visus?dataset=BlueMarble")
  
IdxModule.detach()
+
# get the bounding box of the dataset
</syntaxhighlight>
+
# the box is defined by a set of 2 points (p1 and p2)
 +
# which define the two corners of a bounding box
 +
box=dataset.getLogicBox()
  
== Read from IDX file ==
+
# get the default field of a dataset
 +
field=dataset.getDefaultField()
  
<syntaxhighlight lang="python" line='line'>
+
access=dataset.createAccess()
IdxModule.attach()
 
  
# open an IDX dataset
+
# select the resolution we need
dataset=Dataset_loadDataset(self.filename)
+
resolution = 21
# check it is valid
 
self.assertIsNotNone(dataset)
 
  
# get the bounding box of the dataset
+
# initialize query
# the box is defined by a set of 2 points (p1 and p2)
+
query=BoxQuery(dataset, field, time,ord('r'))
# which define the two corners of a bounding box
 
box=dataset.get().getBox()
 
  
# get the default field of a dataset
+
query.logic_box=dataset.getLogicBox()
field=dataset.get().getDefaultField()
+
query.end_resolutions.push_back(resolution)
  
#create access
+
# execute the query
access=dataset.get().createAccess()
+
dataset.beginQuery(query)
 +
dataset.executeQuery(access,query)
  
# here we read the datasets slice by slice
+
# convert our buffer to a numpy array.
# making box queries
+
# This is useful when we want to read data from
sampleid=0
+
# and IDX dataset and use this array with other libraries
for Z in range(0,16):
+
data=Array.toNumPy(query.buffer,bSqueeze=True,bShareMem=False)
  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()
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 21:06, 28 January 2020

The OpenVisus library is also available in python through a series of packages.

You can install the library using python pip:

python -m pip install OpenVisus
python -m OpenVisus configure

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 OpenVisus 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 DbModule first, as following:

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

Read data into a NumPy array

In order to facilitate interoperation with existing python scientific libraries as NumPy we provide two function to convert data arrays from/to ViSUS Array an NumPy array.

import numpy

# load a remote dataset
dataset=LoadDataset("http://atlantis.sci.utah.edu/mod_visus?dataset=BlueMarble")

# 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.getLogicBox()

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

access=dataset.createAccess()

# select the resolution we need
resolution = 21

# initialize query
query=BoxQuery(dataset, field, time,ord('r'))

query.logic_box=dataset.getLogicBox()
query.end_resolutions.push_back(resolution)

# execute the query
dataset.beginQuery(query)
dataset.executeQuery(access,query)

# convert our buffer to a numpy array.
# This is useful when we want to read data from 
# and IDX dataset and use this array with other libraries
data=Array.toNumPy(query.buffer,bSqueeze=True,bShareMem=False)