<commit_before>"""
Contains information relevant to Responses.
"""

import operator
import unidecode

from nordb.core.validationTools import validateFloat
from nordb.core.validationTools import validateInteger
from nordb.core.validationTools import validateString
from nordb.core.validationTools import validateDate
from nordb.core.utils import addString2String
from nordb.core.utils import addInteger2String
from nordb.core.utils import addFloat2String
from nordb.core.utils import stringToDate

class Response(object):
    """
    Class for response information. Always use eihter PazResponse or FapResponse instead of this class.

    :param array data: all the relevant data for response in an array. These values are accessed by its numerations.
    :ivar int c_id: Id of the response in the database
    :ivar string file_name: Name of the response file from which this object was read from
    :ivar string source: Source of the response
    :ivar string stage: stage of the response
    :ivar string description: description of the response
    :ivar string response_format: format of this response file. Either paz or fap
    :ivar string author: author of the response
    :ivar int FILE_NAME: Enumeration of the data list. Value of 0
    :ivar int SOURCE: Enumeration of the data list. Value of 1
    :ivar int STAGE: Enumeration of the data list. Value of 2
    :ivar int DESCRIPTION: Enumeration of the data list. Value of 3
    :ivar int RESPONSE_FORMAT: Enumeration of the data list. Value of 4
    :ivar int AUTHOR: Enumeration of the data list. Value of 5
    :ivar int ID: Enumeration of the data list. Value of 6
    """
    header_type = 14
    FILE_NAME = 0
    SOURCE = 1
    STAGE = 2
    DESCRIPTION = 3
    RESPONSE_FORMAT = 4
    AUTHOR = 5
    ID = 6

    def __init__(self, data):
        self.c_id = -1
        self.file_name = data[self.FILE_NAME]
        self.source = data[self.SOURCE]
        self.stage = data[self.STAGE]
        self.description = data[self.DESCRIPTION]
        self.response_format = data[self.RESPONSE_FORMAT]
        self.author = data[self.AUTHOR]
        self.response_id = data[self.ID]

    file_name = property(operator.attrgetter('_file_name'), doc="")

    @file_name.setter
    def file_name(self, val):
        val_file_name = validateString(val, "file_name", 0, 32, None, self.header_type)
        self._file_name = val_file_name

    source = property(operator.attrgetter('_source'), doc="")

    @source.setter
    def source(self, val):
        val_source = validateString(val, "source", 0, 32, None, self.header_type)
        self._source = val_source

    stage = property(operator.attrgetter('_stage'), doc="")

    @stage.setter
    def stage(self, val):
        val_stage = validateInteger(val, "stage", 0, 10, self.header_type)
        self._stage = val_stage

    description = property(operator.attrgetter('_description'), doc="")

    @description.setter
    def description(self, val):
        val_description = validateString(val, "description", 0, 32, None, self.header_type)
        self._description = val_description

    response_format = property(operator.attrgetter('_response_format'), doc="")

    @response_format.setter
    def response_format(self, val):
        val_response_format = validateString(val, "response_format", 3, 3, ["paz", "fap"], self.header_type)
        self._response_format = val_response_format

    author = property(operator.attrgetter('_author'), doc="")

    @author.setter
    def author(self, val):
        val_author = validateString(val, "author", 0, 32, None, self.header_type)
        self._author = val_author

    def __str__(self):
        response_string = "{0} {1} {2} {3} {4}\n".format(self.source, self.stage, self.description, self.response_format, self.author)

        return response_string

    def getAsList(self):
        response_list = [
                        self.c_id,
                        self.file_name,
                        self.source,
                        self.stage,
                        self.description,
                        self.response_format,
                        self.author,
                        ]

        return response_list


class PazResponse(Response):
    """
    Class for poles and zeros type of response. Inherits Response class.

    :ivar float scale_factor: scale factor of the response
    :ivar Array poles: array of all the poles in the response. Poles are arrays of floats and contain the imaginary value of the pole and the error of the pole
    :ivar Array zeros: array of all the zeros in the response. Poles are arrays of floats and contain the imaginary value of the zero and the error of the zero
    """
    def __init__(self, response_data, scale_factor, poles, zeros):
        Response.__init__(self, response_data)
        self.scale_factor = scale_factor
        self.poles = poles
        self.zeros = zeros

    scale_factor = property(operator.attrgetter('_scale_factor'), doc="")

    @scale_factor.setter
    def scale_factor(self, val):
        val_scale_factor = validateFloat(val, "scale_factor", None, None, self.header_type)
        self._scale_factor = val_scale_factor

    def getObspyResponse(self, mode="dis"):
        """
        Method for getting the response in a format suited for obspy.

        :param string mode: dis, vel or acc depending on which derivative of paz file you want
        :returns: response in a format fitting to obspy
        """
        if mode not in ["dis", "acc", "vel"]:
            raise Exception("{0} not a valid mode!".format(mode))

        obspy_resp = {'poles':[], 'zeros':[], 'sensitivity':self.scale_factor, 'gain':1.0}
        for p in self.poles:
            obspy_resp['poles'].append(complex(p[0], p[1]))

        ceil = len(self.zeros)
        if (mode == "vel"):
            ceil -= 1
        elif (mode == "acc"):
            ceil -= 2

        for i in range(0, ceil):
            obspy_resp['zeros'].append(complex(self.zeros[i][0],
                                               self.zeros[i][1]))
<commit_msg>Fixed a bug with pazresponse dropping a wrong number when turning it into obspy form<commit_after>"""
Contains information relevant to Responses.
"""

import operator
import unidecode

