Beispiel #1
0
def VarianceAnalysis(*args, **kargs):
    """
    One-way variance analysis.

    :Examples:

    .. doctest::
        :options: +SKIP

        >>> VarianceAnalysis(vec, class_variable, response_variable,
        ... type, FileName="result", Format="SpreadSheet")

    :Parameters:

      * vec (_Vectors),
      * class_variable (int): index of the class or group variable,
      * response_variable (int): index of the response variable,
      * type (string): type of the response variable ("NUMERIC" ("N") or
        "ORDINAL" ("O")).

    :Keywords:

      * FileName (string): name of the result file,
      * Format (string): format of the result file: "ASCII" (default format)
        or "SpreadSheet". This optional argument can only be used in conjunction with the optional argument FileName.

    :Returns:

        The variance analysis result as a string

    """
    error.CheckArgumentsLength(args, 4, 4)
    error.CheckKargs(kargs, possible_kargs = ["FileName", "Format"])

    #kargs
    filename = error.ParseKargs(kargs, "FileName", default="result")
    format = error.ParseKargs(kargs, "Format", default="O",
                              possible=variance_type)

    #args
    vec = args[0]
    class_variable = args[1]
    response_variable = args[2]
    utype = args[3]
    error.CheckType([vec, class_variable, response_variable, utype],
                    [_Vectors, int, int, str])

    try:
        utype = variance_type[args[3]]
    except KeyError:
        raise KeyError("Possible type are : " + str(variance_type.keys()))


    return vec.variance_analysis(class_variable, response_variable, utype,
                                 filename, format)
Beispiel #2
0
    def estimate_convolution(histo, *args, **kargs):
        """ Estimate a convolution

        :Usage:

        .. doctest::
            :options: +SKIP

            >>> Estimate(histo, "CONVOLUTION", dist,
                    MinInfBound=1, Parametric=False)
                    Estimate(histo, "CONVOLUTION", dist,
                    InitialDistribution=initial_dist, Parametric=False)

        """

        if len(args) == 0:
            raise ValueError("expect at least two argument")
        known_distribution = args[0]
        Weight = kargs.get("Weight", -1)
        NbIteration = kargs.get("NbIteration", -1)
        InitialDistribution = kargs.get("InitialDistribution", None)
        MinInfBound = kargs.get("MinInfBound", 0)

        Estimator = error.ParseKargs(kargs, "Estimator", "Likelihood",
                                     estimator_type)
        Penalty = error.ParseKargs(kargs, "Penalty", "SecondDifference",
                                   smoothing_penalty_type)
        Outside = error.ParseKargs(kargs, "Outside", "Zero", outside_type)

        if isinstance(known_distribution, _DiscreteParametricModel):
            known_distribution = _DiscreteParametric(known_distribution)
        elif type(known_distribution) in [
                _DiscreteMixture, _Convolution, _Compound
        ]:
            known_distribution = _Distribution(known_distribution)
        else:
            raise TypeError("""
            argument "known_distribution" must be of type _DiscreteMixture, _COnvolution,
            _Compound or _DiscreteParametricModel""")

        if InitialDistribution:
            return histo.convolution_estimation1(known_distribution,
                                                 InitialDistribution,
                                                 Estimator, NbIteration,
                                                 Weight, Penalty, Outside)

        else:
            return histo.convolution_estimation2(known_distribution,
                                                 MinInfBound, Estimator,
                                                 NbIteration, Weight, Penalty,
                                                 Outside)
Beispiel #3
0
def ContingencyTable(*args, **kargs):
    """
    Computation of a contingency table.

    :Parameters:

      * vec (_Vectors),
      * variable1, variable2 (int): variable indices,

    :Keywords:

      * FileName (string): name of the result file,
      * Format (string): format of the result file: "ASCII" (default format) or "SpreadSheet".
        This optional argument can only be used in conjunction with the optional argument FileName.

    :Returns:

        The contingency table result as a string

    :Examples:

    .. doctest::
        :options: +SKIP

        >>> ContingencyTable(vec, variable1, variable2, FileName="result", Format="SpreadSheet")


    """
    error.CheckArgumentsLength(args, 3, 3)
    error.CheckKargs(kargs, possible_kargs = ["FileName", "Format"])

    possible_v = [str(f) for f in OutputFormat.values.values()] # possible output formats
    #kargs
    filename = error.ParseKargs(kargs, "FileName", default="result")
    format = error.ParseKargs(kargs, "Format", default="ASCII",
                              possible=possible_v)

    #args
    vec = args[0]
    variable1 = args[1]
    variable2 = args[2]
    error.CheckType([vec, variable1, variable2], [_Vectors, int, int])

    of = "OutputFormat." + format + ".real"
    of = eval(of)
    return vec.contingency_table(variable1, variable2, filename, of)
Beispiel #4
0
def ComputeRankCorrelation(*args, **kargs):
    """ComputeRankCorrelation

    Computation of the rank correlation matrix.

    :Usage:

    >>> vec = Vectors([1,2,3,4,5,4,3,2,1])
    >>> ComputeRankCorrelation(vec, Type="Spearman", FileName='')

    :Arguments:

    * vec (vectors).

    :Optional Arguments:

    * Type (string): type of rank correlation coefficient:
      "Spearman" (the default) or "Kendall".

    :Returned Object:

    No object returned.
    """

    func_map = {
            "Spearman": 0,
            "Kendall": 1
            }

    error.CheckArgumentsLength(args, 1, 1)
    error.CheckKargs(kargs, possible_kargs = ["Type", "FileName"])

    #kargs
    utype = error.ParseKargs(kargs, "Type", default="Spearman",
                             possible=func_map)
    filename = error.ParseKargs(kargs, "FileName", default=None)

    #args
    vec = args[0]

    error.CheckType([vec], [_Vectors])

    _a = vec.rank_correlation_computation(utype, filename)
def compare_histo(histo, *args, **kargs):
    """Comparison of frequency distributions.

    :Parameters:
      * `histo1`, `histo2`, ... (histogram, mixture_data, convolution_data, compound_data),
      * `type` (string): variable type ("NUMERIC" ("N"), "ORDINAL" ("O") or "SYMBOLIC" ("S")).

    :Keywords:
      - FileName (string) : name of the result file
      - Format (string) : format of the result file: "ASCII" (default format) or "SpreadSheet".
        This optional argument can only be used in conjunction with the optional argument FileName.

    :Returns:
      The comparison result.

    :Examples:

    .. doctest::
        :options: +SKIP

        >>> compare_histo(histo1, histo2, ..., type, FileName="result",
        ... Format="ASCII")

    .. seealso::
        :func:`~openalea.stat_tool.comparison.Compare`

    """
    utype = args[-1]
    if utype not in variable_type.keys():
        raise KeyError("%s not found. Allowed keys are %s"
                       % (utype, variable_type.keys()))


    utype = variable_type[args[-1]]

    error.CheckType([histo],
                        [[_DiscreteDistributionData, _DiscreteMixtureData,
                          _ConvolutionData, _CompoundData]])

    histos = args[0:-1]
    for h in histos:
        error.CheckType([h],
                        [[_DiscreteDistributionData, _DiscreteMixtureData,
                          _ConvolutionData, _CompoundData]])
    filename = kargs.get("Filename", None)
    format = error.ParseKargs(kargs, "Format", "ASCII",
                                  possible=format_type)

    ret = histo.compare(histos, utype, filename, format)

    return ret
