Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions geos-pv/src/geos/pv/plugins/qc/PVPythonViewConfigurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-FileContributor: Alexandre Benedicto, Martin Lemay
# ruff: noqa: E402 # disable Module level import not at top of file
import sys
import logging
from pathlib import Path
from typing import Any, Union, cast

Expand All @@ -17,6 +18,7 @@
update_paths()

from geos.mesh.utils.multiblockModifiers import mergeBlocks
from geos.utils.Logger import ( CountVerbosityHandler, getLoggerHandlerType )
import geos.pv.utils.paraviewTreatments as pvt
from geos.pv.utils.checkboxFunction import createModifiedCallback # type: ignore[attr-defined]
from geos.pv.utils.DisplayOrganizationParaview import DisplayOrganizationParaview
Expand All @@ -28,6 +30,11 @@
GetActiveSource, GetActiveView, Render, Show, servermanager )
from paraview.util.vtkAlgorithm import VTKPythonAlgorithmBase, smdomain, smproperty # type: ignore[import-not-found]
# source: https://github.com/Kitware/ParaView/blob/master/Wrapping/Python/paraview/util/vtkAlgorithm.py
from paraview.detail.loghandler import ( # type: ignore[import-not-found]
VTKHandler,
) # source: https://github.com/Kitware/ParaView/blob/master/Wrapping/Python/paraview/detail/loghandler.py



from vtkmodules.vtkCommonCore import vtkDataArraySelection, vtkInformation
from vtkmodules.vtkCommonDataModel import vtkDataObject, vtkMultiBlockDataSet
Expand All @@ -50,6 +57,9 @@

"""

loggerTitle: str = "Python View Configurator"
HANDLER: VTKHandler = VTKHandler()


@SISOFilter( category=FilterCategory.QC,
decoratedLabel="Python View Configurator",
Expand All @@ -61,7 +71,6 @@ def __init__( self: Self ) -> None:

Input is a vtkDataObject.
"""
# super().__init__( nInputPorts=1, nOutputPorts=1 )
# Python view layout and object.
self.m_layoutName: str = ""
self.m_pythonView: Any
Expand Down Expand Up @@ -139,6 +148,23 @@ def __init__( self: Self ) -> None:
"curvesAspect": {},
}

self.logger = logging.getLogger( loggerTitle )
self.logger.setLevel( logging.INFO )
self.logger.addHandler( HANDLER )
self.logger.propagate = False

counter: CountVerbosityHandler = CountVerbosityHandler()
self.counter: CountVerbosityHandler
self.nbWarnings: int = 0
try:
self.counter = getLoggerHandlerType( type( counter ), self.logger )
self.counter.resetWarningCount()
except ValueError:
self.counter = counter
self.counter.setLevel( logging.INFO )

self.logger.addHandler( self.counter )

def getUserChoices( self: Self ) -> dict[ str, Any ]:
"""Access the m_userChoices attribute.

Expand Down Expand Up @@ -808,12 +834,29 @@ def ApplyFilter( self, inputMesh: vtkDataObject, outputMesh: vtkDataObject ) ->
outputMesh : A dummy mesh transformed.

"""
assert self.m_pythonView is not None, "No Python View was found."
viewSize = GetActiveView().ViewSize
self.m_userChoices[ "ratio" ] = viewSize[ 0 ] / viewSize[ 1 ]
self.defineInputNames()
self.defineUserChoicesCurves()
self.defineCurvesAspect()
self.m_pythonView.Script = self.buildPythonViewScript()
Render()
self.logger.info( f"Apply the plugin { self.logger.name }." )
try:
if self.m_pythonView is None:
raise ValueError( "No Python View was found." )

viewSize = GetActiveView().ViewSize
self.m_userChoices[ "ratio" ] = viewSize[ 0 ] / viewSize[ 1 ]
self.defineInputNames()
self.defineUserChoicesCurves()
self.defineCurvesAspect()
self.m_pythonView.Script = self.buildPythonViewScript()
Render()

result: str = f"The plugin { self.logger.name } succeeded"
if self.counter.warningCount > 0:
self.logger.warning( f"{ result } but { self.counter.warningCount } warnings have been logged." )
else:
self.logger.info( f"{ result }." )
except Exception as e:
self.logger.error( f"The plugin failed due to:\n{ e }" )
return

self.nbWarnings = self.counter.warningCount
self.counter.resetWarningCount()

return
26 changes: 8 additions & 18 deletions geos-pv/src/geos/pv/pythonViewUtils/Figure2DGenerator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright 2023-2024 TotalEnergies.
# SPDX-FileContributor: Alexandre Benedicto
from logging import Logger
from typing import Any

import pandas as pd # type: ignore[import-untyped]
Expand All @@ -16,7 +15,7 @@

class Figure2DGenerator:

