<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wiki.visus.org/index.php?action=history&amp;feed=atom&amp;title=ViSUS_Python</id>
	<title>ViSUS Python - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.visus.org/index.php?action=history&amp;feed=atom&amp;title=ViSUS_Python"/>
	<link rel="alternate" type="text/html" href="http://wiki.visus.org/index.php?title=ViSUS_Python&amp;action=history"/>
	<updated>2026-04-25T23:03:32Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.0</generator>
	<entry>
		<id>http://wiki.visus.org/index.php?title=ViSUS_Python&amp;diff=445&amp;oldid=prev</id>
		<title>Admin: Created page with &quot;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 ==...&quot;</title>
		<link rel="alternate" type="text/html" href="http://wiki.visus.org/index.php?title=ViSUS_Python&amp;diff=445&amp;oldid=prev"/>
		<updated>2019-03-05T00:31:20Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;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 ==...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;ViSUSpy is a library that allows the user to use the ViSUS framework from python.&lt;br /&gt;
&lt;br /&gt;
In the following sections we provide some examples on how to use it.&lt;br /&gt;
&lt;br /&gt;
== Library imports == &lt;br /&gt;
&lt;br /&gt;
To import and use the library in your python application you need to use the following imports:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from visuspy import *&lt;br /&gt;
from VisusKernelPy import *&lt;br /&gt;
from VisusIdxPy    import *&lt;br /&gt;
from VisusDbPy     import *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These modules allow the basic interactions with IDX datasets, which can be either local or remote (served by a [[ViSUS Server]]).&lt;br /&gt;
&lt;br /&gt;
'''Important''': your code that interacts with IDX datasets has always to attach the ''IdxModule'' first, as following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
IdxModule.attach()&lt;br /&gt;
# your program code&lt;br /&gt;
IdxModule.detach()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== NumPy utils ===&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import numpy&lt;br /&gt;
&lt;br /&gt;
# convert our buffer to a numpy array.&lt;br /&gt;
# This is useful when we want to read data from &lt;br /&gt;
# and IDX dataset and use this array with other libraries&lt;br /&gt;
numpy_array=convertToNumPyArray(in_visus_array)&lt;br /&gt;
&lt;br /&gt;
# convert a numpy array to a ViSUS array&lt;br /&gt;
visus_array = convertToVisusArray(numpy_array)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Write data in IDX format ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; line='line'&amp;gt;&lt;br /&gt;
IdxModule.attach()&lt;br /&gt;
&lt;br /&gt;
# define a box which defines the bounding box&lt;br /&gt;
# of the grid where our data will be stored&lt;br /&gt;
# a box is defined by two points (p1 and p2)&lt;br /&gt;
# which define the two opposite corners of the box&lt;br /&gt;
# in this case the box has size 16x16x16&lt;br /&gt;
dataset_box=NdBox(NdPoint(0,0,0),NdPoint.one(16,16,16))&lt;br /&gt;
    &lt;br /&gt;
