📄 ghmmwrapper.i
字号:
a noise level of 0.0 doesn't change the model @param seed : seed for ramdom number generator*/extern int model_add_noise(model* mo, double level, int seed);/** Apply the background distributions to the emission probabilities of states of the model which have one specified (background_id[state_id] != kNoBackgroundDistribution). @return : -1 on error, 0 else @param mo : a pointer to the model @param background_weight: a parameter controlling the weight given to the background. Note, should be between 0 and 1.*/extern int model_apply_background(model *mo, double* background_weight);%inline%{ /* allocation of an empty model struct*/ model *new_model() { return (struct model *)(struct model *) calloc(1, sizeof(struct model)); } /* allocation of an array of state structs*/ state *arraystate(int size) { return (state *) malloc(size*sizeof(state)); } /* extract pointer to a state */ state *get_stateptr(state *ary, int index) { return ary + index; } void call_model_print(char *filename, model *mo) { FILE *fp=fopen(filename, "a"); if (fp == NULL) { fprintf(stderr, "call_smodel_print(0): cannot open file %s\n", filename); } else { model_print(fp, mo); fclose(fp); } } void call_model_free(model *mo ) {model_free(&mo);} model *get_model_ptr(model **mo, int index) { return mo[index]; } model **cast_model_ptr(model *mo){ model** result = &mo; return result; } %}/*============================================================================================= =============================== labeled models (model.c) ============================================== */extern sequence_t *model_label_generate_sequences(model* mo, int seed, int global_len, long seq_number, int Tmax);/*============================================================================================= =============================== sdmodel.c ============================================== *//** @name state The basic structure, keeps all parameters that belong to a sdstate. */struct sdstate { /** Initial probability */ double pi; /** Output probability */ double *b; /** ID of the following state */ int *out_id; /** ID of the previous state */ int *in_id; /** transition probs to successor states. It is a matrix in case of mult. transition matrices (COS > 1)*/ double **out_a; /** transition probs from predecessor states. It is a matrix in case of mult. transition matrices (COS > 1) */ double **in_a; /** Number of successor states */ int out_states; /** Number of precursor states */ int in_states; /** if fix == 1 --> b stays fix during the training */ int fix;};typedef struct sdstate sdstate;/** @name model The complete HMM. Contains all parameters, that define a HMM.*/struct sdmodel { /** Number of states */ int N; /** Number of outputs */ int M; /** smodel includes continuous model with one transition matrix (cos is set to 1) and an extension for models with several matrices (cos is set to a positive integer value > 1).*/ int cos; /** Vector of the states */ sdstate *s; /** Prior for the a priori probability for the model. A value of -1 indicates that no prior is defined. */ double prior; /** pointer to class function */ int (*get_class)(const int*,int); /** Contains bit flags for various model extensions such as kSilentStates, kTiedEmissions (see ghmm.h for a complete list) */ int model_type; /** Flag variables for each state indicating whether it is emitting or not. Note: silent != NULL iff (model_type & kSilentStates) == 1 */ int* silent; /*AS*/};typedef struct sdmodel sdmodel;/** Frees the memory of a model. @return 0 for succes; -1 for error @param mo: pointer to a model */int sdmodel_free(sdmodel **mo);/** Produces sequences to a given model. All memory that is needed for the sequences is allocated inside the function. It is possible to define the length of the sequences global (global_len > 0) or it can be set inside the function, when a final state in the model is reach (a state with no output). If the model has no final state, the sequences will have length MAX_SEQ_LEN. @return pointer to an array of sequences @param mo: model @param seed: initial parameter for the random value generator (an integer). If seed == 0, then the random value generator is not initialized. @param global_len: length of sequences (=0: automatically via final states) @param seq_number: number of sequences*/sequence_t *sdmodel_generate_sequences(sdmodel* mo, int seed, int global_len, long seq_number, int Tmax);/** Calculates the sum log( P( O | lambda ) ). Sequences, that can not be generated from the given model, are neglected. @return log(P) @param mo model @param sq sequences */extern double sdmodel_likelihood(sdmodel *mo, sequence_t *sq);/** Computes log likelihood for all sequence of seq_w * log( P ( O|lambda ) ). If a sequence can't be generated by smo error cost of seq_w * PRENALTY_LOGP are imposed. @return n: number of evaluated sequences, -1: error @param smo smodel @param sqd sequence struct @param log\_p array of evaluated likelihoods*/extern int smodel_individual_likelihoods(smodel *smo, sequence_d_t *sqd, double *log_ps);/******* Viterbi for switching discrete model (sdviterbi.c) *******/int *sdviterbi(sdmodel *mo, int *o, int len, double *log_p);/******* Forward-Algorithm. (sdfoba.c) ******* Calculates alpha[t][i], scaling factors scale[t] and log( P(O|lambda) ) for a given double sequence and a given model. @param smo model @param O sequence @param length: length of sequence @param alpha: alpha[t][i] @param scale: a reference for double type, scale factors @param log\_p: a reference for double type, log likelihood log( P(O|lambda) ) @return 0 for success, -1 for error */extern int sdfoba_forward(sdmodel *mo, const int *O, int len, double **alpha, double *scale, double *log_p); // default class change function (dummy)extern int cp_class_change(int *seq, int len);extern void setSwitchingFunction( sdmodel *smd ); %inline%{ void call_sdmodel_free(sdmodel * sdm) { sdmodel_free(&sdm); } /* allocation of an array of sdstate structs*/ sdstate *arraysdstate(int size) { return (sdstate *) malloc(size*sizeof(sdstate)); } /* extract pointer to a sdstate */ sdstate *get_sdstateptr(sdstate *ary, int index) { return ary + index; } %}/*============================================================================================= =============================== smodel.c ============================================== */typedef enum { normal, normal_pos, normal_approx, density_number} density_t;/** @name sstate Structure for one state.*/struct sstate { /** initial prob. */ double pi; /** IDs of successor states */ int *out_id; /** IDs of predecessor states */ int *in_id; /** transition probs to successor states. It is a matrix in case of mult. transition matrices (COS > 1)*/ double **out_a; /** transition probs from predecessor states. It is a matrix in case of mult. transition matrices (COS > 1) */ double **in_a; /** number of successor states */ int out_states; /** number of predecessor states */ int in_states; /** weight vector for output function components */ double *c; /** mean vector for output functions (normal density and truncated normal density */ double *mue; /** variance vector for output functions */ double *u; /** flag for fixation of parameter. If fix = 1 do not change parameters of output functions, if fix = 0 do normal training. Default is 0. */ int fix; /** array of flags for fixing mixture components in the reestimation fix[i] = 1 means mu and sigma of component i are fixed. **/ int *mixture_fix;};typedef struct sstate sstate;/** @name smodel continous HMM */struct smodel{ /** Number of states */ int N; /** Number of output densities per state */ int M; /** smodel includes continuous model with one transition matrix (cos is set to 1) and an extension for models with several matrices (cos is set to a positive integer value > 1).*/ int cos; /** Flag for density function. 0: normal density, 1: truncated normal density, 2: approximated normal density */ density_t density; /** prior for a priori prob. of the model. -1 means no prior specified (all models have equal prob. a priori. */ double prior; /** All states of the model. Transition probs are part of the states. */ sstate *s; };typedef struct smodel smodel;/** Free memory smodel @return 0: success, -1: error @param smo pointer pointer of smodel */extern int smodel_free(smodel **smo);/** Reads an ascii file with specifications for one or more smodels. All parameters in matrix or vector form. This is necessary whenever an initial model is needed (e.g. training) or sequences have to be generated from trained models. For each smodel block smodel\_read\_block() is called. @return vector of read smodels @param filename input ascii file @param smo_number number of read smodels */extern smodel** smodel_read(const char *filename, int *smo_number);/** Copies one smodel. Memory alloc is here. @return pointer to smodel copy @param smo smodel to be copied */extern smodel* smodel_copy(const smodel *smo);/** Produces sequences to a given model. All memory that is needed for the sequences is allocated inside the function. It is possible to define the length of the sequences global (global_len > 0) or it can be set inside the function, when a final state in the model is reach (a state with no output). If the model has no final state, the sequences will have length MAX_SEQ_LEN.*/extern sequence_d_t *smodel_generate_sequences(smodel* smo, int seed, int global_len, long seq_number, long label, int Tmax);/** Computes probabilistic distance of two models @return the distance @param cm0 smodel used for generating random output @param cm smodel to compare with @param maxT maximum output length (for HMMs with absorbing states multiple sequences with a toal length of at least maxT will be generated) @param symmetric flag, whether to symmetrize distance (not implemented yet) @param verbose flag, whether to monitor distance in 40 steps. Prints to stdout (yuk!)*/extern double smodel_prob_distance(smodel *cm0, smodel *cm, int maxT, int symmetric, int verbose);/** Forward-Algorithm. (sfoba.c) Calculates alpha[t][i], scaling factors scale[t] and log( P(O|lambda) ) for a given double sequence and a given model. @param smo model @param O sequence @param T length of sequence @param b matrix with precalculated output probabilities. May be NULL @param alpha alpha[t][i] @param scale scale factors @param log_p log likelihood log( P(O|lambda) ) @return 0 for success, -1 for error */extern int sfoba_forward(smodel *smo, const double *O, int T, double ***b, double **alpha, double *scale, double *log_p);/** Backward-Algorithm. (sfoba.c) Calculates beta[t][i] given a double sequence and a model. Scale factors given as parameter (come from sfoba\_forward). @param smo model @param O sequence @param T length of sequence @param b matrix with precalculated output probabilities. May be NULL @param beta beta[t][i] @param scale scale factors @return 0 for success, -1 for error */extern int sfoba_backward(smodel *smo, const double *O, int T, double ***b, double **beta, const double *scale);/** Calculation of log( P(O|lambda) ). (sfoba.c)s Done by calling sfoba\_forward. Use this function if only the log likelihood and not alpha[t][i] is needed, alpha matrix is allocated with stat_matrix_d_alloc @param smo model @param O sequence @param T length of sequence @param log_p log likelihood log( P(O|lambda) ) @return 0 for success, -1 for error */extern int sfoba_logp(smodel *smo, const double *O, int T, double *log_p);/** Computes sum over all sequence of seq_w * log( P ( O|lambda ) ). If a sequence can't be generated by smo error cost of seq_w * PRENALTY_LOGP are imposed. @return n: number of evaluated sequences, -1: error @param smo smodel @param sqd sequence struct @param log\_p evaluated log likelihood*/extern smodel *smodel_alloc_fill(int N, int M, int cos, double prior, int density);extern void smodel_set_pivector(smodel *smo, int i, double piv);extern void smodel_set_fixvector(smodel *smo, int i, double fixv);extern void smodel_set_transition(smodel *smo, int i, int j, int cos, double prob);extern double smodel_get_transition(smodel *smo, int i, int j, int cos);extern void smodel_set_mean(smodel *smo, int i, double *mu);extern void smodel_set_variance(smodel *smo, int i, double *variance);// extern void call_smodel_print(char *filename, smodel *smo);extern int smodel_likelihood(smodel *smo, sequence_d_t *sqd, double *log_p);extern int smodel_sorted_individual_likelihoods(smodel *smo, sequence_d_t *sqd, double *log_ps, int *seq_rank);/** Viterbi for smodel (sviterbi.c) Viterbi algorithm: calculation of the viterbi path (best possible state sequenz for a given sequenz and a given model (smo)). Also calculates logp according to this path, the matrices in the local_store struct are allocated using stat_matrix_d_alloc. @return Viterbi-path @param smo model @param o double-sequence @param T sequence length @param log_p log(p) of the sequence using the vitberbi path */extern int *sviterbi(smodel *smo, double *o, int T, double *log_p);%inline %{ void set_trunc_density(smodel* smo){ smo->density = (density_t) 1 ; } /*allocation of an empty smodel struct */ smodel *new_smodel() { return (struct smodel *)(struct smodel *) calloc(1, sizeof(struct smodel)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -