88from PyQt5 import QtCore , QtWidgets
99
1010from . import io
11+ from .logging_utils import get_logger , get_perf_logger
12+
13+ logger = get_logger ("GUIWidgets" )
14+ perf_logger = get_perf_logger ("GUIWidgets" )
1115
1216matplotlib .use ("Qt5Agg" )
1317
@@ -129,8 +133,7 @@ def __init__(self, parentWiget=None, parentApp=None, width=5, height=4, dpi=100,
129133 def plotSlice (self , F_stack , z_slice , title = None , margins = None , grid_selector = 0 , slice_length = None , points = [], cbar_range = None , extent = None ):
130134 F = F_stack [z_slice ]
131135
132- if self .verbose > 0 :
133- print ("plotSlice F.shape, F.min(), F.max(), margins" , F .shape , F .min (), F .max (), margins )
136+ logger .debug (f"plotSlice F.shape, F.min(), F.max(), margins { F .shape } { F .min ()} { F .max ()} { margins } " )
134137
135138 if self .img is None or self .img .get_array ().shape != F .shape :
136139 self .axes .cla ()
@@ -143,8 +146,7 @@ def plotSlice(self, F_stack, z_slice, title=None, margins=None, grid_selector=0,
143146 for line in self .axes .lines :
144147 line .remove ()
145148 self .axes .set_prop_cycle (None ) # Reset color cycle
146- if self .verbose > 0 :
147- print ("plotSlice: reset points" )
149+ logger .debug ("plotSlice: reset points" )
148150 else :
149151 for p , (ix , iy ) in zip (self .axes .lines , points ):
150152 x = np .atleast_1d (ix )
@@ -155,17 +157,15 @@ def plotSlice(self, F_stack, z_slice, title=None, margins=None, grid_selector=0,
155157 if self .cbar == None :
156158 self .cbar = self .fig .colorbar (self .img , ax = self .axes )
157159 self .cbar .set_label ("df (Hz)" )
158- if self .verbose > 0 :
159- print ("plotSlice: added colorbar" )
160+ logger .debug ("plotSlice: added colorbar" )
160161 self .img .set_clim (vmin = cbar_range [0 ], vmax = cbar_range [1 ])
161162 self .cbar .mappable .set_clim (vmin = cbar_range [0 ], vmax = cbar_range [1 ])
162163 else :
163164 self .img .autoscale ()
164165 if self .cbar is not None :
165166 self .cbar .remove ()
166167 self .cbar = None
167- if self .verbose > 0 :
168- print ("plotSlice: removed colorbar" )
168+ logger .debug ("plotSlice: removed colorbar" )
169169
170170 if extent is not None :
171171 self .img .set_extent (extent )
@@ -197,9 +197,9 @@ def plotSlice2(self, F_stack, title=None, margins=None, grid_selector=0, slice_l
197197 self .axes .cla ()
198198
199199 F = F_stack
200- print ( "plotSlice F.shape, F.min(), F.max() " , F .shape , F .min (), F .max ())
200+ logger . debug ( f "plotSlice F.shape, F.min(), F.max() { F .shape } { F .min ()} { F .max ()} " )
201201
202- print ( "self.margins" , margins )
202+ logger . debug ( f "self.margins { margins } " )
203203 if alpha > 0 and big_len_image is not None :
204204 F = F * (1 - alpha ) + big_len_image * alpha
205205 self .img = self .axes .imshow (F , origin = "lower" , cmap = "viridis" , interpolation = "bicubic" )
@@ -232,8 +232,7 @@ def onclick(self, event):
232232 x = float (event .xdata )
233233 y = float (event .ydata )
234234 except TypeError :
235- if self .verbose > 0 :
236- print ("Invalid click event." )
235+ logger .warning ("Invalid click event." )
237236 return
238237 self .axes .plot ([x ], [y ], "o" , scalex = False , scaley = False )
239238 self .draw ()
@@ -247,15 +246,14 @@ def onscroll(self, event):
247246 x = float (event .xdata )
248247 y = float (event .ydata )
249248 except TypeError :
250- if self .verbose > 0 :
251- print ("Invalid scroll event." )
249+ logger .warning ("Invalid scroll event." )
252250 return
253251 if event .button == "up" :
254252 direction = "in"
255253 elif event .button == "down" :
256254 direction = "out"
257255 else :
258- print (f"Invalid scroll direction { event .button } " )
256+ logger . warning (f"Invalid scroll direction { event .button } " )
259257 return
260258 self .parent .zoomTowards (x , y , direction )
261259
@@ -325,7 +323,7 @@ def save_dat(self):
325323 fileName , _ = QtWidgets .QFileDialog .getSaveFileName (self , "Save df curve raw data" , default_path , "Data files (*.dat)" )
326324 if fileName :
327325 fileName = correct_ext (fileName , ".dat" )
328- print ( "saving data to :" , fileName )
326+ logger . info ( f"Saving data to: { fileName } " )
329327 data = []
330328 data .append (np .array (self .figCan .axes .lines [0 ].get_xdata ()))
331329 for line in self .figCan .axes .lines :
@@ -338,7 +336,7 @@ def save_png(self):
338336 fileName , _ = QtWidgets .QFileDialog .getSaveFileName (self , "Save df curve image" , default_path , "Image files (*.png)" )
339337 if fileName :
340338 fileName = correct_ext (fileName , ".png" )
341- print ( "saving image to :" , fileName )
339+ logger . info ( f"Saving image to: { fileName } " )
342340 self .figCan .fig .savefig (fileName , bbox_inches = "tight" )
343341
344342 def clearFig (self ):
@@ -363,7 +361,7 @@ def setRange(self):
363361 except :
364362 pass
365363 self .figCan .draw ()
366- print ( "range: " , xmin , xmax , ymin , ymax )
364+ logger . debug ( f "range: { xmin } { xmax } { ymin } { ymax } " )
367365
368366
369367# =======================
@@ -576,8 +574,7 @@ def updateFF(self):
576574 if not self .isVisible ():
577575 set_widget_value (self .bxInd , iz )
578576
579- if self .verbose > 0 :
580- print ("FFViewer.updateFF" , self .FE .shape , iz , self .z_step , self .z_min )
577+ logger .debug (f"FFViewer.updateFF { self .FE .shape } { iz } { self .z_step } { self .z_min } " )
581578
582579 def updateView (self ):
583580 t0 = time .perf_counter ()
@@ -588,10 +585,8 @@ def updateView(self):
588585 data = self .FE [..., ic ].transpose (2 , 1 , 0 )
589586 self .figCan .plotSlice (data , iz , title = f"z = { z :.2f} Å" )
590587
591- if self .verbose > 0 :
592- print ("FFViewer.updateView" , ic , iz , data .shape )
593- if self .verbose > 1 :
594- print ("updateView time [s]" , time .perf_counter () - t0 )
588+ logger .debug (f"FFViewer.updateView { ic } { iz } { data .shape } " )
589+ perf_logger .info (f"updateView time [s] { time .perf_counter () - t0 } " )
595590
596591 def saveFF (self ):
597592 comp = self .slComponent .currentText ()
@@ -602,12 +597,11 @@ def saveFF(self):
602597 ext = os .path .splitext (fileName )[1 ]
603598 if ext != ".xsf" :
604599 self .parent .status_message ("Unsupported file type in force field save file path" )
605- print (f"Unsupported file type in force field save file path `{ fileName } `" )
600+ logger . error (f"Unsupported file type in force field save file path `{ fileName } `" )
606601 return
607602 self .parent .status_message ("Saving data..." )
608603
609- if self .verbose > 0 :
610- print (f"Saving force field data to { fileName } ..." )
604+ logger .debug (f"Saving force field data to { fileName } ..." )
611605 ic = self .slComponent .currentIndex ()
612606 data = self .FE .copy ()
613607 # Clamp large values for easier visualization
@@ -620,10 +614,9 @@ def saveFF(self):
620614 lvec = self .parent .afmulator .lvec
621615 xyzs = self .parent .xyzs - lvec [0 ]
622616 atomstring = io .primcoords2Xsf (self .parent .Zs , xyzs .T , lvec )
623- io .saveXSF (fileName , data , lvec , head = atomstring , verbose = 0 )
617+ io .saveXSF (fileName , data , lvec , head = atomstring )
624618
625- if self .verbose > 0 :
626- print ("Done saving force field data." )
619+ logger .info ("Done saving force field data." )
627620 self .parent .status_message ("Ready" )
628621
629622
0 commit comments