# create and IDX file where to to store the data&lt;br /&gt;
idxfile=IdxFile();&lt;br /&gt;
&lt;br /&gt;
# set dataset box&lt;br /&gt;
idxfile.box=NdBox(dataset_box)&lt;br /&gt;
&lt;br /&gt;
# add integer field to the dataset&lt;br /&gt;
idxfile.fields.push_back(Field(&amp;quot;myfield&amp;quot;,DType.fromString(&amp;quot;uint32&amp;quot;)))&lt;br /&gt;
&lt;br /&gt;
# save the metadata on disk (no data are saved yet)&lt;br /&gt;
bSaved=idxfile.save(self.filename)&lt;br /&gt;
self.assertTrue(bSaved)&lt;br /&gt;
&lt;br /&gt;
# load the dataset we just created    &lt;br /&gt;
dataset=Dataset.loadDataset(self.filename)&lt;br /&gt;
self.assertIsNotNone(dataset)&lt;br /&gt;
&lt;br /&gt;
# create access to write the data&lt;br /&gt;
access=dataset.get().createAccess()&lt;br /&gt;
&lt;br /&gt;
# write data slice by slice    &lt;br /&gt;
sampleid=0    &lt;br /&gt;
for Z in range(0,16):&lt;br /&gt;
  # create box to hold the Z slice of the data&lt;br /&gt;
  slice_box=dataset.get().getBox().getZSlab(Z,Z+1)&lt;br /&gt;
  &lt;br /&gt;
  # create write query &lt;br /&gt;
  query=QueryPtr(Query(dataset.get(),ord('w')))&lt;br /&gt;
  # set query box&lt;br /&gt;
  query.get().position=Position(slice_box)&lt;br /&gt;
      &lt;br /&gt;
  # initialize query&lt;br /&gt;
  self.assertTrue(dataset.get().beginQuery(query))&lt;br /&gt;
  # check if size of the query is equal to the dimension of our dataset&lt;br /&gt;
  self.assertEqual(query.get().nsamples.innerProduct(),16*16)&lt;br /&gt;
  &lt;br /&gt;
  # create a Visus Array to store the data&lt;br /&gt;
  buffer=Array(query.get().nsamples,query.get().field.dtype)&lt;br /&gt;
  # set the output buffer of the query to the array we just created&lt;br /&gt;
  query.get().buffer=buffer&lt;br /&gt;
      &lt;br /&gt;
  # convert our buffer to a numpy array &lt;br /&gt;
  fill=convertToNumPyArray(buffer)&lt;br /&gt;
&lt;br /&gt;
  # fill up the array with some values&lt;br /&gt;
  for Y in range(16):&lt;br /&gt;
    for X in range(16):&lt;br /&gt;
      fill[Y,X]=sampleid&lt;br /&gt;
      sampleid+=1&lt;br /&gt;
&lt;br /&gt;
# execute query&lt;br /&gt;
success = dataset.get().executeQuery(access,query)&lt;br /&gt;
&lt;br /&gt;
IdxModule.detach()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Read from IDX file == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; line='line'&amp;gt;&lt;br /&gt;
IdxModule.attach()&lt;br /&gt;
&lt;br /&gt;
# open an IDX dataset&lt;br /&gt;
dataset=Dataset_loadDataset(self.filename)&lt;br /&gt;
# check it is valid&lt;br /&gt;
self.assertIsNotNone(dataset)&lt;br /&gt;
&lt;br /&gt;
# get the bounding box of the dataset &lt;br /&gt;
# the box is defined by a set of 2 points (p1 and p2)&lt;br /&gt;
# which define the two corners of a bounding box&lt;br /&gt;
box=dataset.get().getBox()&lt;br /&gt;
&lt;br /&gt;
# get the default field of a dataset&lt;br /&gt;
field=dataset.get().getDefaultField()&lt;br /&gt;
&lt;br /&gt;
#create access&lt;br /&gt;
access=dataset.get().createAccess()&lt;br /&gt;
&lt;br /&gt;
# here we read the datasets slice by slice&lt;br /&gt;
# making box queries &lt;br /&gt;
sampleid=0&lt;br /&gt;
for Z in range(0,16):&lt;br /&gt;
  slice_box=box.getZSlab(Z,Z+1)&lt;br /&gt;
      &lt;br /&gt;
  # define a read query&lt;br /&gt;
  query=QueryPtr(Query(dataset.get(),ord('r')))&lt;br /&gt;
  # set the box for the query&lt;br /&gt;
  query.get().position=Position(slice_box)&lt;br /&gt;
      &lt;br /&gt;
  self.assertTrue(dataset.get().beginQuery(query))&lt;br /&gt;
  # check the size of the output of the query &lt;br /&gt;
  print(&amp;quot;query size&amp;quot;, query.get().nsamples.innerProduct())&lt;br /&gt;
  # execute query&lt;br /&gt;
  self.assertTrue(dataset.get().executeQuery(access,query))&lt;br /&gt;
  &lt;br /&gt;
  # convert output buffer to a numpy array&lt;br /&gt;
  my_numpy_array=convertToNumPyArray(query.get().buffer)&lt;br /&gt;
&lt;br /&gt;
IdxModule.detach()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
		
	</entry>
</feed>