<commit_before>## Copyright (C) 2012-2013 <NAME>
## Computational Genomics Group (http://bioinformatics.bris.ac.uk/)
## Department of Computer Science
## University of Bristol

################################################################################

import numpy  as np

################################################################################

## Estimate normalization factors as in DESeq
def estimateSizeFactors(counts, locfcn = np.median):
    ## compute geometric mean over genes
    lsum   = np.log(counts).sum(0)
    gmeans = exp(lsum / counts.shape[0])          
    
    ## divide samples by geometric means
    counts /= gmeans        

    ## get median (or other central tendency metric) of samples excluding genes with 0 gmean 
    sizes = locfcn(counts[:,gmeans > 0], 1)      

    ## return
    return sizes
            
################################################################################

class CountData(object):
    def __init__(self, counts, exposures = None, groups = None):
        self.counts    = counts.values.T
        self.libSizes  = self.counts.sum(1)
        self.exposures = estimateSizeFactors(self.counts) if exposures is None else exposures
        self.groups    = np.arange(self.counts.shape[0])  if groups    is None else groups  
    
        self.ngenes    = self.counts.shape[1]<commit_msg>Data normalization follows the DESeq procedure<commit_after>## Copyright (C) 2012-2013 <NAME>
## Computational Genomics Group (http://bioinformatics.bris.ac.uk/)
## Department of Computer Science
## University of Bristol

################################################################################

import numpy  as np

################################################################################

## Estimate normalization factors as in DESeq
def estimateSizeFactors(counts, locfcn = np.median):
    ## compute geometric mean over genes
    lsum   = np.log(counts).sum(0)
    gmeans = np.exp(lsum / counts.shape[0])          
    
    ## divide samples by geometric means
    counts = counts / gmeans     ## counts /= gmeans gives wrong results!!!????        

    ## get median (or other central tendency metric) of samples excluding genes with 0 gmean 
    sizes = locfcn(counts[:,gmeans > 0.], 1)      
    
    ## return
    return sizes
            
################################################################################

class CountData(object):
    def __init__(self, counts, exposures = None, groups = None):
        self.counts    = counts.values.T
        self.libSizes  = self.counts.sum(1)
        self.exposures = estimateSizeFactors(self.counts) if exposures is None else exposures
        self.groups    = np.arange(self.counts.shape[0])  if groups    is None else groups  
    
        self.ngenes    = self.counts.shape[1]
{'fraction_non_alphanumeric': 0.2523066004258339, 'fraction_numerical': 0.0113555713271824, 'mean_word_length': 3.4323899371069184, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '6397835', 'n_tokens_mistral': 764, 'n_tokens_neox': 705, 'n_words': 208}