Difference between revisions of "ViSUSpy"

From
Jump to: navigation, search
(Read from IDX file)
(NumPy utils)
 
(30 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 7: Line 13:
 
To import and use the library in your python application you need to use the following imports:
 
To import and use the library in your python application you need to use the following imports:
  
  from visuspy import *
+
<syntaxhighlight lang="python">
  from VisusKernelPy import *
+
from OpenVisus import *
  from VisusIdxPy    import *
+
</syntaxhighlight>
  from VisusDbPy    import *
 
  
 
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">
 +
DbModule.attach()
 +
# your program code
 +
DbModule.detach()
 +
</syntaxhighlight>
 +
 
 +
=== 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.
 +
 
 +
<syntaxhighlight lang="python">
 +
import numpy
 +
 
 +
# load a remote dataset
 +
dataset=LoadDataset("http://atlantis.sci.utah.edu/mod_visus?dataset=BlueMarble")
  
  IdxModule.attach()
+
# get the bounding box of the dataset
  # your program code
+
# the box is defined by a set of 2 points (p1 and p2)
  IdxModule.detach()
+
# which define the two corners of a bounding box
 +
box=dataset.getLogicBox()
  
== Write data to an IDX file ==
+
# get the default field of a dataset
 +
field=dataset.getDefaultField()
  
    IdxModule.attach()
+
access=dataset.createAccess()
  
    dataset_box=NdBox(NdPoint(0,0,0),NdPoint.one(16,16,16))
+
# select the resolution we need
   
+
resolution = 21
    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()
+
# initialize query
 +
query=BoxQuery(dataset, field, time,ord('r'))
  
== Read from IDX file ==
+
query.logic_box=dataset.getLogicBox()
 +
query.end_resolutions.push_back(resolution)
  
    IdxModule.attach()
+
# execute the query
 +
dataset.beginQuery(query)
 +
dataset.executeQuery(access,query)
  
    dataset=Dataset_loadDataset(self.filename)
+
# convert our buffer to a numpy array.
    self.assertIsNotNone(dataset)
+
# This is useful when we want to read data from
    box=dataset.get().getBox()
+
# and IDX dataset and use this array with other libraries
    field=dataset.get().getDefaultField()
+
data=Array.toNumPy(query.buffer,bSqueeze=True,bShareMem=False)
    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()
+
</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)