Skip to content

Commit

Permalink
added colorbar for spheroid
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerum committed Jan 18, 2024
1 parent 5710e3b commit c957f71
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
66 changes: 66 additions & 0 deletions saenopy/gui/common/ModuleColorBar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import matplotlib.pyplot as plt
import numpy as np
from qtpy import QtCore, QtWidgets, QtGui
from qimage2ndarray import array2qimage


class ModuleColorBar(QtWidgets.QGroupBox):
min_v = None
max_v = None
cmap = None
tick_count = 3

def __init__(self, parent, view):
QtWidgets.QWidget.__init__(self)
self.parent = parent

self.font = QtGui.QFont()
self.font.setPointSize(16)

self.tick = []
for i in range(self.tick_count):
tick_ticks = QtWidgets.QGraphicsTextItem("", view.hud_lowerLeft)
tick_ticks.setFont(self.font)
tick_ticks.setDefaultTextColor(QtGui.QColor("white"))

tick_line = QtWidgets.QGraphicsRectItem(0, 0, 1, 5, tick_ticks)
tick_line.setBrush(QtGui.QBrush(QtGui.QColor("white")))
tick_line.setPen(QtGui.QPen(QtGui.QColor("white")))
self.tick.append([tick_ticks, tick_line])
self.scalebar = QtWidgets.QGraphicsPixmapItem(view.hud_lowerLeft)
self.scalebar.setPos(20, -20)

self.updateStatus()

def updateStatus(self):
self.updateBar()

def setScale(self, min_v, max_v, cmap):
self.min_v = min_v
self.max_v = max_v
self.cmap = cmap
self.updateBar()

def updateBar(self):
if self.min_v is None or self.max_v is None or self.cmap is None:
return
bar_width = 200
ofset_x = 20
ofset_y = 25
colors = np.zeros((10, bar_width, 3), dtype=np.uint8)
for i in range(bar_width):
c = plt.get_cmap(self.cmap)(int(i/bar_width*255))
colors[:, i, :] = [c[0]*255, c[1]*255, c[2]*255]
self.scalebar.setPixmap(QtGui.QPixmap(array2qimage(colors)))
self.scalebar.setPos(ofset_x, -ofset_y)

import matplotlib.ticker as ticker

locator = ticker.MaxNLocator(nbins=self.tick_count-1)
tick_positions = locator.tick_values(self.min_v, self.max_v)
tick_positions = np.linspace(self.min_v, self.max_v, self.tick_count)
for i, pos in enumerate(tick_positions):
self.tick[i][0].setPos(ofset_x - 50 + (bar_width-1)/(self.tick_count-1)*i, -ofset_y - 33)
self.tick[i][1].setPos(+ 50, 33 - 5)
self.tick[i][0].setTextWidth(100)
self.tick[i][0].setHtml(f"<center>{int(pos):d}</center>")
7 changes: 6 additions & 1 deletion saenopy/gui/solver/modules/exporter/ExporterRender2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def render_2d_image(params, result, exporter):
return pil_image, display_image, im_scale, aa_scale


def render_2d_arrows(params, result, pil_image, im_scale, aa_scale, display_image):
def render_2d_arrows(params, result, pil_image, im_scale, aa_scale, display_image, return_scale=False):
def project_data(R, field, skip=1):
length = np.linalg.norm(field, axis=1)
angle = np.arctan2(field[:, 1], field[:, 0])
Expand All @@ -77,6 +77,8 @@ def project_data(R, field, skip=1):
mesh, field, params_arrows, name = get_mesh_arrows(params, result)

if mesh is None:
if return_scale:
return pil_image, None
return pil_image

scale_max = params_arrows["scale_max"] if not params_arrows["autoscale"] else None
Expand Down Expand Up @@ -106,6 +108,7 @@ def project_data(R, field, skip=1):

if scale_max is None:
max_length = np.nanmax(np.linalg.norm(field, axis=1))# * params_arrows["arrow_scale"]
scale_max = max_length / params_arrows["arrow_scale"]
else:
max_length = scale_max * params_arrows["arrow_scale"]

Expand All @@ -129,6 +132,8 @@ def project_data(R, field, skip=1):
width=params["2D_arrows"]["width"],
headlength=params["2D_arrows"]["headlength"],
headheight=params["2D_arrows"]["headheight"])
if return_scale:
return pil_image, scale_max
return pil_image


Expand Down
9 changes: 6 additions & 3 deletions saenopy/gui/spheroid/modules/DeformationDetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from saenopy.gui.common.resources import resource_icon
from saenopy.gui.common.code_export import get_code
from saenopy.gui.common.ModuleScaleBar import ModuleScaleBar
from saenopy.gui.common.ModuleColorBar import ModuleColorBar


class DeformationDetector(PipelineModule):
Expand Down Expand Up @@ -76,6 +77,7 @@ def __init__(self, parent: "BatchEvaluate", layout):
#layout.addWidget(self.plotter.interactor)
self.label = QExtendedGraphicsView.QExtendedGraphicsView().addToLayout()
self.scale1 = ModuleScaleBar(self, self.label)
self.color1 = ModuleColorBar(self, self.label)
#self.label.setMinimumWidth(300)
self.pixmap = QtWidgets.QGraphicsPixmapItem(self.label.origin)
self.contour = QtWidgets.QGraphicsPathItem(self.label.origin)
Expand Down Expand Up @@ -207,10 +209,10 @@ def update_display(self, *, plotter=None):
pil_image = pil_image.resize(
[int(pil_image.width * im_scale * aa_scale), int(pil_image.height * im_scale * aa_scale)])
#print(self.auto_scale.value(), self.getScaleMax())
pil_image = render_2d_arrows({
pil_image, scale_max = render_2d_arrows({
'arrows': 'deformation',
'deformation_arrows': {
"autoscale": not self.auto_scale.value(),
"autoscale": self.auto_scale.value(),
"scale_max": self.getScaleMax(),
"colormap": self.colormap_chooser.value(),
"skip": 1,
Expand All @@ -219,12 +221,13 @@ def update_display(self, *, plotter=None):
},
"time": {"t": t},
'2D_arrows': {'width': 2.0, 'headlength': 5.0, 'headheight': 5.0},
}, self.result, pil_image, im_scale, aa_scale, display_image)
}, self.result, pil_image, im_scale, aa_scale, display_image, return_scale=True)

im = np.asarray(pil_image)
self.pixmap.setPixmap(QtGui.QPixmap(array2qimage(im)))
self.label.setExtend(im.shape[1], im.shape[0])
self.scale1.setScale([self.result.pixel_size])
self.color1.setScale(0, scale_max, self.colormap_chooser.value())

if self.show_seg.value():
thresh_segmentation = self.thresh_segmentation.value()
Expand Down

0 comments on commit c957f71

Please sign in to comment.