from nordb.core.validationTools import validateFloat
from nordb.core.validationTools import validateInteger
from nordb.core.validationTools import validateString
from nordb.core.validationTools import validateDate
from nordb.core.utils import addString2String
from nordb.core.utils import addInteger2String
from nordb.core.utils import addFloat2String
from nordb.core.utils import stringToDate

class Response(object):
    """
    Class for response information. Always use eihter PazResponse or FapResponse instead of this class.

    :param array data: all the relevant data for response in an array. These values are accessed by its numerations.
    :ivar int c_id: Id of the response in the database
    :ivar string file_name: Name of the response file from which this object was read from
    :ivar string source: Source of the response
    :ivar string stage: stage of the response
    :ivar string description: description of the response
    :ivar string response_format: format of this response file. Either paz or fap
    :ivar string author: author of the response
    :ivar int FILE_NAME: Enumeration of the data list. Value of 0
    :ivar int SOURCE: Enumeration of the data list. Value of 1
    :ivar int STAGE: Enumeration of the data list. Value of 2
    :ivar int DESCRIPTION: Enumeration of the data list. Value of 3
    :ivar int RESPONSE_FORMAT: Enumeration of the data list. Value of 4
    :ivar int AUTHOR: Enumeration of the data list. Value of 5
    :ivar int ID: Enumeration of the data list. Value of 6
    """
    header_type = 14
    FILE_NAME = 0
    SOURCE = 1
    STAGE = 2
    DESCRIPTION = 3
    RESPONSE_FORMAT = 4
    AUTHOR = 5
    ID = 6

    def __init__(self, data):
        self.c_id = -1
        self.file_name = data[self.FILE_NAME]
        self.source = data[self.SOURCE]
        self.stage = data[self.STAGE]
        self.description = data[self.DESCRIPTION]
        self.response_format = data[self.RESPONSE_FORMAT]
        self.author = data[self.AUTHOR]
        self.response_id = data[self.ID]

    file_name = property(operator.attrgetter('_file_name'), doc="")

    @file_name.setter
    def file_name(self, val):
        val_file_name = validateString(val, "file_name", 0, 32, None, self.header_type)
        self._file_name = val_file_name

    source = property(operator.attrgetter('_source'), doc="")

    @source.setter
    def source(self, val):
        val_source = validateString(val, "source", 0, 32, None, self.header_type)
        self._source = val_source

    stage = property(operator.attrgetter('_stage'), doc="")

    @stage.setter
    def stage(self, val):
        val_stage = validateInteger(val, "stage", 0, 10, self.header_type)
        self._stage = val_stage

    description = property(operator.attrgetter('_description'), doc="")

    @description.setter
    def description(self, val):
        val_description = validateString(val, "description", 0, 32, None, self.header_type)
        self._description = val_description

    response_format = property(operator.attrgetter('_response_format'), doc="")

    @response_format.setter
    def response_format(self, val):
        val_response_format = validateString(val, "response_format", 3, 3, ["paz", "fap"], self.header_type)
        self._response_format = val_response_format

    author = property(operator.attrgetter('_author'), doc="")

    @author.setter
    def author(self, val):
        val_author = validateString(val, "author", 0, 32, None, self.header_type)
        self._author = val_author

    def __str__(self):
        response_string = "{0} {1} {2} {3} {4}\n".format(self.source, self.stage, self.description, self.response_format, self.author)

        return response_string

    def getAsList(self):
        response_list = [
                        self.c_id,
                        self.file_name,
                        self.source,
                        self.stage,
                        self.description,
                        self.response_format,
                        self.author,
                        ]

        return response_list


class PazResponse(Response):
    """
    Class for poles and zeros type of response. Inherits Response class.

    :ivar float scale_factor: scale factor of the response
    :ivar Array poles: array of all the poles in the response. Poles are arrays of floats and contain the imaginary value of the pole and the error of the pole
    :ivar Array zeros: array of all the zeros in the response. Poles are arrays of floats and contain the imaginary value of the zero and the error of the zero
    """
    def __init__(self, response_data, scale_factor, poles, zeros):
        Response.__init__(self, response_data)
        self.scale_factor = scale_factor
        self.poles = poles
        self.zeros = zeros

    scale_factor = property(operator.attrgetter('_scale_factor'), doc="")

    @scale_factor.setter
    def scale_factor(self, val):
        val_scale_factor = validateFloat(val, "scale_factor", None, None, self.header_type)
        self._scale_factor = val_scale_factor

    def getObspyResponse(self, mode="dis"):
        """
        Method for getting the response in a format suited for obspy.

        :param string mode: dis, vel or acc depending on which derivative of paz file you want
        :returns: response in a format fitting to obspy
        """
        self.zeros = sorted(self.zeros)
        if mode not in ["dis", "acc", "vel"]:
            raise Exception("{0} not a valid mode!".format(mode))

        obspy_resp = {'poles':[], 'zeros':[], 'sensitivity':self.scale_factor, 'gain':1.0}
        for p in self.poles:
            obspy_resp['poles'].append(complex(p[0], p[1]))

        ceil = len(self.zeros)
        if (mode == "vel"):
            ceil -= 1
        elif (mode == "acc"):
            ceil -= 2

        for i in range(0, ceil):
            obspy_resp['zeros'].append(complex(self.zeros[i][0],
                                               self.zeros[i][1]))

{'fraction_non_alphanumeric': 0.09166462967489611, 'fraction_numerical': 0.008473885765501508, 'mean_word_length': 2.735240413877054, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4858813', 'n_tokens_mistral': 3665, 'n_tokens_neox': 3488, 'n_words': 1150}