Beispiel #1
0
    def _create_header(self, columns: FieldLike) -> None:
        # Check types of fieldlike column descriptors and convert them to field
        # descriptors, that are accepted by dataclasses.make_dataclass()
        fields: list = []
        for each in columns:
            if isinstance(each, str):
                fields.append(each)
                continue
            check.has_type(f"field {each}", each, tuple)
            check.has_size(f"field {each}", each, min_size=2, max_size=3)
            check.has_type("first arg", each[0], str)
            check.has_type("second arg", each[1], type)
            if len(each) == 2:
                fields.append(each)
                continue
            check.has_type("third arg", each[2], (Field, dict))
            if isinstance(each[2], Field):
                fields.append(each)
                continue
            field = dataclasses.field(**each[2])
            fields.append(each[:2] + (field,))

        # Create record namespace with table hooks
        namespace = {
            '_create_row_id': self._create_row_id,
            '_delete_hook': self._remove_row_id,
            '_restore_hook': self._append_row_id,
            '_update_hook': self._update_row_diff,
            '_revoke_hook': self._remove_row_diff}

        # Create Record dataclass and constructor
        self._Record = dataclasses.make_dataclass(
            'Row', fields, bases=(Record, ), namespace=namespace)

        # Create slots
        self._Record.__slots__ = ['id', 'state'] + [
            field.name for field in dataclasses.fields(self._Record)]

        # Reset store, diff and index
        self._store = []
        self._diff = []
        self._index = []
class PaginationAdvisory(ABC):
    message: str = field(init=False)
Beispiel #3
0
class DataTrainingArguments:
    """
    Arguments pertaining to what data we are going to input our model for training and eval.
    """

    dataset_name: Optional[str] = field(
        default=None,
        metadata={
            "help":
            "The name of the dataset to use (via the datasets library)."
        })
    dataset_config_name: Optional[str] = field(
        default=None,
        metadata={
            "help":
            "The configuration name of the dataset to use (via the datasets library)."
        })
    train_file: Optional[str] = field(
        default=None,
        metadata={"help": "The input training data file (a text file)."})
    validation_file: Optional[str] = field(
        default=None,
        metadata={
            "help":
            "An optional input evaluation data file to evaluate the perplexity on (a text file)."
        },
    )
    overwrite_cache: bool = field(
        default=False,
        metadata={"help": "Overwrite the cached training and evaluation sets"})
    validation_split_percentage: Optional[int] = field(
        default=5,
        metadata={
            "help":
            "The percentage of the train set used as validation set in case there's no validation split"
        },
    )
    max_seq_length: Optional[int] = field(
        default=None,
        metadata={
            "help":
            ("The maximum total input sequence length after tokenization. Sequences longer "
             "than this will be truncated.")
        },
    )
    preprocessing_num_workers: Optional[int] = field(
        default=None,
        metadata={
            "help": "The number of processes to use for the preprocessing."
        },
    )
    mlm_probability: float = field(
        default=0.15,
        metadata={
            "help": "Ratio of tokens to mask for masked language modeling loss"
        })
    line_by_line: bool = field(
        default=False,
        metadata={
            "help":
            "Whether distinct lines of text in the dataset are to be handled as distinct sequences."
        },
    )
    pad_to_max_length: bool = field(
        default=False,
        metadata={
            "help":
            ("Whether to pad all samples to `max_seq_length`. "
             "If False, will pad the samples dynamically when batching to the maximum length in the batch."
             )
        },
    )
    max_train_samples: Optional[int] = field(
        default=None,
        metadata={
            "help":
            ("For debugging purposes or quicker training, truncate the number of training examples to this "
             "value if set.")
        },
    )
    max_eval_samples: Optional[int] = field(
        default=None,
        metadata={
            "help":
            ("For debugging purposes or quicker training, truncate the number of evaluation examples to this "
             "value if set.")
        },
    )

    def __post_init__(self):
        if self.dataset_name is None and self.train_file is None and self.validation_file is None:
            raise ValueError(
                "Need either a dataset name or a training/validation file.")
        else:
            if self.train_file is not None:
                extension = self.train_file.split(".")[-1]
                assert extension in [
                    "csv", "json", "txt"
                ], "`train_file` should be a csv, a json or a txt file."
            if self.validation_file is not None:
                extension = self.validation_file.split(".")[-1]
                assert extension in [
                    "csv", "json", "txt"
                ], "`validation_file` should be a csv, a json or a txt file."
class DataTrainingArguments:
    """
    Arguments pertaining to what data we are going to input our model for training and eval.
    """

    dataset_name: Optional[str] = field(
        default=None, metadata={"help": "The name of the dataset to use (via the datasets library)."}
    )
    dataset_config_name: Optional[str] = field(
        default=None, metadata={"help": "The configuration name of the dataset to use (via the datasets library)."}
    )
    train_file: Optional[str] = field(default=None, metadata={"help": "The input training data file (a text file)."})
    validation_file: Optional[str] = field(
        default=None,
        metadata={"help": "An optional input evaluation data file to evaluate the perplexity on (a text file)."},
    )
    test_file: Optional[str] = field(
        default=None,
        metadata={"help": "An optional input test data file to test the perplexity on (a text file)."},
    )
    overwrite_cache: bool = field(
        default=False, metadata={"help": "Overwrite the cached training and evaluation sets"}
    )
    preprocessing_num_workers: Optional[int] = field(
        default=None,
        metadata={"help": "The number of processes to use for the preprocessing."},
    )
    max_seq_length: int = field(
        default=384,
        metadata={
            "help": "The maximum total input sequence length after tokenization. Sequences longer "
            "than this will be truncated, sequences shorter will be padded."
        },
    )
    pad_to_max_length: bool = field(
        default=True,
        metadata={
            "help": "Whether to pad all samples to `max_seq_length`. "
            "If False, will pad the samples dynamically when batching to the maximum length in the batch (which can "
            "be faster on GPU but will be slower on TPU)."
        },
    )
    max_train_samples: Optional[int] = field(
        default=None,
        metadata={
            "help": "For debugging purposes or quicker training, truncate the number of training examples to this "
            "value if set."
        },
    )
    max_val_samples: Optional[int] = field(
        default=None,
        metadata={
            "help": "For debugging purposes or quicker training, truncate the number of validation examples to this "
            "value if set."
        },
    )
    max_test_samples: Optional[int] = field(
        default=None,
        metadata={
            "help": "For debugging purposes or quicker training, truncate the number of test examples to this "
            "value if set."
        },
    )
    version_2_with_negative: bool = field(
        default=False, metadata={"help": "If true, some of the examples do not have an answer."}
    )
    null_score_diff_threshold: float = field(
        default=0.0,
        metadata={
            "help": "The threshold used to select the null answer: if the best answer has a score that is less than "
            "the score of the null answer minus this threshold, the null answer is selected for this example. "
            "Only useful when `version_2_with_negative=True`."
        },
    )
    doc_stride: int = field(
        default=128,
        metadata={"help": "When splitting up a long document into chunks, how much stride to take between chunks."},
    )
    n_best_size: int = field(
        default=20,
        metadata={"help": "The total number of n-best predictions to generate when looking for an answer."},
    )
    max_answer_length: int = field(
        default=30,
        metadata={
            "help": "The maximum length of an answer that can be generated. This is needed because the start "
            "and end predictions are not conditioned on one another."
        },
    )

    def __post_init__(self):
        if (
            self.dataset_name is None
            and self.train_file is None
            and self.validation_file is None
            and self.test_file is None
        ):
            raise ValueError("Need either a dataset name or a training/validation/test file.")
        else:
            if self.train_file is not None:
                extension = self.train_file.split(".")[-1]
                assert extension in ["csv", "json"], "`train_file` should be a csv or a json file."
            if self.validation_file is not None:
                extension = self.validation_file.split(".")[-1]
                assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file."
            if self.test_file is not None:
                extension = self.test_file.split(".")[-1]
                assert extension in ["csv", "json"], "`test_file` should be a csv or a json file."
class Parameter:
    """
    {begin_markdown Parameter}

    {spell_markdown param init}

    # `curvefit.core.parameter.Parameter`
    ## A parameter for the functional form of the curve

    A `Parameter` is a parameter of the functional form for the curve. For example, if the parametric curve you want
    to fit has three parameters then you need 3 `Parameter` objects. A `Parameter` is made up of one or more
    [`Variable`](Variable.md) objects, which represent the fixed and random effects. Parameters may have
    link functions that transform the parameter into some other
     space to enforce, for example, positivity of the parameter (e.g. the parameter representing the scale of a
     Gaussian distribution can't be negative).

    ## Arguments

    - `param_name (str)`: name of parameter, e.g. 'alpha'
    - `link_fun (Callable)`: the link function for the parameter
    - `variables (List[curvefit.core.parameter.Variable])`: a list of `Variable` instances

    ## Attributes

    All attributes from the `Variable`s in the list in the `variables` argument are carried over to
    `Parameter` but they are put into a list. For example, the `fe_init` attribute for `Parameter` is a list of
    `fe_init` attributes for each `Variable` in the order that they were passed in `variables` list.

    *Additional* attributes that are not lists of the individual `Variable` attributes are listed below.

    - `self.num_fe (int)`: total number of effects for the parameter (number of variables)

    ## Usage

    ```python
    from curvefit.core.parameter import Parameter, Variable

    var = Variable(covariate='ones', var_link_fun=lambda x: x, fe_init=0., re_init=0.)
    param = Parameter(param_name='alpha', link_fun=lambda x: x, variables=[var])
    ```

    {end_markdown Parameter}
    """

    param_name: str
    link_fun: Callable
    variables: InitVar[List[Variable]]

    num_fe: int = field(init=False)
    covariate: List[str] = field(init=False)
    var_link_fun: List[Callable] = field(init=False)
    fe_init: List[float] = field(init=False)
    re_init: List[float] = field(init=False)
    re_zero_sum_std: List[float] = field(init=False)
    fe_gprior: List[List[float]] = field(init=False)
    re_gprior: List[List[float]] = field(init=False)
    fe_bounds: List[List[float]] = field(init=False)
    re_bounds: List[List[float]] = field(init=False)

    def __post_init__(self, variables):
        assert isinstance(variables, list)
        assert len(variables) > 0
        assert isinstance(variables[0], Variable)
        self.num_fe = len(variables)
        for k, v in consolidate(Variable, variables).items():
            self.__setattr__(k, v)