Beispiel #6
0
def Vectors(*args, **kargs):
    """
    Construction of a set of vectors from a multidimensional array,
    from a set of sequences or from an ASCII file.

    The data structure of type list(list(int)) should be constituted at the
    most internal level of arrays of constant size.

    :Parameters:
      - `list` (list(list(int))) :
      - `seq` (sequences, discrete_sequences, markov_data, semi-markov_data)
      - `file_name` (string) :

    :Keywords:
      - Identifiers (list(int)): explicit identifiers of vectors.
        This optional argument can only be used if the first mandatory argument is of
        type list(list(int)).
      - IndexVariable (bool): taking into account of the implicit index parameter as
        a supplementary variable (default value: False). This optional argument can
        only be used if the first mandatory argument is of type `sequences`,
        `discrete_sequences`, `markov_data` or `semi-markov_data`.

    :Returns:
       If the construction succeeds, an object of type vectors is returned,
       otherwise no object is returned.

    :Examples:

    .. doctest::
        :options: +SKIP

        >>> Vectors(list, Identifiers=[1, 8, 12])
        >>> Vectors(seq, IndexVariable=True)
        >>> Vectors(file_name)

    .. seealso::
        :func:`~openalea.stat_tool.output.Save`,
        :func:`~openalea.stat_tool.data_transform.ExtractHistogram`,
        :func:`~openalea.stat_tool.cluster.Cluster`,
        :func:`~openalea.stat_tool.data_transform.Merge`,
        :func:`~openalea.stat_tool.data_transform.MergeVariable`,
        :func:`~openalea.stat_tool.data_transform.SelectIndividual`,
        :func:`~openalea.stat_tool.data_transform.SelectVariable`,
        :func:`~openalea.stat_tool.data_transform.Shift`,
        :func:`~openalea.stat_tool.cluster.Transcode`,
        :func:`~openalea.stat_tool.data_transform.ValueSelect`,
        :func:`~openalea.stat_tool.comparison.Compare`,
        :func:`~openalea.stat_tool.comparison.ComputeRankCorrelation`,
        :func:`~openalea.stat_tool.comparison.ContingencyTable`,
        :func:`~openalea.stat_tool.comparison.Regression`,
        :func:`~openalea.stat_tool.comparison.VarianceAnalysis`
    """
    error.CheckArgumentsLength(args, 1, 1)
    error.CheckKargs(kargs, possible_kargs = ["Identifiers", 
                                              "IndexVariable"])

    obj = args[0]
    ret = None

    import openalea.core.path

    if isinstance(obj, str):
        # constructor from a filename
        ret = _Vectors(args[0])
    elif isinstance(obj, openalea.core.path.path):
        # constructor from a path
        ret = _Vectors(str(args[0]))
    elif isinstance(obj, list):
        # Normal usage is Vectors([ [1,2,3],  [1,2,3], [4,5,6]])
        # If only one variable is requited, then Normal usage is
        # Vectors([ [1,2,3] ]). Yet, to simplify usage, if there is only
        # one variable, the followin if allows us to use Vectors([1,2,3])
        if type(obj[0])!=list:
            obj = [obj]



        # 0 for int, 1 for float. By default all variables are int
        #now, we loop over all sequences and sequences and if a variable 
        # is found to be float, then the type is float.
        # once a float is found, there is no need to carry on the current variable
        InputTypes = [0] * len(obj[0])
        nb_variables = len(obj[0])
        for vec in obj:
            for index, var in enumerate(vec):
                assert type(var) in [int, float], "wrong types var=%s and its type is %s" % (var, type(var))
                if type(var)==float:
                    InputTypes[index]=1


        # from a list and an optional argument

        # first, get the Identifiers and check its type
        identifiers = error.ParseKargs(kargs, "Identifiers")
        if identifiers:
            error.CheckType([identifiers], [[list]], variable_pos=[2])

            if len(identifiers) != len(obj):
                raise ValueError("""Identifiers must be a list,
                which size equals vectors's length""")
            #iif InputTypes: 
            ret = _Vectors(obj, identifiers, InputTypes)
            #else:
            #    ret = _Vectors(obj, identifiers)
        else:

            #create a standard identifiers list [0,1,2,....] for each sequences ?
            identifiers = []
            for i, vec in enumerate(obj):
                identifiers.append(i+1)
    
            print identifiers
            #if InputTypes:
            ret = _Vectors(obj, identifiers, InputTypes)
            #else:
            #    ret = _Vectors(obj, [])
    else:
        # from a sequence
        index_variable = error.ParseKargs(kargs, "IndexVariable", False,
                                          [True, False])
        error.CheckType([index_variable], [bool], variable_pos=[2])
        ret = obj.build_vectors(index_variable)


    return ret
Beispiel #7
0
def VectorDistance(*args, **kargs):
    """
    Construction of an object of type vector_distance from types (and eventually weights)
    of variables or from an ASCII file.

    The type _VectorDistance implements standardization procedures. The objective of
    standardization is to avoid the dependence on the variable type
    (chosen among symbolic, ordinal, numeric and circular) and, for numeric variables,
    on the choice of the measurement units by converting the original variables to
    unitless variables.

    :Parameters:
      - `type1`, `type2`, ... (string):
        variable types ("NUMERIC" ("N"), "ORDINAL" ("O") or "SYMBOLIC" ("S")),
      - `weight1`, `weight2`, ... (float): weights of variables,
      - `file_name` (string).

    :Keywords:

      * Distance (string): distance type: "ABSOLUTE_VALUE" (default) or "QUADRATIC".
        This optional argument is only relevant in the multivariate case.

    :Returns:
        If the construction succeeds, an object of type vector_distance is returned.

    :Examples:

    .. doctest::
        :options: +SKIP

        >>> VectorDistance(type1, type2,..., Distance="QUADRATIC")
        >>> VectorDistance(weight1, type1, weight2, type2,..., Distance="QUADRATIC")
        >>> VectorDistance(file_name)

    .. seealso::
        :func:`~openalea.stat_tool.comparison.Compare`
    """

    error_arguments = ["",
                       """If first argument is a number, following
    argument must be in ["N", "O", "S"]. Check documentation by typing
    VectorDistance? .""",
    ""]

    distance = error.ParseKargs(kargs, "Distance", "ABSOLUTE_VALUE",
                                distance_type)


    # Case VectorDistance("O", "N", "S")
    if args[0] in variable_type.keys():
        # check that all following arguments (if any) are correct
        types = []
        for arg, index in zip(args, range(0, len(args))):
            # check that the arguments are correct
            if arg not in variable_type.keys():
                raise ValueError(error_arguments[1])
            else:
                types.append(variable_type[arg])
        # assign a uniform weights since none were provided
        weights = [1./len(types) for _elem in types]

        return _VectorDistance(types, weights, distance)
    # same as above but with weights VectorDistance(0.5, "N", 0.5, "S")
    if isinstance(args[0], int) or isinstance(args[0], float):
        types = list(args[1:len(args):2])
        weights = list(args[0:len(args):2])
        assert len(types)==len(weights)

        # check that types are strings
        error.CheckType(types, [str]*len(types))
        # check that weights are integer or floats
        error.CheckType(weights, [[int, float]]*len(weights))

        # convert to vector_distance_type
        for arg, index in zip(types, range(0, len(types))):
            types[index] = variable_type[types[index]]

        return _VectorDistance(types, weights, distance)
    # filename case
    elif isinstance(args[0], str) and len(args)==1 and \
            args[0] not in variable_type.keys():
        return _VectorDistance(args[0])
def ValueSelect(obj, *args, **kargs):
    """Selection of individuals according to the values taken by a variable

    :Parameters:

      * histo (histogram, mixture_data, convolution_data, compound_data),
      * value (int): value,
      * min_value (int): minimum value,
      * max_value (int): maximum value,
      * vec1 (vectors): values,
      * vecn (vectors): vectors,
      * variable (int): variable index,
      * seq1 (sequences, discrete_sequences, markov_data, semi-markov_data): univariate sequences,
      * seqn (sequences, discrete_sequences, markov_data, semi-markov_data): multivariate sequences.

    :Keywords:

      * Mode (string): conservation or rejection of selected individuals: "Keep" (the default) or "Reject".

    :Returns:

        If either value 0 or if 0 < min_value < max_value and if the range of values defined either
        by value or by min_value and max_value enables to select individuals, an object of
        type HISTOGRAM is returned (respectively vectors, sequences or discrete_sequences),
        otherwise no object is returned. In the case of a first argument of type sequences,
        discrete_sequences, markov_data or semi-markov_data, the returned object is of type
        discrete_sequences if all the variables are of type STATE, if the possible values for each
        variable are consecutive from 0 and if the number of possible values for each variable is < 15.

    :Examples:

    .. doctest::
        :options: +SKIP

        >>> ValueSelect(histo, value, Mode="Reject")
        >>> ValueSelect(histo, min_value, max_value, Mode="Reject")
        >>> ValueSelect(vec1, value, Mode="Reject")
        >>> ValueSelect(vec1, min_value, max_value, Mode="Reject")
        >>> ValueSelect(vecn, variable, value, Mode="Reject")
        >>> ValueSelect(vecn, variable, min_value, max_value, Mode="Reject")
        >>> ValueSelect(seq1, value, Mode="Reject")
        >>> ValueSelect(seq1, min_value, max_value, Mode="Reject")
        >>> ValueSelect(seqn, variable, value, Mode="Reject")
        >>> ValueSelect(seqn, variable, min_value, max_value, Mode="Reject")

    .. seealso::
        :func:`~openalea.stat_tool.cluster.Cluster`,
        :func:`~openalea.stat_tool.data_transform.Merge`,
        :func:`~openalea.stat_tool.data_transform.Shift`,
        :func:`~openalea.stat_tool.data_transform.Transcode`,
        :func:`~openalea.stat_tool.data_transform.SelectIndividual`,
        :func:`~openalea.stat_tool.data_transform.MergeVariable`,
        :func:`~openalea.stat_tool.data_transform.SelectVariable`
        Cumulate`
        Difference`
        IndexExtract`
        LengthSelect`,
        MovingAverage`,
        RecurrenceTimeSequences`
        RemoveRun`,
        Reverse`,
        SegmentationExtract`,
        VariableScaling`.
    """
    error.CheckArgumentsLength(args, 1, 3)
    Mode = error.ParseKargs(kargs, "Mode", "Keep", keep_type)
    #keep = bool(Mode == "Keep" or Mode == "keep")
    keep = bool(Mode == "Keep")
    # Test for vectors
    try:
        nb_variable = obj.nb_variable
    except AttributeError:
        nb_variable = 0

    if len(args) == 3:
        variable, umin, umax = args

    elif len(args) == 2:
        # 2 cases (min_value, max_value) or (variable, value)
        if nb_variable:
            variable, umin = args
            umax = umin
        else:
            umin, umax = args

    elif len(args) == 1:
        value = args[0]
        error.CheckType([value], [[int, tuple, list]])
        if isinstance(value, tuple) and len(value) == 2:
            umin, umax = value
        else:
            umin = umax = value

    if (nb_variable):  # Vectors, sequences
        return obj.value_select(variable, umin, umax, keep)
    else:
        return obj.value_select(umin, umax, keep)
