From 571a15afea990b502a69c4e589c448e7e68a429b Mon Sep 17 00:00:00 2001 From: "Olivier J.N. Bertrand" <olivier.bertrand@uni-bielefeld.de> Date: Sat, 13 Jan 2018 23:28:40 +0100 Subject: [PATCH] Update rendering example to avoid duplicate code --- doc/source/blender_run.py | 37 ++++++++++++++++--- .../rendering/blenddemo_beesampling.py | 25 ------------- .../example/rendering/blenddemo_cyberbee.py | 31 ++-------------- navipy/rendering/__init__.py | 2 - navipy/rendering/bee_sampling.py | 2 +- 5 files changed, 37 insertions(+), 60 deletions(-) diff --git a/doc/source/blender_run.py b/doc/source/blender_run.py index b5ffd7d..8b7c151 100644 --- a/doc/source/blender_run.py +++ b/doc/source/blender_run.py @@ -4,12 +4,39 @@ Run several blender instances if blender is installed import pkg_resources import glob import os +import shutil +import sys +import inspect +import tempfile -blendercomand = 'blender' + +def activate_virtualenv(venv_path): + if venv_path is None: + raise NameError('Python is not running within a virtualenv') + filepath = os.path.join(venv_path, 'bin', 'activate_this.py') + with open(filepath, 'r') as f: + exec(f.read(), dict(__file__=filepath)) + + +venv = sys.base_prefix +print(venv) +blendercomand = shutil.which('blender') worldfile = pkg_resources.resource_filename( 'navipy', 'resources/forest_world.blend') +encoding = 'utf-8' for demofile in glob.iglob('**/blenddemo*.py', recursive=True): - command = '{} {} --background --python {}'.format(blendercomand, - worldfile, - demofile) - os.system(command) + with tempfile.NamedTemporaryFile() as tfile: + tfile.write('import os \n'.encode(encoding)) + for line in inspect.getsourcelines(activate_virtualenv)[0]: + tfile.write(line.encode(encoding)) + line = 'activate_virtualenv(\"{}\")\n'.format(venv) + tfile.write(line.encode(encoding)) + with open(demofile) as infile: + for line in infile: + tfile.write(line.encode(encoding)) + tfile.seek(0) + print(tfile.read()) + tfile.seek(0) + command = '{} {} --background --python {}'.format( + blendercomand, worldfile, tfile.name) + os.system(command) diff --git a/doc/source/example/rendering/blenddemo_beesampling.py b/doc/source/example/rendering/blenddemo_beesampling.py index 1d429e1..1ea027c 100644 --- a/doc/source/example/rendering/blenddemo_beesampling.py +++ b/doc/source/example/rendering/blenddemo_beesampling.py @@ -1,32 +1,7 @@ """ Example on how to use the rendering module """ -# Import module from blender -import os import tempfile - - -environment_name = 'toolbox-navigation' - - -def activate_virtualenv(name): - """Activate given virtualenv. - Virtualenv home folder is given by environment variable - ``WORKON_HOME`` or ``~/Envs`. - """ - if 'WORKON_HOME' in os.environ: - home = os.environ['WORKON_HOME'] - else: - home = os.path.expanduser(os.path.join('~', 'Envs')) - filepath = os.path.join(home, name, 'bin', 'activate_this.py') - with open(filepath, 'r') as f: - exec(f.read(), dict(__file__=filepath)) - - -activate_virtualenv(environment_name) -# -# Import module in virtualenv -# import numpy as np # noqa: E402 from navipy.rendering.bee_sampling import BeeSampling # noqa: E402 diff --git a/doc/source/example/rendering/blenddemo_cyberbee.py b/doc/source/example/rendering/blenddemo_cyberbee.py index 19ab95e..badfa9f 100644 --- a/doc/source/example/rendering/blenddemo_cyberbee.py +++ b/doc/source/example/rendering/blenddemo_cyberbee.py @@ -1,30 +1,7 @@ -import os -proci = 0 -nproc = 1 - - -def activate_virtualenv(name): - """Activate given virtualenv. - Virtualenv home folder is given by environment variable ``WORKON_HOME`` or - ``~/Envs`. - """ - if 'WORKON_HOME' in os.environ: - home = os.environ['WORKON_HOME'] - else: - home = os.path.expanduser(os.path.join('~', 'Envs')) - filepath = os.path.join(home, name, 'bin', 'activate_this.py') - with open(filepath, 'r') as f: - exec(f.read(), dict(__file__=filepath)) - - -activate_virtualenv('toolbox-navigation') -# -# Import module in virtualenv -# -import numpy as np # noqa: E402 -from matplotlib.colors import hsv_to_rgb, rgb_to_hsv # noqa: E402 -import matplotlib.pyplot as plt # noqa: E402 -from navipy.rendering.cyber_bee import Cyberbee # noqa: E402 +import numpy as np +from matplotlib.colors import hsv_to_rgb, rgb_to_hsv +import matplotlib.pyplot as plt +from navipy.rendering.cyber_bee import Cyberbee # with tempfile.TemporaryDirectory() as folder: cyberbee = Cyberbee() diff --git a/navipy/rendering/__init__.py b/navipy/rendering/__init__.py index 1c1a00c..5c237d7 100644 --- a/navipy/rendering/__init__.py +++ b/navipy/rendering/__init__.py @@ -39,14 +39,12 @@ Custom sampling Rendering classes ----------------- - BeeSampling ~~~~~~~~~~~ .. autoclass:: navipy.rendering.bee_sampling.BeeSampling :members: Cyberbee - ~~~~~~~~ .. autoclass:: navipy.rendering.cyber_bee.Cyberbee :members: diff --git a/navipy/rendering/bee_sampling.py b/navipy/rendering/bee_sampling.py index 62e0482..5245b5c 100644 --- a/navipy/rendering/bee_sampling.py +++ b/navipy/rendering/bee_sampling.py @@ -158,7 +158,7 @@ harddrive space, as each image is composed of 4 channels of 180x360 pixels. if np.any(np.isnan(posorient)): # warnings.warn('frame_i: {} posorient nans'.format(frame_i)) continue - rowid = dataloger.posid(posorient) + rowid = dataloger.get_posid(posorient) if dataloger.check_data_validity(rowid): warnings.warn( 'frame_i: {} data is valid rowid {}'.format(frame_i, -- GitLab