Skip to content

Commit

Permalink
new tag
Browse files Browse the repository at this point in the history
  • Loading branch information
scrgiorgio committed Aug 31, 2019
1 parent 19cd523 commit f36cf01
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ if (VISUS_PYTHON)
AddOpenVisusTest(VisusTestPyDataflow COMMAND "${PYTHON_EXECUTABLE}" "Samples/python/Dataflow.py" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
AddOpenVisusTest(VisusTestPyArray COMMAND "${PYTHON_EXECUTABLE}" "Samples/python/Array.py" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
AddOpenVisusTest(VisusTestPyIdx COMMAND "${PYTHON_EXECUTABLE}" "Samples/python/Idx.py" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
AddOpenVisusTest(VisusTestPyXIdx COMMAND "${PYTHON_EXECUTABLE}" "Samples/python/Idx.py" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
AddOpenVisusTest(VisusTestPyXIdx COMMAND "${PYTHON_EXECUTABLE}" "Samples/python/XIdx.py" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

AddOpenVisusTest(VisusDataConversion1 COMMAND "${PYTHON_EXECUTABLE}" "Samples/python/DataConversion1.py" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
AddOpenVisusTest(VisusDataConversion2 COMMAND "${PYTHON_EXECUTABLE}" "Samples/python/DataConversion2.py" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Expand Down
4 changes: 1 addition & 3 deletions Samples/python/Array.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ def test4(self):
print("All done")
SetCommandLine("__main__")
KernelModule.attach()
errors=unittest.main(exit=False).result.errors
unittest.main(exit=True)
KernelModule.detach()
print("All done ("+str(len(errors))+" errors)")
sys.exit(len(errors)>0)


3 changes: 2 additions & 1 deletion Samples/python/DataConversion1.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@
# ArrayUtils.saveImageUINT8("tmp/slice%d.offset.png" % (Z,),Array.fromNumPy(fill))

IdxModule.detach()
print("Done with conversion")
print("Done with conversion")
sys.exit(0)
123 changes: 122 additions & 1 deletion Samples/python/DataConversion2.py
Original file line number Diff line number Diff line change
@@ -1 +1,122 @@
import sysimport numpy as npfrom OpenVisus import *if __name__ == '__main__': """ Example of data conversion Input is composed of 3 images, one for each channel (RGB) each channel it's a 3d array Ouput is an IDX For each source slice the original data is shifted by 5 pixels: first slice is shifted by ( 0,0) second slice is shifted by ( 5,0) third slice is shifted by (10,0) """ SetCommandLine("__main__") IdxModule.attach() # trick to speed up the conversion os.environ["VISUS_DISABLE_WRITE_LOCK"]="1" # 3 channels (RGB) each one is a 3d array width,height,depth=2,3,4 channels = [ np.zeros((depth,height,width),dtype=np.uint8) , np.zeros((depth,height,width),dtype=np.uint8) , np.zeros((depth,height,width),dtype=np.uint8) , ] idx_filename='visus.idx' # for each slize along Z axis I shift the original data of offset[0],offset[1],offset[2] (like a shear) offset = (5,0,0) dataset,access,fields=None,None,None for ChannelId,channel in enumerate(channels): # scrgiorgio: just for testing, if I get a 2d image instead of a 3d image stack I simulate a stack if len(channel.shape)==2: channel=np.stack([channel]*7) dims=list(reversed(channel.shape)) # numpy dtype -> OpenVisus dtype typestr=channel.__array_interface__["typestr"] dtype=DType(typestr[1]=="u", typestr[1]=="f", int(typestr[2])*8) idx_logic_box=BoxNi(PointNi(0,0,0),PointNi( dims[0]+ offset[0]*dims[2], dims[1]+ offset[1]*dims[2], dims[2]+ offset[2]*dims[2])) # create the idx if ChannelId==0: print("Creating idx file") idx_file = IdxFile() idx_file.logic_box=idx_logic_box print("\tbox",idx_logic_box.toString()) # assuming all the files are the same type for I in range(len(channels)): field=Field('channel_%d' % (I,),dtype) print("\tfield",field.name,field.dtype.toString()) idx_file.fields.push_back(field) if not idx_file.save(idx_filename): raise Exception("Cannot save idx") dataset = LoadDataset(idx_filename) if not dataset: raise Exception("Cannot load idx") access = dataset.createAccess() if not access: raise Exception("Cannot create access") fields=idx_file.fields else: # check dimensions and dtype are compatible if not (dataset.getLogicBox()==idx_logic_box): raise Exception("Logic box is wrong") if not (dtype==fields[ChannelId].dtype): raise Exception("dtype is wrong") print("Adding","channel",ChannelId,"dims",dims,"dtype",dtype.toString()) # Now we go through each slice and add them one by one since we have to take care of the offset for Z in range(0,dims[2]): # target area in idx (i.e. apply a shear) x1,x2 = 0+offset[0]*Z , dims[0]+offset[0]*Z y1,y2 = 0+offset[1]*Z , dims[1]+offset[1]*Z z1,z2 = Z+offset[2]*Z , Z+1 +offset[2]*Z print("\tProcessing Z",Z,"target_p1",(x1, y1, z1),"target_p2",(x2, y2, z2)) query = BoxQuery(dataset,dataset.getDefaultField(),dataset.getDefaultTime(),ord('w')) query.logic_box = BoxNi(PointNi(x1,y1,z1),PointNi(x2,y2,z2)) query.field = fields[ChannelId] dataset.beginQuery(query) if not query.isRunning(): raise Exception("Begin query failed") query.buffer=Array.fromNumPy(channel[Z,:,:],bShareMem=True) if not dataset.executeQuery(access,query): raise Exception("Cannot execute query") IdxModule.detach() print("Done with conversion")
import sys
import numpy as np
from OpenVisus import *


if __name__ == '__main__':

"""
Example of data conversion
Input is composed of 3 images, one for each channel (RGB)
each channel it's a 3d array
Ouput is an IDX
For each source slice the original data is shifted by 5 pixels:
first slice is shifted by ( 0,0)
second slice is shifted by ( 5,0)
third slice is shifted by (10,0)
"""

SetCommandLine("__main__")
IdxModule.attach()

# trick to speed up the conversion
os.environ["VISUS_DISABLE_WRITE_LOCK"]="1"

# 3 channels (RGB) each one is a 3d array
width,height,depth=2,3,4

channels = [
np.zeros((depth,height,width),dtype=np.uint8) ,
np.zeros((depth,height,width),dtype=np.uint8) ,
np.zeros((depth,height,width),dtype=np.uint8) ,
]

idx_filename='visus.idx'

# for each slize along Z axis I shift the original data of offset[0],offset[1],offset[2] (like a shear)
offset = (5,0,0)

dataset,access,fields=None,None,None

for ChannelId,channel in enumerate(channels):

# scrgiorgio: just for testing, if I get a 2d image instead of a 3d image stack I simulate a stack
if len(channel.shape)==2:
channel=np.stack([channel]*7)

dims=list(reversed(channel.shape))

# numpy dtype -> OpenVisus dtype
typestr=channel.__array_interface__["typestr"]
dtype=DType(typestr[1]=="u", typestr[1]=="f", int(typestr[2])*8)

idx_logic_box=BoxNi(PointNi(0,0,0),PointNi(
dims[0]+ offset[0]*dims[2],
dims[1]+ offset[1]*dims[2],
dims[2]+ offset[2]*dims[2]))

# create the idx
if ChannelId==0:
print("Creating idx file")
idx_file = IdxFile()
idx_file.logic_box=idx_logic_box
print("\tbox",idx_logic_box.toString())

# assuming all the files are the same type
for I in range(len(channels)):
field=Field('channel_%d' % (I,),dtype)
print("\tfield",field.name,field.dtype.toString())
idx_file.fields.push_back(field)

if not idx_file.save(idx_filename):
raise Exception("Cannot save idx")

dataset = LoadDataset(idx_filename)
if not dataset: raise Exception("Cannot load idx")

access = dataset.createAccess()
if not access: raise Exception("Cannot create access")

fields=idx_file.fields

else:
# check dimensions and dtype are compatible
if not (dataset.getLogicBox()==idx_logic_box):
raise Exception("Logic box is wrong")

if not (dtype==fields[ChannelId].dtype):
raise Exception("dtype is wrong")

print("Adding","channel",ChannelId,"dims",dims,"dtype",dtype.toString())

# Now we go through each slice and add them one by one since we have to take care of the offset
for Z in range(0,dims[2]):

# target area in idx (i.e. apply a shear)
x1,x2 = 0+offset[0]*Z , dims[0]+offset[0]*Z
y1,y2 = 0+offset[1]*Z , dims[1]+offset[1]*Z
z1,z2 = Z+offset[2]*Z , Z+1 +offset[2]*Z

print("\tProcessing Z",Z,"target_p1",(x1, y1, z1),"target_p2",(x2, y2, z2))

query = BoxQuery(dataset,dataset.getDefaultField(),dataset.getDefaultTime(),ord('w'))
query.logic_box = BoxNi(PointNi(x1,y1,z1),PointNi(x2,y2,z2))
query.field = fields[ChannelId]
dataset.beginQuery(query)

if not query.isRunning():
raise Exception("Begin query failed")

query.buffer=Array.fromNumPy(channel[Z,:,:],bShareMem=True)

if not dataset.executeQuery(access,query):
raise Exception("Cannot execute query")


IdxModule.detach()
print("Done with conversion")
sys.exit(0)
4 changes: 1 addition & 3 deletions Samples/python/Dataflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ def testDataflow(self):
DataflowModule.attach()
VISUS_REGISTER_NODE_CLASS("PyProducer")
VISUS_REGISTER_NODE_CLASS("PyReceiver")
errors=unittest.main(exit=False).result.errors
unittest.main(exit=True)
DataflowModule.detach()
print("All done ("+str(len(errors))+" errors)")
sys.exit(len(errors)>0)

26 changes: 12 additions & 14 deletions Samples/python/Idx.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def WriteIdx(self):
dataset_box=BoxNi(PointNi(0,0,0),PointNi(16,16,16))

idxfile=IdxFile();
idxfile.box=BoxNi(dataset_box)
idxfile.logic_box=BoxNi(dataset_box)
idxfile.fields.push_back(Field("myfield",DType.fromString("uint32")))

bSaved=idxfile.save(self.filename)
Expand All @@ -78,12 +78,12 @@ def WriteIdx(self):
slice_box=dataset.getLogicBox().getZSlab(Z,Z+1)

query=BoxQuery(dataset,dataset.getDefaultField(),dataset.getDefaultTime(),ord('w'))
query.logic_position=Position(slice_box)
query.logic_box=slice_box
dataset.beginQuery(query)
self.assertTrue(query.isRunning())
self.assertEqual(query.nsamples.innerProduct(),16*16)
self.assertEqual(query.getNumberOfSamples().innerProduct(),16*16)

buffer=Array(query.nsamples,query.field.dtype)
buffer=Array(query.getNumberOfSamples(),query.field.dtype)
query.buffer=buffer

fill=Array.toNumPy(buffer,bSqueeze=True,bShareMem=True)
Expand All @@ -108,10 +108,10 @@ def ReadIdx(self):
slice_box=box.getZSlab(Z,Z+1)

query=BoxQuery(dataset,dataset.getDefaultField(),dataset.getDefaultTime(),ord('r'))
query.logic_position=Position(slice_box)
query.logic_box=slice_box
dataset.beginQuery(query)
self.assertTrue(query.isRunning())
self.assertEqual(query.nsamples.innerProduct(),16*16)
self.assertEqual(query.getNumberOfSamples().innerProduct(),16*16)
self.assertTrue(dataset.executeQuery(access,query))

check=Array.toNumPy(query.buffer,bSqueeze=True,bShareMem=True)
Expand All @@ -136,20 +136,21 @@ def MergeIdx(self):

#create and read data from VisusFIle up to resolution FinalH=8 (<MaxH)
query=BoxQuery(dataset,dataset.getDefaultField(),dataset.getDefaultTime(),ord('r'))
query.logic_position=Position(slice_box)
query.end_resolutions={8,12}
query.logic_box=slice_box
query.end_resolutions.push_back(8)
query.end_resolutions.push_back(12)

# end_resolution=8
dataset.beginQuery(query)
self.assertTrue(query.isRunning())
self.assertTrue(query.nsamples.innerProduct()>0)
self.assertTrue(query.getNumberOfSamples().innerProduct()>0)
self.assertTrue(dataset.executeQuery(access,query))
self.assertEqual(query.getCurrentResolution(),8)

# end_resolution=12
dataset.nextQuery(query)
self.assertTrue(query.isRunning())
self.assertEqual(query.nsamples.innerProduct(),16*16)
self.assertEqual(query.getNumberOfSamples().innerProduct(),16*16)
self.assertTrue(dataset.executeQuery(access,query))
self.assertEqual(query.getCurrentResolution(),12)

Expand All @@ -166,8 +167,5 @@ def MergeIdx(self):
if __name__ == '__main__':
SetCommandLine("__main__")
IdxModule.attach()
errors=unittest.main(exit=False).result.errors
unittest.main(exit=True)
IdxModule.detach()
print("All done ("+str(len(errors))+" errors)")
sys.exit(len(errors)>0)

1 change: 1 addition & 0 deletions Samples/python/Server.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ def runSingleDatasetServer(name,idx_filename,port=10000,dynamic=False):
else:
IdxModule.detach()

sys.exit(0)


2 changes: 1 addition & 1 deletion Samples/python/XIdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,6 @@ def VerifyXIdx(self):
if __name__ == '__main__':
SetCommandLine("__main__")
XIdxModule.attach()
unittest.main(exit=False)
unittest.main(exit=True)
XIdxModule.detach()
print("All done")
2 changes: 1 addition & 1 deletion conda/openvisus/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package:
name: openvisus
version: 1.3.65
version: 1.3.66

source:
path: ../..
Expand Down
2 changes: 1 addition & 1 deletion datasets/google_and_slam.midx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


<!-- already in 0,1 range -->
<dataset name='slam' url='D:/google_sci/visus_slam/Alfalfa/VisusSlamFiles/visus.midx' />
<dataset name='slam' url='D:/GoogleSci/visus_slam/Alfalfa/VisusSlamFiles/visus.midx' />

</dataset>

0 comments on commit f36cf01

Please sign in to comment.