Beispiel #9
0
    def estimate_compound(histo, *args, **kargs):
        """estimate a compound


        :Usage:

        .. doctest::
            :options: +SKIP

            >>> Estimate(histo, "COMPOUND", dist, unknown,
                    Parametric=False, MinInfBound=0)
                    Estimate(histo, "COMPOUND", dist, unknown,
                    InitialDistribution=initial_dist, Parametric=False)
        """

        if len(args) < 2:
            raise ValueError("expect at least three arguments")

        known_distribution = args[0]
        ##if isinstance(known_distribution, _DiscreteParametricModel):
        #    known_distribution = _DiscreteParametric(known_distribution)
        #elif type(known_distribution) in [_DiscreteMixture, _Convolution, _Compound]:
        #    known_distribution = _Distribution(known_distribution)
        #else:
        #    raise TypeError("""
        #    argument "known_distribution" must be of type _DiscreteMixture,
        #     _COnvolution, _Compound or _DiscreteParametricModel""")

        Type = args[1]
        error.CheckType([Type], [str])

        Weight = kargs.get("Weight", -1)
        NbIteration = kargs.get("NbIteration", -1)
        InitialDistribution = kargs.get("InitialDistribution", None)
        MinInfBound = kargs.get("MinInfBound", 0)

        Estimator = error.ParseKargs(kargs, "Estimator", "Likelihood",
                                     estimator_type)
        Penalty = error.ParseKargs(kargs, "Penalty", "SecondDifference",
                                   smoothing_penalty_type)
        Outside = error.ParseKargs(kargs, "Outside", "Zero", outside_type)

        if MinInfBound and InitialDistribution:
            raise ValueError("""MinInfBound and InitialDistribution cannot be
                             used together.""")
        #if Estimator != _stat_tool.PENALIZED_LIKELIHOOD:
        #    if Penalty or Weight or Outside:
        #        raise ValueError("""Estimator cannot be used with O
        #            utside or Weight or Penalty option""")

#The second argument is either a string (e.g.,"Sum") or an unknown
#distribution.
        try:
            if Type:
                Type = compound_type[Type]
        except KeyError:
            raise AttributeError("Bad type. Possible types are %s" %
                                 (str(compound_type.keys())))

        #The second argument is either a string (e.g.,"Sum") or an unknown
        #distribution.
        unknown_distribution = None

        if InitialDistribution:
            unknown_distribution = InitialDistribution
            if isinstance(unknown_distribution, _Distribution):
                unknown_distribution = _DiscreteParametric(
                    unknown_distribution)
            elif type(unknown_distribution) in \
                [_DiscreteMixture, _Convolution, _Compound]:
                unknown_distribution = _Distribution(unknown_distribution)
            else:
                raise TypeError("""
                    argument "known_distribution" must be of type
                     _DiscreteMixture, _COnvolution, _Compound or _DiscreteParametricModel"""
                                )
            if Type == 's':

                return histo.compound_estimation1(unknown_distribution,
                                                  known_distribution, Type,
                                                  Estimator, NbIteration,
                                                  Weight, Penalty, Outside)
            elif Type == 'e':

                return histo.compound_estimation1(known_distribution,
                                                  unknown_distribution, Type,
                                                  Estimator, NbIteration,
                                                  Weight, Penalty, Outside)
            else:
                raise KeyError("should not enter here.")
        else:
            return histo.compound_estimation2(known_distribution, Type,
                                              MinInfBound, Estimator,
                                              NbIteration, Weight, Penalty,
                                              Outside)
