<commit_before># Copyright 2017 Battelle Energy Alliance, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Created on July 10, 2013

@author: alfoa
"""
#External Modules------------------------------------------------------------------------------------
import numpy as np
import copy
from collections import OrderedDict
#External Modules End--------------------------------------------------------------------------------

#Internal Modules------------------------------------------------------------------------------------
from .PostProcessorInterface import PostProcessorInterface
from utils import InputData, InputTypes, utils, mathUtils
from SupervisedLearning import factory as romFactory
import GridEntities
import Files
#Internal Modules End--------------------------------------------------------------------------------

class LimitSurface(PostProcessorInterface):
  """
    LimitSurface filter class. It computes the limit surface associated to a dataset
  """

  @classmethod
  def getInputSpecification(cls):
    """
      Method to get a reference to a class that specifies the input data for
      class cls.
      @ In, cls, the class for which we are retrieving the specification
      @ Out, inputSpecification, InputData.ParameterInput, class to use for
        specifying input of cls.
    """
    ## This will replace the lines above
    inputSpecification = super().getInputSpecification()

    ParametersInput = InputData.parameterInputFactory("parameters", contentType=InputTypes.StringType)
    inputSpecification.addSub(ParametersInput)

    ToleranceInput = InputData.parameterInputFactory("tolerance", contentType=InputTypes.FloatType)
    inputSpecification.addSub(ToleranceInput)

    SideInput = InputData.parameterInputFactory("side", contentType=InputTypes.StringType)
    inputSpecification.addSub(SideInput)

    ROMInput = InputData.parameterInputFactory("ROM", contentType=InputTypes.StringType)
    ROMInput.addParam("class", InputTypes.StringType)
    ROMInput.addParam("type", InputTypes.StringType)
    inputSpecification.addSub(ROMInput)

    FunctionInput = InputData.parameterInputFactory("Function", contentType=InputTypes.StringType)
    FunctionInput.addParam("class", InputTypes.StringType)
    FunctionInput.addParam("type", InputTypes.StringType)
    inputSpecification.addSub(FunctionInput)

    return inputSpecification

  def __init__(self):
    """
      Constructor
      @ In, None
      @ Out, None
    """
    super().__init__()
    self.parameters        = {}               #parameters dictionary (they are basically stored into a dictionary identified by tag "targets"
    self.surfPoint         = None             #coordinate of the points considered on the limit surface
    self.testMatrix        = OrderedDict()    #This is the n-dimensional matrix representing the testing grid
    self.gridCoord         = {}               #Grid coordinates
    self.functionValue     = {}               #This a dictionary that contains np vectors with the value for each variable and for the goal function
    self.ROM               = None             #Pointer to a ROM
    self.externalFunction  = None             #Pointer to an external Function
    self.tolerance         = 1.0e-4           #SubGrid tolerance
    self.gridFromOutside   = False            #The grid has been passed from outside (self._initFromDict)?
    self.lsSide            = "negative"       # Limit surface side to compute the LS for (negative,positive,both)
    self.gridEntity        = None             # the Grid object
    self.bounds            = None             # the container for the domain of search
    self.jobHandler        = None             # job handler pointer
    self.transfMethods     = {}               # transformation methods container
    self.crossedLimitSurf  = False            # Limit surface has been crossed?
    self.addAssemblerObject('ROM', InputData.Quantity.zero_to_one)
    self.addAssemblerObject('Function', InputData.Quantity.one)
    self.printTag = 'POSTPROCESSOR LIMITSURFACE'

  def _localWhatDoINeed(self):
    """
      This method is a local mirror of the general whatDoINeed method.
      It is implemented by the samplers that need to request special objects
      @ In, None
      @ Out, needDict, dict, list of objects needed
    """
    needDict = {'internal':[(None,'jobHandler')]}
    return needDict

  def _localGenerateAssembler(self,initDict):
    """
      Generates the assembler.
      @ In, initDict, dict, dict of init objects
      @ Out, None
    """
    self.jobHandler = initDict['internal']['jobHandler']

  def _initializeLSpp(self, runInfo, inputs, initDict):
    """
      Method to initialize the LS post processor (create grid, etc.)
      @ In, runInfo, dict, dictionary of run info (e.g. working dir, etc)
      @ In, inputs, list, list of inputs
      @ In, initDict, dict, dictionary with initialization options
      @ Out, None
    """
    super().initialize(runInfo, inputs, initDict)
    self.gridEntity = GridEntities.factory.returnInstance("MultiGridEntity")
    self.externalFunction = self.assemblerDict['Function'][0][3]
    if 'ROM' not in self.assemblerDict.keys():
      self.ROM = romFactory.returnInstance('KNeighborsClassifier')
      paramDict = {'Features':list(self.parameters['targets']), 'Target':[self.externalFunction.name]}
      self.ROM.initializeFromDict(paramDict)
      settings = {"n_neighbors":1}
      self.ROM.initializeModel(settings)
    else:
      self.ROM = self.assemblerDict['ROM'][0][3]
    # self.ROM.reset()
    self.indexes = -1
    for index, inp in enumerate(self.inputs):
      if mathUtils.isAString(inp)  or isinstance(inp, bytes):
        self.raiseAnError(IOError, 'LimitSurface PostProcessor only accepts Data(s) as inputs. Got string type!')
      if inp.type == 'PointSet':
        self.indexes = index
    if self.indexes == -1:
      self.raiseAnError(IOError, 'LimitSurface PostProcessor needs a PointSet as INPUT!!!!!!')
    #else:
    #  # check if parameters are contained in the data
    #  inpKeys = self.inputs[self.indexes].getParaKeys("inputs")
    #  outKeys = self.inputs[self.indexes].getParaKeys("outputs")
    #  self.paramType = {}
    #  for param in self.parameters['targets']:
    #    if param not in inpKeys + outKeys:<commit_msg>clean up<commit_after># Copyright 2017 Battelle Energy Alliance, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Created on July 10, 2013

@author: alfoa
"""
#External Modules------------------------------------------------------------------------------------
import numpy as np
import copy
from collections import OrderedDict
#External Modules End--------------------------------------------------------------------------------

