Beispiel #1
0
    def _ast_impl(self):
        args = [ast.Raw(self.numerator), ast.Raw(self.denominator)]

        if self.cap_below:
            args += [(ast.Name('min'), ast.Constant(self.cap_below_at))]
            if self.cap_below_at != self.cap_below_with:
                args += [(ast.Name('min_to'),
                          ast.Constant(self.cap_below_with))]
        if self.cap_above:
            args += [(ast.Name('max'), ast.Constant(self.cap_above_at))]
            if self.cap_above_at != self.cap_above_with:
                args += [(ast.Name('max_to'),
                          ast.Constant(self.cap_above_with))]

        if self.replace_zero:
            args += [(ast.Name('zero_to'),
                      ast.Constant(self.replace_zero_with))]
        if self.replace_inf:
            args += [(ast.Name('inf_to'), ast.Constant(self.replace_inf_with))]
        if self.replace_na:
            args += [(ast.Name('na_to'), ast.Constant(self.replace_na_with))]

        node = ast.Call(ast.Name('ratio'), args)
        if self.log_transform:
            node = ast.Call(ast.Name('safe_log1p'), node)
        return node
Beispiel #2
0
 def ast_for_dbi_call(self, call_name, *call_args):
     """ Returns a Call to an R function supporting the DBI connections parameters.
     """
     if self.dialect == 'db2':
         args = [
             (ast.Name(self.conn)),
             (ast.Name('.connection_string'),
              ast.Constant(self.DB2_connection_str())),
             # (ast.Name('dsn'), ast.Constant(self.dsn)),
             # (ast.Name('driver'), ast.Constant(self.driver)),
         ]
     else:
         args = [
             ast.Call(ast.Name('dbDriver'),
                      ast.Constant(R_DBI_DRIVERS[self.dialect]))
         ]
     args += call_args
     args += [(ast.Name('dbname'), ast.Constant(self.database))]
     # if self.dialect != 'sqlite':
     #     args += [(ast.Name('user'), ast.Constant(self.username)),
     #              (ast.Name('password'), ast.Constant(self.password)),
     #              (ast.Name('host'), ast.Constant(self.host)), ]
     return ast.Call(call_name,
                     args,
                     libraries=['DBI', R_DBI_LIBRARIES[self.dialect]])
Beispiel #3
0
 def ast(self):
     return ast.Call(ast.Name('def_group_metric'),
                     ast.Constant(self.name),
                     ast.Raw(self.expression),
                     ast.Name('entropy_disc'),
                     (ast.Name('type'), ast.Constant(self.method)),
                     print_hint='long')
Beispiel #4
0
 def ast(self):
     R_funcs = {
         'chi_square': 'chisq_test',
         'ks': 'ks.stat',
         'custom': self.custom_function,
     }
     return ast.Call(ast.Name('def_group_metric'),
                     ast.Constant(self.name),
                     ast.Raw(self.expression),
                     ast.Name(R_funcs[self.kind]),
                     print_hint='long')
Beispiel #5
0
    def ast(self):
        """ Generate the AST for a complete R program executing the model.
        """
        # Build program body.
        prog = self._ast_impl()

        # Extract required libraries and load them first.
        libs = ast_transform.find_libraries(prog)
        lib_block = ast.Block(
            [ast.Call(ast.Name('library'), ast.Name(lib)) for lib in libs])
        prog.value.insert(0, lib_block)

        return prog
Beispiel #6
0
 def _ast_impl(self):
     args = [ast.Raw(self.expression)]
     if self.auto_breaks:
         arg_breaks = ast.Constant(self.num_breaks)
     else:
         arg_breaks = ast_macros.seq_to_vector(self.breaks)
     args.append((ast.Name('breaks'), arg_breaks))
     if self.labels:
         args.append(
             (ast.Name('labels'), ast_macros.seq_to_vector(self.labels)))
     if self.closed_on_left:
         # By default, R closes on the right.
         args.append((ast.Name('right'), ast.Constant(False)))
     return ast.Call(ast.Name('cut'), args, print_hint='long')
Beispiel #7
0
def _write_constant(node, out, indent):
    value = node.value
    if isinstance(value, bool):
        out.write('TRUE' if value else 'FALSE')
    elif isinstance(value, float) and math.isinf(value):
        out.write('Inf' if value > 0 else '-Inf')
    elif isinstance(value, float) and math.isnan(value):
        # Follow Pandas in using NaN to represent missing data.
        out.write('NA')
    elif isinstance(value, complex):
        new_node = ast.Call(ast.Name('complex'),
                            (ast.Name('real'), ast.Constant(value.real)),
                            (ast.Name('imaginary'), ast.Constant(value.imag)))
        _write_ast(new_node, out, indent)
    elif isinstance(value, unicode):
        out.write(repr(value.encode('ascii')))
    else:
        out.write(repr(value))
Beispiel #8
0
 def _ast_impl(self):
     self.model.store_input = True
     nodes = [self.model.ast()]
     if self.input_source:
         run_args = self.input_source.ast()
         if isinstance(run_args, ast.Node):
             run_args = [(ast.Name('input'), run_args)]
         if self.output_source:
             conn = self.output_source.ast_for_dbi_call(
                 ast.Name('dbConnect'))
             run_args += [
                 (ast.Name('output'), conn),
                 (ast.Name('store_input'),
                  ast.Constant(self.model.store_input)),
             ]
         run_nodes = [
             ast.Call(ast.Name('run_model'), run_args, print_hint='long'),
         ]
         nodes += [
             ast.Comment('Execute model'),
             ast.Block(run_nodes),
         ]
     return ast.Block(nodes, print_hint='long')
 def ast(self, path):
     return ast.Call(ast.Name('read.xlsx'),
                     ast.Constant(path),
                     ast.Constant(1),  # Sheet index
                     libraries=['xlsx'])
Beispiel #10
0
 def ast(self):
     return ast.Call(ast.Name('def_group_metric'),
                     ast.Constant(self.name),
                     ast.Raw(self.expression),
                     ast.Name('graph_density'),
                     print_hint='long')
Beispiel #11
0
 def ast(self):
     return ast.Call(ast.Name('def_group_metric'),
                     ast.Constant(self.name),
                     ast.Raw(self.expression),
                     ast.Name('uniq_cont'),
                     print_hint='long')