Skip to content

Commit

Permalink
2Dtfm use render_2d functions instead of matplotlib quiver
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerum committed Jan 21, 2025
1 parent 3a349e9 commit 5020d89
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 42 deletions.
18 changes: 16 additions & 2 deletions saenopy/gui/solver/modules/exporter/ExporterRender2D.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pandas as pd
from copy import deepcopy
from PIL import Image, ImageDraw, ImageFont
import matplotlib
import matplotlib.pyplot as plt
Expand All @@ -8,8 +9,21 @@
from saenopy.gui.solver.modules.showVectorField import getVectorFieldImage
from saenopy.gui.solver.modules.exporter.ExportRenderCommon import get_time_text, getVectorFieldImage, get_mesh_arrows

default_data = {'use2D': True, 'image': {'logo_size': 0, 'scale': 1.0, 'antialiase': True, 'scale_overlay': 1.0}, 'camera': {'elevation': 35.0, 'azimuth': 45.0, 'distance': 0, 'offset_x': 0, 'offset_y': 0, 'roll': 0}, 'theme': 'dark', 'show_grid': True, 'use_nans': False, 'arrows': 'deformation', 'averaging_size': 1.0, 'deformation_arrows': {'autoscale': True, 'scale_max': 10.0, 'colormap': 'turbo', 'arrow_scale': 1.0, 'arrow_opacity': 1.0, 'skip': 1}, 'force_arrows': {'autoscale': True, 'scale_max': 1000.0, 'use_center': False, 'use_log': True, 'colormap': 'turbo', 'arrow_scale': 1.0, 'arrow_opacity': 1.0, 'skip': 1}, 'maps': 'None', 'maps_cmap': 'turbo', 'maps_alpha': 0.5, 'stack': {'image': True, 'channel': 'cells', 'z_proj': False, 'use_contrast_enhance': True, 'contrast_enhance': None, 'colormap': 'gray', 'z': 0, 'use_reference_stack': False, 'alpha': 1.0, 'channel_B': '', 'colormap_B': 'gray', 'alpha_B': 1.0}, 'scalebar': {'hide': False, 'length': 0.0, 'width': 5.0, 'xpos': 15.0, 'ypos': 10.0, 'fontsize': 18.0}, 'colorbar': {'hide': False, 'length': 150.0, 'width': 10.0, 'xpos': 15.0, 'ypos': 10.0, 'fontsize': 18.0}, '2D_arrows': {'width': 2.0, 'headlength': 5.0, 'headheight': 5.0}, 'crop': {'x': (913, 1113), 'y': (893, 1093), 'z': (0, 1)}, 'channel0': {'show': False, 'skip': 1, 'sigma_sato': 2, 'sigma_gauss': 0, 'percentiles': (0, 1), 'range': (0, 1), 'alpha': (0.1, 0.5, 1), 'cmap': 'pink'}, 'channel1': {'show': False, 'skip': 1, 'sigma_sato': 0, 'sigma_gauss': 7, 'percentiles': (0, 1), 'range': (0, 1), 'alpha': (0.1, 0.5, 1), 'cmap': 'Greens', 'channel': 1}, 'channel_thresh': 1.0, 'time': {'t': 0, 'format': '%d:%H:%M', 'start': 0.0, 'display': True, 'fontsize': 18}}

def update_dict(new_dict, old_dict, key):
if isinstance(old_dict[key], dict):
for key2 in old_dict[key].keys():
update_dict(new_dict[key], old_dict[key], key2)
else:
new_dict[key] = old_dict[key]

def render_2d(params, result, exporter=None):
default_copy = deepcopy(default_data)
for key in params.keys():
update_dict(default_copy, params, key)
params = default_copy

