Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render set: helper refactoring #34

Merged
merged 10 commits into from
Nov 29, 2019
42 changes: 28 additions & 14 deletions src/omero_cli_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,21 +584,28 @@ def _read_default_planes(self, img, data, ignore_errors=False):
def_t = None
return (def_z, def_t)

@gateway_required
def set(self, args):
""" Implements the 'set' command """
newchannels = {}
data = pydict_text_io.load(
args.channels, session=self.client.getSession())
def _load_rendering_settings(self, source, session=None):
"""Load a rendering dictionary from a source (file or object)"""
try:
data = pydict_text_io.load(source, session=session)
except Exception as e:
self.ctx.dbg(e)
self.ctx.die(103, "Could not read %s" % source)

if 'channels' not in data:
self.ctx.die(104, "ERROR: No channels found in %s" % args.channels)
self.ctx.die(104, "ERROR: No channels found in %s" % source)

version = _getversion(data)
if version == 0:
self.ctx.die(124, "ERROR: Cannot determine version. Specify"
" version or use either start/end or min/max"
" (not both).")
return data

def _read_channels(self, data):
"""Read new channels from settings dictionary"""
newchannels = {}
version = _getversion(data)
# Read channel setttings from rendering dictionary
for chindex, chdict in data['channels'].items():
try:
Expand All @@ -611,18 +618,12 @@ def set(self, args):
try:
cobj = ChannelObject(chdict, version)
newchannels[cindex] = cobj
print('%d:%s' % (cindex, cobj))
self.ctx.dbg('%d:%s' % (cindex, cobj))
except Exception as e:
self.ctx.err('ERROR: %s' % e)
self.ctx.die(
105, "Invalid channel description: %s" % chdict)

try:
greyscale = data['greyscale']
print('greyscale=%s' % data['greyscale'])
except KeyError:
greyscale = None

namedict = {}
cindices = []
rangelist = []
Expand All @@ -636,11 +637,21 @@ def set(self, args):
cindices.append(i)
rangelist.append([c.start, c.end])
colourlist.append(c.color)
return (namedict, cindices, rangelist, colourlist)

@gateway_required
def set(self, args):
""" Implements the 'set' command """
data = self._load_rendering_settings(
args.channels, session=self.client.getSession())

iids = []
for img in self.render_images(self.gateway, args.object, batch=1):
iids.append(img.id)

# Extract settings from dictionary
(namedict, cindices, rangelist, colourlist) = self._read_channels(
data)
sbesson marked this conversation as resolved.
Show resolved Hide resolved
(def_z, def_t) = self._read_default_planes(
img, data, ignore_errors=args.ignore_errors)

Expand All @@ -657,7 +668,10 @@ def set(self, args):

img.set_active_channels(
cindices, windows=rangelist, colors=colourlist)

greyscale = data.get('greyscale', None)
if greyscale is not None:
self.ctx.dbg('greyscale=%s' % greyscale)
if greyscale:
img.setGreyscaleRenderingModel()
else:
Expand Down
85 changes: 85 additions & 0 deletions test/unit/test_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# Copyright (C) 2015-2018 University of Dundee & Open Microscopy Environment.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


from omero.cli import CLI, NonZeroReturnCode
from omero_cli_render import RenderControl

import pytest
import uuid
import yaml


def write_yaml(d, tmpdir):
f = tmpdir.join(str(uuid.uuid4()) + ".yml")
f.write(yaml.dump(d, explicit_start=True, width=80, indent=4,
default_flow_style=False))
return str(f)


class TestLoadRenderingSettings:
def setup_method(self):
self.cli = CLI()
self.cli.register("render", RenderControl, "TEST")
self.render = self.cli.controls['render']

def test_none(self):
with pytest.raises(NonZeroReturnCode):
self.render._load_rendering_settings(None)

def test_non_existing_file(self):
with pytest.raises(NonZeroReturnCode) as e:
self.render._load_rendering_settings(str(uuid.uuid4()) + '.yml')
assert e.value.rv == 103

def test_no_channels(self, tmpdir):
d = {'version': 1}
f = write_yaml(d, tmpdir)
with pytest.raises(NonZeroReturnCode) as e:
self.render._load_rendering_settings(f)
assert e.value.rv == 104

def test_bad_version(self, tmpdir):
d = {'channels': {1: {'label': 'foo'}}}
f = write_yaml(d, tmpdir)
with pytest.raises(NonZeroReturnCode) as e:
self.render._load_rendering_settings(f)
assert e.value.rv == 124


class TestReadChannels:
def setup_method(self):
self.cli = CLI()
self.cli.register("render", RenderControl, "TEST")
self.render = self.cli.controls['render']

def test_non_integer_channel(self):
d = {'channels': {'GFP': {'label': 'foo'}}}
with pytest.raises(NonZeroReturnCode) as e:
self.render._read_channels(d)
assert e.value.rv == 105

@pytest.mark.parametrize('key', ['min', 'max', 'start', 'end'])
def test_float_keys(self, key):
d = {'channels': {1: {key: 'foo'}}}
with pytest.raises(NonZeroReturnCode) as e:
self.render._read_channels(d)
assert e.value.rv == 105