Skip to content

Commit

Permalink
remove some verbose print() calls (#219)
Browse files Browse the repository at this point in the history
* remove import printout from UsdViewer.py

* remove printout from Data.py

* only warn if the types are actually used

* fix global variable usage

* Update Data.py
  • Loading branch information
ManuelHu authored Dec 19, 2024
1 parent ebbcc37 commit f0fddf1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
22 changes: 20 additions & 2 deletions src/pyg4ometry/analysis/Data.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import logging
import numpy as _np
import matplotlib.pyplot as _plt
from ..analysis.flukaData import FlukaBdxData as _FlukaBdxData
from ..analysis.flukaData import FlukaBinData as _FlukaBinData

_import_failed = []
try:
from pybdsim.Data import TH1 as _TH1
from pybdsim.Data import TH2 as _TH2
from pybdsim.Data import TH3 as _TH3
except ImportError:
print("Failed to find pybdsim")
_import_failed.append("pybdsim")

try:
from ROOT import TH1D as _TH1D
Expand All @@ -18,7 +20,17 @@
from ROOT import TH2F as _TH2F
from ROOT import TH3F as _TH3F
except ImportError:
print("Failed to find ROOT")
_import_failed.append("ROOT")

_log = logging.getLogger(__name__)


def _warn_import_failed():
global _import_failed
if _import_failed != []:
msg = f"Failed to import {_import_failed}"
_log.warning(msg)
_import_failed = []


class TH1:
Expand All @@ -33,6 +45,8 @@ def __init__(self, data, nPrimary=1, flukaBdxVariable="energy"):
self.xlowedges = None
self.xhighedges = None

_warn_import_failed()

if type(data) is _FlukaBdxData:
print("flukaBdxData")
self.name = data.name.strip()
Expand Down Expand Up @@ -80,6 +94,8 @@ def plot(self, ms="."):

class TH2:
def __init__(self, data, nPrimary=1):
_warn_import_failed()

if type(data) is _TH2:
self.name = data.name
self.nPrimary = nPrimary
Expand Down Expand Up @@ -114,6 +130,8 @@ def plot(self):

class TH3:
def __init__(self, data, nPrimary=1):
_warn_import_failed()

if type(data) is _FlukaBinData:
self.name = data.name.strip()
self.nPrimary = data
Expand Down
7 changes: 4 additions & 3 deletions src/pyg4ometry/visualisation/UsdViewer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
try:
from pxr import Usd, Gf, UsdGeom, UsdShade, Sdf

print("Openusd pyg4ometry imported")
except ImportError:
pass
Usd = None

from .ViewerHierarchyBase import ViewerHierarchyBase as _ViewerHierarchyBase
import numpy as _np
Expand Down Expand Up @@ -69,6 +67,9 @@ def visOptions2MaterialPrim(stage, visOptions, materialPrim):
class UsdViewer(_ViewerHierarchyBase):

def __init__(self, filePath="./test.usd"):
if Usd is None:
msg = "Failed to import open usd"
raise RuntimeError(msg)

self.filePath = filePath

Expand Down

0 comments on commit f0fddf1

Please sign in to comment.