pil_image, display_image, im_scale, aa_scale = render_2d_image(params, result, exporter)
if pil_image is None:
return np.zeros((10, 10))
Expand All @@ -20,7 +34,7 @@ def render_2d(params, result, exporter=None):
pil_image = pil_image.resize([pil_image.width // 2, pil_image.height // 2])
aa_scale = 1

if params["maps"] != "None":
if params.get("maps", "None") != "None":
pil_image, disp_params = render_map(params, result, pil_image, im_scale, aa_scale, display_image, return_scale=True)

if params["scalebar"]["hide"] is False:
Expand All @@ -39,7 +53,7 @@ def render_2d_image(params, result, exporter):
display_image = getVectorFieldImage(result, params, use_fixed_contrast_if_available=True, use_2D=True, exporter=exporter)
if display_image is None:
return None, None, 1, 1
if params["stack"]["channel_B"] != "":
if params["stack"].get("channel_B", "") != "":
params["stack"]["channel"] = params["stack"]["channel_B"]
display_imageB = getVectorFieldImage(result, params, use_fixed_contrast_if_available=True, exporter=exporter)
else:
Expand Down
11 changes: 3 additions & 8 deletions saenopy/gui/tfm2d/modules/TabCellImage.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
from saenopy.gui.common.TabModule import TabModule
import os
from .result import Result2D

from qtpy import QtCore, QtWidgets, QtGui

import traceback

from saenopy import get_stacks
from saenopy.gui.common import QtShortCuts

class TabCellImage(TabModule):
result: Result2D

def __init__(self, parent=None):
def __init__(self, parent: "BatchEvaluate"):
super().__init__(parent)
with self.parent.tabs.createTab("Cell Image") as self.tab:
pass
Expand Down
23 changes: 21 additions & 2 deletions saenopy/gui/tfm2d/modules/TabDeformations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from .result import Result2D
from saenopy.gui.common.TabModule import TabModule
from saenopy.gui.solver.modules.exporter.ExporterRender2D import render_2d


class TabDeformation(TabModule):
result: Result2D

def __init__(self, parent: "BatchEvaluate"):
super().__init__(parent)
Expand All @@ -16,5 +18,22 @@ def check_evaluated(self, result: Result2D) -> bool:
def tabChanged(self, tab):
if self.tab is not None and self.tab.parent() == tab:
if self.check_evaluated(self.result):
im = self.result.get_deformation_field()
self.parent.draw.setImage(im*255)

if self.result is None:
return
im = render_2d({
"stack": {
"channel": "cells",
},
"arrows": "deformation",

"colorbar": {
"hide": True,
},

"scalebar": {
"hide": True,
},
}, self.result)

self.parent.draw.setImage(im)
4 changes: 3 additions & 1 deletion saenopy/gui/tfm2d/modules/TabDeformed.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from saenopy.gui.common.TabModule import TabModule
from .result import Result2D


class TabDeformed(TabModule):
result: Result2D

def __init__(self, parent=None):
def __init__(self, parent: "BatchEvaluate"):
super().__init__(parent)

with self.parent.tabs.createTab("Deformed") as self.tab:
Expand Down
22 changes: 19 additions & 3 deletions saenopy/gui/tfm2d/modules/TabForces.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from .result import Result2D
from saenopy.gui.common.TabModule import TabModule
from saenopy.gui.solver.modules.exporter.ExporterRender2D import render_2d


class TabForces(TabModule):
result: Result2D

def __init__(self, parent=None):
def __init__(self, parent: "BatchEvaluate"):
super().__init__(parent)

with self.parent.tabs.createTab("Forces") as self.tab:
Expand All @@ -16,5 +18,19 @@ def check_evaluated(self, result: Result2D) -> bool:
def tabChanged(self, tab):
if self.tab is not None and self.tab.parent() == tab:
if self.check_evaluated(self.result):
im = self.result.get_force_field()
self.parent.draw.setImage(im*255)
im = render_2d({
"stack": {
"channel": "cells",
},
"arrows": "stress",

"colorbar": {
"hide": True,
},

"scalebar": {
"hide": True,
},
}, self.result)

self.parent.draw.setImage(im)
5 changes: 4 additions & 1 deletion saenopy/gui/tfm2d/modules/TabRelaxed.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from saenopy.gui.common.TabModule import TabModule
from .result import Result2D


class TabRelaxed(TabModule):
result: Result2D

def __init__(self, parent=None):
def __init__(self, parent: "BatchEvaluate"):
super().__init__(parent)
with self.parent.tabs.createTab("Referenece") as self.tab:
pass
Expand Down
15 changes: 3 additions & 12 deletions saenopy/gui/tfm2d/modules/TabStress.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
from qtpy import QtWidgets
from saenopy.gui.common import QtShortCuts
from typing import List, Tuple

from saenopy.gui.common.gui_classes import CheckAbleGroup
from saenopy.gui.common.code_export import get_code, export_as_string

from .PipelineModule import PipelineModule
from saenopy.gui.common.TabModule import TabModule
from .result import Result2D

from saenopy.pyTFM.calculate_stress import calculate_stress

from saenopy.gui.common.TabModule import TabModule

class TabStress(TabModule):
result: Result2D

def __init__(self, parent=None):
def __init__(self, parent: "BatchEvaluate"):
super().__init__(parent)

with self.parent.tabs.createTab("Line Tension") as self.tab:
Expand Down
13 changes: 0 additions & 13 deletions saenopy/gui/tfm2d/modules/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,6 @@ def get_image(self, index, corrected=True):
self.shape = im.shape
return im

def get_deformation_field(self):
if self.im_displacement is None:
fig1, ax = show_quiver(self.u, self.v, cbar_str="deformations\n[pixels]")
self.im_displacement = fig_to_numpy(fig1, self.shape)
return self.im_displacement

def get_force_field(self):
if self.im_force is None:
fig1, ax = show_quiver(self.tx, self.ty, cbar_str="tractions\n[Pa]")
self.im_force = fig_to_numpy(fig1, self.shape)
return self.im_force

def get_line_tensions(self):
if self.im_tension is None:
fig3, ax = plot_continuous_boundary_stresses([self.borders_inter_shape, self.borders_edge_lines, self.lt, self.min_v, self.max_v],
Expand Down Expand Up @@ -289,7 +277,6 @@ class Mesh2D:
vy = self.v*self.pixel_size
vf = 10/self.pixel_size
if name == "stress":
print("do force")
vx = self.tx
vy = self.ty
vf = 0.1
Expand Down

0 comments on commit 5020d89

Please sign in to comment.