Beispiel #10
0
def Cluster(obj, utype, *args, **kargs):
    """Clustering of values.

    In the case of the clustering of values of a frequency distribution on the
    basis of an information measure criterion (argument `Information`), both the
    information measure ratio and the selected optimal step are given in the
    shell window.

    The clustering mode `Step` (and its variant `Information`) is naturally
    adapted to numeric variables while the clustering mode `Limit` applies to
    both symbolic (nominal) and numeric variables. In the case of a symbolic
    variable, the function `Cluster` with the mode `Limit` can be seen as a
    dedicated interface of the more general function `Transcode`.

    :Parameters:

      * `histo` (`_FrequencyDistribution`, `_DiscreteMixtureData`, `_ConvolutionData`, `_CompoundData`),
      * `step` (int) - step for the clustering of values
      * `information_ratio` (float) - proportion of the information measure of \
        the original sample for determining the clustering step,
      * `limits` (list(int)) - first values corresponding to the new classes \
        classes 1, ..., nb_class - 1. By convention, the first value corresponding \
        to the first class is 0,
      * `vec1` (`_Vector`) - values,
      * `vecn` (`_Vectors`) - vectors,
      * `variable` (int) - variable index,
      * `seq1` (`_Sequences`) - univariate sequences,
      * `seqn` (`_Sequences`) - multivariate sequences,
      * `discrete_seq1` (`_DiscreteSequences`, `_Markov`, `_SemiMarkovData`) -
        discrete univariate sequences,
      * `discrete_seqn` (`_DiscreteSequences`, `_Markov`, `_SemiMarkovData`) -
        discrete multivariate sequences.

    :Keywords:

      * `AddVariable` (bool) : addition (instead of simple replacement) of the variable
        corresponding to the clustering of values (default value: False).
        This optional argument can only be used if the first argument is of
        type `_DiscreteSequences`, `_Markov` or `_SemiMarkovData`. The addition
        of the clustered variable is particularly useful if one wants to evaluate
        a lumpability hypothesis.

    :Returns:

      * If `step` > 0, or if 0 <  `information_ratio` <  1, or if 0 < limits[1]
        < limits[2] < ... < limits[nb_class - 1] < (maximum possible value of histo),
        an object of type _FrequencyDistribution is returned.
      * If variable is a valid index of a variable and if `step` > 0, or
        if 0 < limits[1] < limits[2] < ... < limits[nb_class - 1] < (maximum possible
        value taken by the selected variable of `vec1` or `vecn`), an object of type
        `_Vectors` is returned.
      * If variable is a valid index of a variable of type STATE and if `step` > 0, or \
        if 0 < limits[1] < limits[2] < ... < limits[nb_class - 1] < (maximum
        possible value taken by the selected variable of `seq1`, `seqn`, `discrete_seq1`
        or `discrete_seqn`), an object of type `_Sequences` or `_DiscreteSequences`
        is returned.
      * In the case of a first argument of type `_Sequences`, an object of type
        `_DiscreteSequences` is returned if all the variables are of type STATE,
        if the possible values taken by each variable are consecutive from 0 and
        if the number of possible values for each variable is < 15.

    :Examples:

    .. doctest::
        :options: +SKIP

        >>> Cluster(histo, "Step", step)
        >>> Cluster(histo, "Information", information_ratio)
        >>> Cluster(histo, "Limit", limits)
        >>> Cluster(vec1, "Step", step)
        >>> Cluster(vecn, "Step", variable, step)
        >>> Cluster(vec1, "Limit", limits)
        >>> Cluster(vecn, "Limit", variable, limits)
        >>> Cluster(seq1, "Step", step)
        >>> Cluster(seqn, "Step", variable, step)
        >>> Cluster(discrete_seq1, "Step", step, AddVariable=True)
        >>> Cluster(discrete_seqn, "Step", variable, step, AddVariable=True)
        >>> Cluster(seq1, "Limit", limits)
        >>> Cluster(seqn, "Limit", variable, limits)
        >>> Cluster(discrete_seq1, "Limit", limits, AddVariable=True)
        >>> Cluster(discrete_seqn, "Limit", variable, limits, AddVariable=True)

    .. seealso::
        :func:`~openalea.stat_tool.data_transform.Merge`,
        :func:`~openalea.stat_tool.data_transform.Shift`,
        :func:`~openalea.stat_tool.data_transform.ValueSelect`,
        :func:`~openalea.stat_tool.data_transform.MergeVariable`,
        :func:`~openalea.stat_tool.data_transform.SelectIndividual`,
        :func:`~openalea.stat_tool.data_transform.SelectVariable`,
        :func:`~openalea.stat_tool.cluster.Transcode`,
        :func:`~openalea.stat_tool.data_transform.AddAbsorbingRun`,
        :func:`~openalea.stat_tool.data_transform.Cumulate`,
        :func:`~openalea.stat_tool.data_transform.Difference`,
        :func:`~openalea.stat_tool.data_transform.IndexExtract`,
        :func:`~openalea.stat_tool.data_transform.LengthSelect`,
        :func:`~vplants.sequence_analysis.data_transform.MovingAverage`,
        :func:`~openalea.stat_tool.data_transform.RecurrenceTimeSequences`,
        :func:`~openalea.stat_tool.data_transform.Removerun`,
        :func:`~openalea.stat_tool.data_transform.Reverse`,
        :func:`~openalea.stat_tool.data_transform.SegmentationExtract`,
        :func:`~openalea.stat_tool.data_transform.VariableScaling`.
    """

    # fixme: what about the Mode in the Step case ?
    # check markovian_sequences call in Sequences
    AddVariable = error.ParseKargs(kargs,
                                   "AddVariable",
                                   False,
                                   possible=[False, True])

    possible_r = [str(f) for f in mode_type]  # possible rounding modes
    RoundingVariable = error.ParseKargs(kargs,
                                        "Round",
                                        "ROUND",
                                        possible=possible_r)

    error.CheckArgumentsLength(args, 1, 2)

    # search for the function name
    if hasattr(obj, cluster_type[utype]):
        func = getattr(obj, cluster_type[utype])
    else:
        raise KeyError("""Possible action are : 'Step', 'Information' or
        'Limit'. Information cannot be used with Vectors objects""")

    # check if nb_variable is available (vectors, sequences)
    if hasattr(obj, 'nb_variable'):
        nb_variable = obj.nb_variable
    else:
        nb_variable = 1

    #check types
    if nb_variable == 1:
        if len(args) == 1:
            if utype == "Step":
                error.CheckType([args[0]], [int])
            if utype == "Limit":
                error.CheckType([args[0]], [list])
            if utype == "Information":
                error.CheckType([args[0]], [[int, float]])
            try:
                ret = func(args[0])  # histogram case
            except:
                try:
                    ret = func(1, args[0])  # vector case
                except:
                    try:
                        ret = func(1, args[0], AddVariable)  # sequences case
                    except:
                        pass
        else:
            raise ValueError("""Extra arguments provided 
            (to specify variable value ?). Consider removing it. 
            Be aware that nb_variable equals 1""")

    else:
        if len(args) == 2:
            if utype == "Step":
                error.CheckType([args[0]], [int])
                error.CheckType([args[1]], [[int, float]])
            if utype == "Limit":
                error.CheckType([args[0]], [int])
                error.CheckType([args[1]], [list])
            try:
                ret = func(*args)
            except:
                ret = func(args[0], args[1],
                           mode_type[RoundingVariable].real)  # sequences case
        else:
            raise ValueError("""Extra arguments provided 
            (to specify variable value ?).
            Consider removing it. Be aware that nb_variable equals 1""")

    if hasattr(ret, 'markovian_sequences'):
        ret = ret.markovian_sequences()

    return ret
Beispiel #11
0
    def estimate_DiscreteMixture(histo, *args, **kargs):
        """ Estimate a finite  mixture of discrete distributions


        :Parameters:

          * histo (histogram, mixture_data, convolution_data, compound_data),
          * distributions (list) : a list of distribution object
                                   or distribution label(string) : 'B', 'NB', 'U', 'P', ...
          * unknown (string): type of unknown distribution: "Sum" or "Elementary".

        :Keywords:

          * MinInfBound (int): lower bound to the range of possible values (0 -default- or 1). \
                               This optional argument cannot be used in conjunction \
                               with the optional argument InitialDistribution.
          * InfBoundStatus (string): shifting or not of the distribution: "Free" (default value) or "Fixed".
          * DistInfBoundStatus (string): shifting or not of the subsequent components of \
                                         the mixture: "Free" (default value) or "Fixed".
          * NbComponent (string): estimation of the number of components of the mixture: \
                                  "Fixed" (default value) or "Estimated". Le number of estimated \
                                  components is comprised between\
                                  1 and a maximum number which is given by the number of specified \
                                  parametric distributions in the mandatory arguments \
                                  (all of these distributions are assumed to be unknown).
          * Penalty (string): type of Penalty function for model selection: \
                              "AIC" (Akaike Information Criterion), \
                              "AICc" (corrected Akaike Information Criterion) \
                              "BIC" (Bayesian Information Criterion - default value). \
                              "BICc" (corrected Bayesian Information Criterion). \

                              This optional argument can only be used if the optional argument
                              NbComponent is set at "Estimated".

        :Examples:

        .. doctest::
            :options: +SKIP

            >>> estimate_DiscreteMixture(histo, "MIXTURE", "B", dist,...,,
                             MinInfBound=1, InfBoundStatus="Fixed",
                             DistInfBoundStatus="Fixed")
            >>> estimate_DiscreteMixture(histo, "MIXTURE", "B", "NB",...,,
                               MinInfBound=1, InfBoundStatus="Fixed",
                               DistInfBoundStatus="Fixed",
                               NbComponent="Estimated", Penalty="AIC")
            >>> Estimate(histo, "MIXTURE", "B", dist, MinInfBound=1, InfBoundStatus="Fixed",
                    DistInfBoundStatus="Fixed")
            >>> Estimate(histo, "MIXTURE", "B", "NB",
                    MinInfBound=1, InfBoundStatus="Fixed",
                    DistInfBoundStatus="Fixed",
                    NbComponent="Estimated", Penalty="AIC")


        """
        #alias

        #error.CheckArgumentsLength(args, 1, 1)

        # get user arguments
        # list of distributions can be either a list or several arguments
        # e.g.: estimate_DiscreteMixture(["B","B"]) or estimate_DiscreteMixture("B", "B")
        if len(args) == 1 and type(args[0]) == list:
            distributions = args[0]
        else:
            distributions = list(args)

        InfBoundStatus = kargs.get("InfBoundStatus", "Free")
        DistInfBoundStatus = kargs.get("DistInfBoundStatus", "Free")
        NbComponent = kargs.get("NbComponent", "Fixed")

        MinInfBound = kargs.get("MinInfBound", 0)
        Penalty = error.ParseKargs(kargs, "Penalty", "AIC",
                                   likelihood_penalty_type)

        #should be before the conversion to booleans
        error.CheckType([
            MinInfBound, InfBoundStatus, DistInfBoundStatus, NbComponent,
            Penalty
        ], [int, str, str, str, LikelihoodPenaltyType])

        # transform into boolean when needed
        InfBoundStatus = bool(InfBoundStatus == "Free")
        DistInfBoundStatus = bool(DistInfBoundStatus == "Free")
        NbComponent = bool(NbComponent == "Estimated")

        estimate = []  # list of bool
        pcomponent = []  # list of distribution
        ident = []  # list of distribution identifier

        # Parse list of distribution that could be defined by a distribution,
        # compound, mixture, convolution or simplya string such as "B",
        # "Mixture", ...

        for dist in distributions:

            if isinstance(dist, str):
                dist_authorised = [
                    "BINOMIAL", "B", "POISSON", "P", "NB", "NEGATIVE_BINOMIAL"
                ]
                if dist not in dist_authorised:
                    raise ValueError("""If distribution is a string, then it
                        must be in %s. You provided %s""" %
                                     (dist_authorised, dist))
                #todo: check that poisson is allowed

                pcomponent.append(_DiscreteParametric(0, dist_type[dist]))
                ident.append(dist_type[dist])
                estimate.append(True)
            elif isinstance(dist, _DiscreteParametricModel):
                pcomponent.append(_DiscreteParametric(dist))
                ident.append(None)
                estimate.append(False)
            elif type(dist) in [_DiscreteMixture, _Convolution, _Compound]:
                pcomponent.append(_Distribution(dist))
                ident.append(None)
                estimate.append(False)
            else:
                raise ValueError("""In the case of a MIXTURE estimation,
                argument related to distributions must be either string, or
                Distribution, Mixture, Convolution, Compound. %s provided""" %
                                 dist)

        # check parameters
        if not NbComponent and Penalty:
            raise TypeError("""
            Penalty can only be used with NbComponent set to 'Estimated'""")

        if not NbComponent:  # "FIXED"
            imixt = _DiscreteMixture(pcomponent)
            ret = histo.discrete_mixture_estimation1(imixt, estimate,
                                                     MinInfBound,
                                                     InfBoundStatus,
                                                     DistInfBoundStatus)

            return ret
        else:  # "ESTIMATED"
            ret = histo.discrete_mixture_estimation2(ident, MinInfBound,
                                                     InfBoundStatus,
                                                     DistInfBoundStatus,
                                                     Penalty)
            return ret
