Return a DIMACS SAT formula.
16176
def _sat_formula(lexer, varname, fmt, nvars):
    """"""
    types = {IntegerToken, LPAREN} | _SAT_TOKS[fmt]
    tok = _expect_token(lexer, types)
    # INT
    if isinstance(tok, IntegerToken):
        index = tok.value
        if not 0 < index <= nvars:
            fstr = "formula literal {} outside valid range: (0, {}]"
            raise Error(fstr.format(index, nvars))
        return ('var', (varname, ), (index, ))
    # '-'
    elif isinstance(tok, OP_not):
        tok = _expect_token(lexer, {IntegerToken, LPAREN})
        # '-' INT
        if isinstance(tok, IntegerToken):
            index = tok.value
            if not 0 < index <= nvars:
                fstr = "formula literal {} outside valid range: (0, {}]"
                raise Error(fstr.format(index, nvars))
            return ('not', ('var', (varname, ), (index, )))
        # '-' '(' FORMULA ')'
        else:
            formula = _sat_formula(lexer, varname, fmt, nvars)
            _expect_token(lexer, {RPAREN})
            return ('not', formula)
    # '(' FORMULA ')'
    elif isinstance(tok, LPAREN):
        formula = _sat_formula(lexer, varname, fmt, nvars)
        _expect_token(lexer, {RPAREN})
        return formula
    # OP '(' FORMULAS ')'
    else:
        _expect_token(lexer, {LPAREN})
        formulas = _formulas(lexer, varname, fmt, nvars)
        _expect_token(lexer, {RPAREN})
        return (tok.ASTOP, ) + formulas
504661
def _high_dim_sim(self, v, w, normalize=False, X=None, idx=0):
        """"""

        sim = np.exp((-np.linalg.norm(v - w) ** 2) / (2*self._sigma[idx] ** 2))

        if normalize:
            return sim / sum(map(lambda x: x[1], self._knn(idx, X, high_dim=True)))
        else:
            return sim
5698
def create_calcs(self):
        """"""
        specs = self._combine_core_aux_specs()
        for spec in specs:
            spec['dtype_out_time'] = _prune_invalid_time_reductions(spec)
        return [Calc(**sp) for sp in specs]
