<commit_before>#
# Class for specifying the order of regresssion by given test
#

from epistasis.stats import log_likelihood_ratio, F_test
from epistasis.models.regression import EpistasisRegression

# -----------------------------------------------------------------------
# Model Specifier Object
# -----------------------------------------------------------------------

class ModelSpecifier:

    def __init__(self, wildtype, genotypes, phenotypes, test_cutoff=0.05, log_transform=False, mutations=None, model_type="local", test_type="ftest"):
        """
        Model specifier. Chooses the order of model based on statistical test.

        """
        # Defaults to binary mapping if not specific mutations are named
        if mutations is None:
            mutant = farthest_genotype(wildtype, genotypes)
            mutations = binary_mutations_map(wildtype, mutant)

        # Select the statistical test for specifying model
        test_types = {"likelihood": log_likelihood_ratio, "ftest": F_test}

        # Testing specs
        self.test_type = test_type
        self.test_method = test_types[test_type]
        self.test_cutoff = test_cutoff

        # Best fit model specs
        self.model_type = model_type
        self.model_order = 1
        self.model_p_value = None
        self.model_stat = None
        self.model = EpistasisRegression(wildtype, genotypes, phenotypes, order=self.model_order, log_transform=log_transform, mutations=mutations, model_type=self.model_type)
        self.model.fit()
        self._specifier()

    def _specifier(self):
        """ Run the specifier method. """
        # Get model specs
        wildtype = self.model.wildtype
        genotypes = self.model.genotypes
        phenotypes = self.model.phenotypes
        log_transform = self.model.log_transform
        mutations = self.model.mutations

        # Construct the range of order
        orders = range(2, len(wildtype)+1)

        self.test_stats = []

        # Iterate through orders until we reach our significance statistic
        for order in orders:
            # alternative model
            alt_model = EpistasisRegression(wildtype, genotypes, phenotypes, order=order, log_transform=log_transform, mutations=mutations, model_type=self.model_type)
            alt_model.fit()

            # Run test and append statistic to test_stats
            model_stat = self.test_method(self.model, alt_model)
            self.test_stats.append(model_stat)

            # If test statistic is less than f-statistic cutoff, than keep alternative model
            if model_stat < test_cutoff:
                self.model = alt_model
            # Else, the null model is sufficient and we keep it
            else:
                self.model_order = order-1
                self.model_stat = model_stat<commit_msg>Small bug fixes in the specifier object<commit_after>#
# Class for specifying the order of regresssion by given test
#
# imports from seqspace dependency
from seqspace.utils import farthest_genotype, binary_mutations_map

from epistasis.stats import log_likelihood_ratio, F_test
from epistasis.models.regression import EpistasisRegression

# -----------------------------------------------------------------------
# Model Specifier Object
# -----------------------------------------------------------------------

class ModelSpecifier:

    def __init__(self, wildtype, genotypes, phenotypes, test_cutoff=0.05, log_transform=False, mutations=None, model_type="local", test_type="ftest"):
        """
        Model specifier. Chooses the order of model based on statistical test.

        """
        # Defaults to binary mapping if not specific mutations are named
        if mutations is None:
            mutant = farthest_genotype(wildtype, genotypes)
            mutations = binary_mutations_map(wildtype, mutant)

        # Select the statistical test for specifying model
        test_types = {"likelihood": log_likelihood_ratio, "ftest": F_test}

        # Testing specs
        self.test_type = test_type
        self.test_method = test_types[test_type]
        self.test_cutoff = test_cutoff

        # Best fit model specs
        self.model_type = model_type
        self.model_order = 1
        self.model_p_value = None
        self.model_stat = None
        self.model = EpistasisRegression(wildtype, genotypes, phenotypes, order=self.model_order, log_transform=log_transform, mutations=mutations, model_type=self.model_type)
        self.model.fit()
        self._specifier()

    def _specifier(self):
        """ Run the specifier method. """
        # Get model specs
        wildtype = self.model.wildtype
        genotypes = self.model.genotypes
        phenotypes = self.model.phenotypes
        log_transform = self.model.log_transform
        mutations = self.model.mutations

        # Construct the range of order
        orders = range(2, len(wildtype)+1)

        self.test_stats = []

        # Iterate through orders until we reach our significance statistic
        for order in orders:
            # alternative model
            alt_model = EpistasisRegression(wildtype, genotypes, phenotypes, order=order, log_transform=log_transform, mutations=mutations, model_type=self.model_type)
            alt_model.fit()

            # Run test and append statistic to test_stats
            model_stat, p_value = self.test_method(self.model, alt_model)
            self.test_stats.append(model_stat)

            # If test statistic is less than f-statistic cutoff, than keep alternative model
            if p_value < self.test_cutoff:
                self.model = alt_model
            # Else, the null model is sufficient and we keep it
            else:
                self.model_order = order-1
                self.model_stat = model_stat
                self.model_p_value = p_value
{'fraction_non_alphanumeric': 0.11443722203215874, 'fraction_numerical': 0.0023947998631542937, 'mean_word_length': 2.1950819672131145, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5260142', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1482, 'n_words': 423}