Beispiel #12
0
def Clustering(matrix, utype, *args, **kargs):
    """
    Application of clustering methods (either partitioning methods or hierarchical methods)
    to dissimilarity matrices between patterns.

    In the case where the composition of clusters is a priori fixed,
    the function Clustering simply performs an evaluation of the a priori fixed
    partition.

    :Parameters:
      * `dissimilarity_matrix` (distance_matrix) - dissimilarity matrix between patterns,
      * `nb_cluster` (int) - number of clusters,
      * `clusters` (list(list(int))) - cluster composition.

    :Keywords:
      * `Prototypes` (list(int)): cluster prototypes.
      * `Algorithm` (string): "Agglomerative", "Divisive" or "Ordering"
      * `Criterion` (string): "FarthestNeighbor" or "Averaging"
      * `Filename` (string): filename
      * `Format` (string) : "ASCII" or "SpreadSheet"

    :Returns:
        If the second mandatory argument is "Partitioning" and
        if 2 < nb_cluster < (number of patterns), an object of type clusters
        is returned

    :Examples:

    .. doctest::
        :options: +SKIP

        >>> Clustering(dissimilarity_matrix, "Partition", nb_cluster, Prototypes=[1, 3, 12])
        >>> Clustering(dissimilarity_matrix, "Partition", clusters)
        >>> Clustering(dissimilarity_matrix, "Hierarchy", Algorithm="Agglomerative")
        >>> Clustering(dissimilarity_matrix, "Hierarchy", Algorithm="Divisive")

    .. seealso::
        :func:`~openalea.stat_tool.data_transform.SelectIndividual`,
        `Symmetrize`,
        :func:`~openalea.stat_tool.comparison.Compare`,
        :func:`~openalea.stat_tool.cluster.ToDistanceMatrix`.

    .. note:: if type=Partition, Algorthim must be 1 (divisive) or 2 (ordering).

    .. note:: if type!=Divisive criterion must be provided
    """
    #TODO: check this case :
    #Clustering(dissimilarity_matrix, "Partition", clusters)

    error.CheckType([matrix], [_DistanceMatrix])

    Algorithm = error.ParseKargs(kargs,
                                 "Algorithm",
                                 default="Divisive",
                                 possible=algorithm_type)
    # Switch for each type of clustering
    # first the partition case
    if utype == "Partition":
        error.CheckArgumentsLength(args, 1, 1)
        error.CheckKargs(kargs, ["Algorithm", "Prototypes", "Initialization"])
        Initialization = error.ParseKargs(kargs,
                                          "Initialization",
                                          1,
                                          possible=[1, 2])

        if Algorithm == algorithm_type["Agglomerative"]:
            raise ValueError("""If partition is on, Algorithm cannot
                    be agglomerative""")

        if (isinstance(args[0], int)):  #int case
            # if Prototypes is empty, the wrapping will send an
            # int * = 0 to the prototyping function, as expected
            Prototypes = kargs.get("Prototypes", [])
            nb_cluster = args[0]
            return matrix.partitioning_prototype(nb_cluster, Prototypes,
                                                 Initialization, Algorithm)
        elif isinstance(args[0], list):  # array case
            #todo:: array of what kind of object?
            #need a test
            return matrix.partitioning_clusters(args[0])
        else:
            raise TypeError("""
            With Partition as second argument, the third one must be either
            an int or an array.""")

    elif utype == "Hierarchy":
        error.CheckKargs(kargs,
                         ["Algorithm", "FileName", "Criterion", "Format"])

        Algorithm = error.ParseKargs(kargs,
                                     "Algorithm",
                                     default="Agglomerative",
                                     possible=algorithm_type)

        Criterion = error.ParseKargs(kargs,
                                     "Criterion",
                                     "Averaging",
                                     possible=criterion_type)

        # fixme: is it correct to set "" to the filename by defautl ?
        # if set to None, the prototype does not match
        filename = kargs.get("Filename", None)
        format = error.ParseKargs(kargs,
                                  "Format",
                                  "ASCII",
                                  possible=format_type)
        #check options
        if Algorithm != algorithm_type["Agglomerative"] and \
            kargs.get("Criterion"):

            raise ValueError("""
                In the Hierarchy case, if Algorithm is different from
                AGGLOMERATIVE, then Criterion cannot be used.""")
        return matrix.hierarchical_clustering(Algorithm, Criterion, filename,
                                              format)

    else:
        raise KeyError("Second argument must be 'Partitioning' or 'Hierarchy'")
Beispiel #13
0
def Transcode(obj, *args, **kargs):
    """
    Transcoding of values.

    The function `Cluster` with the mode "Limit" can be seen as a dedicated interface of
    the more general function Transcode.

    :Parameters:

      * `histo` (_FrequencyDistribution, _MixtureData, _ConvolutionData, _CompoundData),
      * `new_values` (array(int)) - new values replacing the old ones min, min + 1, ..., max.
      * `vec1` (_Vectors) - values,
      * `vecn` (_Vectors) - vectors,
      * `variable` (int) - variable index,
      * `seq1` (_Sequences) - univariate sequences,
      * `seqn` (_Ssequences) - multivariate sequences,
      * `discrete_seq1` (_DiscreteSequences, _MarkovData, _SemiMarkovData) - discrete univariate sequences,
      * `discrete_seqn` (_DiscreteSequences, _MarkovData, _SemiMarkovData) - discrete multivariate sequences.

    :Keywords:

      * AddVariable (bool): addition (instead of simple replacement) of the variable
        to which the transcoding is applied (default value: False). This optional argument
        can only be used if the first argument is of type (_DiscreteSequences, _MarkovData,
        _SemiMarkovData).

    :Returns:

        If the new values are in same number as the old values and are consecutive from 0,
        an object of type _FrequencyDistribution is returned (respectively _Vectors, _Sequences or
        _DiscreteSequences). In the case of a first argument of type _Sequences, the
        returned object is of type _DiscreteSequences if all the variables are of type STATE,
        if the possible values for each variable are consecutive from 0 and if the number of
        possible values for each variable is < 15.

    :Examples:

    .. doctest::
        :options: +SKIP

        >>> Transcode(histo, new_values)
        >>> Transcode(vec1, new_values)
        >>> Transcode(vecn, variable, new_values)
        >>> Transcode(seq1, new_values)
        >>> Transcode(seqn, variable, new_values)
        >>> Transcode(discrete_seq1, new_values, AddVariable=True)
        >>> Transcode(discrete_seqn, variable, new_values, AddVariable=True)

    .. seealso::

        :func:`~openalea.stat_tool.cluster.Clustering`,
        :func:`~openalea.stat_tool.data_transform.Merge`,
        :func:`~openalea.stat_tool.data_transform.Shift`,
        :func:`~openalea.stat_tool.data_transform.ValueSelect`,
        :func:`~openalea.stat_tool.data_transform.MergeVariable`,
        :func:`~openalea.stat_tool.data_transform.SelectIndividual`,
        :func:`~openalea.stat_tool.data_transform.SelectVariable`,
        :func:`~openalea.stat_tool.cumulate.Cumulate`,
        :func:`~openalea.stat_tool.data_transform.AddAbsorbingRun`,
        :func:`~openalea.stat_tool.cumulate.Cumulate`,
        :func:`~openalea.stat_tool.data_transform.Difference`,
        :func:`~openalea.stat_tool.data_transform.IndexExtract`,
        :func:`~openalea.stat_tool.data_transform.LengthSelect`,
        :func:`~openalea.stat_tool.data_transform.MovingAverage`,
        :func:`~openalea.stat_tool.data_transform.RecurrenceTimeSequences`,
        :func:`~openalea.stat_tool.data_transform.Removerun`,
        :func:`~openalea.stat_tool.data_transform.Reverse`,
        :func:`~openalea.stat_tool.data_transform.SegmentationExtract`,
        :func:`~openalea.stat_tool.data_transform.VariableScaling`.
    """
    AddVariable = error.ParseKargs(kargs,
                                   "AddVariable",
                                   False,
                                   possible=[False, True])

    myerror = "Arguments do not seem to be correct"

    if hasattr(obj, 'nb_variable'):  # case sequence, vectors
        nb_variable = obj.nb_variable
        if len(args) == 1 and nb_variable == 1:

            try:
                ret = obj.transcode(1, args[0], AddVariable)
            except:
                ret = obj.transcode(1, args[0])

        elif len(args) == 2 and nb_variable != 1:
            try:
                ret = obj.transcode(args[0], args[1], AddVariable)
            except:
                ret = obj.transcode(args[0], args[1])

        else:
            raise ValueError(myerror)

    else:  # case histogram and co
        nb_variable = None
        new_values = args[0]
        if len(args) > 1:
            raise ValueError(myerror)
        ret = obj.transcode(new_values)

    if ret is None:
        raise Exception("transcode function did not return anything...")
    else:
        #Sequence case to be converted to Markovian_sequences
        # todo: test and checks
        if hasattr(ret, 'markovian_sequences'):
            func = getattr(ret, 'markovian_sequences')
            ret = func()
        return ret
