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

100 luminance interactor #113

Closed
wants to merge 11 commits into from
5 changes: 3 additions & 2 deletions config/little_liver_interaction.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@
},

"models": {
"models_dir": "data/models",
"models_dir": "/home/thompson/data/bard_data/Depth_Perception",
"reference_to_model": "data/reference_to_model.txt",
"visible_anatomy": 1
},
"interaction": {
"keyboard": true,
"footswitch": true,
"maximum delay": 2.0,
"mouse": true
"mouse": true,
"green": true
},
"pointer": {
"pointer_tag_to_tip": "data/pointer_tip.txt"
Expand Down
3 changes: 2 additions & 1 deletion sksurgerybard/algorithms/bard_config_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,6 @@ def configure_interaction(interaction_config, vtk_window, pointer_writer,
BardFootSwitchEvent(max_delay, bard_visualisation))

if interaction_config.get('mouse', False):
green = interaction_config.get('green', True)
vtk_window.AddObserver("LeftButtonPressEvent",
BardMouseEvent(bard_visualisation))
BardMouseEvent(bard_visualisation, green))
12 changes: 9 additions & 3 deletions sksurgerybard/interaction/interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,18 @@ def __del__(self):
class BardMouseEvent:
"""
Handles mouse events for BARD.
:param: a visualisation control object
:param: if green is true we use green colours for luminance,
otherwise we use yellow.
"""
def __init__(self, visualisation_control):
def __init__(self, visualisation_control, green=True):
self.screen_interaction_layout = {
'x_right_edge' : 0.80,
'x_left_edge' : 0.20
}

self._visualisation_control = visualisation_control
self._green = green

def __call__(self, event, _event_type_not_used):
mouse_x, mouse_y = event.GetEventPosition()
Expand All @@ -172,7 +176,9 @@ def __call__(self, event, _event_type_not_used):
mouse_y /= window_y

if mouse_x > self.screen_interaction_layout.get('x_right_edge'):
self._visualisation_control.visibility_toggle(mouse_y)
self._visualisation_control.luminance_change_right(mouse_y,
self._green)

if mouse_x < self.screen_interaction_layout.get('x_left_edge'):
self._visualisation_control.change_opacity(mouse_y)
self._visualisation_control.luminance_change_left(mouse_y,
self._green)
36 changes: 36 additions & 0 deletions sksurgerybard/visualisation/bard_visualisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np
from sksurgeryvtk.models.vtk_sphere_model import VTKSphereModel
from sksurgerybard.visualisation.colours import get_yellow, get_green

def configure_model_and_ref(configuration, transform_manager):
"""
Expand Down Expand Up @@ -239,6 +240,41 @@ def visibility_toggle(self, y_pos):
actor.SetVisibility(True)
return

def luminance_change_right(self, y_pos, green):
"""
Changes the luminance of either of the last two actors of the actors
At the moment it's hard coded to change the second to last last anatomy
actor on the green scale
"""
if len(self._target_anatomy_actors) < 2:
return
actor_index = len(self._target_anatomy_actors) - 2
if actor_index >= 0:
luminance, target_colour = get_green(y_pos)
if not green:
luminance, target_colour = get_yellow(y_pos)
self._target_anatomy_actors[
actor_index].GetProperty().SetColor(target_colour)
print(f"Changing luminance for actor {actor_index} to ",
f"{luminance}, RGB={target_colour}", flush=True)

def luminance_change_left(self, y_pos, green):
"""
Changes the luminance of either of the last two actors of the actors
At the moment it's hard coded to change the last last anatomy
actor on the yellow scale
"""
if len(self._target_anatomy_actors) < 1:
return
actor_index = len(self._target_anatomy_actors) - 1
luminance, target_colour = get_green(y_pos)
if not green:
luminance, target_colour = get_yellow(y_pos)
self._target_anatomy_actors[
actor_index].GetProperty().SetColor(target_colour)
print(f"Changing luminance for actor {actor_index} to ",
f"{luminance}, RGB={target_colour}", flush=True)

def next_target(self):
"""
turns off visibility of all targets except the next one
Expand Down
86 changes: 86 additions & 0 deletions sksurgerybard/visualisation/colours.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""
Preset colours for Athena's luminance study
"""

# from here
# Grayscale Design palette: https://grayscale.design/app

#lets create dictionaries we can use of pre set colours
grayscale = {
"50": [255, 255, 255],
"100": [242, 242, 242],
"200": [228, 228, 228],
"300": [213, 213, 213],
"400": [197, 197, 197],
"500": [178, 178, 178],
"600": [156, 156, 156],
"700": [130, 130, 130],
"800": [94, 94, 94],
"900": [0, 0, 0]
}

yellow = {
"50": [255, 255, 142],
"100": [245, 245, 0],
"200": [232, 232, 0],
"300": [217, 217, 0],
"400": [203, 203, 0],
"500": [184, 184, 0],
"600": [164, 164, 0],
"700": [141, 141, 0],
"800": [111, 111, 0],
"900": [63, 63, 0]
}

green = {
"50": [233, 255, 233],
"100": [181, 255, 181],
"200": [92, 255, 92],
"300": [0, 244, 0],
"400": [0, 227, 0],
"500": [0, 207, 0],
"600": [0, 185, 0],
"700": [0, 159, 0],
"800": [0, 125, 0],
"900": [0, 72, 0]
}

def y_pos_to_luminance(y_pos):
"""
y_pos is a float between 0 and 1 representing the position of the
mouse on the screen. High y_pos corresponds to high luminance (50)
low y_pos to low luminance (900)
"""
luminances = ["50", "100", "200", "300", "400", "500", "600",
"700", "800", "900"]
index = 0
threshold = 0.9
while index < len(luminances):
if y_pos > threshold:
return luminances[index]
index += 1
threshold -= 0.1

return luminances[index-1]


def integer_colour_to_float(colour):
"""
converts an integer colour to a float colour
"""
out=[]
for value in colour:
out.append((value)/255.0)

return out


def get_green(y_pos):
"""Gets a green shade with luminace set by y_pos"""
luminance = y_pos_to_luminance(y_pos)
return luminance, integer_colour_to_float(green.get(luminance))

def get_yellow(y_pos):
"""Gets a yellow shade with luminace set by y_pos"""
luminance = y_pos_to_luminance(y_pos)
return luminance, integer_colour_to_float(yellow.get(luminance))
14 changes: 10 additions & 4 deletions tests/interaction/test_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class TurnOnAllEvent(Exception):#pylint: disable=missing-class-docstring
pass
class VisibilityToggleEvent(Exception):#pylint: disable=missing-class-docstring
pass
class LuminanceChangeEvent(Exception):#pylint: disable=missing-class-docstring
pass
class ChangeOpacityEvent(Exception):#pylint: disable=missing-class-docstring
pass
class PositionModelEvent(Exception):#pylint: disable=missing-class-docstring
Expand Down Expand Up @@ -70,9 +72,13 @@ def turn_on_all_targets(self): # pylint: disable=no-self-use
"""Raises an error so we know when it's run"""
raise TurnOnAllEvent

