
import numpy as np
from numpy import ma

from matplotlib.colors import Normalize


__all__ = ['ImageNormalize']


class ImageNormalize(Normalize):

    def __init__(self, vmin=None, vmax=None, stretch=None, clip=True):

        super(ImageNormalize, self).__init__(vmin=vmin, vmax=vmax, clip=clip)

        self.vmin = vmin
        self.vmax = vmax
        self.stretch = stretch
        self.inverse_stretch = stretch.inverted()

    def __call__(self, values, clip=None):

        if clip is None:
            clip = self.clip

        # Convert to masked array and make sure scalars get broadcast to 1-d
        values = np.atleast_1d(values)

        # Normalize based on vmin and vmax
        values_norm = (values - self.vmin) / (self.vmax - self.vmin)

        # Clip to the 0 to 1 range
        if self.clip:
            values_norm = np.clip(values_norm, 0., 1.)

        # Stretch values
        new_values = self.stretch(values_norm)

        # Don't assume stretch returned a masked array
        return ma.asarray(new_values)

    def inverse(self, values):

        # Find unstretched values in range 0 to 1
        values_norm = self.inverse_stretch(values)

        # Scale to original range
        return values_norm * (self.vmax - self.vmin) + self.vmin


import numpy as np
from numpy import ma

from matplotlib.colors import Normalize


__all__ = ['ImageNormalize']


class ImageNormalize(Normalize):

    def __init__(self, vmin=None, vmax=None, stretch=None, clip=True):

        super(ImageNormalize, self).__init__(vmin=vmin, vmax=vmax, clip=clip)

        self.vmin = vmin
        self.vmax = vmax
        self.stretch = stretch
        self.inverse_stretch = stretch.inverted()

    def __call__(self, values, clip=None):

        if clip is None:
            clip = self.clip

        # Make sure scalars get broadcast to 1-d
        if np.isscalar(values):
            values = np.atleast_1d(values)
        else:
            values = values.copy()  # copy because of in-place operations after

        # Normalize based on vmin and vmax
        np.subtract(values, self.vmin, out=values)
        np.divide(values, self.vmax - self.vmin, out=values)

        # Clip to the 0 to 1 range
        if self.clip:
            values = np.clip(values, 0., 1., out=values)

        # Stretch values
        values = self.stretch(values, out=values)

        # Convert to masked array for matplotlib
        return ma.array(values)

    def inverse(self, values):

        # Find unstretched values in range 0 to 1
        values_norm = self.inverse_stretch(values)

        # Scale to original range
        return values_norm * (self.vmax - self.vmin) + self.vmin

Use in-place operations in ImageNormalize
Use in-place operations in ImageNormalize

  
  import numpy as np
  from numpy import ma
  
  from matplotlib.colors import Normalize
  
  
  __all__ = ['ImageNormalize']
  
  
  class ImageNormalize(Normalize):
  
      def __init__(self, vmin=None, vmax=None, stretch=None, clip=True):
  
          super(ImageNormalize, self).__init__(vmin=vmin, vmax=vmax, clip=clip)
  
          self.vmin = vmin
          self.vmax = vmax
          self.stretch = stretch
          self.inverse_stretch = stretch.inverted()
  
      def __call__(self, values, clip=None):
  
          if clip is None:
              clip = self.clip
  
-         # Convert to masked array and make sure scalars get broadcast to 1-d
+         # Make sure scalars get broadcast to 1-d
+         if np.isscalar(values):
-         values = np.atleast_1d(values)
+             values = np.atleast_1d(values)
+         else:
+             values = values.copy()  # copy because of in-place operations after
  
          # Normalize based on vmin and vmax
-         values_norm = (values - self.vmin) / (self.vmax - self.vmin)
+         np.subtract(values, self.vmin, out=values)
+         np.divide(values, self.vmax - self.vmin, out=values)
  
          # Clip to the 0 to 1 range
          if self.clip:
-             values_norm = np.clip(values_norm, 0., 1.)
+             values = np.clip(values, 0., 1., out=values)
  
          # Stretch values
-         new_values = self.stretch(values_norm)
+         values = self.stretch(values, out=values)
  
-         # Don't assume stretch returned a masked array
+         # Convert to masked array for matplotlib
-         return ma.asarray(new_values)
+         return ma.array(values)
  
      def inverse(self, values):
  
          # Find unstretched values in range 0 to 1
          values_norm = self.inverse_stretch(values)
  
          # Scale to original range
          return values_norm * (self.vmax - self.vmin) + self.vmin
  
Use in-place operations in ImageNormalize
## Code Before:

import numpy as np
from numpy import ma

from matplotlib.colors import Normalize


__all__ = ['ImageNormalize']


class ImageNormalize(Normalize):

    def __init__(self, vmin=None, vmax=None, stretch=None, clip=True):

        super(ImageNormalize, self).__init__(vmin=vmin, vmax=vmax, clip=clip)

        self.vmin = vmin
        self.vmax = vmax
        self.stretch = stretch
        self.inverse_stretch = stretch.inverted()

    def __call__(self, values, clip=None):

        if clip is None:
            clip = self.clip

        # Convert to masked array and make sure scalars get broadcast to 1-d
        values = np.atleast_1d(values)

        # Normalize based on vmin and vmax
        values_norm = (values - self.vmin) / (self.vmax - self.vmin)

        # Clip to the 0 to 1 range
        if self.clip:
            values_norm = np.clip(values_norm, 0., 1.)

        # Stretch values
        new_values = self.stretch(values_norm)

        # Don't assume stretch returned a masked array
        return ma.asarray(new_values)

    def inverse(self, values):

        # Find unstretched values in range 0 to 1
        values_norm = self.inverse_stretch(values)

        # Scale to original range
        return values_norm * (self.vmax - self.vmin) + self.vmin

## Instruction:
Use in-place operations in ImageNormalize
## Code After:

import numpy as np
from numpy import ma

from matplotlib.colors import Normalize


__all__ = ['ImageNormalize']


class ImageNormalize(Normalize):

    def __init__(self, vmin=None, vmax=None, stretch=None, clip=True):

        super(ImageNormalize, self).__init__(vmin=vmin, vmax=vmax, clip=clip)

        self.vmin = vmin
        self.vmax = vmax
        self.stretch = stretch
        self.inverse_stretch = stretch.inverted()

    def __call__(self, values, clip=None):

        if clip is None:
            clip = self.clip

        # Make sure scalars get broadcast to 1-d
        if np.isscalar(values):
            values = np.atleast_1d(values)
        else:
            values = values.copy()  # copy because of in-place operations after

        # Normalize based on vmin and vmax
        np.subtract(values, self.vmin, out=values)
        np.divide(values, self.vmax - self.vmin, out=values)

        # Clip to the 0 to 1 range
        if self.clip:
            values = np.clip(values, 0., 1., out=values)

        # Stretch values
        values = self.stretch(values, out=values)

        # Convert to masked array for matplotlib
        return ma.array(values)

    def inverse(self, values):

        # Find unstretched values in range 0 to 1
        values_norm = self.inverse_stretch(values)

        # Scale to original range
        return values_norm * (self.vmax - self.vmin) + self.vmin