def Regression(vec, utype, explanatory, response, *args, **kargs):
    """
    Simple regression (with a single explanatory variable).

    :Parameters:
    
      * vec : vectors
        vectors
      * type : string
        `"Linear"` or `"MovingAverage"` or `"NearestNeighbors"`
      * explanatory_variable : int
        index of the explanatory variable
      * response_variable : int 
        index of the response variable
      * filter : list of float
        filter values on the half width i.e. from one extremity to the central value
        (with the constraint filter[i] + filter[m] = 1),
      * frequencies : list of float
        frequencies defining the filter,
      * dist : distribution, mixture, convolution, compound 
        symmetric distribution, whose size of the support is even, defining the filter 
        (for instance Distribution("BINOMIAL",0,4,0.5)),
      * span : float
        proportion of individuals in each neighbourhood. 

    :Keywords:
    
      * Algorithm : string
          - `"Averaging"` (default) 
          - `"LeastSquares"`
            
        This optional argument can only be used if the second mandatory argument
        specifying the regression type is "MovingAverage".

      * Weighting : bool 
        weighting or not of the neighbors according to their distance to the 
        computed point (default value: True). This optional argument can only
        be used if the second mandatory argument specifying the regression type 
        is "NearestNeighbors". 

    :Returns:
    
        An object of type regression is returned.

    :Examples:

    .. doctest::
        :options: +SKIP
    
        >>> Regression(vec, "Linear", explanatory_variable, response_variable)
        >>> Regression(vec, "MovingAverage", explanatory_variable, 
        ...    response_variable, filter, Algorithm="LeastSquares")
        >>> Regression(vec, "MovingAverage", explanatory_variable,
        ..     response_variable, frequencies, Algorithm="LeastSquares")
        >>> Regression(vec, "MovingAverage", explanatory_variable, 
        ...    response_variable, dist, Algorithm="LeastSquares")
        >>> Regression(vec, "NearestNeighbors", explanatory_variable, 
        ...    response_variable, span, Weighting=False)
    
    .. seealso::
        :func:`~openalea.stat_tool.output.Plot`

    """
    STAT_MINIMUM_SPAN = 0.05  # from aml not stat_tool or sequence headers

    error.CheckType([vec, utype, explanatory, response],
                    [_Vectors, str, int, int])

    possible_types = [
        "Linear", "NearestNeighbors", "NearestNeighbours", "MovingAverage"
    ]

    Algorithm = error.ParseKargs(kargs, "Algorithm", 'Averaging', algo_map)
    Weighting = error.ParseKargs(kargs, "Weighting", True, bool_type)

    if utype == "Linear":
        error.CheckArgumentsLength(args, 0, 0)
        return vec.linear_regression(explanatory, response)

    elif utype == "MovingAverage":
        error.CheckArgumentsLength(args, 1, 1)
        param = args[0]
        #todo add CheckType for int and models
        # param is a list of float, int
        if isinstance(args[0], list):
            # todo: check that sum equals 1
            return vec.moving_average_regression_values(
                explanatory, response, param, Algorithm)
        # or a set of distributions
        # todo: test case of compound, convolution, mixture
        else:
            error.CheckType([param], [[
                _DiscreteParametricModel, _DiscreteMixture, _Convolution,
                _Compound
            ]])
            return vec.moving_average_regression_distribution(
                explanatory, response, param, Algorithm)

    elif utype in ["NearestNeighbors", "NearestNeighbours"]:
        error.CheckArgumentsLength(args, 1, 1)
        span = args[0]
        error.CheckType([span], [[float, int]])

        assert span >= STAT_MINIMUM_SPAN
        #todo: check this assert

        return vec.nearest_neighbours_regression(explanatory, response,
                                                 float(span), Weighting)
    else:
        raise TypeError("Bad Regression type. Must be in %s" % possible_types)
