Skip to content

Commit

Permalink
fix issue #45: convert several classes to abstract
Browse files Browse the repository at this point in the history
LayerModel, Material, and DarwinModelAlloy are now abstract classes
  • Loading branch information
dkriegner committed Oct 30, 2017
1 parent 64f4506 commit 05099ef
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
7 changes: 1 addition & 6 deletions xrayutilities/gridder.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@
from . import config, cxrayutilities, utilities
from .exception import InputError

try: # works in Python >3.4
ABC = abc.ABC
except: # Python 2.7
ABC = abc.ABCMeta('ABC', (object, ), {'__slots__': ()})


def delta(min_value, max_value, n):
"""
Expand Down Expand Up @@ -75,7 +70,7 @@ def ones(*args):
return numpy.ones(args, dtype=numpy.double)


class Gridder(ABC):
class Gridder(utilities.ABC):
"""
Basis class for gridders in xrayutilities. A gridder is a function mapping
irregular spaced data onto a regular grid by binning the data into equally
Expand Down
14 changes: 12 additions & 2 deletions xrayutilities/materials/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
materials their crystalline state is defined few materials are also included as
amorphous which can be useful for calculation of their optical properties.
"""

import abc
import copy
import numbers
import operator
Expand Down Expand Up @@ -120,7 +120,7 @@ def Cijkl2Cij(cijkl):
return cij


class Material(object):
class Material(utilities.ABC):
"""
base class for all Materials. common properties of amorphous and
crystalline materials are described by this class from which Amorphous and
Expand Down Expand Up @@ -176,10 +176,20 @@ def _getdensity(self):
lam = property(_getlam)
nu = property(_getnu)

@abc.abstractmethod
def delta(self, en='config'):
"""
abstract method which every implementation of a Material has to
override
"""
pass

@abc.abstractmethod
def ibeta(self, en='config'):
"""
abstract method which every implementation of a Material has to
override
"""
pass

def chi0(self, en='config'):
Expand Down
16 changes: 8 additions & 8 deletions xrayutilities/simpack/darwin_theory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2016 Dominik Kriegner <dominik.kriegner@gmail.com>

import abc
import collections
import copy
import warnings
Expand All @@ -24,7 +24,7 @@
from scipy.misc import derivative

from . import LayerModel
from .. import materials
from .. import materials, utilities
from ..math import heaviside


Expand Down Expand Up @@ -112,7 +112,7 @@ def __init__(self, qz, qx=0, qy=0, **kwargs):
super(LayerModel, self).__init__(exp, **kwargs)

self.npoints = len(qz)
self.qz = qz
self.qz = numpy.asarray(qz)
self.qinp = (qx, qy)
if self.qinp != (0, 0):
raise NotImplementedError('asymmetric CTR simulation is not yet '
Expand Down Expand Up @@ -242,7 +242,7 @@ def _recur_sim(self, nrep, ml, r, rbar, t, pol):
return r, rbar, t


class DarwinModelAlloy(DarwinModel):
class DarwinModelAlloy(DarwinModel, utilities.ABC):
"""
extension of the DarwinModel for an binary alloy system were one parameter
is used to determine the chemical composition
Expand All @@ -251,11 +251,11 @@ class DarwinModelAlloy(DarwinModel):
get_dperp_apar() method and define the substrate lattice parameter (asub).
See the DarwinModelSiGe001 class for an implementation example.
"""
@classmethod
def get_dperp_apar(cls, x, apar, r=1):
@abc.abstractmethod
def get_dperp_apar(self, x, apar, r=1):
"""
calculate inplane lattice parameter and the out of plane lattice plane
spacing (of the atomic planes!) from composition and relaxation
spacing (of the atomic planes!) from composition and relaxation.
Parameters
----------
Expand All @@ -269,7 +269,7 @@ def get_dperp_apar(cls, x, apar, r=1):
-------
dperp, apar
"""
pass
raise NotImplementedError("abstract method needs to be overwritten")

def make_monolayers(self, s):
"""
Expand Down
12 changes: 11 additions & 1 deletion xrayutilities/simpack/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#
# Copyright (C) 2016 Dominik Kriegner <dominik.kriegner@gmail.com>

import abc

import numpy
import scipy.constants as constants
import scipy.interpolate as interpolate
Expand Down Expand Up @@ -124,7 +126,7 @@ def scale_simulation(self, y):
return y * self.I0 + self.background


class LayerModel(Model):
class LayerModel(Model, utilities.ABC):
"""
generic model class from which further thin film models can be derived from
"""
Expand All @@ -150,6 +152,14 @@ def __init__(self, *args, **kwargs):
self.lstack = LayerStack('Stack for %s' % self.__class__.__name__,
*args)

@abc.abstractmethod
def simulate(self):
"""
abstract method that every implementation of a LayerModel has to
override
"""
pass

def _create_return(self, x, E, ai=None, af=None, Ir=None,
rettype='intensity'):
"""
Expand Down
6 changes: 6 additions & 0 deletions xrayutilities/utilities_noconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
this part of utilities does not need the config class
"""

import abc
import numbers
import os.path

Expand All @@ -28,6 +29,11 @@

from .exception import InputError

try: # works in Python >3.4
ABC = abc.ABC
except: # Python 2.7
ABC = abc.ABCMeta('ABC', (object, ), {'__slots__': ()})

# python 2to3 compatibility
try:
basestring
Expand Down

0 comments on commit 05099ef

Please sign in to comment.