Create a Python wrapper
This commit is contained in:
parent
952f2ddea5
commit
5daa290127
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
[Bb]uild
|
||||
__pycache__
|
17
python/test.py
Normal file
17
python/test.py
Normal file
@ -0,0 +1,17 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
# \file %filename%.py
|
||||
# \brief TODO
|
||||
# \author Florent Guiotte <florent.guiotte@gmail.com>
|
||||
# \version 0.1
|
||||
# \date 22 mars 2018
|
||||
#
|
||||
# TODO details
|
||||
|
||||
import triskele
|
||||
import numpy as np
|
||||
|
||||
A = np.arange(10*10).reshape(10,10)
|
||||
|
||||
t = triskele.Triskele(A)
|
||||
print (t.filter())
|
83
python/triskele/Triskele.py
Normal file
83
python/triskele/Triskele.py
Normal file
@ -0,0 +1,83 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
# \file Triskele.py
|
||||
# \brief TODO
|
||||
# \author Florent Guiotte <florent.guiotte@gmail.com>
|
||||
# \version 0.1
|
||||
# \date 22 mars 2018
|
||||
#
|
||||
# TODO details
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
import numpy as np
|
||||
import libtiff
|
||||
|
||||
class Triskele:
|
||||
def __init__(self, raster, dtype=np.uint8, cache_dir='/tmp', verbose=True):
|
||||
|
||||
self.verbose = verbose
|
||||
self.triskele_bin = Path(os.path.dirname(os.path.realpath(__file__))\
|
||||
+ '/../../build/out/apGenerator')
|
||||
self.cache_dir = Path(cache_dir)
|
||||
|
||||
if not self.triskele_bin.exists():
|
||||
raise EnvironmentError ('TRISKELE bin not found: {}'.format(self.triskele_bin))
|
||||
|
||||
if not self.cache_dir.exists():
|
||||
raise EnvironmentError ('Cache directory not found: {}'.format(self.cache_dir))
|
||||
|
||||
self.infile = self.cache_dir.joinpath('infile.tif')
|
||||
self.outfile = self.cache_dir.joinpath('outfile.tif')
|
||||
|
||||
self.areafile = self.cache_dir.joinpath('areafile.txt')
|
||||
self.sdfile = self.cache_dir.joinpath('sdfile.txt')
|
||||
self.moifile = self.cache_dir.joinpath('moifile.txt')
|
||||
|
||||
self._write_infile(raster, dtype)
|
||||
|
||||
def filter(self, tree='max-tree', area=None, standard_deviation=None, moment_of_inertia=None):
|
||||
self._setup(tree, area, standard_deviation, moment_of_inertia)
|
||||
self._run()
|
||||
return self._read_outfile()
|
||||
|
||||
def _read_outfile(self):
|
||||
return libtiff.TIFF.open(self.outfile).read_image()
|
||||
|
||||
def _write_infile(self, raster, dtype):
|
||||
## Scale to new dtype
|
||||
rep = np.iinfo(dtype)
|
||||
|
||||
raster = raster.astype(np.float32)
|
||||
raster -= raster.min() - rep.min
|
||||
raster *= (rep.max - rep.min) / (raster.max() - raster.min())
|
||||
raster = raster.astype(dtype)
|
||||
|
||||
tif = libtiff.TIFFimage(raster, description='TRISKELE Python wrapper')
|
||||
tif.write_file(self.infile, verbose=self.verbose, compression=None) # 'lzw'
|
||||
|
||||
def _setup(self, tree, area, standard_deviation, moment_of_inertia):
|
||||
self.process = [self.triskele_bin,
|
||||
'-i', '{}'.format(self.infile),
|
||||
'-o', '{}'.format(self.outfile),
|
||||
'--{}'.format(tree)]
|
||||
|
||||
if area is not None:
|
||||
np.savetxt(self.areafile, area, fmt='%d')
|
||||
self.process.extend(['--area', '{}'.format(self.areafile)])
|
||||
|
||||
if standard_deviation is not None:
|
||||
np.savetxt(self.sdfile, standard_deviation, fmt='%f')
|
||||
self.process.extend(['--standard-deviation', '{}'.format(self.sdfile)])
|
||||
|
||||
if moment_of_inertia is not None:
|
||||
np.savetxt(self.moifile, moment_of_inertia, fmt='%f')
|
||||
self.process.extend(['--moment-of-inertia', '{}'.format(self.moifile)])
|
||||
|
||||
def _run(self):
|
||||
|
||||
tryskele = subprocess.Popen(self.process, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
if self.verbose:
|
||||
print('STDOUT:\n' + tryskele.stdout.read().decode() + \
|
||||
'\nSTDERR:\n' + tryskele.stderr.read().decode())
|
1
python/triskele/__init__.py
Normal file
1
python/triskele/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .Triskele import Triskele
|
Loading…
Reference in New Issue
Block a user