Beispiel #15
0
    def display(self, *args, **kargs):
        format_map = {'c': 'c', 'l': 'l', 'Column': 'c', 'Line': 'l'}
        viewpoint_map = {
            'v': 'v',
            "Data": "d",
            "Survival": 's',
            "SegmentProfile": 'q',
            "StateProfile": 'p'
        }

        segmentations_map = {
            "DynamicProgramming": FORWARD_DYNAMIC_PROGRAMMING,
            "ForwardBackwardSampling": FORWARD_BACKWARD_SAMPLING
        }

        state_seq_map = {
            "GeneralizedViterbi": GENERALIZED_VITERBI,
            "ForwardBackwardSampling": FORWARD_BACKWARD_SAMPLING
        }

        # Detail level
        Detail = error.ParseKargs(kargs, "Detail", 1, [1, 2])
        if Detail == 2:
            exhaustive = True
        else:
            exhaustive = False
        Format = error.ParseKargs(kargs, "Format", "c", format_map)
        ViewPoint = error.ParseKargs(kargs, "ViewPoint", "v", viewpoint_map)
        NbStateSequence = error.ParseKargs(kargs, "NbStateSequence",
                                           NB_STATE_SEQUENCE)

        error.CheckType([NbStateSequence], [[int, float]])
        NbSegmentation = error.ParseKargs(kargs, "NbSegmentation",
                                          NB_SEGMENTATION)
        error.CheckType([NbSegmentation], [[int, float]])

        StateSequence = error.ParseKargs(kargs, "StateSequence",
                                         "GeneralizedViterbi", state_seq_map)
        Segmentation = error.ParseKargs(kargs, "Segmentation",
                                        "DynamicProgramming",
                                        segmentations_map)
        #todo it seems that by default, segmentation = FORWARD_DYNAMIC_PROGRAMMING ,

        # !! in AML, Output is not set y default, i.e. equivalent to None
        # the ParseKargs does not accept None sinc we provide the list of
        # possible keys in output_display (which do not contain None)
        #, so we first need to check the presence of Output in the kargs
        # then, to give a default value!=None. But be aware that tis default
        # value is a dummy variable that is not used.
        try:
            from openalea.sequence_analysis.enums import output_display
        except:
            from openalea.stat_tool.enums import output_display

        if kargs.get('Output'):
            try:
                Output = None
                Output = error.ParseKargs(kargs, "Output", 'Segment',
                                          output_display)
            except:
                print 'warning could not import output_display from sequence_analysis'
        else:
            try:
                from openalea.sequence_analysis.enums import output_display
            except:
                from openalea.stat_tool.enums import output_display
            Output = None

        if Output is None:
            if ViewPoint == 'q':
                Output = output_display['Segment']
            elif ViewPoint == 'p':
                Output = output_display['State']
        elif (ViewPoint == 'q' and Output not in [output_display['ChangePoint'], output_display['Segment']]) \
            or (ViewPoint == 'p'  and Output not in [output_display['State'], output_display['InState'], output_display['OutState']]):
            raise ValueError(
                " INCOMPATIBLE_OPTIONS between ViewPoint and Output")

        #check arguments compatibilities

        if Detail == 2 and ViewPoint not in ['v', 'd']:
            raise ValueError("incompatible options")
        if Format == 'l' and ViewPoint != 'd':
            raise ValueError("incompatible options")
        """if segmentations_option or nb_segmentation_option)  and \
           (view_point!='q' or args[0] not in
            (
                (args[0].tag() != AMObjType::SEQUENCES)
                && (args[0].tag() != AMObjType::MARKOVIAN_SEQUENCES)
                && (args[0].tag() != AMObjType::VARIABLE_ORDER_MARKOV_DATA)
                && (args[0].tag() != AMObjType::SEMI_MARKOV_DATA)
                && (args[0].tag() != AMObjType::NONHOMOGENEOUS_MARKOV_DATA)
            )
        )
  if (((state_sequences_option) || (nb_state_sequence_option)) && ((view_point != 'p') ||
       ((args[0].tag() != AMObjType::HIDDEN_VARIABLE_ORDER_MARKOV) &&
        (args[0].tag() != AMObjType::HIDDEN_SEMI_MARKOV) &&
        (args[0].tag() != AMObjType::MARKOVIAN_SEQUENCES) &&
        (args[0].tag() != AMObjType::VARIABLE_ORDER_MARKOV_DATA) &&
        (args[0].tag() != AMObjType::SEMI_MARKOV_DATA) &&
        (args[0].tag() != AMObjType::NONHOMOGENEOUS_MARKOV_DATA)))) {
    status = false;
    genAMLError(ERRORMSG(INCOMPATIBLE_OPTIONS_s) , "Display");
  }
  if ((output_option) && ((view_point != 'q') ||
       ((args[0].tag() != AMObjType::SEQUENCES) && (args[0].tag() != AMObjType::MARKOVIAN_SEQUENCES) &&
        (args[0].tag() != AMObjType::VARIABLE_ORDER_MARKOV_DATA) &&
        (args[0].tag() != AMObjType::SEMI_MARKOV_DATA) &&
        (args[0].tag() != AMObjType::NONHOMOGENEOUS_MARKOV_DATA))) &&
      ((view_point != 'p') || ((args[0].tag() != AMObjType::HIDDEN_SEMI_MARKOV) &&
        (args[0].tag() != AMObjType::MARKOVIAN_SEQUENCES) &&
        (args[0].tag() != AMObjType::VARIABLE_ORDER_MARKOV_DATA) &&
        (args[0].tag() != AMObjType::SEMI_MARKOV_DATA) &&
        (args[0].tag() != AMObjType::NONHOMOGENEOUS_MARKOV_DATA)))) {
    status = false;
    genAMLError(ERRORMSG(INCOMPATIBLE_OPTIONS_s) , "Display");
  }
        """

        # ---------------- ViewPoint
        # 1-Survival
        if ViewPoint == 's':
            from openalea.stat_tool.enums import histogram_types
            from openalea.stat_tool.enums import model_distribution_types

            if type(self) in model_distribution_types:
                output = self.survival_ascii_write()
            elif type(self) in histogram_types:
                output = self.survival_ascii_write()
            else:
                raise ValueError("""(%s) has no survival point. Use another
                Viewpoint or use a first argument in DISTRIBUTION or MIXTURE or
                CONVOLUTION or COMPOUND or FREQUENCY_DISTRIBUTION or
                MIXTURE_DATA or CONVOLUTION_DATA or COMPOUND_DATA""" %
                                 str(type(self)))

        # Data
        elif ViewPoint == "d":
            try:
                #todo checkType
                # Markovian_Sequences, VOMData, SMData,
                # or Nonhomogenous_Markov_data
                output = self.ascii_data_write(exhaustive, Format)

            except Exception, e:
                #for vectors only
                #todo checkType
                try:
                    output = self.ascii_data_write(exhaustive)

                except AttributeError:
                    raise AttributeError("""
                        %s has not 'data' viewpoint""" % (str(type(self))))