def visibility_toggle(self, _): # pylint: disable=no-self-use
def luminance_change_left(self, _y_pos, _green): # pylint: disable=no-self-use
"""Raises an error so we know when it's run"""
raise LuminanceChangeEvent

def luminance_change_right(self, _y_pos, _green): # pylint: disable=no-self-use
"""Raises an error so we know when it's run"""
raise VisibilityToggleEvent
raise LuminanceChangeEvent

def change_opacity(self, _): # pylint: disable=no-self-use
"""Raises an error so we know when it's run"""
Expand Down Expand Up @@ -313,12 +319,12 @@ def test_mouse_event():

fake_mouse_event = _FakeMouseEvent([100, 100], [90, 10])

with pytest.raises(VisibilityToggleEvent):
with pytest.raises(LuminanceChangeEvent):
mouse_event(fake_mouse_event, None)

fake_mouse_event = _FakeMouseEvent([100, 100], [17, 90])

with pytest.raises(ChangeOpacityEvent):
with pytest.raises(LuminanceChangeEvent):
mouse_event(fake_mouse_event, None)

fake_mouse_event = _FakeMouseEvent([100, 100], [27, 90])
Expand Down
34 changes: 34 additions & 0 deletions tests/visualisation/test_colours.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
"""Tests for BARD pointer module"""
import sksurgerybard.visualisation.colours as cls

#pylint:disable=no-member

def test_y_pos_to_luminance():
"""Checks that the y_pos to luminance look up works"""

assert cls.y_pos_to_luminance(1.0) == "50"
assert cls.y_pos_to_luminance(0.91) == "50"
assert cls.y_pos_to_luminance(0.90) == "100"
assert cls.y_pos_to_luminance(0.80) == "200"
assert cls.y_pos_to_luminance(0.70) == "300"
assert cls.y_pos_to_luminance(0.60) == "400"
assert cls.y_pos_to_luminance(0.50) == "500"
assert cls.y_pos_to_luminance(0.40) == "600"
assert cls.y_pos_to_luminance(0.30) == "700"
assert cls.y_pos_to_luminance(0.20) == "800"
assert cls.y_pos_to_luminance(0.10) == "900"
assert cls.y_pos_to_luminance(0.00) == "900"


def test_integer_colour_to_float():
"""Tests that integer to float colour works"""
assert cls.integer_colour_to_float([255,51,0]) == [1.0, 0.2, 0.0]

def test_get_yellow():
"""Checks that get yellow returns the right value"""
assert cls.get_yellow(1.0)[1] == [1.0, 1.0, 142./255.]

def test_get_green():
"""Checks that get green returns the right value"""
assert cls.get_green(1.0)[1] == [233./255., 1.0, 233./255.]
22 changes: 22 additions & 0 deletions tests/visualisation/test_visualisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,25 @@ def test_change_opacity():
_check_state_transition(actors, None, None, None, None,
set_opac_state, expected_opac_state,
bard_vis.change_opacity, 0.7)

def test_change_luminance():
"""
Tests luminance changes
"""

actors = []

for _ in range(8):
actor = vtk.vtkActor()
actors.append(actor)

model_list = {
'visible anatomy' : 3,
'target anatomy' : 2,
'reference' : 1
}

bard_vis = vis.BardVisualisation(actors, model_list)

bard_vis.luminance_change_left(0.7, True)
bard_vis.luminance_change_right(0.3, False)