Beispiel #6
0
class CoctMt090003Uv01Device:
    class Meta:
        name = "COCT_MT090003UV01.Device"

    realm_code: List[Cs] = field(
        default_factory=list,
        metadata={
            "name": "realmCode",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    type_id: Optional[Ii] = field(
        default=None,
        metadata={
            "name": "typeId",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    template_id: List[Ii] = field(
        default_factory=list,
        metadata={
            "name": "templateId",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    id: List[Ii] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    manufacturer_model_name: Optional[Sc] = field(
        default=None,
        metadata={
            "name": "manufacturerModelName",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    software_name: Optional[Sc] = field(
        default=None,
        metadata={
            "name": "softwareName",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    null_flavor: Optional[NullFlavor] = field(
        default=None,
        metadata={
            "name": "nullFlavor",
            "type": "Attribute",
        }
    )
    class_code: Optional[EntityClassDevice] = field(
        default=None,
        metadata={
            "name": "classCode",
            "type": "Attribute",
            "required": True,
        }
    )
    determiner_code: EntityDeterminer = field(
        init=False,
        default=EntityDeterminer.INSTANCE,
        metadata={
            "name": "determinerCode",
            "type": "Attribute",
            "required": True,
        }
    )
Beispiel #7
0
class BaseConfig(JsonSchemaMixin, allow_additional_props=False):
    """Taskcat configuration file"""

    general: GeneralConfig = field(default_factory=GeneralConfig)
    project: ProjectConfig = field(default_factory=ProjectConfig)
    tests: Dict[TestName, TestConfig] = field(default_factory=dict)

    # pylint doesn't like instance variables being added in post_init
    # pylint: disable=attribute-defined-outside-init
    def __post_init__(self):
        self._source: Dict[str, Any] = {}
        self._propogate()
        self.set_source("UNKNOWN")
        self._propogate_source()

    @staticmethod
    def _merge(source, dest):
        for section_key, section_value in source.items():
            if section_key in PROPAGATE_KEYS + PROPOGATE_ITEMS:
                if section_key not in dest:
                    dest[section_key] = section_value
                    continue
                if section_key in PROPAGATE_KEYS:
                    for key, value in section_value.items():
                        dest[section_key][key] = value
        return dest

    def _propogate(self):
        project_dict = self._merge(self.general.to_dict(),
                                   self.project.to_dict())
        self.project = ProjectConfig.from_dict(project_dict)
        for test_key, test in self.tests.items():
            test_dict = self._merge(self.project.to_dict(), test.to_dict())
            self.tests[test_key] = TestConfig.from_dict(test_dict)

    def _propogate_source(self):
        self._source["project"] = self._merge(self._source["general"],
                                              self._source["project"])
        for test_key in self._source["tests"]:
            test = self._merge(self._source["project"],
                               self._source["tests"][test_key])
            self._source["tests"][test_key] = test

    def set_source(self,
                   source_name: str,
                   dest: Optional[Any] = None) -> Optional[Union[str, dict]]:
        base_case = False
        if dest is None:
            base_case = True
            self._source = self.to_dict()
            dest = self._source
        if not isinstance(dest, dict):
            return source_name
        if isinstance(dest, dict):
            for item in dest:
                dest[item] = self.set_source(source_name, dest[item])
        if not base_case:
            return dest
        return None

    @classmethod
    def merge(cls, base_config: "BaseConfig",
              merge_config: "BaseConfig") -> "BaseConfig":

        merged = base_config.to_dict()
        merge_nested_dict(merged, merge_config.to_dict())

        merged_source = base_config._source.copy()
        merge_nested_dict(merged_source, merge_config._source)

        config = cls.from_dict(merged)

        config._source = merged_source
        config._propogate_source()  # pylint: disable=protected-access
        return config
Beispiel #8
0
class BswOsTaskExecutionEvent:
    """This BswEvent is supposed to execute BswSchedulableEntitys which have to
    react on the execution of specific OsTasks.

    Therefore, this event is unconditionally raised whenever the OsTask
    on which it is mapped is executed. The main use case for this event
    is scheduling of Runnables of Complex Drivers which have to react on
    task executions.

    :ivar short_name: This specifies an identifying shortName for the
        object. It needs to be unique within its context and is intended
        for humans but even more for technical reference.
    :ivar short_name_fragments: This specifies how the
        Referrable.shortName is composed of several shortNameFragments.
    :ivar long_name: This specifies the long name of the object. Long
        name is targeted to human readers and acts like a headline.
    :ivar desc: This represents a general but brief (one paragraph)
        description what the object in question is about. It is only one
        paragraph! Desc is intended to be collected into overview
        tables. This property helps a human reader to identify the
        object in question. More elaborate documentation, (in particular
        how the object is built or used) should go to "introduction".
    :ivar category: The category is a keyword that specializes the
        semantics of the Identifiable. It affects the expected existence
        of attributes and the applicability of constraints.
    :ivar admin_data: This represents the administrative data for the
        identifiable object.
    :ivar introduction: This represents more information about how the
        object in question is built or is used. Therefore it is a
        DocumentationBlock.
    :ivar annotations: Possibility to provide additional notes while
        defining a model element (e.g. the ECU Configuration Parameter
        Values). These are not intended as documentation but are mere
        design notes.
    :ivar activation_reason_representation_ref: If the
        activationReasonRepresentation is referenced from the enclosing
        AbstractEvent this shall be taken as an indication that the
        latter contributes to the activating vector of this
        ExecutableEntity that owns the referenced
        ExecutableEntityActivationReason.
    :ivar context_limitation_refs: The existence of this reference
        indicates that the usage of the event is limited to the context
        of the referred BswDistinguishedPartitions.
    :ivar disabled_in_mode_irefs: The modes, in which this event is
        disabled.
    :ivar starts_on_event_ref: The entity which is started by the event.
    :ivar variation_point: This element was generated/modified due to an
        atpVariation stereotype.
    :ivar s: Checksum calculated by the user's tool environment for an
        ArObject. May be used in an own tool environment to determine if
        an ArObject has changed. The checksum has no semantic meaning
        for an AUTOSAR model and there is no requirement for AUTOSAR
        tools to manage the checksum.
    :ivar t: Timestamp calculated by the user's tool environment for an
        ArObject. May be used in an own tool environment to determine
        the last change of an ArObject. The timestamp has no semantic
        meaning for an AUTOSAR model and there is no requirement for
        AUTOSAR tools to manage the timestamp.
    :ivar uuid: The purpose of this attribute is to provide a globally
        unique identifier for an instance of a meta-class. The values of
        this attribute should be globally unique strings prefixed by the
        type of identifier.  For example, to include a DCE UUID as
        defined by The Open Group, the UUID would be preceded by "DCE:".
        The values of this attribute may be used to support merging of
        different AUTOSAR models. The form of the UUID (Universally
        Unique Identifier) is taken from a standard defined by the Open
        Group (was Open Software Foundation). This standard is widely
        used, including by Microsoft for COM (GUIDs) and by many
        companies for DCE, which is based on CORBA. The method for
        generating these 128-bit IDs is published in the standard and
        the effectiveness and uniqueness of the IDs is not in practice
        disputed. If the id namespace is omitted, DCE is assumed. An
        example is "DCE:2fac1234-31f8-11b4-a222-08002b34c003". The uuid
        attribute has no semantic meaning for an AUTOSAR model and there
        is no requirement for AUTOSAR tools to manage the timestamp.
    """
    class Meta:
        name = "BSW-OS-TASK-EXECUTION-EVENT"

    short_name: Optional[Identifier] = field(
        default=None,
        metadata={
            "name": "SHORT-NAME",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
            "required": True,
        }
    )
    short_name_fragments: Optional["BswOsTaskExecutionEvent.ShortNameFragments"] = field(
        default=None,
        metadata={
            "name": "SHORT-NAME-FRAGMENTS",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    long_name: Optional[MultilanguageLongName] = field(
        default=None,
        metadata={
            "name": "LONG-NAME",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    desc: Optional[MultiLanguageOverviewParagraph] = field(
        default=None,
        metadata={
            "name": "DESC",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    category: Optional[CategoryString] = field(
        default=None,
        metadata={
            "name": "CATEGORY",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    admin_data: Optional[AdminData] = field(
        default=None,
        metadata={
            "name": "ADMIN-DATA",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    introduction: Optional[DocumentationBlock] = field(
        default=None,
        metadata={
            "name": "INTRODUCTION",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    annotations: Optional["BswOsTaskExecutionEvent.Annotations"] = field(
        default=None,
        metadata={
            "name": "ANNOTATIONS",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    activation_reason_representation_ref: Optional["BswOsTaskExecutionEvent.ActivationReasonRepresentationRef"] = field(
        default=None,
        metadata={
            "name": "ACTIVATION-REASON-REPRESENTATION-REF",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    context_limitation_refs: Optional["BswOsTaskExecutionEvent.ContextLimitationRefs"] = field(
        default=None,
        metadata={
            "name": "CONTEXT-LIMITATION-REFS",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    disabled_in_mode_irefs: Optional["BswOsTaskExecutionEvent.DisabledInModeIrefs"] = field(
        default=None,
        metadata={
            "name": "DISABLED-IN-MODE-IREFS",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    starts_on_event_ref: Optional["BswOsTaskExecutionEvent.StartsOnEventRef"] = field(
        default=None,
        metadata={
            "name": "STARTS-ON-EVENT-REF",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    variation_point: Optional[VariationPoint] = field(
        default=None,
        metadata={
            "name": "VARIATION-POINT",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    s: Optional[str] = field(
        default=None,
        metadata={
            "name": "S",
            "type": "Attribute",
        }
    )
    t: Optional[str] = field(
        default=None,
        metadata={
            "name": "T",
            "type": "Attribute",
            "pattern": r"([0-9]{4}-[0-9]{2}-[0-9]{2})(T[0-9]{2}:[0-9]{2}:[0-9]{2}(Z|([+\-][0-9]{2}:[0-9]{2})))?",
        }
    )
    uuid: Optional[str] = field(
        default=None,
        metadata={
            "name": "UUID",
            "type": "Attribute",
        }
    )

    @dataclass
    class ShortNameFragments:
        short_name_fragment: List[ShortNameFragment] = field(
            default_factory=list,
            metadata={
                "name": "SHORT-NAME-FRAGMENT",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            }
        )

    @dataclass
    class Annotations:
        annotation: List[Annotation] = field(
            default_factory=list,
            metadata={
                "name": "ANNOTATION",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            }
        )

    @dataclass
    class ActivationReasonRepresentationRef(Ref):
        dest: Optional[ExecutableEntityActivationReasonSubtypesEnum] = field(
            default=None,
            metadata={
                "name": "DEST",
                "type": "Attribute",
                "required": True,
            }
        )

    @dataclass
    class ContextLimitationRefs:
        context_limitation_ref: List["BswOsTaskExecutionEvent.ContextLimitationRefs.ContextLimitationRef"] = field(
            default_factory=list,
            metadata={
                "name": "CONTEXT-LIMITATION-REF",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            }
        )

        @dataclass
        class ContextLimitationRef(Ref):
            dest: Optional[BswDistinguishedPartitionSubtypesEnum] = field(
                default=None,
                metadata={
                    "name": "DEST",
                    "type": "Attribute",
                    "required": True,
                }
            )

    @dataclass
    class DisabledInModeIrefs:
        disabled_in_mode_iref: List[ModeInBswModuleDescriptionInstanceRef] = field(
            default_factory=list,
            metadata={
                "name": "DISABLED-IN-MODE-IREF",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            }
        )

    @dataclass
    class StartsOnEventRef(Ref):
        dest: Optional[BswModuleEntitySubtypesEnum] = field(
            default=None,
            metadata={
                "name": "DEST",
                "type": "Attribute",
                "required": True,
            }
        )
Beispiel #9
0
    class ContextLimitationRefs:
        context_limitation_ref: List["BswOsTaskExecutionEvent.ContextLimitationRefs.ContextLimitationRef"] = field(
            default_factory=list,
            metadata={
                "name": "CONTEXT-LIMITATION-REF",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            }
        )

        @dataclass
        class ContextLimitationRef(Ref):
            dest: Optional[BswDistinguishedPartitionSubtypesEnum] = field(
                default=None,
                metadata={
                    "name": "DEST",
                    "type": "Attribute",
                    "required": True,
                }
            )
Beispiel #10
0
class SwAddrMethod:
    """Used to assign a common addressing method, e.g. common memory section,
    to data or code objects.

    These objects could actually live in different modules or
    components.

    :ivar short_name: This specifies an identifying shortName for the
        object. It needs to be unique within its context and is intended
        for humans but even more for technical reference.
    :ivar short_name_fragments: This specifies how the
        Referrable.shortName is composed of several shortNameFragments.
    :ivar long_name: This specifies the long name of the object. Long
        name is targeted to human readers and acts like a headline.
    :ivar desc: This represents a general but brief (one paragraph)
        description what the object in question is about. It is only one
        paragraph! Desc is intended to be collected into overview
        tables. This property helps a human reader to identify the
        object in question. More elaborate documentation, (in particular
        how the object is built or used) should go to "introduction".
    :ivar category: The category is a keyword that specializes the
        semantics of the Identifiable. It affects the expected existence
        of attributes and the applicability of constraints.
    :ivar admin_data: This represents the administrative data for the
        identifiable object.
    :ivar introduction: This represents more information about how the
        object in question is built or is used. Therefore it is a
        DocumentationBlock.
    :ivar annotations: Possibility to provide additional notes while
        defining a model element (e.g. the ECU Configuration Parameter
        Values). These are not intended as documentation but are mere
        design notes.
    :ivar variation_point: This element was generated/modified due to an
        atpVariation stereotype.
    :ivar blueprint_policys: This role indicates whether the
        blueprintable element will be modifiable or not motifiable.
    :ivar short_name_pattern: This attribute represents the pattern
        which shall be used to build the shortName of the derived
        elements. As of now it is modeled as a String.  In general it
        should follow the pattern: pattern = (placeholder | namePart)*
        placeholder = "{" namePart "}" namePart = identifier | "_" This
        is subject to be refined in subsequent versions. Note that this
        is marked as obsolete. Use the xml attribute namePattern instead
        as it applies to Identifier and CIdentifier (shortName, symbol
        etc.)
    :ivar memory_allocation_keyword_policy: Enumeration to specify the
        name pattern of the Memory Allocation Keyword.
    :ivar options:
    :ivar section_initialization_policy: Specifies the expected
        initialization of the variables (inclusive those which are
        implementing VariableDataPrototypes). Therefore this is an
        implementation constraint for initialization code of BSW modules
        (especially RTE) as well as the start-up code which initializes
        the memory segment to which the AutosarDataPrototypes referring
        to the SwAddrMethod's are later on mapped. If the attribute is
        not defined it has the identical semantic as the attribute value
        "INIT"
    :ivar section_type: Defines the type of memory sections which can be
        associated with this addresssing method.
    :ivar s: Checksum calculated by the user's tool environment for an
        ArObject. May be used in an own tool environment to determine if
        an ArObject has changed. The checksum has no semantic meaning
        for an AUTOSAR model and there is no requirement for AUTOSAR
        tools to manage the checksum.
    :ivar t: Timestamp calculated by the user's tool environment for an
        ArObject. May be used in an own tool environment to determine
        the last change of an ArObject. The timestamp has no semantic
        meaning for an AUTOSAR model and there is no requirement for
        AUTOSAR tools to manage the timestamp.
    :ivar uuid: The purpose of this attribute is to provide a globally
        unique identifier for an instance of a meta-class. The values of
        this attribute should be globally unique strings prefixed by the
        type of identifier.  For example, to include a DCE UUID as
        defined by The Open Group, the UUID would be preceded by "DCE:".
        The values of this attribute may be used to support merging of
        different AUTOSAR models. The form of the UUID (Universally
        Unique Identifier) is taken from a standard defined by the Open
        Group (was Open Software Foundation). This standard is widely
        used, including by Microsoft for COM (GUIDs) and by many
        companies for DCE, which is based on CORBA. The method for
        generating these 128-bit IDs is published in the standard and
        the effectiveness and uniqueness of the IDs is not in practice
        disputed. If the id namespace is omitted, DCE is assumed. An
        example is "DCE:2fac1234-31f8-11b4-a222-08002b34c003". The uuid
        attribute has no semantic meaning for an AUTOSAR model and there
        is no requirement for AUTOSAR tools to manage the timestamp.
    """
    class Meta:
        name = "SW-ADDR-METHOD"

    short_name: Optional[Identifier] = field(
        default=None,
        metadata={
            "name": "SHORT-NAME",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
            "required": True,
        }
    )
    short_name_fragments: Optional["SwAddrMethod.ShortNameFragments"] = field(
        default=None,
        metadata={
            "name": "SHORT-NAME-FRAGMENTS",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    long_name: Optional[MultilanguageLongName] = field(
        default=None,
        metadata={
            "name": "LONG-NAME",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    desc: Optional[MultiLanguageOverviewParagraph] = field(
        default=None,
        metadata={
            "name": "DESC",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    category: Optional[CategoryString] = field(
        default=None,
        metadata={
            "name": "CATEGORY",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    admin_data: Optional[AdminData] = field(
        default=None,
        metadata={
            "name": "ADMIN-DATA",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    introduction: Optional[DocumentationBlock] = field(
        default=None,
        metadata={
            "name": "INTRODUCTION",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    annotations: Optional["SwAddrMethod.Annotations"] = field(
        default=None,
        metadata={
            "name": "ANNOTATIONS",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    variation_point: Optional[VariationPoint] = field(
        default=None,
        metadata={
            "name": "VARIATION-POINT",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    blueprint_policys: Optional["SwAddrMethod.BlueprintPolicys"] = field(
        default=None,
        metadata={
            "name": "BLUEPRINT-POLICYS",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    short_name_pattern: Optional[String] = field(
        default=None,
        metadata={
            "name": "SHORT-NAME-PATTERN",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    memory_allocation_keyword_policy: Optional[MemoryAllocationKeywordPolicyType] = field(
        default=None,
        metadata={
            "name": "MEMORY-ALLOCATION-KEYWORD-POLICY",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    options: Optional["SwAddrMethod.Options"] = field(
        default=None,
        metadata={
            "name": "OPTIONS",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    section_initialization_policy: Optional[SectionInitializationPolicyType] = field(
        default=None,
        metadata={
            "name": "SECTION-INITIALIZATION-POLICY",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    section_type: Optional[MemorySectionType] = field(
        default=None,
        metadata={
            "name": "SECTION-TYPE",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        }
    )
    s: Optional[str] = field(
        default=None,
        metadata={
            "name": "S",
            "type": "Attribute",
        }
    )
    t: Optional[str] = field(
        default=None,
        metadata={
            "name": "T",
            "type": "Attribute",
            "pattern": r"([0-9]{4}-[0-9]{2}-[0-9]{2})(T[0-9]{2}:[0-9]{2}:[0-9]{2}(Z|([+\-][0-9]{2}:[0-9]{2})))?",
        }
    )
    uuid: Optional[str] = field(
        default=None,
        metadata={
            "name": "UUID",
            "type": "Attribute",
        }
    )

    @dataclass
    class ShortNameFragments:
        short_name_fragment: List[ShortNameFragment] = field(
            default_factory=list,
            metadata={
                "name": "SHORT-NAME-FRAGMENT",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            }
        )

    @dataclass
    class Annotations:
        annotation: List[Annotation] = field(
            default_factory=list,
            metadata={
                "name": "ANNOTATION",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            }
        )

    @dataclass
    class BlueprintPolicys:
        blueprint_policy_list: List[BlueprintPolicyList] = field(
            default_factory=list,
            metadata={
                "name": "BLUEPRINT-POLICY-LIST",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            }
        )
        blueprint_policy_not_modifiable: List[BlueprintPolicyNotModifiable] = field(
            default_factory=list,
            metadata={
                "name": "BLUEPRINT-POLICY-NOT-MODIFIABLE",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            }
        )
        blueprint_policy_single: List[BlueprintPolicySingle] = field(
            default_factory=list,
            metadata={
                "name": "BLUEPRINT-POLICY-SINGLE",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            }
        )

    @dataclass
    class Options:
        """
        :ivar option: This attribute introduces the ability to specify
            further intended properties of the MemorySection in with the
            related objects shall be placed. These properties are
            handled as to be selected. The intended options are
            mentioned in the list. In the Memory Mapping configuration,
            this option list is used to determine an appropriate
            MemMapAddressingModeSet.
        """
        option: List[Identifier] = field(
            default_factory=list,
            metadata={
                "name": "OPTION",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            }
        )
Beispiel #11
0
class BaseGANVocoderConfig(BaseVocoderConfig):
    """Base config class used among all the GAN based vocoders.
    Args:
        use_stft_loss (bool):
            enable / disable the use of STFT loss. Defaults to True.
        use_subband_stft_loss (bool):
            enable / disable the use of Subband STFT loss. Defaults to True.
        use_mse_gan_loss (bool):
            enable / disable the use of Mean Squared Error based GAN loss. Defaults to True.
        use_hinge_gan_loss (bool):
            enable / disable the use of Hinge GAN loss. Defaults to True.
        use_feat_match_loss (bool):
            enable / disable feature matching loss. Defaults to True.
        use_l1_spec_loss (bool):
            enable / disable L1 spectrogram loss. Defaults to True.
        stft_loss_weight (float):
            Loss weight that multiplies the computed loss value. Defaults to 0.
        subband_stft_loss_weight (float):
            Loss weight that multiplies the computed loss value. Defaults to 0.
        mse_G_loss_weight (float):
            Loss weight that multiplies the computed loss value. Defaults to 1.
        hinge_G_loss_weight (float):
            Loss weight that multiplies the computed loss value. Defaults to 0.
        feat_match_loss_weight (float):
            Loss weight that multiplies the computed loss value. Defaults to 100.
        l1_spec_loss_weight (float):
            Loss weight that multiplies the computed loss value. Defaults to 45.
        stft_loss_params (dict):
            Parameters for the STFT loss. Defaults to `{"n_ffts": [1024, 2048, 512], "hop_lengths": [120, 240, 50], "win_lengths": [600, 1200, 240]}`.
        l1_spec_loss_params (dict):
            Parameters for the L1 spectrogram loss. Defaults to
            `{
                "use_mel": True,
                "sample_rate": 22050,
                "n_fft": 1024,
                "hop_length": 256,
                "win_length": 1024,
                "n_mels": 80,
                "mel_fmin": 0.0,
                "mel_fmax": None,
            }`
        target_loss (str):
            Target loss name that defines the quality of the model. Defaults to `avg_G_loss`.
        gen_clip_grad (float):
            Gradient clipping threshold for the generator model. Any value less than 0 disables clipping.
            Defaults to -1.
        disc_clip_grad (float):
            Gradient clipping threshold for the discriminator model. Any value less than 0 disables clipping.
            Defaults to -1.
        lr_gen (float):
            Generator model initial learning rate. Defaults to 0.0002.
        lr_disc (float):
            Discriminator model initial learning rate. Defaults to 0.0002.
        optimizer (torch.optim.Optimizer):
            Optimizer used for the training. Defaults to `AdamW`.
        optimizer_params (dict):
            Optimizer kwargs. Defaults to `{"betas": [0.8, 0.99], "weight_decay": 0.0}`
        lr_scheduler_gen (torch.optim.Scheduler):
            Learning rate scheduler for the generator. Defaults to `ExponentialLR`.
        lr_scheduler_gen_params (dict):
            Parameters for the generator learning rate scheduler. Defaults to `{"gamma": 0.999, "last_epoch": -1}`.
        lr_scheduler_disc (torch.optim.Scheduler):
            Learning rate scheduler for the discriminator. Defaults to `ExponentialLR`.
        lr_scheduler_dict_params (dict):
            Parameters for the discriminator learning rate scheduler. Defaults to `{"gamma": 0.999, "last_epoch": -1}`.
        use_pqmf (bool):
            enable / disable PQMF for subband approximation at training. Defaults to False.
        steps_to_start_discriminator (int):
            Number of steps required to start training the discriminator. Defaults to 0.
        diff_samples_for_G_and_D (bool):
            enable / disable use of different training samples for the generator and the discriminator iterations.
            Enabling it results in slower iterations but faster convergance in some cases. Defaults to False.
    """

    # LOSS PARAMETERS
    use_stft_loss: bool = True
    use_subband_stft_loss: bool = True
    use_mse_gan_loss: bool = True
    use_hinge_gan_loss: bool = True
    use_feat_match_loss: bool = True  # requires MelGAN Discriminators (MelGAN and HifiGAN)
    use_l1_spec_loss: bool = True

    # loss weights
    stft_loss_weight: float = 0
    subband_stft_loss_weight: float = 0
    mse_G_loss_weight: float = 1
    hinge_G_loss_weight: float = 0
    feat_match_loss_weight: float = 100
    l1_spec_loss_weight: float = 45

    stft_loss_params: dict = field(
        default_factory=lambda: {
            "n_ffts": [1024, 2048, 512],
            "hop_lengths": [120, 240, 50],
            "win_lengths": [600, 1200, 240],
        })

    l1_spec_loss_params: dict = field(
        default_factory=lambda: {
            "use_mel": True,
            "sample_rate": 22050,
            "n_fft": 1024,
            "hop_length": 256,
            "win_length": 1024,
            "n_mels": 80,
            "mel_fmin": 0.0,
            "mel_fmax": None,
        })

    target_loss: str = "avg_G_loss"  # loss value to pick the best model to save after each epoch

    # optimizer
    gen_clip_grad: float = -1  # Generator gradient clipping threshold. Apply gradient clipping if > 0
    disc_clip_grad: float = -1  # Discriminator gradient clipping threshold.
    lr_gen: float = 0.0002  # Initial learning rate.
    lr_disc: float = 0.0002  # Initial learning rate.
    optimizer: str = "AdamW"
    optimizer_params: dict = field(default_factory=lambda: {
        "betas": [0.8, 0.99],
        "weight_decay": 0.0
    })
    lr_scheduler_gen: str = "ExponentialLR"  # one of the schedulers from https:#pytorch.org/docs/stable/optim.html
    lr_scheduler_gen_params: dict = field(default_factory=lambda: {
        "gamma": 0.999,
        "last_epoch": -1
    })
    lr_scheduler_disc: str = "ExponentialLR"  # one of the schedulers from https:#pytorch.org/docs/stable/optim.html
    lr_scheduler_disc_params: dict = field(default_factory=lambda: {
        "gamma": 0.999,
        "last_epoch": -1
    })

    use_pqmf: bool = False  # enable/disable using pqmf for multi-band training. (Multi-band MelGAN)
    steps_to_start_discriminator = 0  # start training the discriminator after this number of steps.
    diff_samples_for_G_and_D: bool = False  # use different samples for G and D training steps.
Beispiel #12
0
class ReadContext:
    session: Session
    resource: "BaseResource"
    uid: int

    # request vars
    include: Optional[List]
    query: Optional[List[FilterItem]]
    sort: Optional[list]
    resource_id: Optional[int]
    limit: Optional[int]
    offset: Optional[int]

    # runtime vars
    q: Select = field(default_factory=Select)
    obj_ids: List[int] = field(default_factory=list)
    parent_payload: List = field(default_factory=list)
    related_payload: Dict = field(default_factory=dict)
    total: int = 0

    # for Aggregation Resources
    time_scale = None

    @property
    def is_list(self):
        return not bool(self.resource_id)

    def read__filtering(self):
        if not self.query:
            return

        filters_to_apply = []

        for f in self.query:
            resource_field = self.resource.fields.get(f.field)

            model_field = resource_field.metadata.get("model_field")

            if model_field is None:
                raise BadFilter(filter=f.field)

            value = f.wrapper(f.value)
            value = filter_value_to_python(value)

            list_deserialization = isinstance(value, list) and not isinstance(
                resource_field, ListField
            )

            if value is not None:
                if list_deserialization:
                    value = [resource_field.deserialize(item) for item in value]
                else:
                    value = resource_field.deserialize(value)

            field_expr = getattr(model_field, f.op)(value)
            filters_to_apply.append(field_expr)

        self.q = self.q.where(sa.and_(*filters_to_apply))

    def read__sorting(self):
        if self.sort:
            # TODO: add support for routes
            # example: (xxx.names.display_name)
            for sort_item in self.sort:
                sort_route, sort_way = get_sort_way(sort_item)

                if sort_route in self.resource.fields:
                    self.q = self.q.order_by(
                        sort_way(
                            self.resource.fields[sort_route].metadata.get(
                                "model_field"
                            )
                        )
                    )

    def read__pagination(self):
        if self.limit:
            self.q = self.q.limit(self.limit)
        if self.offset:
            self.q = self.q.offset(self.offset)

    def read__query(self):
        fields_to_select = {}
        to_group_by = []

        for field_name, field in self.resource.fields.items():
            model_field = field.metadata.get("model_field")
            if field.load_only:
                continue

            if model_field is None:
                raise Exception(
                    f"{self.resource.Meta.name}.{field_name} field must have "
                    f"'model_field' argument"
                )

            if isinstance(field, ToMany):
                fields_to_select[field_name] = sa.func.array_remove(
                    sa.func.array_agg(model_field), None
                )
            elif isinstance(field, ToOne):
                fields_to_select[field_name] = model_field
            else:
                fields_to_select[field_name] = model_field
                if (
                    isinstance(model_field, InstrumentedAttribute)
                    and model_field.class_ is not self.resource.Meta.model
                ):
                    to_group_by.append(model_field)

        q = sa.select([clm.label(lbl) for lbl, clm in fields_to_select.items()])

        joins = getattr(self.resource.Meta, "select_from", None)
        if joins is not None:
            q = q.select_from(joins)

        if not self.is_list:
            model_id = get_id_field(self.resource)
            q = q.where(model_id == self.resource_id)

        if self.resource.Meta.auth:
            q = self.resource.Meta.auth.can_read(self, q)

        if joins is not None:
            model_id = get_id_field(self.resource)
            q = q.group_by(model_id, *to_group_by)

        self.q = q

    def __add_related_payload(self, related_res, related_data: list):
        if related_res.Meta.name in self.related_payload:
            related_res = related_res()
            related_res_id_field = get_id_field(related_res, name_only=True)
            existing_record_ids = [
                rec[related_res_id_field]
                for rec in self.related_payload[related_res.Meta.name]
            ]
            for rec in related_data:
                if rec[related_res_id_field] not in existing_record_ids:
                    self.related_payload[related_res.Meta.name].append(rec)
        else:
            self.related_payload[related_res.Meta.name] = related_data

    def read__includes(self):
        for i in self.include or []:
            if "." in i:
                raise BadRequest()

            elif not isinstance(self.resource.fields.get(i), (ToOne, ToMany)):
                raise RelationNotFound(f"Relation <{i}> not found")

            related_resource_name = self.resource.fields[i].metadata["resource"]
            related_res = self.resource.RESOURCES[related_resource_name]
            related_field = self.resource.fields[i].metadata.get("model_field")
            method_name = f"get_by_{self.resource.Meta.name.lower()}_ids"

            if not hasattr(related_res, method_name):
                raise BadRequest(
                    f"Relation {related_resource_name} doesn't ready yet. "
                    f"Ask developers to add {method_name} method "
                    f"to {related_resource_name} resource"
                )

            related_res_obj = related_res()
            related_data = getattr(related_res_obj, method_name)(
                self.session, self, related_field
            )
            self.__add_related_payload(related_res, related_data)

    def read__serializing(self) -> dict:
        response = self.resource.Response(self.resource, self.is_list)
        response.set_parent_payload(self.parent_payload)
        response.set_related_payload(self.related_payload)
        response.set_total(self.total)
        serialized_response = response.serialize()
        return serialized_response

    def read__execute_query(self):
        if not self.resource.Meta.disable_total:
            self.q.append_column(sa.func.count().over().label("total"))

        result = self.session.execute(self.q).fetchall()

        serialized_data = self.resource.dump(result, many=True)

        id_field = get_id_field(self.resource, name_only=True)
        self.obj_ids.extend([_i[id_field] for _i in serialized_data])
        self.parent_payload = serialized_data

        if serialized_data and not self.resource.Meta.disable_total:
            self.total = result[0].total
Beispiel #13
0
class _Name_default:
    middle_name_1: Optional[str] = field(default=None)
    middle_name_2: Optional[str] = field(default=None)
    maiden_name: Optional[str] = field(default=None)
    divorcée: Optional[str] = field(default=None)
Beispiel #14
0
class _Party_default:
    party_entry: str = field(default="unknown")
    party_exit: str = field(default="unknown")
Beispiel #15
0
class Regex:
    att: Optional[str] = field(default=None,
                               metadata={
                                   "type": "Attribute",
                                   "pattern": r"\p{IsHangulJamo}+",
                               })
Beispiel #16
0
class Cnmt():
    title_id: str
    version: int
    type: SwitchContentMetaType
    contents: dict[bytes, tuple[int, ContentType]] = field(compare=False)
Beispiel #17
0
class CoctMt090003Uv01AssignedEntity:
    class Meta:
        name = "COCT_MT090003UV01.AssignedEntity"

    realm_code: List[Cs] = field(
        default_factory=list,
        metadata={
            "name": "realmCode",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    type_id: Optional[Ii] = field(
        default=None,
        metadata={
            "name": "typeId",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    template_id: List[Ii] = field(
        default_factory=list,
        metadata={
            "name": "templateId",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    id: List[Ii] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
            "min_occurs": 1,
        }
    )
    code: Optional[Ce] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    addr: List[AdExplicit] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    telecom: List[TelExplicit] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    assigned_person: Optional[CoctMt090003Uv01Person] = field(
        default=None,
        metadata={
            "name": "assignedPerson",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
            "nillable": True,
        }
    )
    assigned_device: Optional[CoctMt090003Uv01Device] = field(
        default=None,
        metadata={
            "name": "assignedDevice",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
            "nillable": True,
        }
    )
    assigned_organization: Optional[CoctMt090003Uv01Organization] = field(
        default=None,
        metadata={
            "name": "assignedOrganization",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
            "nillable": True,
        }
    )
    represented_organization: Optional[CoctMt150003Uv03Organization] = field(
        default=None,
        metadata={
            "name": "representedOrganization",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
            "nillable": True,
        }
    )
    null_flavor: Optional[NullFlavor] = field(
        default=None,
        metadata={
            "name": "nullFlavor",
            "type": "Attribute",
        }
    )
    class_code: Optional[RoleClassAssignedEntity] = field(
        default=None,
        metadata={
            "name": "classCode",
            "type": "Attribute",
            "required": True,
        }
    )
Beispiel #18
0
class TransformerLanguageModelConfig(FairseqDataclass):
    activation_fn: ChoiceEnum(utils.get_available_activation_fns()) = field(
        default="relu", metadata={"help": "activation function to use"})
    dropout: float = field(default=0.1,
                           metadata={"help": "dropout probability"})
    attention_dropout: float = field(
        default=0.0,
        metadata={"help": "dropout probability for attention weights"})
    activation_dropout: float = field(
        default=0.0,
        metadata={"help": "dropout probability after activation in FFN."})
    relu_dropout: float = field(
        default=0.0,
        metadata={"help": "dropout probability after activation in FFN."})
    decoder_embed_dim: int = field(
        default=512, metadata={"help": "decoder embedding dimension"})
    decoder_output_dim: int = field(
        default=512, metadata={"help": "decoder output dimension"})
    decoder_input_dim: int = field(
        default=512, metadata={"help": "decoder input dimension"})
    decoder_ffn_embed_dim: int = field(
        default=2048, metadata={"help": "decoder embedding dimension for FFN"})
    decoder_layers: int = field(default=6,
                                metadata={"help": "num decoder layers"})
    decoder_attention_heads: int = field(
        default=8, metadata={"help": "num decoder attention heads"})
    decoder_normalize_before: bool = field(
        default=False,
        metadata={"help": "apply layernorm before each decoder block"})
    no_decoder_final_norm: bool = field(
        default=False,
        metadata={
            "help": "don't add an extra layernorm after the last decoder block"
        },
    )
    adaptive_softmax_cutoff: Optional[str] = field(
        default=None,
        metadata={
            "help":
            "comma separated list of adaptive softmax cutoff points. "
            "Must be used with adaptive_loss criterion"
        },
    )
    adaptive_softmax_dropout: float = field(
        default=0,
        metadata={
            "help": "sets adaptive softmax dropout for the tail projections"
        },
    )
    adaptive_softmax_factor: float = field(
        default=4, metadata={"help": "adaptive input factor"})
    no_token_positional_embeddings: bool = field(
        default=False,
        metadata={
            "help":
            "if set, disables positional embeddings (outside self attention)"
        },
    )
    share_decoder_input_output_embed: bool = field(
        default=False,
        metadata={"help": "share decoder input and output embeddings"})
    character_embeddings: bool = field(
        default=False,
        metadata={
            "help":
            "if set, uses character embedding convolutions to produce token embeddings"
        },
    )
    character_filters: str = field(
        default=
        "[(1, 64), (2, 128), (3, 192), (4, 256), (5, 256), (6, 256), (7, 256)]",
        metadata={"help": "size of character embeddings"},
    )
    character_embedding_dim: int = field(
        default=4, metadata={"help": "size of character embeddings"})
    char_embedder_highway_layers: int = field(
        default=2,
        metadata={
            "help": "number of highway layers for character token embeddder"
        },
    )
    adaptive_input: bool = field(
        default=False, metadata={"help": "if set, uses adaptive input"})
    adaptive_input_factor: float = field(
        default=4, metadata={"help": "adaptive input factor"})
    adaptive_input_cutoff: Optional[str] = field(
        default=None,
        metadata={
            "help": "comma separated list of adaptive input cutoff points."
        },
    )
    tie_adaptive_weights: bool = field(
        default=False,
        metadata={
            "help":
            "if set, ties the weights of adaptive softmax and adaptive input"
        },
    )
    tie_adaptive_proj: bool = field(
        default=False,
        metadata={
            "help":
            "if set, ties the projection weights of adaptive softmax and adaptive input"
        },
    )
    decoder_learned_pos: bool = field(
        default=False,
        metadata={"help": "use learned positional embeddings in the decoder"},
    )
    layernorm_embedding: bool = field(
        default=False, metadata={"help": "add layernorm to embedding"})
    no_scale_embedding: bool = field(
        default=False, metadata={"help": "if True, dont scale embeddings"})
    checkpoint_activations: bool = field(
        default=False,
        metadata={"help": "checkpoint activations at each layer"})
    offload_activations: bool = field(
        default=False,
        metadata={
            "help": "move checkpointed activations to CPU after they are used."
        },
    )
    # config for "Reducing Transformer Depth on Demand with Structured Dropout" (Fan et al., 2019)
    decoder_layerdrop: float = field(
        default=0.0, metadata={"help": "LayerDrop probability for decoder"})
    decoder_layers_to_keep: Optional[str] = field(
        default=None,
        metadata={
            "help":
            "which layers to *keep* when pruning as a comma-separated list"
        },
    )
    # config for Training with Quantization Noise for Extreme Model Compression ({Fan*, Stock*} et al., 2020)
    quant_noise_pq: float = field(
        default=0.0,
        metadata={"help": "iterative PQ quantization noise at training time"},
    )
    quant_noise_pq_block_size: int = field(
        default=8,
        metadata={"help": "block size of quantization noise at training time"},
    )
    quant_noise_scalar: float = field(
        default=0.0,
        metadata={
            "help":
            "scalar quantization noise and scalar quantization at training time"
        },
    )
    # config for Fully Sharded Data Parallel (FSDP) training
    min_params_to_wrap: int = field(
        default=DEFAULT_MIN_PARAMS_TO_WRAP,
        metadata={
            "help":
            ("minimum number of params for a layer to be wrapped with FSDP() when "
             "training with --ddp-backend=fully_sharded. Smaller values will "
             "improve memory efficiency, but may make torch.distributed "
             "communication less efficient due to smaller input sizes. This option "
             "is set to 0 (i.e., always wrap) when --checkpoint-activations or "
             "--offload-activations are passed.")
        })
    # options from other parts of the config
    add_bos_token: bool = II("task.add_bos_token")
    tokens_per_sample: int = II("task.tokens_per_sample")
    max_target_positions: Optional[int] = II("task.max_target_positions")
    tpu: bool = II("common.tpu")
Beispiel #19
0
class ProjectConfig(JsonSchemaMixin, allow_additional_props=False):
    """Project specific configuration section"""

    name: Optional[ProjectName] = field(default=None,
                                        metadata=METADATA["project__name"])
    auth: Optional[Dict[Region, str]] = field(default=None,
                                              metadata=METADATA["auth"])
    owner: Optional[str] = field(default=None,
                                 metadata=METADATA["project__owner"])
    regions: Optional[List[Region]] = field(default=None,
                                            metadata=METADATA["regions"])
    az_blacklist: Optional[List[AzId]] = field(default=None,
                                               metadata=METADATA["az_ids"])
    package_lambda: Optional[bool] = field(default=None,
                                           metadata=METADATA["package_lambda"])
    lambda_zip_path: Optional[str] = field(
        default=None, metadata=METADATA["lambda_zip_path"])
    lambda_source_path: Optional[str] = field(
        default=None, metadata=METADATA["lambda_source_path"])
    s3_bucket: Optional[S3BucketName] = field(default=None,
                                              metadata=METADATA["s3_bucket"])
    parameters: Optional[Dict[ParameterKey, ParameterValue]] = field(
        default=None, metadata=METADATA["parameters"])
    build_submodules: Optional[bool] = field(
        default=None, metadata=METADATA["build_submodules"])
    template: Optional[str] = field(default=None,
                                    metadata=METADATA["template"])
    tags: Optional[Dict[TagKey, TagValue]] = field(default=None,
                                                   metadata=METADATA["tags"])
    s3_enable_sig_v2: Optional[bool] = field(
        default=None, metadata=METADATA["enable_sig_v2"])
    s3_object_acl: Optional[str] = field(default=None,
                                         metadata=METADATA["s3_object_acl"])
Beispiel #20
0
class Armor:
    name: str = ''
    dur: int = 0
    mass: float = 0
    mat: Tuple = field(default_factory=tuple)
    defl: HarmStats = field(default_factory=HarmStats)
Beispiel #21
0
class Context:
    Schema: ClassVar[Type[Schema]] = Schema
    name: str
    account_id: str
    region: str
    env_tag: str
    env_stack_name: str
    env_ssm_parameter_name: str
    eks_stack_name: str
    ssm_parameter_name: str
    ssm_dockerhub_parameter_name: str
    toolkit: ToolkitManifest
    cdk_toolkit: CdkToolkitManifest
    networking: NetworkingContext
    user_pool_id: Optional[str] = None
    scratch_bucket_arn: Optional[str] = None
    cognito_users_url: Optional[str] = None
    cognito_external_provider_redirect: Optional[str] = None
    cognito_external_provider_domain: Optional[str] = None
    landing_page_url: Optional[str] = None
    k8_dashboard_url: Optional[str] = None
    codeartifact_domain: Optional[str] = None
    codeartifact_repository: Optional[str] = None
    cognito_external_provider: Optional[str] = None
    cognito_external_provider_label: Optional[str] = None
    eks_cluster_role_arn: Optional[str] = None
    eks_fargate_profile_role_arn: Optional[str] = None
    eks_env_nodegroup_role_arn: Optional[str] = None
    eks_oidc_provider: Optional[str] = None
    eks_system_masters_roles: Optional[List[str]] = cast(
        List[str], field(default_factory=list))
    user_pool_client_id: Optional[str] = None
    identity_pool_id: Optional[str] = None
    teams: List[TeamContext] = field(default_factory=list)
    images: ImagesManifest = ImagesManifest()
    elbs: Optional[Dict[str, Dict[str, Any]]] = None
    shared_efs_fs_id: Optional[str] = None
    shared_efs_sg_id: Optional[str] = None
    cluster_sg_id: Optional[str] = None
    cluster_pod_sg_id: Optional[str] = None
    managed_nodegroups: List[ManagedNodeGroupManifest] = field(
        default_factory=list)
    policies: Optional[List[str]] = cast(List[str],
                                         field(default_factory=list))
    orbit_version: Optional[str] = aws_orbit.__version__
    helm_repository: Optional[str] = None
    install_ssm_agent: Optional[bool] = False
    install_image_replicator: Optional[bool] = False

    def get_team_by_name(self, name: str) -> Optional[TeamContext]:
        for t in self.teams:
            if t.name == name:
                return t
        return None

    def remove_team_by_name(self, name: str) -> None:
        self.teams = [t for t in self.teams if t.name != name]

    def fetch_env_data(self) -> None:
        _logger.debug("Fetching Env data...")
        values = ssm.get_parameter(name=self.env_ssm_parameter_name)
        self.eks_cluster_role_arn = values["EksClusterRoleArn"]
        self.eks_fargate_profile_role_arn = values["EksFargateProfileRoleArn"]
        self.eks_env_nodegroup_role_arn = values["EksEnvNodegroupRoleArn"]
        self.user_pool_id = values["UserPoolId"]
        self.user_pool_client_id = values["UserPoolClientId"]
        self.identity_pool_id = values["IdentityPoolId"]
        self.cluster_pod_sg_id = values["ClusterPodSecurityGroupId"]
        self.fetch_cognito_external_idp_data()
        _logger.debug("Env data fetched successfully.")

    def fetch_cognito_external_idp_data(self) -> None:
        _logger.debug("Fetching Cognito External IdP data...")
        if self.cognito_external_provider:
            client = boto3_client(service_name="cognito-idp")
            response: Dict[str, Any] = client.describe_user_pool(
                UserPoolId=self.user_pool_id)
            domain: str = response["UserPool"].get("Domain")
            self.cognito_external_provider_domain = f"{domain}.auth.{self.region}.amazoncognito.com"
            _logger.debug("cognito_external_provider_domain: %s",
                          self.cognito_external_provider_domain)
            response = client.describe_user_pool_client(
                UserPoolId=self.user_pool_id,
                ClientId=self.user_pool_client_id)
            self.cognito_external_provider_redirect = response[
                "UserPoolClient"]["CallbackURLs"][0]
            _logger.debug("cognito_external_provider_redirect: %s",
                          self.cognito_external_provider_redirect)
            _logger.debug("Cognito External IdP data fetched successfully.")
        else:
            self.cognito_external_provider_domain = None
            self.cognito_external_provider_domain_label = None
            _logger.debug("No Cognito External IdP data to fetch.")

    def fetch_teams_data(self) -> None:
        _logger.debug("Fetching Teams data...")
        for team in self.teams:
            team.fetch_team_data()
        self.fetch_cognito_external_idp_data()
        _logger.debug("Env data fetched successfully.")
Beispiel #22
0
class DefendStats:
    skill: float = 1.0                  # combination of experience, combat training, reflex training, observation, spidy-sense...
    playerstats: PlayerStats = field(default_factory=PlayerStats)
class ParameterSet(Prototype):
    """
    {begin_markdown ParameterSet}

    {spell_markdown init inits param params gprior}

    # `curvefit.core.parameter.ParameterSet`
    ## A set of parameters that together specify the functional form of a curve

    A `ParameterSet` is a set of parameters that define the functional form for the curve.
    For example, if the parametric curve you want
    to fit has three parameters then you need 1 `ParameterSet` objects that consists of 3 `Parameter` objects.

    A `ParameterSet` is made up of one or more
    [`Parameter`](Parameter.md) objects, which are each made up of one or more [`Variable`](Variable.md) objects.
    Please refer to their documentation for more details on those objects.

    A `ParameterSet` can also encode functional priors -- priors for functions of the parameter list that is
    passed into a `ParameterSet`.

    ## Arguments

    - `parameters (List[curvefit.core.parameter.Parameter])`: a list of `Parameter` instances
    - `parameter_functions (List[Tuple[Callable, List[float]]]`: a list of tuples which each contain
    (0) functions to apply to the `parameters` list and
    (1) a prior for the parameter function (mean and standard deviation --
    see [`Variable`](Variable.md#arguments) for more details about priors)

    ## Attributes

    All attributes from the `Parameter`s in the list in the `parameters` argument are carried over to
    `ParameterSet` but they are put into a list. For example, the `fe_init` attribute for `ParameterSet` is a list of
    the `fe_init` attributes for each `Parameter` in the order that they were passed in `parameters` list (which
    are lists of `fe_inits` for each `Variable` within a `Parameter` (see [here](Parameter.md#attributes) for more).

    *Additional* attributes that are not lists of the individual `Parameter` attributes are listed below.

    - `self.num_fe (int)`: total number of effects for the parameter set (number of variables)

    ## Methods

    ### `delete_random_effects`
    Returns a copy of itself but with random effects bounds set to 0. This means that the parameter set
    will not have any random effects in the model. Useful for when the same parameter set will be used to fit
    jointly to many groups before being fit to individual groups.

    ## Usage

    ```python
    from curvefit.core.parameter import Parameter, Variable, ParameterSet

    var = Variable(covariate='ones', var_link_fun=lambda x: x, fe_init=0., re_init=0.)
    param = Parameter(param_name='alpha', link_fun=lambda x: x, variables=[var])
    param_function = ParameterFunction(
        param_function_name='alpha_squared',
        param_function=lambda params: params[0] ** 2,
        param_function_fe_gprior=[0., np.inf]
    )
    param_set = ParameterSet(
        parameters=[param], parameter_functions=[param_function]
    )
    ```

    {end_markdown ParameterSet}
    """

    parameters: InitVar[List[Parameter]]
    parameter_functions: InitVar[List[ParameterFunction]] = None

    param_name: List[str] = field(init=False)
    num_fe: int = field(init=False)
    link_fun: List[Callable] = field(init=False)
    covariate: List[List[str]] = field(init=False)
    var_link_fun: List[List[Callable]] = field(init=False)
    fe_init: List[List[float]] = field(init=False)
    re_init: List[List[float]] = field(init=False)
    re_zero_sum_std: List[List[float]] = field(init=False)
    fe_gprior: List[List[List[float]]] = field(init=False)
    re_gprior: List[List[List[float]]] = field(init=False)
    fe_bounds: List[List[List[float]]] = field(init=False)
    re_bounds: List[List[List[float]]] = field(init=False)

    param_function_name: List[str] = field(init=False)
    param_function: List[Callable] = field(init=False)
    param_function_fe_gprior: List[List[float]] = field(init=False)

    def __post_init__(self, parameters, parameter_functions):

        for k, v in consolidate(Parameter, parameters, exclude=['num_fe']).items():
            self.__setattr__(k, v)

        for k, v in consolidate(ParameterFunction, parameter_functions).items():
            self.__setattr__(k, v)

        if len(set(self.param_name)) != len(self.param_name):
            raise RuntimeError("Cannot have duplicate parameters in a set.")
        if len(set(self.param_function_name)) != len(self.param_function_name):
            raise RuntimeError("Cannot have duplicate parameter functions in a set.")

        self.num_fe = 0
        for param in parameters:
            self.num_fe += param.num_fe

    def get_param_index(self, param_name):
        try:
            param_index = self.param_name.index(param_name)
        except ValueError:
            raise RuntimeError(f"No {param_name} parameter in this parameter set.")
        return param_index

    def get_param_function_index(self, param_function_name):
        try:
            param_function_index = self.param_function_name.index(param_function_name)
        except ValueError:
            raise RuntimeError(f"No {param_function_name} parameter in this parameter set.")
        return param_function_index

    def delete_random_effects(self):
        param_set = self.clone()
        bounds = np.array(self.re_bounds)
        bounds[:] = 0.
        param_set.re_bounds = bounds.tolist()
        return param_set
Beispiel #24
0
class TrainingArguments:
    """
    TrainingArguments is the subset of the arguments we use in our example scripts **which relate to the training loop
    itself**.

    Using [`PdArgumentParser`] we can turn this class into
    [argparse](https://docs.python.org/3/library/argparse#module-argparse) arguments that can be specified on the
    command line.

    Parameters:
        output_dir (`str`):
            The output directory where the model predictions and checkpoints will be written.
        overwrite_output_dir (`bool`, *optional*, defaults to `False`):
            If `True`, overwrite the content of the output directory. Use this to continue training if `output_dir`
            points to a checkpoint directory.
        do_train (`bool`, *optional*, defaults to `False`):
            Whether to run training or not. This argument is not directly used by [`Trainer`], it's intended to be used
            by your training/evaluation scripts instead. See the [example
            scripts](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/examples) for more details.
        do_eval (`bool`, *optional*):
            Whether to run evaluation on the validation set or not. Will be set to `True` if `evaluation_strategy` is
            different from `"no"`. This argument is not directly used by [`Trainer`], it's intended to be used by your
            training/evaluation scripts instead. See the [example
            scripts](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/examples) for more details.
        do_predict (`bool`, *optional*, defaults to `False`):
            Whether to run predictions on the test set or not. This argument is not directly used by [`Trainer`], it's
            intended to be used by your training/evaluation scripts instead. See the [example
            scripts](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/examples) for more details.
        do_export (`bool`, *optional*, defaults to `False`):
            Whether to export inference model or not. This argument is not directly used by [`Trainer`], it's
            intended to be used by your training/evaluation scripts instead.
        evaluation_strategy (`str` or [`~trainer_utils.IntervalStrategy`], *optional*, defaults to `"no"`):
            The evaluation strategy to adopt during training. Possible values are:

                - `"no"`: No evaluation is done during training.
                - `"steps"`: Evaluation is done (and logged) every `eval_steps`.
                - `"epoch"`: Evaluation is done at the end of each epoch.

        prediction_loss_only (`bool`, *optional*, defaults to `False`):
            When performing evaluation and generating predictions, only returns the loss.
        per_device_train_batch_size (`int`, *optional*, defaults to 8):
            The batch size per GPU core/CPU for training.
        per_device_eval_batch_size (`int`, *optional*, defaults to 8):
            The batch size per GPU core/CPU for evaluation.
        gradient_accumulation_steps (`int`, *optional*, defaults to 1):
            Number of updates steps to accumulate the gradients for, before performing a backward/update pass.

            <Tip warning={true}>

            When using gradient accumulation, one step is counted as one step with backward pass. Therefore, logging,
            evaluation, save will be conducted every `gradient_accumulation_steps * xxx_step` training examples.

            </Tip>

        learning_rate (`float`, *optional*, defaults to 5e-5):
            The initial learning rate for [`AdamW`] optimizer.
        weight_decay (`float`, *optional*, defaults to 0):
            The weight decay to apply (if not zero) to all layers except all bias and LayerNorm weights in [`AdamW`]
            optimizer.
        adam_beta1 (`float`, *optional*, defaults to 0.9):
            The beta1 hyperparameter for the [`AdamW`] optimizer.
        adam_beta2 (`float`, *optional*, defaults to 0.999):
            The beta2 hyperparameter for the [`AdamW`] optimizer.
        adam_epsilon (`float`, *optional*, defaults to 1e-8):
            The epsilon hyperparameter for the [`AdamW`] optimizer.
        max_grad_norm (`float`, *optional*, defaults to 1.0):
            Maximum gradient norm (for gradient clipping).
        num_train_epochs(`float`, *optional*, defaults to 3.0):
            Total number of training epochs to perform (if not an integer, will perform the decimal part percents of
            the last epoch before stopping training).
        max_steps (`int`, *optional*, defaults to -1):
            If set to a positive number, the total number of training steps to perform. Overrides `num_train_epochs`.
            In case of using a finite iterable dataset the training may stop before reaching the set number of steps
            when all data is exhausted
        lr_scheduler_type (`str` or [`SchedulerType`], *optional*, defaults to `"linear"`):
            The scheduler type to use. See the documentation of [`SchedulerType`] for all possible values.
        warmup_ratio (`float`, *optional*, defaults to 0.0):
            Ratio of total training steps used for a linear warmup from 0 to `learning_rate`.
        warmup_steps (`int`, *optional*, defaults to 0):
            Number of steps used for a linear warmup from 0 to `learning_rate`. Overrides any effect of `warmup_ratio`.
        log_on_each_node (`bool`, *optional*, defaults to `True`):
            In multinode distributed training, whether to log using `log_level` once per node, or only on the main
            node.
        logging_dir (`str`, *optional*):
            log directory. Will default to *output_dir/runs/**CURRENT_DATETIME_HOSTNAME***.
        logging_strategy (`str` or [`~trainer_utils.IntervalStrategy`], *optional*, defaults to `"steps"`):
            The logging strategy to adopt during training. Possible values are:

                - `"no"`: No logging is done during training.
                - `"epoch"`: Logging is done at the end of each epoch.
                - `"steps"`: Logging is done every `logging_steps`.

        logging_first_step (`bool`, *optional*, defaults to `False`):
            Whether to log and evaluate the first `global_step` or not.
        logging_steps (`int`, *optional*, defaults to 500):
            Number of update steps between two logs if `logging_strategy="steps"`.
        save_strategy (`str` or [`~trainer_utils.IntervalStrategy`], *optional*, defaults to `"steps"`):
            The checkpoint save strategy to adopt during training. Possible values are:

                - `"no"`: No save is done during training.
                - `"epoch"`: Save is done at the end of each epoch.
                - `"steps"`: Save is done every `save_steps`.
        save_steps (`int`, *optional*, defaults to 500):
            Number of updates steps before two checkpoint saves if `save_strategy="steps"`.
        save_total_limit (`int`, *optional*):
            If a value is passed, will limit the total amount of checkpoints. Deletes the older checkpoints in
            `output_dir`.
        save_on_each_node (`bool`, *optional*, defaults to `False`):
            When doing multi-node distributed training, whether to save models and checkpoints on each node, or only on
            the main one.

            This should not be activated when the different nodes use the same storage as the files will be saved with
            the same names for each node.
        no_cuda (`bool`, *optional*, defaults to `False`):
            Whether to not use CUDA even when it is available or not.
        seed (`int`, *optional*, defaults to 42):
            Random seed that will be set at the beginning of training. To ensure reproducibility across runs, use the
            [`~Trainer.model_init`] function to instantiate the model if it has some randomly initialized parameters.
        fp16 (`bool`, *optional*, defaults to `False`):
            Whether to use fp16 16-bit (mixed) precision training instead of 32-bit training.
        fp16_opt_level (`str`, *optional*, defaults to 'O1'):
            For `fp16` training,  AMP optimization level selected in ['O0', 'O1', 'O2']. See details at 
            https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/amp/auto_cast_cn.html
        scale_loss (`float`,  *optional*, defaults to 32768):
            The value of initial scale_loss for fp16. (default: 32768)
        local_rank (`int`, *optional*, defaults to -1):
            Rank of the process during distributed training.
        dataloader_drop_last (`bool`, *optional*, defaults to `False`):
            Whether to drop the last incomplete batch (if the length of the dataset is not divisible by the batch size)
            or not.
        eval_steps (`int`, *optional*):
            Number of update steps between two evaluations if `evaluation_strategy="steps"`. Will default to the same
            value as `logging_steps` if not set.
        dataloader_num_workers (`int`, *optional*, defaults to 0):
            Number of subprocesses to use for data loading. 0 means that the data will be loaded in the
            main process.
        past_index (`int`, *optional*, defaults to -1):
            Some models like TransformerXL or XLNet can make use of the past hidden states for their predictions. 
            If this argument is set to a positive int, the `Trainer` will use the corresponding output (usually index 2) as
            the past state and feed it to the model at the next training step under the keyword argument `mems`.
        run_name (`str`, *optional*):
            A descriptor for the run. Typically used for logging.
        disable_tqdm (`bool`, *optional*):
            Whether or not to disable the tqdm progress bars and table of metrics. Will default to `True` if the logging
            level is set to warn or lower (default), `False` otherwise.
        remove_unused_columns (`bool`, *optional*, defaults to `True`):
            If using `datasets.Dataset` datasets, whether or not to automatically remove the columns unused by the
            model forward method.
        label_names (`List[str]`, *optional*):
            The list of keys in your dictionary of inputs that correspond to the labels.
            Will eventually default to `["labels"]` except if the model used is one of the `XxxForQuestionAnswering` in
            which case it will default to `["start_positions", "end_positions"]`.
        load_best_model_at_end (`bool`, *optional*, defaults to `False`):
            Whether or not to load the best model found during training at the end of training.

            <Tip>

            When set to `True`, the parameters `save_strategy` needs to be the same as `eval_strategy`, and in the case
            it is "steps", `save_steps` must be a round multiple of `eval_steps`.

            </Tip>

        metric_for_best_model (`str`, *optional*):
            Use in conjunction with `load_best_model_at_end` to specify the metric to use to compare two different
            models. Must be the name of a metric returned by the evaluation with or without the prefix `"eval_"`. Will
            default to `"loss"` if unspecified and `load_best_model_at_end=True` (to use the evaluation loss).

            If you set this value, `greater_is_better` will default to `True`. Don't forget to set it to `False` if
            your metric is better when lower.
        greater_is_better (`bool`, *optional*):
            Use in conjunction with `load_best_model_at_end` and `metric_for_best_model` to specify if better models
            should have a greater metric or not. Will default to:

            - `True` if `metric_for_best_model` is set to a value that isn't `"loss"` or `"eval_loss"`.
            - `False` if `metric_for_best_model` is not set, or set to `"loss"` or `"eval_loss"`.
        ignore_data_skip (`bool`, *optional*, defaults to `False`):
            When resuming training, whether or not to skip the epochs and batches to get the data loading at the same
            stage as in the previous training. If set to `True`, the training will begin faster (as that skipping step
            can take a long time) but will not yield the same results as the interrupted training would have.
        optim (`str` or [`training_args.OptimizerNames`], *optional*, defaults to `"adamw"`):
            The optimizer to use: adamw, or adafactor.
        length_column_name (`str`, *optional*, defaults to `"length"`):
            Column name for precomputed lengths. If the column exists, grouping by length will use these values rather
            than computing them on train startup. Ignored unless `group_by_length` is `True` and the dataset is an
            instance of `Dataset`.
        report_to (`str` or `List[str]`, *optional*, defaults to `"visualdl"`):
            The list of integrations to report the results and logs to. Supported platforms is `"visualdl"`.
            `"none"` for no integrations.
        resume_from_checkpoint (`str`, *optional*):
            The path to a folder with a valid checkpoint for your model. This argument is not directly used by
            [`Trainer`], it's intended to be used by your training/evaluation scripts instead. See the [example
            scripts](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/examples) for more details.
    """

    output_dir: str = field(metadata={
        "help":
        "The output directory where the model predictions and checkpoints will be written."
    }, )
    overwrite_output_dir: bool = field(
        default=False,
        metadata={
            "help":
            ("Overwrite the content of the output directory. "
             "Use this to continue training if output_dir points to a checkpoint directory."
             )
        },
    )

    do_train: bool = field(default=False,
                           metadata={"help": "Whether to run training."})
    do_eval: bool = field(
        default=False,
        metadata={"help": "Whether to run eval on the dev set."})
    do_predict: bool = field(
        default=False,
        metadata={"help": "Whether to run predictions on the test set."})
    do_export: bool = field(
        default=False, metadata={"help": "Whether to export infernece model."})
    evaluation_strategy: IntervalStrategy = field(
        default="no",
        metadata={"help": "The evaluation strategy to use."},
    )
    prediction_loss_only: bool = field(
        default=False,
        metadata={
            "help":
            "When performing evaluation and predictions, only returns the loss."
        },
    )

    per_device_train_batch_size: int = field(
        default=8,
        metadata={"help": "Batch size per GPU core/CPU for training."})
    per_device_eval_batch_size: int = field(
        default=8,
        metadata={"help": "Batch size per GPU core/CPU for evaluation."})

    gradient_accumulation_steps: int = field(
        default=1,
        metadata={
            "help":
            "Number of updates steps to accumulate before performing a backward/update pass."
        },
    )

    learning_rate: float = field(
        default=5e-5,
        metadata={"help": "The initial learning rate for AdamW."})
    weight_decay: float = field(
        default=0.0,
        metadata={"help": "Weight decay for AdamW if we apply some."})
    adam_beta1: float = field(default=0.9,
                              metadata={"help": "Beta1 for AdamW optimizer"})
    adam_beta2: float = field(default=0.999,
                              metadata={"help": "Beta2 for AdamW optimizer"})
    adam_epsilon: float = field(
        default=1e-8, metadata={"help": "Epsilon for AdamW optimizer."})
    max_grad_norm: float = field(default=1.0,
                                 metadata={"help": "Max gradient norm."})

    num_train_epochs: float = field(
        default=3.0,
        metadata={"help": "Total number of training epochs to perform."})
    max_steps: int = field(
        default=-1,
        metadata={
            "help":
            "If > 0: set total number of training steps to perform. Override num_train_epochs."
        },
    )
    lr_scheduler_type: str = field(
        default="linear",
        metadata={"help": "The scheduler type to use."},
    )
    warmup_ratio: float = field(
        default=0.0,
        metadata={
            "help": "Linear warmup over warmup_ratio fraction of total steps."
        })
    warmup_steps: int = field(
        default=0, metadata={"help": "Linear warmup over warmup_steps."})

    log_on_each_node: bool = field(
        default=True,
        metadata={
            "help":
            "When doing a multinode distributed training, whether to log once per node or just once on the main node."
        },
    )
    logging_dir: Optional[str] = field(default=None,
                                       metadata={"help": "VisualDL log dir."})
    logging_strategy: IntervalStrategy = field(
        default="steps",
        metadata={"help": "The logging strategy to use."},
    )
    logging_first_step: bool = field(
        default=False, metadata={"help": "Log the first global_step"})
    logging_steps: int = field(default=500,
                               metadata={"help": "Log every X updates steps."})

    save_strategy: IntervalStrategy = field(
        default="steps",
        metadata={"help": "The checkpoint save strategy to use."},
    )
    save_steps: int = field(
        default=500,
        metadata={"help": "Save checkpoint every X updates steps."})
    save_total_limit: Optional[int] = field(
        default=None,
        metadata={
            "help":
            ("Limit the total amount of checkpoints. "
             "Deletes the older checkpoints in the output_dir. Default is unlimited checkpoints"
             )
        },
    )
    save_on_each_node: bool = field(
        default=False,
        metadata={
            "help":
            "When doing multi-node distributed training, whether to save models and checkpoints on each node, or only on the main one"
        },
    )
    no_cuda: bool = field(
        default=False,
        metadata={"help": "Do not use CUDA even when it is available"})
    seed: int = field(
        default=42,
        metadata={
            "help":
            "Random seed that will be set at the beginning of training."
        })

    fp16: bool = field(
        default=False,
        metadata={
            "help": "Whether to use fp16 (mixed) precision instead of 32-bit"
        },
    )
    fp16_opt_level: str = field(
        default="O1",
        metadata={
            "help":
            ("For fp16: AMP optimization level selected in ['O0', 'O1', and 'O2']. "
             "See details at https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/amp/auto_cast_cn.html"
             )
        },
    )

    scale_loss: float = field(
        default=2**15,
        metadata={"help": "The value of initial scale_loss for fp16."})

    minimum_eval_times: int = field(
        default=None,
        metadata={
            "help":
            "If under eval_steps, the valid time is less then minimum_eval_times, the config of override eval_steps."
        })

    local_rank: int = field(
        default=-1, metadata={"help": "For distributed training: local_rank"})

    dataloader_drop_last: bool = field(
        default=False,
        metadata={
            "help":
            "Drop the last incomplete batch if it is not divisible by the batch size."
        })
    eval_steps: int = field(
        default=None, metadata={"help": "Run an evaluation every X steps."})
    dataloader_num_workers: int = field(
        default=0,
        metadata={
            "help":
            "Number of subprocesses to use for data loading. 0 means that the data will be loaded in the main process."
        },
    )

    past_index: int = field(
        default=-1,
        metadata={
            "help":
            "If >=0, uses the corresponding part of the output as the past state for next step."
        },
    )

    run_name: Optional[str] = field(
        default=None, metadata={"help": "An optional descriptor for the run."})

    device: Optional[str] = field(
        default="gpu", metadata={"help": "select cpu, gpu, xpu devices."})

    disable_tqdm: Optional[bool] = field(
        default=None,
        metadata={"help": "Whether or not to disable the tqdm progress bars."})

    remove_unused_columns: Optional[bool] = field(
        default=True,
        metadata={
            "help":
            "Remove columns not required by the model when using an nlp.Dataset."
        })

    label_names: Optional[List[str]] = field(
        default=None,
        metadata={
            "help":
            "The list of keys in your dictionary of inputs that correspond to the labels."
        })

    load_best_model_at_end: Optional[bool] = field(
        default=False,
        metadata={
            "help":
            "Whether or not to load the best model found during training at the end of training."
        },
    )
    metric_for_best_model: Optional[str] = field(
        default=None,
        metadata={
            "help": "The metric to use to compare two different models."
        })
    greater_is_better: Optional[bool] = field(
        default=None,
        metadata={
            "help":
            "Whether the `metric_for_best_model` should be maximized or not."
        })
    ignore_data_skip: bool = field(
        default=False,
        metadata={
            "help":
            "When resuming training, whether or not to skip the first epochs and batches to get to the same training data."
        },
    )
    optim: str = field(
        default="adamw",
        metadata={"help": "The optimizer to use."},
    )
    report_to: Optional[List[str]] = field(
        default=None,
        metadata={
            "help":
            "The list of integrations to report the results and logs to."
        })
    resume_from_checkpoint: Optional[str] = field(
        default=None,
        metadata={
            "help":
            "The path to a folder with a valid checkpoint for your model."
        },
    )

    def __post_init__(self):
        env_local_rank = int(os.environ.get("PADDLE_RANK_IN_NODE", -1))
        if env_local_rank != -1 and env_local_rank != self.local_rank and paddle.distributed.get_world_size(
        ) > 1:
            self.local_rank = env_local_rank

        # convert to int
        self.log_level = -1
        self.log_level_replica = -1

        # expand paths, if not os.makedirs("~/bar") will make directory
        # in the current directory instead of the actual home
        if self.output_dir is not None:
            self.output_dir = os.path.expanduser(self.output_dir)
        if self.logging_dir is None and self.output_dir is not None:
            self.logging_dir = os.path.join(self.output_dir, default_logdir())
        if self.logging_dir is not None:
            self.logging_dir = os.path.expanduser(self.logging_dir)

        if self.disable_tqdm is None:
            self.disable_tqdm = False  # logger.getEffectiveLevel() > logging.WARN

        self.evaluation_strategy = IntervalStrategy(self.evaluation_strategy)
        self.logging_strategy = IntervalStrategy(self.logging_strategy)
        self.save_strategy = IntervalStrategy(self.save_strategy)

        self.lr_scheduler_type = SchedulerType(self.lr_scheduler_type)
        if self.do_eval is False and self.evaluation_strategy != IntervalStrategy.NO:
            self.do_eval = True

        if self.do_eval and self.evaluation_strategy == IntervalStrategy.NO:
            logger.warning(
                "evaluation_strategy reset to IntervalStrategy.STEPS for do_eval is True. you can also set evaluation_strategy='epoch'."
            )
            self.evaluation_strategy = IntervalStrategy.STEPS

        # eval_steps has to be defined and non-zero, fallbacks to logging_steps if the latter is non-zero
        if self.evaluation_strategy == IntervalStrategy.STEPS and (
                self.eval_steps is None or self.eval_steps == 0):
            if self.logging_steps > 0:
                logger.info(
                    f"using `logging_steps` to initialize `eval_steps` to {self.logging_steps}"
                )
                self.eval_steps = self.logging_steps
            else:
                raise ValueError(
                    f"evaluation strategy {self.evaluation_strategy} requires either non-zero --eval_steps or --logging_steps"
                )

        # logging_steps must be non-zero for logging_strategy that is other than 'no'
        if self.logging_strategy == IntervalStrategy.STEPS and self.logging_steps == 0:
            raise ValueError(
                f"logging strategy {self.logging_strategy} requires non-zero --logging_steps"
            )

        # Sanity checks for load_best_model_at_end: we require save and eval strategies to be compatible.
        if self.load_best_model_at_end:
            if self.evaluation_strategy != self.save_strategy:
                raise ValueError(
                    "--load_best_model_at_end requires the save and eval strategy to match, but found\n- Evaluation "
                    f"strategy: {self.evaluation_strategy}\n- Save strategy: {self.save_strategy}"
                )
            if self.evaluation_strategy == IntervalStrategy.STEPS and self.save_steps % self.eval_steps != 0:
                raise ValueError(
                    "--load_best_model_at_end requires the saving steps to be a round multiple of the evaluation "
                    f"steps, but found {self.save_steps}, which is not a round multiple of {self.eval_steps}."
                )

        if self.load_best_model_at_end and self.metric_for_best_model is None:
            self.metric_for_best_model = "loss"
        if self.greater_is_better is None and self.metric_for_best_model is not None:
            self.greater_is_better = self.metric_for_best_model not in [
                "loss", "eval_loss"
            ]
        if self.run_name is None:
            self.run_name = self.output_dir

        self.optim = OptimizerNames(self.optim)

        if self.report_to is None:
            logger.info(
                "The default value for the training argument `--report_to` will change in v5 (from all installed "
                "integrations to none). In v5, you will need to use `--report_to all` to get the same behavior as "
                "now. You should start updating your code and make this info disappear :-)."
            )
            self.report_to = "all"
        if self.report_to == "all" or self.report_to == ["all"]:
            # Import at runtime to avoid a circular import.
            from .integrations import get_available_reporting_integrations

            self.report_to = get_available_reporting_integrations()
        elif self.report_to == "none" or self.report_to == ["none"]:
            self.report_to = []
        elif not isinstance(self.report_to, list):
            self.report_to = [self.report_to]

        if self.warmup_ratio < 0 or self.warmup_ratio > 1:
            raise ValueError("warmup_ratio must lie in range [0,1]")
        elif self.warmup_ratio > 0 and self.warmup_steps > 0:
            logger.info(
                "Both warmup_ratio and warmup_steps given, warmup_steps will override any effect of warmup_ratio during training"
            )

    def __str__(self):
        self_as_dict = asdict(self)
        self_as_dict = {
            k: f"<{k.upper()}>" if k.endswith("_token") else v
            for k, v in self_as_dict.items()
        }

        attrs_as_str = [f"{k}={v},\n" for k, v in sorted(self_as_dict.items())]
        return f"{self.__class__.__name__}(\n{''.join(attrs_as_str)})"

    __repr__ = __str__

    @property
    def train_batch_size(self) -> int:
        """
        The actual batch size for training.
        """
        train_batch_size = self.per_device_train_batch_size
        return train_batch_size

    @property
    def eval_batch_size(self) -> int:
        """
        The actual batch size for evaluation.
        """
        eval_batch_size = self.per_device_eval_batch_size
        return eval_batch_size

    @property
    def current_device(self) -> "paddle.device":
        """
        The device used by this process.
        """
        return paddle.device.get_device()

    @property
    def world_size(self):
        """
        The number of processes used in parallel.
        """
        if self.local_rank != -1:
            return paddle.distributed.get_world_size()
        return 1

    @property
    def process_index(self):
        """
        The index of the current process used.
        """
        if self.local_rank != -1:
            return paddle.distributed.get_rank()
        return 0

    @property
    def local_process_index(self):
        """
        The index of the local process used.
        """
        if self.local_rank != -1:
            return self.local_rank
        return 0

    @property
    def should_log(self):
        """
        Whether or not the current process should produce log.
        """
        if self.log_on_each_node:
            return self.local_process_index == 0
        else:
            return self.process_index == 0

    @property
    def should_save(self):
        """
        Whether or not the current process should write to disk, e.g., to save models and checkpoints.
        """
        if self.save_on_each_node:
            return self.local_process_index == 0
        else:
            return self.process_index == 0

    @property
    def _no_sync_in_gradient_accumulation(self):
        """
        Whether or not to use no_sync for the gradients when doing gradient accumulation.
        """
        return True

    @contextlib.contextmanager
    def main_process_first(self, local=True, desc="work"):
        """
        A context manager for paddle distributed environment where on needs to do something on the main process, while
        blocking replicas, and when it's finished releasing the replicas.

        One such use is for `datasets`'s `map` feature which to be efficient should be run once on the main process,
        which upon completion saves a cached version of results and which then automatically gets loaded by the
        replicas.

        Args:
            local (`bool`, *optional*, defaults to `True`):
                if `True` first means process of rank 0 of each node if `False` first means process of rank 0 of node
                rank 0 In multi-node environment with a shared filesystem you most likely will want to use
                `local=False` so that only the main process of the first node will do the processing. If however, the
                filesystem is not shared, then the main process of each node will need to do the processing, which is
                the default behavior.
            desc (`str`, *optional*, defaults to `"work"`):
                a work description to be used in debug logs

        """
        if self.world_size > 1:
            if local:
                is_main_process = self.local_process_index == 0
                main_process_desc = "main local process"
            else:
                is_main_process = self.process_index == 0
                main_process_desc = "main process"

            try:
                if not is_main_process:
                    # tell all replicas to wait
                    logger.debug(
                        f"{self.process_index}: waiting for the {main_process_desc} to perform {desc}"
                    )
                    paddle.distributed.barrier()
                yield
            finally:
                if is_main_process:
                    # the wait is over
                    logger.debug(
                        f"{self.process_index}: {main_process_desc} completed {desc}, releasing all replicas"
                    )
                    paddle.distributed.barrier()
        else:
            yield

    def get_warmup_steps(self, num_training_steps: int):
        """
        Get number of steps used for a linear warmup.
        """
        warmup_steps = (self.warmup_steps if self.warmup_steps > 0 else
                        math.ceil(num_training_steps * self.warmup_ratio))
        return warmup_steps

    def to_dict(self):
        """
        Serializes this instance while replace `Enum` by their values (for JSON serialization support). It obfuscates
        the token values by removing their value.
        """
        d = asdict(self)
        for k, v in d.items():
            if isinstance(v, Enum):
                d[k] = v.value
            if isinstance(v, list) and len(v) > 0 and isinstance(v[0], Enum):
                d[k] = [x.value for x in v]
            if k.endswith("_token"):
                d[k] = f"<{k.upper()}>"
        return d

    def to_json_string(self):
        """
        Serializes this instance to a JSON string.
        """
        return json.dumps(self.to_dict(), indent=2)

    def to_sanitized_dict(self) -> Dict[str, Any]:
        """
        Sanitized serialization
        """
        d = self.to_dict()
        d = {
            **d,
            **{
                "train_batch_size": self.train_batch_size,
                "eval_batch_size": self.eval_batch_size
            }
        }

        valid_types = [bool, int, float, str]
        valid_types.append(paddle.Tensor)

        return {
            k: v if type(v) in valid_types else str(v)
            for k, v in d.items()
        }

    def print_config(self, args=None, key=""):
        """
        print all config values.
        """
        logger.info("=" * 60)
        if args is None:
            args = self
            key = "Training"

        logger.info('{:^40}'.format("{} Configuration Arguments".format(key)))
        logger.info('{:30}:{}'.format("paddle commit id",
                                      paddle.version.commit))

        for a in dir(args):
            if a[:2] != "__":  #don't print double underscore methods
                v = getattr(args, a)
                if not isinstance(v, types.MethodType):
                    logger.info('{:30}:{}'.format(a, v))

        logger.info("")
class Variable:
    """
    {begin_markdown Variable}

    {spell_markdown init gprior}

    # `curvefit.core.parameter.Variable`
    ## A variable to be estimated during the fit

    A `Variable` is the most detailed unit to be estimated for curve fitting. A `Variable` corresponds
    to an effect on a parameter -- and can contain a combination of both a fixed effect and random effects.
    A `Variable` needs a `"covariate"`, but the covariate in the data can be just a column of 1's, in which
    case a `Variable` is equivalent to a [`Parameter`](Parameter.md). If instead the values of
    the `"covariate"` argument differ for different rows of the data, `Variable` multiplies the covariate
    to get the `Parameter` for that data row.

    A `curvefit` model is made up of multiple parameters. For more information, see
    [`Parameter`](Parameter.md) and [`ParameterSet`](ParameterSet.md).

    ## Arguments

    - `covariate (str)`: name of the covariate for this variable (corresponds to what it will be in the data
        that is eventually used to fit the model)
    - `var_link_fun (Callable)`: link function for the variable
    - `fe_init (float)`: initial value to be used in the optimization for the fixed effect
    - `re_init (float)`: initial value to be used in the optimization for the random effect
    - `re_zero_sum_std (float)`: standard deviation of the zero sum prior
        for he random effects corresponding to this variable.
    - `fe_gprior (optional, List[float])`: list of Gaussian priors
        the fixed effect where the first element is the prior
        mean and the second element is the prior standard deviation
    - `re_gprior (optional, List[float])`: list of Gaussian priors
        the random effect where the first element is the prior
        mean and the second element is the prior standard deviation
    - `fe_bounds (optional, List[float])`: list of box constraints
        for the fixed effects during the optimization where the first element is the lower bound
        and the second element is the upper bound
    - `re_bounds (optional, List[float])`: list of box constraints
        for the fixed effects during the optimization where the first element is the lower bound
        and the second element is the upper bound

    ## Usage

    ```python
    from curvefit.core.parameter import Variable

    var = Variable(covariate='ones', var_link_fun=lambda x: x, fe_init=0., re_init=0.)
    ```

    {end_markdown Variable}
    """

    covariate: str
    var_link_fun: Callable
    fe_init: float
    re_init: float
    re_zero_sum_std: float = field(default=np.inf)
    fe_gprior: List[float] = field(default_factory=lambda: [0.0, np.inf])
    re_gprior: List[float] = field(default_factory=lambda: [0.0, np.inf])
    fe_bounds: List[float] = field(default_factory=lambda: [-np.inf, np.inf])
    re_bounds: List[float] = field(default_factory=lambda: [-np.inf, np.inf])

    def __post_init__(self):
        assert isinstance(self.covariate, str)
        assert len(self.fe_gprior) == 2
        assert len(self.re_gprior) == 2
        assert len(self.fe_bounds) == 2
        assert len(self.re_bounds) == 2
        assert self.fe_gprior[1] > 0.0
        assert self.re_gprior[1] > 0.0
Beispiel #26
0
class TrainingArguments:
    output_dir: str = field(
        metadata={"help": "The output directory where the model predictions and checkpoints will be written."},
    )
    overwrite_output_dir: bool = field(
        default=False,
        metadata={
            "help": (
                "Overwrite the content of the output directory. "
                "Use this to continue training if output_dir points to a checkpoint directory."
            )
        },
    )
    do_train: bool = field(default=False, metadata={"help": "Whether to run training."})
    do_eval: bool = field(default=False, metadata={"help": "Whether to run eval on the dev set."})
    do_predict: bool = field(default=False, metadata={"help": "Whether to run predictions on the test set."})
    per_device_train_batch_size: int = field(
        default=8, metadata={"help": "Batch size per GPU/TPU core/CPU for training."}
    )
    per_device_eval_batch_size: int = field(
        default=8, metadata={"help": "Batch size per GPU/TPU core/CPU for evaluation."}
    )
    learning_rate: float = field(default=5e-5, metadata={"help": "The initial learning rate for AdamW."})
    weight_decay: float = field(default=0.0, metadata={"help": "Weight decay for AdamW if we apply some."})
    adam_beta1: float = field(default=0.9, metadata={"help": "Beta1 for AdamW optimizer"})
    adam_beta2: float = field(default=0.999, metadata={"help": "Beta2 for AdamW optimizer"})
    adam_epsilon: float = field(default=1e-8, metadata={"help": "Epsilon for AdamW optimizer."})
    adafactor: bool = field(default=False, metadata={"help": "Whether or not to replace AdamW by Adafactor."})
    num_train_epochs: float = field(default=3.0, metadata={"help": "Total number of training epochs to perform."})
    warmup_steps: int = field(default=0, metadata={"help": "Linear warmup over warmup_steps."})
    logging_steps: int = field(default=500, metadata={"help": "Log every X updates steps."})
    save_steps: int = field(default=500, metadata={"help": "Save checkpoint every X updates steps."})
    eval_steps: int = field(default=None, metadata={"help": "Run an evaluation every X steps."})
    seed: int = field(default=42, metadata={"help": "Random seed that will be set at the beginning of training."})
    push_to_hub: bool = field(
        default=False, metadata={"help": "Whether or not to upload the trained model to the model hub after training."}
    )
    hub_model_id: str = field(
        default=None, metadata={"help": "The name of the repository to keep in sync with the local `output_dir`."}
    )
    hub_token: str = field(default=None, metadata={"help": "The token to use to push to the Model Hub."})

    def __post_init__(self):
        if self.output_dir is not None:
            self.output_dir = os.path.expanduser(self.output_dir)

    def to_dict(self):
        """
        Serializes this instance while replace `Enum` by their values (for JSON serialization support). It obfuscates
        the token values by removing their value.
        """
        d = asdict(self)
        for k, v in d.items():
            if isinstance(v, Enum):
                d[k] = v.value
            if isinstance(v, list) and len(v) > 0 and isinstance(v[0], Enum):
                d[k] = [x.value for x in v]
            if k.endswith("_token"):
                d[k] = f"<{k.upper()}>"
        return d
class StructuralPred:
    """Class that organises Structural Predictions output for single protein

    Attributes
    ----------
    DIS_threshold : float with values between 0. and 1. (default=0.50)
            Threshold used for disorder class definition of All predictors.

    ACC_threshold: probability threshold for Scratch1D (only) rellative
            solvent prediction class definition, default=0.20


    RaptorXDir : Path
        Path of the folder where RaptorX prediction output is being stored.

    PsiPredDir : Path
        Path of the folder where PsiPred prediction output is being stored.

    DisoPredDir : Path
        Path of the folder where DisoPred prediction output is being stored.

    Scratch1dDir : Path
        Path of the folder where Scratch1d prediction output is being stored.


    RaptorXPred : RaptorX object
        Contains parsed predictions output of RaptorX

    Scratch1dPred : Scratch1D object
        Contains parsed predictions output of Scratch1D

    PsiPredPred : PsiPred object
        Contains parsed predictions output of PsiPred

    DisoPredPred : DisoPred object
        Contains parsed predictions output of DisoPred


    Public Methods
    --------------
    parsestructural()
        Parses all the prediction output files and add the data inside the
        above attribute data structures.

    printSingleProtVertical( self )
        Prints all structural predictors in a vertical layout

    """

    DIS_threshold: float = 0.5
    ACC_threshold: float = 0.2

    predictions: dict = field(default_factory=dict)
    paths: dict = field(default_factory=dict)

    @staticmethod
    def parseall(paths: dict,
                 DIS_threshold: float = None,
                 ACC_threshold: float = None) -> StructuralPred:
        """
        Parses all the prediction output files and add the data inside the
        above attribute data structures.
        """

        data = StructuralPred()

        data.DIS_threshold = DIS_threshold if (DIS_threshold is not None and 0.0 < DIS_threshold < 1.0) \
            else 0.50

        data.ACC_threshold = ACC_threshold if (ACC_threshold is not None and 0.0 < ACC_threshold < 1.0) \
            else 0.20

        for prot in paths:
            for predictor in paths[prot]:

                if predictor.lower() == "raptorx":
                    preddata = RaptorXData.parse(paths[prot][predictor])
                elif predictor.lower() == "psipred":
                    preddata = PsiPredData.parse(paths[prot][predictor])
                elif predictor.lower() == "disopred":
                    preddata = DisoPredData.parse(paths[prot][predictor])
                elif predictor.lower() == "scratch1d":
                    preddata = Scratch1dData.parse(paths[prot][predictor])
                else:
                    print("Unknown predictor key: ", predictor)
                    raise

                if prot not in data.predictions:
                    data.predictions[prot] = {}

                data.predictions[prot][predictor] = preddata

        return data

    def printSingleProtVertical(self: StructuralPred, protname: str):

        try:

            data = self.predictions[protname]

            # Print header
            print("#Protname: ", protname, sep='\t')
            print("#DIS threshold: ", self.DIS_threshold, sep='\t')
            print("#ACC threshold (Scratch only): ",
                  self.ACC_threshold,
                  sep='\t')
            print("\n")

            print("#resid", "aa", \
                  "_", "RaptorX_SS3", "Scratch1D_SS3", "PsiPred_SS3", \
                  "_", "RaptorX_SS8", "Scratch1D_SS8", \
                  "_", "RaptorX_ACC3", "Scratch1D__ACC2", \
                  "_", "RaptorX_DIS", "DisoPred_DIS", sep='\t')

            seq = data["raptorx"].sequence
            protsize = len(seq)

            for id in range(protsize):
                print(id + 1, seq[id], \
                      "_", data["raptorx"].SS3_classes[id], data["scratch1d"].SS3_classes[id], \
                      data["psipred"].SS3_classes[id], \
                      "_", data["raptorx"].SS8_classes[id], data["scratch1d"].SS8_classes[id], \
                      "_", data["raptorx"].ACC3_classes[id], data["scratch1d"].ACC2_classes[id], \
                      "_", data["raptorx"].DIS_classes[id], data["disopred"].DIS_classes[id], sep='\t')
        except:
            print("Unexpected error:", sys.exc_info()[0])
            raise
class RestEndpointDelete:
    """
    This meta-class represents the ability to model a REST endpoint with DELETE
    semantics.

    :ivar short_name: This specifies an identifying shortName for the
        object. It needs to be unique within its context and is intended
        for humans but even more for technical reference.
    :ivar short_name_fragments: This specifies how the
        Referrable.shortName is composed of several shortNameFragments.
    :ivar long_name: This specifies the long name of the object. Long
        name is targeted to human readers and acts like a headline.
    :ivar desc: This represents a general but brief (one paragraph)
        description what the object in question is about. It is only one
        paragraph! Desc is intended to be collected into overview
        tables. This property helps a human reader to identify the
        object in question. More elaborate documentation, (in particular
        how the object is built or used) should go to "introduction".
    :ivar category: The category is a keyword that specializes the
        semantics of the Identifiable. It affects the expected existence
        of attributes and the applicability of constraints.
    :ivar admin_data: This represents the administrative data for the
        identifiable object.
    :ivar introduction: This represents more information about how the
        object in question is built or is used. Therefore it is a
        DocumentationBlock.
    :ivar annotations: Possibility to provide additional notes while
        defining a model element (e.g. the ECU Configuration Parameter
        Values). These are not intended as documentation but are mere
        design notes.
    :ivar blueprint_policys: This role indicates whether the
        blueprintable element will be modifiable or not motifiable.
    :ivar short_name_pattern: This attribute represents the pattern
        which shall be used to build the shortName of the derived
        elements. As of now it is modeled as a String.  In general it
        should follow the pattern: pattern = (placeholder | namePart)*
        placeholder = "{" namePart "}" namePart = identifier | "_" This
        is subject to be refined in subsequent versions. Note that this
        is marked as obsolete. Use the xml attribute namePattern instead
        as it applies to Identifier and CIdentifier (shortName, symbol
        etc.)
    :ivar arguments: Some endpoints can require a list of arguments.
    :ivar s: Checksum calculated by the user's tool environment for an
        ArObject. May be used in an own tool environment to determine if
        an ArObject has changed. The checksum has no semantic meaning
        for an AUTOSAR model and there is no requirement for AUTOSAR
        tools to manage the checksum.
    :ivar t: Timestamp calculated by the user's tool environment for an
        ArObject. May be used in an own tool environment to determine
        the last change of an ArObject. The timestamp has no semantic
        meaning for an AUTOSAR model and there is no requirement for
        AUTOSAR tools to manage the timestamp.
    :ivar uuid: The purpose of this attribute is to provide a globally
        unique identifier for an instance of a meta-class. The values of
        this attribute should be globally unique strings prefixed by the
        type of identifier.  For example, to include a DCE UUID as
        defined by The Open Group, the UUID would be preceded by "DCE:".
        The values of this attribute may be used to support merging of
        different AUTOSAR models. The form of the UUID (Universally
        Unique Identifier) is taken from a standard defined by the Open
        Group (was Open Software Foundation). This standard is widely
        used, including by Microsoft for COM (GUIDs) and by many
        companies for DCE, which is based on CORBA. The method for
        generating these 128-bit IDs is published in the standard and
        the effectiveness and uniqueness of the IDs is not in practice
        disputed. If the id namespace is omitted, DCE is assumed. An
        example is "DCE:2fac1234-31f8-11b4-a222-08002b34c003". The uuid
        attribute has no semantic meaning for an AUTOSAR model and there
        is no requirement for AUTOSAR tools to manage the timestamp.
    """
    class Meta:
        name = "REST-ENDPOINT-DELETE"

    short_name: Optional[Identifier] = field(
        default=None,
        metadata={
            "name": "SHORT-NAME",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
            "required": True,
        })
    short_name_fragments: Optional[
        "RestEndpointDelete.ShortNameFragments"] = field(
            default=None,
            metadata={
                "name": "SHORT-NAME-FRAGMENTS",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            })
    long_name: Optional[MultilanguageLongName] = field(
        default=None,
        metadata={
            "name": "LONG-NAME",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        })
    desc: Optional[MultiLanguageOverviewParagraph] = field(
        default=None,
        metadata={
            "name": "DESC",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        })
    category: Optional[CategoryString] = field(
        default=None,
        metadata={
            "name": "CATEGORY",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        })
    admin_data: Optional[AdminData] = field(
        default=None,
        metadata={
            "name": "ADMIN-DATA",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        })
    introduction: Optional[DocumentationBlock] = field(
        default=None,
        metadata={
            "name": "INTRODUCTION",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        })
    annotations: Optional["RestEndpointDelete.Annotations"] = field(
        default=None,
        metadata={
            "name": "ANNOTATIONS",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        })
    blueprint_policys: Optional["RestEndpointDelete.BlueprintPolicys"] = field(
        default=None,
        metadata={
            "name": "BLUEPRINT-POLICYS",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        })
    short_name_pattern: Optional[String] = field(
        default=None,
        metadata={
            "name": "SHORT-NAME-PATTERN",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        })
    arguments: Optional["RestEndpointDelete.Arguments"] = field(
        default=None,
        metadata={
            "name": "ARGUMENTS",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
        })
    s: Optional[str] = field(default=None,
                             metadata={
                                 "name": "S",
                                 "type": "Attribute",
                             })
    t: Optional[str] = field(
        default=None,
        metadata={
            "name":
            "T",
            "type":
            "Attribute",
            "pattern":
            r"([0-9]{4}-[0-9]{2}-[0-9]{2})(T[0-9]{2}:[0-9]{2}:[0-9]{2}(Z|([+\-][0-9]{2}:[0-9]{2})))?",
        })
    uuid: Optional[str] = field(default=None,
                                metadata={
                                    "name": "UUID",
                                    "type": "Attribute",
                                })

    @dataclass
    class ShortNameFragments:
        short_name_fragment: List[ShortNameFragment] = field(
            default_factory=list,
            metadata={
                "name": "SHORT-NAME-FRAGMENT",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            })

    @dataclass
    class Annotations:
        annotation: List[Annotation] = field(
            default_factory=list,
            metadata={
                "name": "ANNOTATION",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            })

    @dataclass
    class BlueprintPolicys:
        blueprint_policy_list: List[BlueprintPolicyList] = field(
            default_factory=list,
            metadata={
                "name": "BLUEPRINT-POLICY-LIST",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            })
        blueprint_policy_not_modifiable: List[
            BlueprintPolicyNotModifiable] = field(
                default_factory=list,
                metadata={
                    "name": "BLUEPRINT-POLICY-NOT-MODIFIABLE",
                    "type": "Element",
                    "namespace": "http://autosar.org/schema/r4.0",
                })
        blueprint_policy_single: List[BlueprintPolicySingle] = field(
            default_factory=list,
            metadata={
                "name": "BLUEPRINT-POLICY-SINGLE",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            })

    @dataclass
    class Arguments:
        rest_endpoint_argument: List[RestEndpointArgument] = field(
            default_factory=list,
            metadata={
                "name": "REST-ENDPOINT-ARGUMENT",
                "type": "Element",
                "namespace": "http://autosar.org/schema/r4.0",
            })
Beispiel #29
0
class ModelArguments:
    """
    Arguments pertaining to which model/config/tokenizer we are going to fine-tune, or train from scratch.
    """

    model_name_or_path: Optional[str] = field(
        default=None,
        metadata={
            "help":
            ("The model checkpoint for weights initialization.Don't set if you want to train a model from scratch."
             )
        },
    )
    model_type: Optional[str] = field(
        default=None,
        metadata={
            "help":
            "If training from scratch, pass a model type from the list: " +
            ", ".join(MODEL_TYPES)
        },
    )
    config_overrides: Optional[str] = field(
        default=None,
        metadata={
            "help":
            ("Override some existing default config settings when a model is trained from scratch. Example: "
             "n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index"
             )
        },
    )
    config_name: Optional[str] = field(
        default=None,
        metadata={
            "help":
            "Pretrained config name or path if not the same as model_name"
        })
    tokenizer_name: Optional[str] = field(
        default=None,
        metadata={
            "help":
            "Pretrained tokenizer name or path if not the same as model_name"
        })
    cache_dir: Optional[str] = field(
        default=None,
        metadata={
            "help":
            "Where do you want to store the pretrained models downloaded from huggingface.co"
        },
    )
    use_fast_tokenizer: bool = field(
        default=True,
        metadata={
            "help":
            "Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."
        },
    )
    model_revision: str = field(
        default="main",
        metadata={
            "help":
            "The specific model version to use (can be a branch name, tag name or commit id)."
        },
    )
    use_auth_token: bool = field(
        default=False,
        metadata={
            "help":
            ("Will use the token generated when running `transformers-cli login` (necessary to use this script "
             "with private models).")
        },
    )

    def __post_init__(self):
        if self.config_overrides is not None and (self.config_name is not None
                                                  or self.model_name_or_path
                                                  is not None):
            raise ValueError(
                "--config_overrides can't be used in combination with --config_name or --model_name_or_path"
            )
Beispiel #30
0
class CoctMt090003Uv01Person:
    class Meta:
        name = "COCT_MT090003UV01.Person"

    realm_code: List[Cs] = field(
        default_factory=list,
        metadata={
            "name": "realmCode",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    type_id: Optional[Ii] = field(
        default=None,
        metadata={
            "name": "typeId",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    template_id: List[Ii] = field(
        default_factory=list,
        metadata={
            "name": "templateId",
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
        }
    )
    name: List[EnExplicit] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "urn:hl7-org:v3",
            "min_occurs": 1,
        }
    )
    null_flavor: Optional[NullFlavor] = field(
        default=None,
        metadata={
            "name": "nullFlavor",
            "type": "Attribute",
        }
    )
    class_code: EntityClass = field(
        init=False,
        default=EntityClass.PSN,
        metadata={
            "name": "classCode",
            "type": "Attribute",
            "required": True,
        }
    )
    determiner_code: EntityDeterminer = field(
        init=False,
        default=EntityDeterminer.INSTANCE,
        metadata={
            "name": "determinerCode",
            "type": "Attribute",
            "required": True,
        }
    )
Beispiel #31
0
def field(number: int, *args, **kwargs) -> dataclasses.Field:
    """
    Convenience method to assign field numbers.
    Calls the standard ``dataclasses.field`` function with the metadata assigned.
    """
    return dataclasses.field(*args, metadata={'number': number}, **kwargs)
Beispiel #32
0
class Spectrum(SpectralData):
    r"""Create a spectrum object.

        A spectrum array is a subclass of a record or structured numpy array,
        with one axis representing the wavelength vector (w) and other the
        reflectance (r). The optional

    Attributes
    ----------
        w: numpy array
            array corresponding to the wavelength vector

        r: numpy array
            array corresponding to the relative reflectance of
            the asteroid

        unit: str
            The wavelength units. Default is 'microns'.

        r_unc: numpy array (optional)
            array corresponding to the relative reflectance
            uncertainty of the asteroid

        path: None or str (optional)
            The path of the spectrum file

        label: None or str
            The spectrum label

        res: float
            The spectrum resolution (number of points).

    Methods
    -------
    trim
    fit
    autofit
    estimate_rms
    clean_spec
    mad
    rebin
    normalize
    mask_region
    save
    plot

    """

    w: np.ndarray
    r: np.ndarray
    r_unc: np.ndarray = field(default=None)
    label: str = field(default='asteroid')
    unit: str = field(default='micron')

    def __post_init__(self):
        r"""Inicialize class."""
        self.w = np.array(self.w, ndmin=1)
        self.r = np.array(self.r, ndmin=1)
        assert self.w.size == self.r.size
        if self.r_unc is None:
            dtype = ('w', 'r')
        else:
            self.r_unc = np.array(self.r_unc, ndmin=1)
            assert self.r_unc.size == self.r.size
            dtype = ('w', 'r', 'r_unc')
        SpectralData.__init__(self, self.w, dtype, self.label, unit=self.unit)

    def angstrom2micron(self):
        r"""Convert wavenlength axis from angstrom to micron."""
        self.w = self.w / 10000.0
        self.unit = 'micron'
        return self

    def micron2angstrom(self):
        r"""Convert wavenlength axis from micron to angstrom."""
        self.w = self.w * 10000.
        self.unit = 'angstrom'
        return self

    def fit(self, order=4, ftype='spline'):
        r"""
        Fit the spectrum using a polynomial or a smooth spline.

        Parameters
        ----------
        order: int
            Order of the fitting.

        ftype: str
            Type of algorithm to use for the fitting.
            Options are: 'spline' or 'polynomial'.

        Returns
        -------
        fspec: Spectrum Object
            The fitted spectrum array

        fcoefs: array-like
            the fitting coefficients

        """
        # Performing the fit
        if ftype == 'spline':
            fcoefs = UnivariateSpline(self.w, self.r, k=order)
            fspec_y = fcoefs(self.w)
        elif ftype == 'polynomial':
            fcoefs = np.polyfit(self.w, self.r, order)
            fspec_y = np.polyval(fcoefs, self.w)
        y_err = np.abs(fspec_y - self.r)
        # building new array
        fspec = self.__class__(w=self.w, r=fspec_y, r_unc=y_err, unit=self.unit,
                               label=self.label + '_fit')
        return fspec, fcoefs

    def autofit(self, degree_min=1, degree_max=12):
        r"""
        Find the best order of the polynomial fitting of the spectrum.

        Parameters
        ----------
        degree_min: int
            The minimal order for a fit

        degree_max: int
            The maximal order for a fit

        Returns
        -------
        fspec: Spectrum Object
            The fitted spectrum array

        fcoefs: array-like
            the fitting coefficients
        """
        # degrees which we will test the fitting
        degrees = np.arange(degree_min, degree_max+1)
        # calculation cross-validation score and error for each degree
        cross, _ = np.array([self._autofit_aux(deg) for deg in degrees]).T
        # getting minimun cross validation score
        aux = np.argmin(cross)
        bestfit = degrees[aux]
        # fitting spectrum
        fspec, fcoefs = self.fit(order=bestfit, ftype='polynomial')
        return fspec, fcoefs

    def _autofit_aux(self, degree):
        r"""Auxiliary funtion for the autofit method."""
        # creating polynomial and reshaping array for model validation
        polynom = PolynomialFeatures(degree=degree, include_bias=False)
        x_train = self.w.reshape((-1, 1))
        x_train_trans = polynom.fit_transform(x_train)
        # Create the linear regression model and train
        model = LinearRegression()
        model.fit(x_train_trans, self.r)
        # Calculate the cross validation score
        cross_valid = cross_val_score(model, x_train_trans, self.r,
                                      scoring='neg_mean_squared_error', cv=10)
        # Training predictions and error
        y_train_predictions = model.predict(x_train_trans)
        training_error = mean_squared_error(self.r, y_train_predictions)
        return -np.mean(cross_valid), training_error

    def estimate_rms(self, ftype='auto', order=5):
        r"""
        Estimate the signal-to-noise ratio in a spectrum.

        Parameters
        ----------
        ftype: str
            Type of fitting for the snr estimation. Options are:
            'spline', 'polynomial', 'auto'. Default is 'auto'.

        order: int
            Order of the adjust. Ignored if ftype is 'auto'.

        Returns
        -------
        rms: float
            The estimated snr value

        """
        # fitting the spectrum
        if ftype == 'auto':
            fspec, _ = self.autofit()
        else:
            fspec, _ = self.fit(order, ftype)
        # Estimating the SNR
        std_arr = np.abs(self.r - fspec.r)
        rms = np.std(std_arr)
        return rms

    def clean_spec(self, method='sigmaclip', sigma=3, fit='auto'):
        r"""
        Remove outliers from the spectrum.

        Parameters
        ----------
        method: str
            Method for detecting outliers. Currently only 'sigmaclip' available
            Default is 'sigmaclip'.

        sigma: int
            Remove points higher than sigma.

        fit: 'auto' or integer
            The order of the polynomial fit. If auto it will try to find
            automaticaly. Default is 'auto'.

        Returns
        -------
        spec: Spectrum
            Spectrum object with outliers removed.

        """
        if fit == 'auto':
            fspec, _ = self.autofit()
        else:
            fspec, _ = self.fit(order=fit, ftype='polynomial')
        if method == 'sigmaclip':
            cspec = np.divide(self.r, fspec.r)
            cspec_index = [self._sigma_clip(val, sigma=sigma, cspec=cspec)
                           for val in cspec]
        aux = self[cspec_index]

        spec = self.__class__(aux.w, aux.r, r_unc=aux.r_unc, unit=self.unit,
                              label=self.label + '_cleaned')
        return spec

    def _sigma_clip(self, val, sigma, cspec):
        r"""Auxiliary method to perform sigma-clipping on array elements."""
        if (np.median(cspec) - self._mad(cspec) * sigma < val) and \
           (val < np.median(cspec) + self._mad(cspec)*sigma):
            return True
        return False

    def mad(self, axis=None):
        r"""
        Calculate the median absolute deviation.

        Parameters
        ----------
        axis: str
            'wave', 'ref' or None.
            It will return the mad in the defined axis.
            If None, than returns the mad in both axis

        Returns
        -------
        The median absolute deviation

        """
        if axis is not None:
            return self._mad(axis)
        return self._mad('w'), self._mad('r')

    @staticmethod
    def _mad(arr):
        r"""Auxiliary function for calculating the MAD."""
        return np.median(np.abs(arr - np.median(arr)))

    def rebin(self, binsize=11, method='median', std=True,
              rem_trend=False):
        r"""
        Rebin the spectrum.

        Parameters
        ----------
        binsize: int
            The number of points in the bin.

        method: str
            The method for the rebinning.
            Options are:'mean and 'median'.

        std: boolean
            If True, also returns the deviation.
            In the case of the median, returns the
            MAD (median absolute deviation).

        rem_trend: boolean

        Returns
        -------
        The rebined spectrum

        """
        spec_size = len(self.w)
        y_stop = (spec_size//binsize)*binsize
        wave_arr = self.w[:y_stop]
        ref_arr = self.r[:y_stop]
        if method == 'median':
            func = np.median
            std_func = np.std  # ->>>>>>change later
        if method == 'mean':
            func = np.mean
            std_func = np.std
        wave_reb = func(wave_arr.reshape(spec_size // binsize, binsize),
                        axis=-1).T
        ref_reb = func(ref_arr.reshape(spec_size // binsize, binsize),
                       axis=-1).T
        if rem_trend:
            fspec = self.autofit()[0]
            ref_arr = np.divide(ref_arr, fspec.r[:y_stop])
        if std:
            std = std_func(ref_arr.reshape(spec_size // binsize, binsize),
                           axis=-1).T
            std = np.array(std)
        else:
            std = None
        return self.__class__(w=wave_reb, r=ref_reb, r_unc=std, unit=self.unit,
                              label=self.label + '_binned')

    def ref_from_wavelength(self, w, interpolate=True):
        r"""
        Get the spectrum reflectance in a particular wavelength.

        Parameters
        ----------
        w: float
            Wavelength value
            If interpolate=False, The code will search the closest
            value.

        interpolate: boolean (optional)
            If interpolate=False, The code will search the closest
            value. If True it will interpolate the value of w.

        Returns
        -------
        The reflectance value

        """
        if not interpolate:
            aux = find_nearest(self.w, w)[0]
            ref = self.r[aux]
        else:
            ref = np.interp(w, self.w, self.r)
        return ref

    def normalize(self, wnorm=0.55, window=None, interpolate=True):
        r"""
        Normalize the spectrum in a particular wavelength.

        Parameters
        ----------
        wnorm: float
            Wavelength value to normalize the spectrum.
            If interpolate=False, The code will search the closest
            value.

        window: None or float (optional)
            The wavelenght window size for normalizing.
            If None it will normalize in the wnorm point only.

        interpolate: boolean (optional)
            If interpolate=False, The code will search the closest
            value. If True it will interpolate the value of wnorm.

        Returns
        -------
        The normalized Spectrum

        """
        if window is None:
            norm_factor = self.ref_from_wavelength(wnorm,
                                                   interpolate=interpolate)
        else:
            aux = np.argwhere((self.w > wnorm-window) &
                              (self.w < wnorm+window))
            norm_factor = np.mean(self.r[aux])
        self.r = self.r / norm_factor
        if self.r_unc is not None:
            self.r_unc = self.r_unc / norm_factor
        return self

    def mask_region(self, region=[(1.3, 1.45), (1.8, 1.95)]):
        r"""
        Exclude a region of the spectrum.

        Parameters
        ----------
        w_min: float
            Wavelength lower limit of the masked region

        w_max: float
            Wavelength upper limit of the masked region

        Returns
        -------
        masked_spec: Spectrum
            The Spectrum array without the masked region

        """
        if isinstance(region[0], (float, int)):
            masked_spec = self.mask_region_aux(self, wmin=region[0],
                                               wmax=region[1])
        else:
            masked_spec = self
            for rr in region:
                masked_spec = self.mask_region_aux(masked_spec,
                                                   wmin=rr[0],
                                                   wmax=rr[1])
        return masked_spec

    def mask_region_aux(self, spec, wmin, wmax):
        r"""
        Exclude a region of the spectrum.

        Parameters
        ----------
        w_min: float
            Wavelength lower limit of the masked region

        w_max: float
            Wavelength upper limit of the masked region

        Returns
        -------
        The Spectrum array without the masked region

        """
        aux = np.argwhere((spec.w > wmin) & (spec.w < wmax))
        mask = np.ones(len(spec.w), dtype=bool)
        mask[aux] = 0
        w = spec.w[mask]
        r = spec.r[mask]
        r_unc = spec.r_unc
        if r_unc is not None:
            r_unc = spec.r_unc[mask]
        return self.__class__(w=w, r=r, r_unc=r_unc, unit=spec.unit,
                              label=spec.label)


    def plot(self, fax=None, show=False, savefig=None,
             axistitles=True, speckwargs=None, legendkwargs=None):
        r"""
        Quick plot of the Spectrum.

        Parameters
        ----------
        fax (Optional): matplotlib.axes
            If desired to subplot image in a figure. Default is 'None', which
            will open a new plt.figure()

        show (Optional): boolean
            True if want to plt.show(). Default is True.

        savefig (Optional): str
            The path to save the figure. If set to None, wont save the figure.
            Default is None

        axistitles: boolean
            If True will label the axis. Default is True.

        speckwargs: dict
            Arguments for matplotlib plot function.
            default values: {'c':'0.9', 'lw':'1'}.

        legendkwargs: dict
            Arguments for matplotlib legend function.
            default values: {'loc':'best'}.

        Returns
        -------
        the matplotlib.axes of the figure

        """
        # checking if plot in another frame
        if fax is None:
            fig = plt.figure()
            fax = plt.gca()
        # setting default values for image plot with matplotlib
        specsty_defaults = {'c': '0.1', 'lw': 1}
        legendsty_defaults = {'loc': 'best'}
        # updating plot styles
        speckwargs = kwargupdate(specsty_defaults, speckwargs)
        legendkwargs = kwargupdate(legendsty_defaults, legendkwargs)
        # Ploting the spec
        fax.errorbar(self.w, self.r, yerr=self.r_unc, **speckwargs)
        # Checking if desired to plot the axis labels
        if axistitles:
            if self.unit == 'micron':
                unit_label = '$\mu$m'
            fax.set_xlabel('Wavelength (%s)' % unit_label)
            fax.set_ylabel('Reflectance')
        # plot legend?
        if 'label' in speckwargs:
            fax.legend(**legendkwargs)
        # check if save the image
        if savefig is not None:
            plt.savefig(savefig)
            if not show:
                plt.clf()
            matplotlib.use('TkAgg')
        # show in the matplotlib window?
        if show:
            plt.show()