Beispiel #16
0
    def plot(self, *args, **kargs):

        Title = kargs.get("Title", "")

        params = kargs.get("Params", ())
        groups = kargs.get("Groups", ())

        possible_modes = {'Blocking': False, 'NonBlocking': True}
        Mode = error.ParseKargs(kargs,
                                'Mode',
                                'Blocking',
                                possible=possible_modes)

        viewpoint_map = {
            'v': 'v',
            "Data": "d",
            "Survival": 's',
            "SegmentProfile": 'q',
            "StateProfile": 'p'
        }
        ViewPoint = error.ParseKargs(kargs,
                                     "ViewPoint",
                                     "v",
                                     possible=viewpoint_map)

        #todo: check the compatibilities between options
        """
        if ((output_option) && ((view_point != 'q') ||
       ((args[0].tag() != AMObjType::SEQUENCES) && (args[0].tag()
       != AMObjType::MARKOVIAN_SEQUENCES) &&
        (args[0].tag() != AMObjType::VARIABLE_ORDER_MARKOV_DATA) &&
        (args[0].tag() != AMObjType::SEMI_MARKOV_DATA) &&
        (args[0].tag() != AMObjType::NONHOMOGENEOUS_MARKOV_DATA))) &&
      ((view_point != 'p') || ((args[0].tag() != AMObjType::HIDDEN_SEMI_MARKOV)
      &&
        (args[0].tag() != AMObjType::MARKOVIAN_SEQUENCES) &&
        (args[0].tag() != AMObjType::VARIABLE_ORDER_MARKOV_DATA) &&
        (args[0].tag() != AMObjType::SEMI_MARKOV_DATA) &&
        (args[0].tag() != AMObjType::NONHOMOGENEOUS_MARKOV_DATA)))) {
    status = false;
    genAMLError(ERRORMSG(INCOMPATIBLE_OPTIONS_s) , "Plot");
  }


  if ((config) && (view_point != 'p') && ((args[0].tag() ==
      AMObjType::MARKOVIAN_SEQUENCES) ||
       (args[0].tag() == AMObjType::HIDDEN_VARIABLE_ORDER_MARKOV)
           || (args[0].tag() == AMObjType::HIDDEN_SEMI_MARKOV) ||
       (args[0].tag() == AMObjType::VARIABLE_ORDER_MARKOV_DATA)
        || (args[0].tag() == AMObjType::SEMI_MARKOV_DATA))) {
    variable = args[1].val.i;

    switch (args[0].tag()) {

    case AMObjType::MARKOVIAN_SEQUENCES : {
      seq = (MarkovianSequences*)((STAT_model*)args[0].val.p)->pt;
      if ((variable <= seq->get_nb_variable()) && (seq->get_characteristics(variable - 1))) {
        status = false;
        genAMLError(ERRORMSG(K_NB_ARG_ERR_s) , "Plot");
      }
      break;
    }

    case AMObjType::HIDDEN_VARIABLE_ORDER_MARKOV : {
      hmarkov = (HiddenVariableOrderMarkov*)((STAT_model*)args[0].val.p)->pt;
      if ((variable <= hmarkov->get_nb_output_process()) &&
          (hmarkov->get_nonparametric_process(variable))) {
        status = false;
        genAMLError(ERRORMSG(K_NB_ARG_ERR_s) , "Plot");
      }
      break;
    }

    case AMObjType::HIDDEN_SEMI_MARKOV : {
      hsmarkov = (HiddenSemiMarkov*)((STAT_model*)args[0].val.p)->pt;
      if ((variable <= hsmarkov->get_nb_output_process()) &&
          (hsmarkov->get_nonparametric_process(variable))) {
        status = false;
        genAMLError(ERRORMSG(K_NB_ARG_ERR_s) , "Plot");
      }
      break;
    }

    case AMObjType::VARIABLE_ORDER_MARKOV_DATA : {
      seq = (VariableOrderMarkovData*)((STAT_model*)args[0].val.p)->pt;
      if ((variable < seq->get_nb_variable()) && (seq->get_characteristics(variable))) {
        status = false;
        genAMLError(ERRORMSG(K_NB_ARG_ERR_s) , "Plot");
      }
      break;
    }

    case AMObjType::SEMI_MARKOV_DATA : {
      seq = (SemiMarkovData*)((STAT_model*)args[0].val.p)->pt;
      if ((variable < seq->get_nb_variable()) && (seq->get_characteristics(variable))) {
        status = false;
        genAMLError(ERRORMSG(K_NB_ARG_ERR_s) , "Plot");
      }
      break;
    }
    }
  }

        """
        try:
            from openalea.sequence_analysis.enums import output_display
        except:
            from openalea.stat_tool.enums import output_display

        if kargs.get('Output'):
            try:
                Output = None
                Output = error.ParseKargs(kargs, "Output", 'Segment',
                                          output_display)
            except:
                print 'warning could not import output_display from sequence_analysis'
        else:
            try:
                from openalea.sequence_analysis.enums import output_display
            except:
                from openalea.stat_tool.enums import output_display
            Output = None

        if Output is None:
            if ViewPoint == 'q':
                Output = output_display['Segment']
            elif ViewPoint == 'p':
                Output = output_display['State']
        elif (ViewPoint == 'q' and Output not in [output_display['ChangePoint'], output_display['Segment']]) \
            or (ViewPoint == 'p'  and Output not in [output_display['State'], output_display['InState'], output_display['OutState']]):
            raise ValueError(
                " INCOMPATIBLE_OPTIONS between ViewPoint and Output")

        #calling the plot functions from here
        try:
            if ViewPoint == 's':
                from openalea.stat_tool.enums import histogram_types
                from openalea.stat_tool.enums import model_distribution_types
                #todo is *params needed or not?
                if type(self) in model_distribution_types:
                    #equivalent to dist->suvival_plot_write(error, Plot_prefix, title)
                    plotable = self.survival_get_plotable(*params)
                elif type(self) in histogram_types:
                    #equivalent to histo->survival_plot_write(error , Plot_prefix , title)
                    output = self.survival_get_plotable(*params)
                else:
                    raise ValueError("""(%s) has no survival point. Use another
                        Viewpoint or use a first argument in DISTRIBUTION or MIXTURE or
                        CONVOLUTION or COMPOUND or FREQUENCY_DISTRIBUTION or
                        MIXTURE_DATA or CONVOLUTION_DATA or COMPOUND_DATA""" %
                                     str(type(self)))

            elif ViewPoint == 'p':
                #print 'viewpoint = state-profile'
                Plot_prefix = ''
                plotable = None
                from openalea.sequence_analysis._sequence_analysis import \
                    _HiddenVariableOrderMarkov, _HiddenSemiMarkov
                if type(self) == _HiddenVariableOrderMarkov:
                    plotable = self.state_profile_plotable_write(args[0])
                elif type(self) == _HiddenSemiMarkov:
                    if len(args) == 0:
                        raise SyntaxError(
                            "expect an identifier (Plot(hsmc25, 1, ViewPoint='StateProfile')"
                        )
                    elif len(args) == 1:
                        identifier = args[0]
                    else:
                        #print 'iiiiiiiiiiiiiii'
                        raise SyntaxError(
                            "expect only one identifier Plot(hsmc25, 1, ViewPoint='StateProfile'"
                        )
                    plotable = self.state_profile_plotable_write(
                        identifier, Output)
                else:
                    #todo 3 args required
                    from openalea.sequence_analysis._sequence_analysis import _MarkovianSequences, _VariableOrderMarkovData, _SemiMarkovData, _NonhomogeneousMarkovData
                    assert type(self) in [
                        _MarkovianSequences, _VariableOrderMarkovData,
                        _SemiMarkovData, _NonhomogeneousMarkovData
                    ]
                    if type(args[1]) == _HiddenVariableOrderMarkov:
                        plotable = args[1].state_profile_plotable_write2(
                            self, args[0])
                    elif type(args[1]) == _HiddenSemiMarkov:
                        plotable = args[1].state_profile_plotable_write2(
                            self, args[0], Output)
                    else:
                        raise TypeError(
                            "expect HiddenVariableOrderMarkov or HiddenSemiMarkov"
                        )

                if plotable == None:
                    try:
                        plotable = self.stateprofile_get_plotable(*params)
                    except:
                        pass

            elif ViewPoint == 'q':
                from openalea.sequence_analysis._sequence_analysis import _Sequences, _MarkovianSequences, _VariableOrderMarkovData, _SemiMarkovData
                if type(self) not in [
                        _Sequences, _MarkovianSequences,
                        _VariableOrderMarkovData, _SemiMarkovData
                ]:
                    raise TypeError(
                        'object must be in SEQUENCES or MARKOVIAN_SEQUENCES or VARIABLE_ORDER_MARKOV_DATA or SEMI-MARKOV_DATA'
                    )

                try:
                    self.nb_variable
                except:
                    raise ValueError(
                        "object has no nb_variable. check that it is a sequence"
                    )
                nb_variable = self.nb_variable
                assert len(args) >= 2
                error.CheckType([args[0], args[1]], [[int], [int]])
                #construct model_type
                from openalea.sequence_analysis.enums import model_type
                types = []
                for i in range(0, nb_variable):
                    error.CheckType([args[i + 2]], [str])
                    if i == 0:
                        types.append(model_type[args[i + 2]])
                        #Multinomial or Poisson or Ordinal or Gaussian or
                        # Mean or Variance or MeanVariance
                        if args[i + 2] in ["Mean", "MeanVariance"]:
                            for j in range(1, nb_variable):
                                types.append(types[i])
                            break
                    else:
                        # Multinomial or Poisson or Ordinal or Gaussian
                        # or Variance
                        types.append(model_type[args[i + 2]])
                #seq->segment_profile_plot_write(
                #         error , Plot_prefix , args[1].val.i ,
                #           args[2].val.i , model_type , output , title);

                plotable = self.segment_profile_plotable_write(
                    args[0], args[1], types, Output)

            #data viewPoint
            elif ViewPoint == 'd':
                from openalea.sequence_analysis._sequence_analysis import _SemiMarkovData, _MarkovianSequences, _Sequences, _NonHomogeneousMarkovData, _Tops
                if type(self) in [
                        _SemiMarkovData, _MarkovianSequences, _Sequences,
                        _NonHomogeneousMarkovData, _Tops
                ]:
                    #status = seq->plot_data_write(error , Plot_prefix , title);
                    plotable = self.get_plotable_data(*params)
            elif ViewPoint == 'v':
                # plot_write(error , Plot_prefix , title);

                if args:
                    #sequence case:
                    #todo: make it looser: observation, intensity INTENSITY?
                    choices = [
                        "SelfTransition", "Observation", "Intensity",
                        "FirstOccurrence", "Recurrence", "Sojourn", "Counting"
                    ]
                    if args[0] in choices:
                        multiplotset = self.get_plotable()
                        viewpoints = [x for x in multiplotset.viewpoint]
                        plotable = []
                        try:
                            from openalea.sequence_analysis import enums
                        except:
                            raise ImportError(
                                "sequence analysis not installed !!")

                        if len(args) == 1:
                            variable = 0
                        elif len(args) == 2:
                            variable = args[1]
                        for index, xx in enumerate(viewpoints):
                            if xx == enums.markovian_sequence_type[args[0]]:
                                if multiplotset.variable[index] == variable:
                                    plotable.append(multiplotset[index])
                    elif len(args) == 1 and type(args[0]) == str:
                        raise SyntaxError(
                            "first argument must be in %s and second arg (int) may be provided."
                            % choices)
                    elif len(args) == 1 and type(args[0]) == int:
                        from openalea.stat_tool._stat_tool import _Vectors
                        if type(self) == _Vectors:
                            #Plot(vector, 1)
                            multiplotset = self.get_plotable()
                            viewpoints = [x for x in multiplotset.viewpoint]
                            plotable = []
                            try:
                                from openalea.sequence_analysis import enums
                            except:
                                raise ImportError(
                                    "sequence analysis not installed !!")
                            plotable = [multiplotset[args[0]]]
                        else:
                            #Plot(hist1, hist2, hist3)
                            plotable = self.get_plotable_list()
                    elif len(args) == 1:
                        #e.g., list of histograms
                        plotable = self.get_plotable_list(list(args), *params)
                    else:
                        plotable = self.get_plotable_list(list(args), *params)
                else:
                    plotable = self.get_plotable(*params)
            plotter = plot.get_plotter()
        except:
            import warnings
            warnings.warn("Cannot use new plotter. Use old style plot.")
            plotable = None

        if plot.DISABLE_PLOT:
            return

        if (plotable is not None):
            plotter.plot(plotable, Title, groups, *args, **kargs)
        else:
            self.old_plot(*args, **kargs)