<commit_before># Copyright (c) 2011 <NAME>
# All rights reserved.
#
# Please see the file COPYING in this distribution for usage
# terms.

import copy
from glimpse.models.misc import ImageLayerFromInputArray, SampleC1Patches
from glimpse.util import kernel
from glimpse.util import ACTIVATION_DTYPE
import numpy as np

class ModelOps(object):
  """Base class for the Viz2 model. This class implements all single-layer
  transformations."""

  def __init__(self, backend, params):
    self._backend = copy.copy(backend)
    self._params = copy.copy(params)
    self._s1_kernels = None
    self._s2_kernels = None

  @property
  def s1_kernels(self):
    """Get the S1 kernels array, generating it if unset."""
    # if kernels array is empty, then generate it using current model parameters
    if self._s1_kernels == None:
      p = self._params
      self._s1_kernels = kernel.MakeMultiScaleGaborKernels(
        kwidth = p.s1_kwidth, num_scales = p.num_scales,
        num_orientations = p.s1_num_orientations, num_phases = p.s1_num_phases,
        shift_orientations = True, scale_norm = True)
    return self._s1_kernels

  @s1_kernels.setter
  def s1_kernels(self, kernels):
    p = self._params
    expected_shape = (p.num_scales, p.s1_num_orientations, p.s1_num_phases,
        p.s1_kwidth, p.s1_kwidth)
    if kernels.shape != expected_shape:
      raise ValueError("S1 kernels have wrong shape: expected %s, got %s" % \
          (expected_shape, kernels.shape))
    else:
      self._s1_kernels = kernels.astype(ACTIVATION_DTYPE)

  @property
  def s2_kernels(self):
    return self._s2_kernels

  @s2_kernels.setter
  def s2_kernels(self, kernels):
    p = self._params
    expected_shape = p.s1_num_orientations, p.s2_kwidth, p.s2_kwidth
    if kernels.shape[1:] != expected_shape:
      raise ValueError("S2 kernels have wrong shape: expected "
          " (*, %s, %s, %s), got %s" % (expected_shape + \
          [kernels.shape]))
    else:
      self._s2_kernels = kernels.astype(ACTIVATION_DTYPE)

  def BuildImageFromInput(self, input_):
    """Create the initial image layer from some input.
    input_ -- Image or (2-D) array of input data. If array, values should lie in
              the range [0, 1].
    RETURNS (2-D) array containing image layer data
    """
    return ImageLayerFromInputArray(input_, self._backend)

  def BuildRetinaFromImage(self, img):
    p = self._params
    if not p.retina_enabled:
      return img
    retina = self._backend.ContrastEnhance(img, kwidth = p.retina_kwidth,
        bias = p.retina_bias, scaling = 1)
    return retina

  def BuildS1FromRetina(self, retina):
    """Apply S1 processing to some existing retinal layer data.
    retina -- (2-D array) result of retinal layer processing
    RETURNS list of (4-D) S1 activity arrays, with one array per scale
    """
    # Reshape retina to be 3D array<commit_msg>Fixed bug when unsetting Viz2 kernels.<commit_after># Copyright (c) 2011 <NAME>
# All rights reserved.
#
# Please see the file COPYING in this distribution for usage
# terms.

import copy
from glimpse.models.misc import ImageLayerFromInputArray, SampleC1Patches
from glimpse.util import kernel
from glimpse.util import ACTIVATION_DTYPE
import numpy as np

class ModelOps(object):
  """Base class for the Viz2 model. This class implements all single-layer
  transformations."""

  def __init__(self, backend, params):
    self._backend = copy.copy(backend)
    self._params = copy.copy(params)
    self._s1_kernels = None
    self._s2_kernels = None

  @property
  def s1_kernels(self):
    """Get the S1 kernels array, generating it if unset."""
    # if kernels array is empty, then generate it using current model parameters
    if self._s1_kernels == None:
      p = self._params
      self._s1_kernels = kernel.MakeMultiScaleGaborKernels(
        kwidth = p.s1_kwidth, num_scales = p.num_scales,
        num_orientations = p.s1_num_orientations, num_phases = p.s1_num_phases,
        shift_orientations = True, scale_norm = True)
    return self._s1_kernels

  @s1_kernels.setter
  def s1_kernels(self, kernels):
    if kernels == None:
      self._s1_kernels = None
      return
    p = self._params
    expected_shape = (p.num_scales, p.s1_num_orientations, p.s1_num_phases,
        p.s1_kwidth, p.s1_kwidth)
    if kernels.shape != expected_shape:
      raise ValueError("S1 kernels have wrong shape: expected %s, got %s" % \
          (expected_shape, kernels.shape))
    else:
      self._s1_kernels = kernels.astype(ACTIVATION_DTYPE)

  @property
  def s2_kernels(self):
    return self._s2_kernels

  @s2_kernels.setter
  def s2_kernels(self, kernels):
    if kernels == None:
      self._s2_kernels = None
      return
    p = self._params
    expected_shape = p.s1_num_orientations, p.s2_kwidth, p.s2_kwidth
    if kernels.shape[1:] != expected_shape:
      raise ValueError("S2 kernels have wrong shape: expected "
          " (*, %s, %s, %s), got %s" % (expected_shape + \
          [kernels.shape]))
    else:
      self._s2_kernels = kernels.astype(ACTIVATION_DTYPE)

  def BuildImageFromInput(self, input_):
    """Create the initial image layer from some input.
    input_ -- Image or (2-D) array of input data. If array, values should lie in
              the range [0, 1].
    RETURNS (2-D) array containing image layer data
    """
    return ImageLayerFromInputArray(input_, self._backend)

  def BuildRetinaFromImage(self, img):
    p = self._params
    if not p.retina_enabled:
      return img
    retina = self._backend.ContrastEnhance(img, kwidth = p.retina_kwidth,
        bias = p.retina_bias, scaling = 1)
    return retina

  def BuildS1FromRetina(self, retina):
    """Apply S1 processing to some existing retinal layer data.
    retina -- (2-D array) result of retinal layer processing
    RETURNS list of (4-D) S1 activity arrays, with one array per scale
    """
    # Reshape retina to be 3D array
{'fraction_non_alphanumeric': 0.09121678795058386, 'fraction_numerical': 0.015738703672364192, 'mean_word_length': 3.236559139784946, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1329946', 'n_tokens_mistral': 2005, 'n_tokens_neox': 1908, 'n_words': 558}