def __init__( self: Self, dataframe: pd.DataFrame, userChoices: dict[ str, list[ str ] ], logger: Logger ) -> None:
def __init__( self: Self, dataframe: pd.DataFrame, userChoices: dict[ str, list[ str ] ] ) -> None:
"""Utility to create cross plots using Python View.

We want to plot f(X) = Y where in this class,
Expand All @@ -25,29 +24,20 @@ def __init__( self: Self, dataframe: pd.DataFrame, userChoices: dict[ str, list[
Args:
dataframe (pd.DataFrame): Data to plot.
userChoices (dict[str, list[str]]): User choices.
logger (Logger): Logger to use.
"""
self.m_dataframe: pd.DataFrame = dataframe
self.m_userChoices: dict[ str, Any ] = userChoices
self.m_fig: figure.Figure
self.m_axes: list[ axes._axes.Axes ] = []
self.m_lines: list[ lines.Line2D ] = []
self.m_labels: list[ str ] = []
self.m_logger: Logger = logger

try:
# Apply minus 1 multiplication on certain columns.
self.initMinus1Multiplication()
# Defines m_fig, m_axes, m_lines and m_labels.
self.plotInitialFigure()
# Then to edit and customize the figure.
self.enhanceFigure()
self.m_logger.info( "Data were successfully plotted." )

except Exception as e:
mess: str = "Plot creation failed due to:"
self.m_logger.critical( mess )
self.m_logger.critical( e, exc_info=True )

# Apply minus 1 multiplication on certain columns.
self.initMinus1Multiplication()
# Defines m_fig, m_axes, m_lines and m_labels.
self.plotInitialFigure()
# Then to edit and customize the figure.
self.enhanceFigure()

def initMinus1Multiplication( self: Self ) -> None:
"""Multiply by -1 certain columns of the input dataframe."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ def setupAllAxes(
first_ax.ticklabel_format( style="sci", axis="x", scilimits=( 0, 0 ), useMathText=True )
for i in range( 1, len( associatedProperties.keys() ) ):
second_ax = first_ax.twinx()
assert isinstance( second_ax, axes.Axes )
if not isinstance( second_ax, axes.Axes ):
raise TypeError( "The second ax has not the right type.")
all_ax.append( second_ax )
all_ax[ i ].spines[ "right" ].set_position( ( "axes", 1 + 0.07 * ( i - 1 ) ) )
all_ax[ i ].tick_params( axis="y", which="both", left=False, right=True )
Expand All @@ -304,7 +305,8 @@ def setupAllAxes(
first_ax.ticklabel_format( style="sci", axis="y", scilimits=( 0, 0 ), useMathText=True )
for i in range( 1, len( associatedProperties.keys() ) ):
second_ax = first_ax.twiny()
assert isinstance( second_ax, axes.Axes )
if not isinstance( second_ax, axes.Axes ):
raise TypeError( "The second ax has not the right type.")
all_ax.append( second_ax )
all_ax[ i ].spines[ "bottom" ].set_position( ( "axes", -0.08 * i ) )
all_ax[ i ].xaxis.set_label_position( "bottom" )
Expand Down Expand Up @@ -383,7 +385,8 @@ def getExtremaAllAxes( axes: list[ axes.Axes ], ) -> tuple[ tuple[ float, float
Returns:
tuple[tuple[float, float], tuple[float, float]]: ((xMin, xMax), (yMin, yMax))
"""
assert len( axes ) > 0
if len( axes ) <= 0:
raise ValueError( "The list of axes can not be empty.")
xMin, xMax, yMin, yMax = getAxeLimits( axes[ 0 ] )
if len( axes ) > 1:
for i in range( 1, len( axes ) ):
Expand Down
14 changes: 2 additions & 12 deletions geos-pv/src/geos/pv/pythonViewUtils/mainPythonView.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@
# SPDX-FileContributor: Alexandre Benedicto
# type: ignore
# ruff: noqa
from logging import Logger, getLogger, INFO
from paraview.detail.loghandler import ( # type: ignore[import-not-found]
VTKHandler,
) # source: https://github.com/Kitware/ParaView/blob/master/Wrapping/Python/paraview/detail/loghandler.py

logger: Logger = getLogger( "Python View Configurator" )
logger.setLevel( INFO )
vtkHandler: VTKHandler = VTKHandler()
logger.addHandler( vtkHandler )

try:
import matplotlib.pyplot as plt
from paraview import python_view
Expand All @@ -30,7 +20,7 @@
variableName # noqa: F821
)
dataframe = pvt.mergeDataframes( dataframes, variableName ) # noqa: F821
obj_figure = Figure2DGenerator( dataframe, userChoices, logger ) # noqa: F821
obj_figure = Figure2DGenerator( dataframe, userChoices ) # noqa: F821
fig = obj_figure.getFigure()

def setup_data( view ) -> None: # noqa
Expand All @@ -42,4 +32,4 @@ def render( view, width: int, height: int ): # noqa
return imageToReturn

except Exception as e:
logger.critical( e, exc_info=True )
raise ChildProcessError( f"Error during the plot:\n{ e }" ) from e