-
Notifications
You must be signed in to change notification settings - Fork 22
Logging #368
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
base: main
Are you sure you want to change the base?
Logging #368
Changes from all commits
de2e62b
d0ee140
a62861e
019cba7
5a4d6d0
497e01c
977d89d
9668798
9390e59
b7dc650
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,10 @@ | |
| from PyQt5 import QtCore, QtWidgets | ||
|
|
||
| from . import io | ||
| from .logging_utils import get_logger, get_perf_logger | ||
|
|
||
| logger = get_logger("GUIWidgets") | ||
| perf_logger = get_perf_logger("GUIWidgets") | ||
|
|
||
| matplotlib.use("Qt5Agg") | ||
|
|
||
|
|
@@ -118,19 +122,17 @@ class FigImshow(FigCanvas): | |
|
|
||
| cbar = None | ||
|
|
||
| def __init__(self, parentWiget=None, parentApp=None, width=5, height=4, dpi=100, verbose=0): | ||
| def __init__(self, parentWiget=None, parentApp=None, width=5, height=4, dpi=100): | ||
| super(self.__class__, self).__init__(parentWiget=parentWiget, parentApp=parentApp, width=width, height=height, dpi=dpi) | ||
| self.fig.canvas.mpl_connect("button_press_event", self.onclick) | ||
| self.fig.canvas.mpl_connect("scroll_event", self.onscroll) | ||
| self.verbose = verbose | ||
| self.img = None | ||
| self.cbar = None | ||
|
|
||
| def plotSlice(self, F_stack, z_slice, title=None, margins=None, grid_selector=0, slice_length=None, points=[], cbar_range=None, extent=None): | ||
| F = F_stack[z_slice] | ||
|
|
||
| if self.verbose > 0: | ||
| print("plotSlice F.shape, F.min(), F.max(), margins", F.shape, F.min(), F.max(), margins) | ||
| logger.debug(f"plotSlice F.shape, F.min(), F.max(), margins {F.shape} {F.min()} {F.max()} {margins}") | ||
|
|
||
| if self.img is None or self.img.get_array().shape != F.shape: | ||
| self.axes.cla() | ||
|
|
@@ -143,8 +145,7 @@ def plotSlice(self, F_stack, z_slice, title=None, margins=None, grid_selector=0, | |
| for line in self.axes.lines: | ||
| line.remove() | ||
| self.axes.set_prop_cycle(None) # Reset color cycle | ||
| if self.verbose > 0: | ||
| print("plotSlice: reset points") | ||
| logger.debug("plotSlice: reset points") | ||
| else: | ||
| for p, (ix, iy) in zip(self.axes.lines, points): | ||
| x = np.atleast_1d(ix) | ||
|
|
@@ -155,17 +156,15 @@ def plotSlice(self, F_stack, z_slice, title=None, margins=None, grid_selector=0, | |
| if self.cbar == None: | ||
| self.cbar = self.fig.colorbar(self.img, ax=self.axes) | ||
| self.cbar.set_label("df (Hz)") | ||
| if self.verbose > 0: | ||
| print("plotSlice: added colorbar") | ||
| logger.debug("plotSlice: added colorbar") | ||
| self.img.set_clim(vmin=cbar_range[0], vmax=cbar_range[1]) | ||
| self.cbar.mappable.set_clim(vmin=cbar_range[0], vmax=cbar_range[1]) | ||
| else: | ||
| self.img.autoscale() | ||
| if self.cbar is not None: | ||
| self.cbar.remove() | ||
| self.cbar = None | ||
| if self.verbose > 0: | ||
| print("plotSlice: removed colorbar") | ||
| logger.debug("plotSlice: removed colorbar") | ||
|
|
||
| if extent is not None: | ||
| self.img.set_extent(extent) | ||
|
|
@@ -197,9 +196,9 @@ def plotSlice2(self, F_stack, title=None, margins=None, grid_selector=0, slice_l | |
| self.axes.cla() | ||
|
|
||
| F = F_stack | ||
| print("plotSlice F.shape, F.min(), F.max() ", F.shape, F.min(), F.max()) | ||
| logger.debug(f"plotSlice F.shape, F.min(), F.max() {F.shape} {F.min()} {F.max()}") | ||
|
|
||
| print("self.margins", margins) | ||
| logger.debug(f"self.margins {margins}") | ||
| if alpha > 0 and big_len_image is not None: | ||
| F = F * (1 - alpha) + big_len_image * alpha | ||
| self.img = self.axes.imshow(F, origin="lower", cmap="viridis", interpolation="bicubic") | ||
|
|
@@ -232,8 +231,7 @@ def onclick(self, event): | |
| x = float(event.xdata) | ||
| y = float(event.ydata) | ||
| except TypeError: | ||
| if self.verbose > 0: | ||
| print("Invalid click event.") | ||
| logger.warning("Invalid click event.") | ||
| return | ||
| self.axes.plot([x], [y], "o", scalex=False, scaley=False) | ||
| self.draw() | ||
|
|
@@ -247,15 +245,14 @@ def onscroll(self, event): | |
| x = float(event.xdata) | ||
| y = float(event.ydata) | ||
| except TypeError: | ||
| if self.verbose > 0: | ||
| print("Invalid scroll event.") | ||
| logger.warning("Invalid scroll event.") | ||
| return | ||
| if event.button == "up": | ||
| direction = "in" | ||
| elif event.button == "down": | ||
| direction = "out" | ||
| else: | ||
| print(f"Invalid scroll direction {event.button}") | ||
| logger.warning(f"Invalid scroll direction {event.button}") | ||
| return | ||
| self.parent.zoomTowards(x, y, direction) | ||
|
|
||
|
|
@@ -325,7 +322,7 @@ def save_dat(self): | |
| fileName, _ = QtWidgets.QFileDialog.getSaveFileName(self, "Save df curve raw data", default_path, "Data files (*.dat)") | ||
| if fileName: | ||
| fileName = correct_ext(fileName, ".dat") | ||
| print("saving data to :", fileName) | ||
| logger.info(f"Saving data to: {fileName}") | ||
| data = [] | ||
| data.append(np.array(self.figCan.axes.lines[0].get_xdata())) | ||
| for line in self.figCan.axes.lines: | ||
|
|
@@ -338,7 +335,7 @@ def save_png(self): | |
| fileName, _ = QtWidgets.QFileDialog.getSaveFileName(self, "Save df curve image", default_path, "Image files (*.png)") | ||
| if fileName: | ||
| fileName = correct_ext(fileName, ".png") | ||
| print("saving image to :", fileName) | ||
| logger.info(f"Saving image to: {fileName}") | ||
| self.figCan.fig.savefig(fileName, bbox_inches="tight") | ||
|
|
||
| def clearFig(self): | ||
|
|
@@ -363,7 +360,7 @@ def setRange(self): | |
| except: | ||
| pass | ||
| self.figCan.draw() | ||
| print("range: ", xmin, xmax, ymin, ymax) | ||
| logger.debug(f"range: {xmin} {xmax} {ymin} {ymax}") | ||
|
|
||
|
|
||
| # ======================= | ||
|
|
@@ -511,9 +508,8 @@ def updateParent(self): | |
|
|
||
|
|
||
| class FFViewer(SlaveWindow): | ||
| def __init__(self, parent=None, title="View Forcefield", width=5, height=4, dpi=100, verbose=0): | ||
| def __init__(self, parent=None, title="View Forcefield", width=5, height=4, dpi=100): | ||
| super().__init__(parent=parent, title=title) | ||
| self.verbose = verbose | ||
|
|
||
| self.figCan = FigImshow(parent, width=width, height=height, dpi=dpi) | ||
| self.centralLayout.addWidget(self.figCan) | ||
|
|
@@ -576,8 +572,7 @@ def updateFF(self): | |
| if not self.isVisible(): | ||
| set_widget_value(self.bxInd, iz) | ||
|
|
||
| if self.verbose > 0: | ||
| print("FFViewer.updateFF", self.FE.shape, iz, self.z_step, self.z_min) | ||
| logger.debug(f"FFViewer.updateFF {self.FE.shape} {iz} {self.z_step} {self.z_min}") | ||
|
|
||
| def updateView(self): | ||
| t0 = time.perf_counter() | ||
|
|
@@ -588,10 +583,8 @@ def updateView(self): | |
| data = self.FE[..., ic].transpose(2, 1, 0) | ||
| self.figCan.plotSlice(data, iz, title=f"z = {z:.2f}Å") | ||
|
|
||
| if self.verbose > 0: | ||
| print("FFViewer.updateView", ic, iz, data.shape) | ||
| if self.verbose > 1: | ||
| print("updateView time [s]", time.perf_counter() - t0) | ||
| logger.debug(f"FFViewer.updateView {ic} {iz} {data.shape}") | ||
| perf_logger.info(f"updateView time [s] {time.perf_counter() - t0}") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I previously had the performance benchmark prints at higher verbose level, but now it's in a separate system that can be enabled as needed. Basically performance logger prints lines with info on the amount of time taken for various tasks, for example: |
||
|
|
||
| def saveFF(self): | ||
| comp = self.slComponent.currentText() | ||
|
|
@@ -602,12 +595,11 @@ def saveFF(self): | |
| ext = os.path.splitext(fileName)[1] | ||
| if ext != ".xsf": | ||
| self.parent.status_message("Unsupported file type in force field save file path") | ||
| print(f"Unsupported file type in force field save file path `{fileName}`") | ||
| logger.error(f"Unsupported file type in force field save file path `{fileName}`") | ||
| return | ||
| self.parent.status_message("Saving data...") | ||
|
|
||
| if self.verbose > 0: | ||
| print(f"Saving force field data to {fileName}...") | ||
| logger.debug(f"Saving force field data to {fileName}...") | ||
| ic = self.slComponent.currentIndex() | ||
| data = self.FE.copy() | ||
| # Clamp large values for easier visualization | ||
|
|
@@ -620,10 +612,9 @@ def saveFF(self): | |
| lvec = self.parent.afmulator.lvec | ||
| xyzs = self.parent.xyzs - lvec[0] | ||
| atomstring = io.primcoords2Xsf(self.parent.Zs, xyzs.T, lvec) | ||
| io.saveXSF(fileName, data, lvec, head=atomstring, verbose=0) | ||
| io.saveXSF(fileName, data, lvec, head=atomstring) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it the information about verbosity send through somehow?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The verbosity level is now handled globally by setting the logger logging level, so there is no need to pass it manually everywhere. |
||
|
|
||
| if self.verbose > 0: | ||
| print("Done saving force field data.") | ||
| logger.info("Done saving force field data.") | ||
| self.parent.status_message("Ready") | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,9 @@ | |
| import numpy as np | ||
|
|
||
| from . import cpp_utils | ||
| from .logging_utils import get_logger | ||
|
|
||
| logger = get_logger("GridUtils") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the file/routine type has to be passed to the logger function?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's just a label that allows differentiating between messages coming from different modules if needed. The name is used in the log format with The |
||
|
|
||
| # ============================== interface to C++ core | ||
|
|
||
|
|
@@ -97,7 +100,7 @@ def interpolate_cartesian(F, pos, cell=None, result=None): | |
| if cell is not None: | ||
| setGridCell(cell) | ||
| nDim = np.array(pos.shape) | ||
| print(nDim) | ||
| logger.debug(nDim) | ||
| if result is None: | ||
| result = np.zeros((nDim[0], nDim[1], nDim[2])) | ||
| n = nDim[0] * nDim[1] * nDim[2] | ||
|
|
@@ -120,9 +123,9 @@ def dens2Q_CHGCARxsf(data, lvec): | |
| nDim = data.shape | ||
| Ntot = nDim[0] * nDim[1] * nDim[2] | ||
| Vtot = np.linalg.det(lvec[1:]) | ||
| print("dens2Q Volume : ", Vtot) | ||
| print("dens2Q Ntot : ", Ntot) | ||
| print("dens2Q Vtot/Ntot : ", Vtot / Ntot) | ||
| logger.debug(f"dens2Q Volume : {Vtot}") | ||
| logger.debug(f"dens2Q Ntot : {Ntot}") | ||
| logger.debug(f"dens2Q Vtot/Ntot : {Vtot / Ntot}") | ||
| # Qsum = rho1.sum() | ||
| return Vtot / Ntot | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.