656768
def dim_dc(self, pars):
        
        """
        self._set_parameters(pars)
        # term 1
        num1a = self.m * np.sin(self.ang) * np.log(self.w * self.tau)\
            * self.otc
        num1b = self.m * self.otc * np.pi / 2 * np.cos(np.pi / 2)
        term1 = self.sigma0 * (-num1a - num1b) / self.denom

        # term 2
        num2a = -self.m * self.otc * np.cos(self.ang)
        num2b = -2 * np.log(self.w * self.tau) * self.otc * np.cos(self.ang)
        num2c = 2 * self.otc * np.pi / 2 * np.cos(self.ang)
        num2d = 2 * np.log(self.w * self.tau) * self.otc2
        numerator = num2a * (num2b + num2c) + num2d
        term2 = self.sigma0 * numerator / (self.denom ** 2)

        result = term1 + term2

        return result
886695
def _check_and_expand_subformula(expression, parent_data, variables, data):
    """"""
    grouping_depth = 0
    for i, char in enumerate(expression):
        if char == '(':
            if grouping_depth == 0:
                formula_delimiter = i + 1
            grouping_depth += 1
        elif char == ')':
            grouping_depth -= 1
            if grouping_depth == 0:
                expr = expression[formula_delimiter:i].strip()
                return parse_formula(expr, parent_data)
    return variables, data
579069
def _check_satisfy_constraints(self, label, xmin, ymin, xmax, ymax, width, height):
        """"""
        if (xmax - xmin) * (ymax - ymin) < 2:
            return False  # only 1 pixel
        x1 = float(xmin) / width
        y1 = float(ymin) / height
        x2 = float(xmax) / width
        y2 = float(ymax) / height
        object_areas = self._calculate_areas(label[:, 1:])
        valid_objects = np.where(object_areas * width * height > 2)[0]
        if valid_objects.size < 1:
            return False
        intersects = self._intersect(label[valid_objects, 1:], x1, y1, x2, y2)
        coverages = self._calculate_areas(intersects) / object_areas[valid_objects]
        coverages = coverages[np.where(coverages > 0)[0]]
        return coverages.size > 0 and np.amin(coverages) > self.min_object_covered
622209
def metric_from_cell(cell):
    """"""
    cell = np.asarray(cell, dtype=float)
    return np.dot(cell, cell.T)
732077
def get_vector(self, max_choice=3):
        """"""
        vec = {}
        for dim in ['forbidden', 'required', 'permitted']:
            if self.meta[dim] is None:
                continue
            dim_vec = map(lambda x: (x, max_choice), self.meta[dim])
            vec[dim] = dict(dim_vec)
        return vec
652706
def truthtable2expr(tt, conj=False):
    """"""
    if conj:
        outer, inner = (And, Or)
        nums = tt.pcdata.iter_zeros()
    else:
        outer, inner = (Or, And)
        nums = tt.pcdata.iter_ones()
    inputs = [exprvar(v.names, v.indices) for v in tt.inputs]
    terms = [boolfunc.num2term(num, inputs, conj) for num in nums]
    return outer(*[inner(*term) for term in terms])
953835
def getdim(lsp):
    '''
    
    '''
    dims= ['x','y', 'z'];
    rxs = ['{}-cells *([0-9]+)'.format(x) for x in ['x','y','z']];
    return [
        x for x,rx in zip(dims,rxs)
        if re.search(rx,lsp) and int(re.search(rx,lsp).group(1)) > 0 ];
183666
def alignVec_quat(vec):
    """"""
    alpha = np.arctan2(vec[1], vec[0])
    beta = np.arccos(vec[2])
    gamma = -alpha*vec[2]
    cb = np.cos(0.5*beta)
    sb = np.sin(0.5*beta)
    return np.array([cb*np.cos(0.5*(alpha + gamma)),
                     sb*np.sin(0.5*(gamma - alpha)),
                     sb*np.cos(0.5*(gamma - alpha)),
                     cb*np.sin(0.5*(alpha + gamma))])
467296
def _values(self):
        """"""
        return [abs(val) for val in super(Dot, self)._values if val != 0]
994864
def _lits(lexer, varname, nvars):
    """"""
    tok = _expect_token(lexer, {OP_not, IntegerToken})
    if isinstance(tok, IntegerToken) and tok.value == 0:
        return tuple()
    else:
        if isinstance(tok, OP_not):
            neg = True
            tok = _expect_token(lexer, {IntegerToken})
        else:
            neg = False
        index = tok.value

        if index > nvars:
            fstr = "formula literal {} is greater than {}"
            raise Error(fstr.format(index, nvars))

        lit = ('var', (varname, ), (index, ))
        if neg:
            lit = ('not', lit)

        return (lit, ) + _lits(lexer, varname, nvars)
969025
def doymax(da):
    """"""
    i = da.argmax(dim='time')
    out = da.time.dt.dayofyear[i]
    out.attrs['units'] = ''
    return out
665422
def get_astrom(official='%',provisional='%'):
    """"""

    sql= "SELECT m.* FROM measure m "
    sql+="LEFT JOIN object o ON m.provisional LIKE o.provisional "
    if not official:
        sql+="WHERE  o.official IS NULL"
    else:
        sql+="WHERE o.official LIKE '%s' " % ( official, ) 
    sql+=" AND m.provisional LIKE '%s' " % ( provisional, )

    cfeps.execute(sql)
    return mk_dict(cfeps.fetchall(), cfeps.description)
1119698
def dims_intersect(self):
        
        return set.intersection(*map(
            set, (getattr(arr, 'dims_intersect', arr.dims) for arr in self)))
585088
def _get_col_dimstr(tdim, is_string=False):
    
    dimstr = ''
    if tdim is None:
        dimstr = 'array[bad TDIM]'
    else:
        if is_string:
            if len(tdim) > 1:
                dimstr = [str(d) for d in tdim[1:]]
        else:
            if len(tdim) > 1 or tdim[0] > 1:
                dimstr = [str(d) for d in tdim]
        if dimstr != '':
            dimstr = ','.join(dimstr)
            dimstr = 'array[%s]' % dimstr

    return dimstr