Skip to content
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

Easy promotion #2366

Merged
merged 3 commits into from
Jan 8, 2025
Merged
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
12 changes: 9 additions & 3 deletions src/pyFAI/geometry/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ class Geometry(object):
"chiDiscAtPi", "_wavelength", "_dssa_order",
'_oversampling', '_correct_solid_angle_for_spline',
'_transmission_normal')

PROMOTION = {"AzimuthalIntegrator": "pyFAI.integrator.azimuthal.AzimuthalIntegrator",
"FiberIntegrator": "pyFAI.integrator.fiber.FiberIntegrator",
"GeometryRefinement": "pyFAI.geometryRefinement.GeometryRefinement",
"Geometry": "pyFAI.geometry.core.Geometry"}

def __init__(self, dist=1, poni1=0, poni2=0, rot1=0, rot2=0, rot3=0,
pixel1=None, pixel2=None, splineFile=None, detector=None, wavelength=None,
Expand Down Expand Up @@ -2088,7 +2091,7 @@ def calcfrom2d(self, I, tth, chi, shape=None, mask=None,
calcimage[numpy.where(mask)] = dummy
return calcimage

def promote(self, type_="pyFAI.azimuthalIntegrator.AzimuthalIntegrator", kwargs=None):
def promote(self, type_="pyFAI.integrator.azimuthal.AzimuthalIntegrator", kwargs=None):
"""Promote this instance into one of its derived class (deep copy like)

:param type_: Fully qualified name of the class to promote to, or the class itself
Expand All @@ -2099,6 +2102,9 @@ def promote(self, type_="pyFAI.azimuthalIntegrator.AzimuthalIntegrator", kwargs=
"""
GeometryClass = self.__class__.__mro__[-2] # actually pyFAI.geometry.core.Geometry
if isinstance(type_, str):
if "." not in type_:
if type_ in self.PROMOTION:
type_ = self.PROMOTION[type_]
import importlib
modules = type_.split(".")
module_name = ".".join(modules[:-1])
Expand All @@ -2107,7 +2113,7 @@ def promote(self, type_="pyFAI.azimuthalIntegrator.AzimuthalIntegrator", kwargs=
elif isinstance(type_, type):
klass = type_
else:
raise ValueError("`type_` must be a class (or a fully qualified class name) of a Geometry derived class")
raise ValueError("`type_` must be a class (or class name) of a Geometry derived class")

if kwargs == None:
kwargs = {}
Expand Down
4 changes: 3 additions & 1 deletion src/pyFAI/test/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,9 @@ def test_promotion(self):
idmask = id(g.detector.mask)
ai = g.promote()
self.assertEqual(type(ai).__name__, "AzimuthalIntegrator", "Promote to AzimuthalIntegrator by default")
gr = g.promote("pyFAI.geometryRefinement.GeometryRefinement")
ai = g.promote("FiberIntegrator")
self.assertEqual(type(ai).__name__, "FiberIntegrator", "Promote to FiberIntegrator when requested")
gr = g.promote("GeometryRefinement")
self.assertEqual(type(gr).__name__, "GeometryRefinement", "Promote to GeometryRefinement when requested")
gr = ai.promote("pyFAI.geometryRefinement.GeometryRefinement")
self.assertEqual(type(gr).__name__, "GeometryRefinement", "Promote to GeometryRefinement when requested")
Expand Down
Loading