Python numpy.log2() Examples

The following are 30 code examples of numpy.log2(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: basenji_motifs.py    From basenji with Apache License 2.0 6 votes vote down vote up
def info_content(pwm, transpose=False, bg_gc=0.415):
  """ Compute PWM information content.

    In the original analysis, I used a bg_gc=0.5. For any
    future analysis, I ought to switch to the true hg19
    value of 0.415.
    """
  pseudoc = 1e-9

  if transpose:
    pwm = np.transpose(pwm)

  bg_pwm = [1 - bg_gc, bg_gc, bg_gc, 1 - bg_gc]

  ic = 0
  for i in range(pwm.shape[0]):
    for j in range(4):
      # ic += 0.5 + pwm[i][j]*np.log2(pseudoc+pwm[i][j])
      ic += -bg_pwm[j] * np.log2(
          bg_pwm[j]) + pwm[i][j] * np.log2(pseudoc + pwm[i][j])

  return ic 
Example #2
Source File: dataset.py    From disentangling_conditional_gans with MIT License 6 votes vote down vote up
def __init__(self, resolution=1024, num_channels=3, dtype='uint8', dynamic_range=[0,255], label_size=0, label_dtype='float32'):
        self.resolution         = resolution
        self.resolution_log2    = int(np.log2(resolution))
        self.shape              = [num_channels, resolution, resolution]
        self.dtype              = dtype
        self.dynamic_range      = dynamic_range
        self.label_size         = label_size
        self.label_dtype        = label_dtype
        self._tf_minibatch_var  = None
        self._tf_lod_var        = None
        self._tf_minibatch_np   = None
        self._tf_labels_np      = None

        assert self.resolution == 2 ** self.resolution_log2
        with tf.name_scope('Dataset'):
            self._tf_minibatch_var = tf.Variable(np.int32(0), name='minibatch_var')
            self._tf_lod_var = tf.Variable(np.int32(0), name='lod_var') 
Example #3
Source File: model.py    From models with MIT License 6 votes vote down vote up
def predict_on_batch(self, inputs):
            if inputs.shape == (2,):
                inputs = inputs[np.newaxis, :]
            # Encode
            max_len = len(max(inputs, key=len))
            one_hot_ref =  self.encode(inputs[:,0])
            one_hot_alt = self.encode(inputs[:,1])
            # Construct dummy library indicator
            indicator = np.zeros((inputs.shape[0],2))
            indicator[:,1] = 1
            # Compute fold change for all three frames
            fc_changes = []
            for shift in range(3):
                if shift > 0:
                    shifter = np.zeros((one_hot_ref.shape[0],1,4))
                    one_hot_ref = np.concatenate([one_hot_ref, shifter], axis=1)
                    one_hot_alt = np.concatenate([one_hot_alt, shifter], axis=1)
                pred_ref = self.model.predict_on_batch([one_hot_ref, indicator]).reshape(-1)
                pred_variant = self.model.predict_on_batch([one_hot_alt, indicator]).reshape(-1)
                fc_changes.append(np.log2(pred_variant/pred_ref))
            # Return
            return {"mrl_fold_change":fc_changes[0], 
                    "shift_1":fc_changes[1],
                    "shift_2":fc_changes[2]} 
Example #4
Source File: utils.py    From contextualbandits with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _check_beta_prior(beta_prior, nchoices, for_ucb=False):
    if beta_prior == 'auto':
        if not for_ucb:
            out = ( (2.0 / np.log2(nchoices), 4.0), 2 )
        else:
            out = ( (3.0 / np.log2(nchoices), 4.0), 2 )
    elif beta_prior is None:
        out = ((1.0,1.0), 0)
    else:
        assert len(beta_prior) == 2
        assert len(beta_prior[0]) == 2
        assert isinstance(beta_prior[1], int)
        assert isinstance(beta_prior[0][0], int) or isinstance(beta_prior[0][0], float)
        assert isinstance(beta_prior[0][1], int) or isinstance(beta_prior[0][1], float)
        assert (beta_prior[0][0] > 0.) and (beta_prior[0][1] > 0.)
        out = beta_prior
    return out 
Example #5
Source File: dataset_tool.py    From disentangling_conditional_gans with MIT License 6 votes vote down vote up
def add_image(self, img):
        if self.print_progress and self.cur_images % self.progress_interval == 0:
            print('%d / %d\r' % (self.cur_images, self.expected_images), end='', flush=True)
            sys.stdout.flush()
        if self.shape is None:
            self.shape = img.shape
            self.resolution_log2 = int(np.log2(self.shape[1]))
            assert self.shape[0] in [1, 3]
            assert self.shape[1] == self.shape[2]
            assert self.shape[1] == 2**self.resolution_log2
            tfr_opt = tf.python_io.TFRecordOptions(tf.python_io.TFRecordCompressionType.NONE)
            for lod in range(self.resolution_log2 - 1):
                tfr_file = self.tfr_prefix + '-r%02d.tfrecords' % (self.resolution_log2 - lod)
                self.tfr_writers.append(tf.python_io.TFRecordWriter(tfr_file, tfr_opt))
        assert img.shape == self.shape
        for lod, tfr_writer in enumerate(self.tfr_writers):
            if lod:
                img = img.astype(np.float32)
                img = (img[:, 0::2, 0::2] + img[:, 0::2, 1::2] + img[:, 1::2, 0::2] + img[:, 1::2, 1::2]) * 0.25
            quant = np.rint(img).clip(0, 255).astype(np.uint8)
            ex = tf.train.Example(features=tf.train.Features(feature={
                'shape': tf.train.Feature(int64_list=tf.train.Int64List(value=quant.shape)),
                'data': tf.train.Feature(bytes_list=tf.train.BytesList(value=[quant.tostring()]))}))
            tfr_writer.write(ex.SerializeToString())
        self.cur_images += 1 
Example #6
Source File: measures.py    From discomll with Apache License 2.0 6 votes vote down vote up
def multinomLog2(selectors):
    """
    Function calculates logarithm 2 of a kind of multinom.

    selectors: list of integers
    """

    ln2 = 0.69314718055994528622
    noAll = sum(selectors)
    lgNf = math.lgamma(noAll + 1.0) / ln2  # log2(N!)

    lgnFac = []
    for selector in selectors:
        if selector == 0 or selector == 1:
            lgnFac.append(0.0)
        elif selector == 2:
            lgnFac.append(1.0)
        elif selector == noAll:
            lgnFac.append(lgNf)
        else:
            lgnFac.append(math.lgamma(selector + 1.0) / ln2)
    return lgNf - sum(lgnFac) 
Example #7
Source File: basisconstructors.py    From pyGSTi with Apache License 2.0 6 votes vote down vote up
def pp_labels(matrix_dim):
    def _is_integer(x):
        return bool(abs(x - round(x)) < 1e-6)
    if matrix_dim == 0: return []
    if matrix_dim == 1: return ['']  # special case - use empty label instead of "I"

    nQubits = _np.log2(matrix_dim)
    if not _is_integer(nQubits):
        raise ValueError("Dimension for Pauli tensor product matrices must be an integer *power of 2*")
    nQubits = int(round(nQubits))

    lblList = []
    basisLblList = [['I', 'X', 'Y', 'Z']] * nQubits
    for sigmaLbls in _itertools.product(*basisLblList):
        lblList.append(''.join(sigmaLbls))
    return lblList 
Example #8
Source File: xrft.py    From xrft with MIT License 6 votes vote down vote up
def fit_loglog(x, y):
    """
    Fit a line to isotropic spectra in log-log space

    Parameters
    ----------
    x : `numpy.array`
        Coordinate of the data
    y : `numpy.array`
        data

    Returns
    -------
    y_fit : `numpy.array`
        The linear fit
    a : float64
        Slope of the fit
    b : float64
        Intercept of the fit
    """
    # fig log vs log
    p = np.polyfit(np.log2(x), np.log2(y), 1)
    y_fit = 2**(np.log2(x)*p[0] + p[1])

    return y_fit, p[0], p[1] 
Example #9
Source File: smq1Q_Xpi2_rpe.py    From pyGSTi with Apache License 2.0 6 votes vote down vote up
def get_rpe_experiment_design(max_max_length, qubit_labels=None, req_counts=None):
    max_log_lengths = _np.log2(max_max_length)
    if not (int(max_log_lengths) - max_log_lengths == 0):
        raise ValueError('Only integer powers of two accepted for max_max_length.')

    assert(qubit_labels is None or qubit_labels == (0,)), "Only qubit_labels=(0,) is supported so far"
    return _rpe.RobustPhaseEstimationDesign(
        _obj.Circuit([('Gxpi2', 0)], line_labels=(0,)),
        [2**i for i in range(int(max_log_lengths) + 1)],
        _obj.Circuit([], line_labels=(0,)),
        _obj.Circuit([('Gxpi2', 0)], line_labels=(0,)),
        ['1'],
        ['0'],
        _obj.Circuit([], line_labels=(0,)),
        _obj.Circuit([], line_labels=(0,)),
        ['0'],
        ['1'],
        qubit_labels=qubit_labels,
        req_counts=req_counts) 
Example #10
Source File: smq1Q_Ypi2_rpe.py    From pyGSTi with Apache License 2.0 6 votes vote down vote up
def get_rpe_experiment_design(max_max_length, qubit_labels=None, req_counts=None):
    max_log_lengths = _np.log2(max_max_length)
    if not (int(max_log_lengths) - max_log_lengths == 0):
        raise ValueError('Only integer powers of two accepted for max_max_length.')

    assert(qubit_labels is None or qubit_labels == (0,)), "Only qubit_labels=(0,) is supported so far"
    return _rpe.RobustPhaseEstimationDesign(
        _obj.Circuit([('Gypi2', 0)], line_labels=(0,)),
        [2**i for i in range(int(max_log_lengths) + 1)],
        _obj.Circuit([], line_labels=(0,)),
        _obj.Circuit([('Gypi2', 0)], line_labels=(0,)),
        ['1'],
        ['0'],
        _obj.Circuit([], line_labels=(0,)),
        _obj.Circuit([], line_labels=(0,)),
        ['0'],
        ['1'],
        qubit_labels=qubit_labels,
        req_counts=req_counts) 
Example #11
Source File: model.py    From rcan-tensorflow with MIT License 6 votes vote down vote up
def up_scaling(self, x, f, scale_factor, name):
        """
        :param x: image
        :param f: conv2d filter
        :param scale_factor: scale factor
        :param name: scope name
        :return:
        """
        with tf.variable_scope(name):
            if scale_factor == 3:
                x = tfu.conv2d(x, f * 9, k=1, name='conv2d-image_scaling-0')
                x = tfu.pixel_shuffle(x, 3)
            elif scale_factor & (scale_factor - 1) == 0:  # is it 2^n?
                log_scale_factor = int(np.log2(scale_factor))
                for i in range(log_scale_factor):
                    x = tfu.conv2d(x, f * 4, k=1, name='conv2d-image_scaling-%d' % i)
                    x = tfu.pixel_shuffle(x, 2)
            else:
                raise NotImplementedError("[-] Not supported scaling factor (%d)" % scale_factor)
            return x 
Example #12
Source File: core.py    From astropy-healpix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def nside_to_level(nside):
    """
    Find the HEALPix level for a given nside.

    This is given by ``level = log2(nside)``.

    This function is the inverse of `level_to_nside`.

    Parameters
    ----------
    nside : int
        The number of pixels on the side of one of the 12 'top-level' HEALPix tiles.
        Must be a power of two.

    Returns
    -------
    level : int
        The level of the HEALPix cells
    """
    nside = np.asarray(nside, dtype=np.int64)

    _validate_nside(nside)
    return np.log2(nside).astype(np.int64) 
Example #13
Source File: histograms.py    From recruit with Apache License 2.0 6 votes vote down vote up
def _hist_bin_sturges(x, range):
    """
    Sturges histogram bin estimator.

    A very simplistic estimator based on the assumption of normality of
    the data. This estimator has poor performance for non-normal data,
    which becomes especially obvious for large data sets. The estimate
    depends only on size of the data.

    Parameters
    ----------
    x : array_like
        Input data that is to be histogrammed, trimmed to range. May not
        be empty.

    Returns
    -------
    h : An estimate of the optimal bin width for the given data.
    """
    del range  # unused
    return x.ptp() / (np.log2(x.size) + 1.0) 
Example #14
Source File: test_umath.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_branch_cuts(self):
        # check branch cuts and continuity on them
        _check_branch_cut(np.log,   -0.5, 1j, 1, -1, True)
        _check_branch_cut(np.log2,  -0.5, 1j, 1, -1, True)
        _check_branch_cut(np.log10, -0.5, 1j, 1, -1, True)
        _check_branch_cut(np.log1p, -1.5, 1j, 1, -1, True)
        _check_branch_cut(np.sqrt,  -0.5, 1j, 1, -1, True)

        _check_branch_cut(np.arcsin, [ -2, 2],   [1j, 1j], 1, -1, True)
        _check_branch_cut(np.arccos, [ -2, 2],   [1j, 1j], 1, -1, True)
        _check_branch_cut(np.arctan, [0-2j, 2j],  [1,  1], -1, 1, True)

        _check_branch_cut(np.arcsinh, [0-2j,  2j], [1,   1], -1, 1, True)
        _check_branch_cut(np.arccosh, [ -1, 0.5], [1j,  1j], 1, -1, True)
        _check_branch_cut(np.arctanh, [ -2,   2], [1j, 1j], 1, -1, True)

        # check against bogus branch cuts: assert continuity between quadrants
        _check_branch_cut(np.arcsin, [0-2j, 2j], [ 1,  1], 1, 1)
        _check_branch_cut(np.arccos, [0-2j, 2j], [ 1,  1], 1, 1)
        _check_branch_cut(np.arctan, [ -2,  2], [1j, 1j], 1, 1)

        _check_branch_cut(np.arcsinh, [ -2,  2, 0], [1j, 1j, 1], 1, 1)
        _check_branch_cut(np.arccosh, [0-2j, 2j, 2], [1,  1,  1j], 1, 1)
        _check_branch_cut(np.arctanh, [0-2j, 2j, 0], [1,  1,  1j], 1, 1) 
Example #15
Source File: test_umath.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_branch_cuts_complex64(self):
        # check branch cuts and continuity on them
        _check_branch_cut(np.log,   -0.5, 1j, 1, -1, True, np.complex64)
        _check_branch_cut(np.log2,  -0.5, 1j, 1, -1, True, np.complex64)
        _check_branch_cut(np.log10, -0.5, 1j, 1, -1, True, np.complex64)
        _check_branch_cut(np.log1p, -1.5, 1j, 1, -1, True, np.complex64)
        _check_branch_cut(np.sqrt,  -0.5, 1j, 1, -1, True, np.complex64)

        _check_branch_cut(np.arcsin, [ -2, 2],   [1j, 1j], 1, -1, True, np.complex64)
        _check_branch_cut(np.arccos, [ -2, 2],   [1j, 1j], 1, -1, True, np.complex64)
        _check_branch_cut(np.arctan, [0-2j, 2j],  [1,  1], -1, 1, True, np.complex64)

        _check_branch_cut(np.arcsinh, [0-2j,  2j], [1,   1], -1, 1, True, np.complex64)
        _check_branch_cut(np.arccosh, [ -1, 0.5], [1j,  1j], 1, -1, True, np.complex64)
        _check_branch_cut(np.arctanh, [ -2,   2], [1j, 1j], 1, -1, True, np.complex64)

        # check against bogus branch cuts: assert continuity between quadrants
        _check_branch_cut(np.arcsin, [0-2j, 2j], [ 1,  1], 1, 1, False, np.complex64)
        _check_branch_cut(np.arccos, [0-2j, 2j], [ 1,  1], 1, 1, False, np.complex64)
        _check_branch_cut(np.arctan, [ -2,  2], [1j, 1j], 1, 1, False, np.complex64)

        _check_branch_cut(np.arcsinh, [ -2,  2, 0], [1j, 1j, 1], 1, 1, False, np.complex64)
        _check_branch_cut(np.arccosh, [0-2j, 2j, 2], [1,  1,  1j], 1, 1, False, np.complex64)
        _check_branch_cut(np.arctanh, [0-2j, 2j, 0], [1,  1,  1j], 1, 1, False, np.complex64) 
Example #16
Source File: utils_deblur.py    From KAIR with MIT License 6 votes vote down vote up
def p2o(psf, shape):
    '''
    # psf: NxCxhxw
    # shape: [H,W]
    # otf: NxCxHxWx2
    '''
    otf = torch.zeros(psf.shape[:-2] + shape).type_as(psf)
    otf[...,:psf.shape[2],:psf.shape[3]].copy_(psf)
    for axis, axis_size in enumerate(psf.shape[2:]):
        otf = torch.roll(otf, -int(axis_size / 2), dims=axis+2)
    otf = torch.rfft(otf, 2, onesided=False)
    n_ops = torch.sum(torch.tensor(psf.shape).type_as(psf) * torch.log2(torch.tensor(psf.shape).type_as(psf)))
    otf[...,1][torch.abs(otf[...,1])<n_ops*2.22e-16] = torch.tensor(0).type_as(psf)
    return otf



# otf2psf: not sure where I got this one from. Maybe translated from Octave source code or whatever. It's just math. 
Example #17
Source File: utils_sisr.py    From KAIR with MIT License 6 votes vote down vote up
def p2o(psf, shape):
    '''
    Args:
        psf: NxCxhxw
        shape: [H,W]

    Returns:
        otf: NxCxHxWx2
    '''
    otf = torch.zeros(psf.shape[:-2] + shape).type_as(psf)
    otf[...,:psf.shape[2],:psf.shape[3]].copy_(psf)
    for axis, axis_size in enumerate(psf.shape[2:]):
        otf = torch.roll(otf, -int(axis_size / 2), dims=axis+2)
    otf = torch.rfft(otf, 2, onesided=False)
    n_ops = torch.sum(torch.tensor(psf.shape).type_as(psf) * torch.log2(torch.tensor(psf.shape).type_as(psf)))
    otf[...,1][torch.abs(otf[...,1])<n_ops*2.22e-16] = torch.tensor(0).type_as(psf)
    return otf 
Example #18
Source File: basenji_sad.py    From basenji with Apache License 2.0 6 votes vote down vote up
def write_snp(ref_preds, alt_preds, sad_out, si, sad_stats, log_pseudo):
  """Write SNP predictions to HDF."""

  # sum across length
  ref_preds_sum = ref_preds.sum(axis=0, dtype='float64')
  alt_preds_sum = alt_preds.sum(axis=0, dtype='float64')

  # compare reference to alternative via mean subtraction
  if 'SAD' in sad_stats:
    sad = alt_preds_sum - ref_preds_sum
    sad_out['SAD'][si,:] = sad.astype('float16')

  # compare reference to alternative via mean log division
  if 'SAR' in sad_stats:
    sar = np.log2(alt_preds_sum + log_pseudo) \
                   - np.log2(ref_preds_sum + log_pseudo)
    sad_out['SAR'][szi,:] = sar.astype('float16')

  # compare geometric means
  if 'geoSAD' in sad_stats:
    sar_vec = np.log2(alt_preds.astype('float64') + log_pseudo) \
                - np.log2(ref_preds.astype('float64') + log_pseudo)
    geo_sad = sar_vec.sum(axis=0)
    sad_out['geoSAD'][szi,:] = geo_sad.astype('float16') 
Example #19
Source File: basenji_motifs_denovo.py    From basenji with Apache License 2.0 6 votes vote down vote up
def plot_kernel(kernel_weights, out_pdf):
    depth, width = kernel_weights.shape
    fig_width = 2 + 1.5*np.log2(width)

    # normalize
    kernel_weights -= kernel_weights.mean(axis=0)

    # plot
    sns.set(font_scale=1.5)
    plt.figure(figsize=(fig_width, depth))
    sns.heatmap(kernel_weights, cmap='PRGn', linewidths=0.2, center=0)
    ax = plt.gca()
    ax.set_xticklabels(range(1,width+1))

    if depth == 4:
        ax.set_yticklabels('ACGT', rotation='horizontal')
    else:
        ax.set_yticklabels(range(1,depth+1), rotation='horizontal')

    plt.savefig(out_pdf)
    plt.close() 
Example #20
Source File: memory.py    From DOTA_models with Apache License 2.0 5 votes vote down vote up
def __init__(self, key_dim, memory_size, vocab_size,
               choose_k=256, alpha=0.1, correct_in_top=1, age_noise=8.0,
               var_cache_device='', nn_device='',
               num_hashes=None, num_libraries=None):
    super(LSHMemory, self).__init__(
        key_dim, memory_size, vocab_size,
        choose_k=choose_k, alpha=alpha, correct_in_top=1, age_noise=age_noise,
        var_cache_device=var_cache_device, nn_device=nn_device)

    self.num_libraries = num_libraries or int(self.choose_k ** 0.5)
    self.num_per_hash_slot = max(1, self.choose_k // self.num_libraries)
    self.num_hashes = (num_hashes or
                       int(np.log2(self.memory_size / self.num_per_hash_slot)))
    self.num_hashes = min(max(self.num_hashes, 1), 20)
    self.num_hash_slots = 2 ** self.num_hashes

    # hashing vectors
    self.hash_vecs = [
        tf.get_variable(
            'hash_vecs%d' % i, [self.num_hashes, self.key_dim],
            dtype=tf.float32, trainable=False,
            initializer=tf.truncated_normal_initializer(0, 1))
        for i in xrange(self.num_libraries)]

    # map representing which hash slots map to which mem keys
    self.hash_slots = [
        tf.get_variable(
            'hash_slots%d' % i, [self.num_hash_slots, self.num_per_hash_slot],
            dtype=tf.int32, trainable=False,
            initializer=tf.random_uniform_initializer(maxval=self.memory_size,
                                                      dtype=tf.int32))
        for i in xrange(self.num_libraries)] 
Example #21
Source File: transforms.py    From seizure-prediction with MIT License 5 votes vote down vote up
def apply_one(self, fft_mag, meta):
        num_time_samples = (fft_mag.shape[-1] - 1) * 2 # revert FFT shape change

        X = fft_mag ** 2
        for ch in X:
            ch /= np.sum(ch + 1e-12)

        psd = X # pdf

        out = []

        #[0,1,2] -> [[0,1], [1,2]]
        for start_freq, end_freq in zip(self.freq_ranges[:-1], self.freq_ranges[1:]):
            start_index = np.floor((start_freq / meta.sampling_frequency) * num_time_samples)
            end_index = np.floor((end_freq / meta.sampling_frequency) * num_time_samples)
            selected = psd[:, start_index:end_index]

            entropies = - np.sum(selected * np.log2(selected + 1e-12), axis=selected.ndim-1) / np.log2(end_index - start_index)
            if self.flatten:
                out.append(entropies.ravel())
            else:
                out.append(entropies)

        if self.flatten:
            return np.concatenate(out)
        else:
            return to_np_array(out) 
Example #22
Source File: transforms.py    From seizure-prediction with MIT License 5 votes vote down vote up
def apply(self, data, meta=None):
        indices = np.where(data <= 0)
        data[indices] = np.max(data)
        data[indices] = (np.min(data) * 0.1)
        return np.log2(data) 
Example #23
Source File: decision_tree_submit.py    From Python-Machine-Learning-By-Example-Second-Edition with MIT License 5 votes vote down vote up
def entropy(labels):
    if not labels:
        return 0
    counts = np.unique(labels, return_counts=True)[1]
    fractions = counts / float(len(labels))
    return - np.sum(fractions * np.log2(fractions)) 
Example #24
Source File: transforms.py    From seizure-prediction with MIT License 5 votes vote down vote up
def get_name(self):
        return "log2" 
Example #25
Source File: bit_counter.py    From imgcomp-cvpr with GNU General Public License v3.0 5 votes vote down vote up
def _encode(foutid, syms, ctx_shape, get_freqs, get_pr, printer):
    """
    :param foutid:
    :param syms: CHW, padded
    :param ctx_shape:
    :param get_freqs:
    :param get_pr:
    :return:
    """
    with open(foutid, 'wb') as fout:
        bit_out = ac.CountingBitOutputStream(
            bit_out=ac.BitOutputStream(fout))
        enc = ac.ArithmeticEncoder(bit_out)
        ctx_sym_itr = _new_ctx_sym_itr(syms, ctx_shape=ctx_shape)
        # First sym is stored separately using log2(L) bits or sth
        first_ctx, first_sym = next(ctx_sym_itr)
        first_pr = get_pr(first_ctx)
        first_bc = -np.log2(first_pr[first_sym])
        theoretical_bit_cost = first_bc
        num_ctxs = _get_num_ctxs(syms.shape, ctx_shape)
        # Encode other symbols
        for i, (ctx, sym) in enumerate(ctx_sym_itr):
            freqs = get_freqs(ctx)
            pr = get_pr(ctx)
            theoretical_bit_cost += -np.log2(pr[sym])
            enc.write(freqs, sym)
            if i % 1000 == 0:
                printer('\rFeeding context for symbol #{}/{}...'.format(i, num_ctxs), end='', flush=True)
        printer('\r\033[K', end='')  # clear line
        enc.finish()
        bit_out.close()
        return bit_out.num_bits, first_sym, theoretical_bit_cost 
Example #26
Source File: unet.py    From nsf with MIT License 5 votes vote down vote up
def __init__(self,
                 in_features,
                 max_hidden_features,
                 num_layers,
                 out_features,
                 nonlinearity=F.relu):
        super().__init__()

        assert utils.is_power_of_two(max_hidden_features), \
            '\'max_hidden_features\' must be a power of two.'
        assert max_hidden_features // 2 ** num_layers > 1, \
            '\'num_layers\' must be {} or fewer'.format(int(np.log2(max_hidden_features) - 1))

        self.nonlinearity = nonlinearity
        self.num_layers = num_layers

        self.initial_layer = nn.Linear(in_features, max_hidden_features)

        self.down_layers = nn.ModuleList([
            nn.Linear(
                in_features=max_hidden_features // 2 ** i,
                out_features=max_hidden_features // 2 ** (i + 1)
            )
            for i in range(num_layers)
        ])

        self.middle_layer = nn.Linear(
            in_features=max_hidden_features // 2 ** num_layers,
            out_features=max_hidden_features // 2 ** num_layers)

        self.up_layers = nn.ModuleList([
            nn.Linear(
                in_features=max_hidden_features // 2 ** (i + 1),
                out_features=max_hidden_features // 2 ** i
            )
            for i in range(num_layers - 1, -1, -1)
        ])

        self.final_layer = nn.Linear(max_hidden_features, out_features) 
Example #27
Source File: core.py    From astropy-healpix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _validate_nside(nside):
    log_2_nside = np.round(np.log2(nside))
    if not np.all(2 ** log_2_nside == nside):
        raise ValueError('nside must be a power of two') 
Example #28
Source File: core.py    From astropy-healpix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def uniq_to_level_ipix(uniq):
    """
    Convert a HEALPix cell uniq number to its (level, ipix) equivalent.

    A uniq number is a 64 bits integer equaling to : ipix + 4*(4**level). Please read
    this `paper <http://ivoa.net/documents/MOC/20140602/REC-MOC-1.0-20140602.pdf>`_
    for more details about uniq numbers.

    Parameters
    ----------
    uniq : int
        The uniq number of a HEALPix cell.

    Returns
    -------
    level, ipix: int, int
        The level and index of the HEALPix cell computed from ``uniq``.
    """
    uniq = np.asarray(uniq, dtype=np.int64)

    level = (np.log2(uniq//4)) // 2
    level = level.astype(np.int64)
    _validate_level(level)

    ipix = uniq - (1 << 2*(level + 1))
    _validate_npix(level, ipix)

    return level, ipix 
Example #29
Source File: measures.py    From discomll with Apache License 2.0 5 votes vote down vote up
def h(values):
    """
    Function calculates entropy.

    values: list of integers
    """
    ent = np.true_divide(values, np.sum(values))
    return -np.sum(np.multiply(ent, np.log2(ent))) 
Example #30
Source File: decision_tree_submit.py    From Python-Machine-Learning-By-Example-Second-Edition with MIT License 5 votes vote down vote up
def entropy_np(labels):
    # When the set is empty, it is also pure
    if labels.size == 0:
        return 0
    counts = np.unique(labels, return_counts=True)[1]
    fractions = counts / float(len(labels))
    return - np.sum(fractions * np.log2(fractions))