#Internal Modules------------------------------------------------------------------------------------
from .PostProcessorInterface import PostProcessorInterface
from utils import InputData, InputTypes, utils, mathUtils
from SupervisedLearning import factory as romFactory
import GridEntities
import Files
#Internal Modules End--------------------------------------------------------------------------------

class LimitSurface(PostProcessorInterface):
  """
    LimitSurface filter class. It computes the limit surface associated to a dataset
  """

  @classmethod
  def getInputSpecification(cls):
    """
      Method to get a reference to a class that specifies the input data for
      class cls.
      @ In, cls, the class for which we are retrieving the specification
      @ Out, inputSpecification, InputData.ParameterInput, class to use for
        specifying input of cls.
    """
    ## This will replace the lines above
    inputSpecification = super().getInputSpecification()

    ParametersInput = InputData.parameterInputFactory("parameters", contentType=InputTypes.StringType)
    inputSpecification.addSub(ParametersInput)

    ToleranceInput = InputData.parameterInputFactory("tolerance", contentType=InputTypes.FloatType)
    inputSpecification.addSub(ToleranceInput)

    SideInput = InputData.parameterInputFactory("side", contentType=InputTypes.StringType)
    inputSpecification.addSub(SideInput)

    ROMInput = InputData.parameterInputFactory("ROM", contentType=InputTypes.StringType)
    ROMInput.addParam("class", InputTypes.StringType)
    ROMInput.addParam("type", InputTypes.StringType)
    inputSpecification.addSub(ROMInput)

    FunctionInput = InputData.parameterInputFactory("Function", contentType=InputTypes.StringType)
    FunctionInput.addParam("class", InputTypes.StringType)
    FunctionInput.addParam("type", InputTypes.StringType)
    inputSpecification.addSub(FunctionInput)

    return inputSpecification

  def __init__(self):
    """
      Constructor
      @ In, None
      @ Out, None
    """
    super().__init__()
    self.parameters        = {}               #parameters dictionary (they are basically stored into a dictionary identified by tag "targets"
    self.surfPoint         = None             #coordinate of the points considered on the limit surface
    self.testMatrix        = OrderedDict()    #This is the n-dimensional matrix representing the testing grid
    self.gridCoord         = {}               #Grid coordinates
    self.functionValue     = {}               #This a dictionary that contains np vectors with the value for each variable and for the goal function
    self.ROM               = None             #Pointer to a ROM
    self.externalFunction  = None             #Pointer to an external Function
    self.tolerance         = 1.0e-4           #SubGrid tolerance
    self.gridFromOutside   = False            #The grid has been passed from outside (self._initFromDict)?
    self.lsSide            = "negative"       # Limit surface side to compute the LS for (negative,positive,both)
    self.gridEntity        = None             # the Grid object
    self.bounds            = None             # the container for the domain of search
    self.jobHandler        = None             # job handler pointer
    self.transfMethods     = {}               # transformation methods container
    self.crossedLimitSurf  = False            # Limit surface has been crossed?
    self.addAssemblerObject('ROM', InputData.Quantity.zero_to_one)
    self.addAssemblerObject('Function', InputData.Quantity.one)
    self.printTag = 'POSTPROCESSOR LIMITSURFACE'

  def _localWhatDoINeed(self):
    """
      This method is a local mirror of the general whatDoINeed method.
      It is implemented by the samplers that need to request special objects
      @ In, None
      @ Out, needDict, dict, list of objects needed
    """
    needDict = {'internal':[(None,'jobHandler')]}
    return needDict

  def _localGenerateAssembler(self,initDict):
    """
      Generates the assembler.
      @ In, initDict, dict, dict of init objects
      @ Out, None
    """
    self.jobHandler = initDict['internal']['jobHandler']

  def _initializeLSpp(self, runInfo, inputs, initDict):
    """
      Method to initialize the LS post processor (create grid, etc.)
      @ In, runInfo, dict, dictionary of run info (e.g. working dir, etc)
      @ In, inputs, list, list of inputs
      @ In, initDict, dict, dictionary with initialization options
      @ Out, None
    """
    super().initialize(runInfo, inputs, initDict)
    self.gridEntity = GridEntities.factory.returnInstance("MultiGridEntity")
    self.externalFunction = self.assemblerDict['Function'][0][3]
    if 'ROM' not in self.assemblerDict.keys():
      self.ROM = romFactory.returnInstance('KNeighborsClassifier')
      paramDict = {'Features':list(self.parameters['targets']), 'Target':[self.externalFunction.name]}
      self.ROM.initializeFromDict(paramDict)
      settings = {"n_neighbors":1}
      self.ROM.initializeModel(settings)
    else:
      self.ROM = self.assemblerDict['ROM'][0][3]
    self.indexes = -1
    for index, inp in enumerate(self.inputs):
      if mathUtils.isAString(inp)  or isinstance(inp, bytes):
        self.raiseAnError(IOError, 'LimitSurface PostProcessor only accepts Data(s) as inputs. Got string type!')
      if inp.type == 'PointSet':
        self.indexes = index
    if self.indexes == -1:
      self.raiseAnError(IOError, 'LimitSurface PostProcessor needs a PointSet as INPUT!!!!!!')
    #else:
    #  # check if parameters are contained in the data
    #  inpKeys = self.inputs[self.indexes].getParaKeys("inputs")
    #  outKeys = self.inputs[self.indexes].getParaKeys("outputs")
    #  self.paramType = {}
    #  for param in self.parameters['targets']:
    #    if param not in inpKeys + outKeys:
{'fraction_non_alphanumeric': 0.1314743967039435, 'fraction_numerical': 0.003531489111241907, 'mean_word_length': 2.67775974025974, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4221750', 'n_tokens_mistral': 3501, 'n_tokens_neox': 3408, 'n_words': 1099}