Exemple #1
0
    def init_algo_params(self):
        '''Initialize other algorithm parameters'''
        algorithm_spec = self.agent.spec['algorithm']
        net_spec = self.agent.spec['net']
        self.set_action_fn()
        util.set_attr(self, _.pick(algorithm_spec, [
            'gamma',
            'num_epis_to_collect',
            'add_entropy', 'entropy_weight',
            'continuous_action_clip',
            'lamda', 'num_step_returns',
            'training_frequency', 'training_iters_per_batch',
            'use_GAE',
            'policy_loss_weight', 'val_loss_weight',

        ]))
        util.set_attr(self, _.pick(net_spec, [
            'decay_lr', 'decay_lr_frequency', 'decay_lr_min_timestep',
        ]))
        '''Select appropriate function for calculating state-action-value estimate (target)'''
        self.get_target = self.get_nstep_target
        if self.use_GAE:
            self.get_target = self.get_gae_target
        self.set_memory_flag()
        '''To save on a forward pass keep the log probs and entropy from each action'''
        self.saved_log_probs = []
        self.entropy = []
        self.to_train = 0
Exemple #2
0
 def init_algo_params(self):
     '''Initialize other algorithm parameters'''
     algorithm_spec = self.agent.spec['algorithm']
     net_spec = self.agent.spec['net']
     # Automatically selects appropriate discrete or continuous action policy if setting is default
     action_fn = algorithm_spec['action_policy']
     if action_fn == 'default':
         if self.is_discrete:
             self.action_policy = act_fns['softmax']
         else:
             self.action_policy = act_fns['gaussian']
     else:
         self.action_policy = act_fns[action_fn]
     util.set_attr(
         self,
         _.pick(algorithm_spec, [
             'gamma', 'num_epis_to_collect', 'add_entropy',
             'entropy_weight', 'continuous_action_clip'
         ]))
     util.set_attr(
         self,
         _.pick(net_spec, [
             'decay_lr', 'decay_lr_frequency', 'decay_lr_min_timestep',
             'gpu'
         ]))
     if not hasattr(self, 'gpu'):
         self.gpu = False
     logger.info(f'Training on gpu: {self.gpu}')
     # To save on a forward pass keep the log probs from each action
     self.saved_log_probs = []
     self.entropy = []
     self.to_train = 0
Exemple #3
0
def plot_session(session, session_data):
    '''Plot the session graph, 2 panes: reward, loss & explore_var. Each aeb_df gets its own color'''
    aeb_count = len(session_data)
    if aeb_count <= 8:
        palette = cl.scales[str(max(3, aeb_count))]['qual']['Set2']
    else:
        palette = util.interp(cl.scales['8']['qual']['Set2'], aeb_count)
    fig = viz.tools.make_subplots(rows=3, cols=1, shared_xaxes=True)
    for idx, (a, e, b) in enumerate(session_data):
        aeb_str = f'{a}{e}{b}'
        aeb_df = session_data[(a, e, b)]
        fig_1 = viz.plot_line(aeb_df,
                              'reward',
                              'epi',
                              legend_name=aeb_str,
                              draw=False,
                              trace_kwargs={
                                  'legendgroup': aeb_str,
                                  'line': {
                                      'color': palette[idx]
                                  }
                              })
        fig.append_trace(fig_1.data[0], 1, 1)

        fig_2 = viz.plot_line(aeb_df, ['loss'],
                              'epi',
                              y2_col=['explore_var'],
                              trace_kwargs={
                                  'legendgroup': aeb_str,
                                  'showlegend': False,
                                  'line': {
                                      'color': palette[idx]
                                  }
                              },
                              draw=False)
        fig.append_trace(fig_2.data[0], 2, 1)
        fig.append_trace(fig_2.data[1], 3, 1)

    fig.layout['xaxis1'].update(title='epi', zerolinewidth=1)
    fig.layout['yaxis1'].update(fig_1.layout['yaxis'])
    fig.layout['yaxis1'].update(domain=[0.55, 1])

    fig.layout['yaxis2'].update(fig_2.layout['yaxis'])
    fig.layout['yaxis2'].update(showgrid=False, domain=[0, 0.45])
    fig.layout['yaxis3'].update(fig_2.layout['yaxis2'])
    fig.layout['yaxis3'].update(overlaying='y2', anchor='x2')
    fig.layout.update(_.pick(fig_1.layout, ['legend']))
    fig.layout.update(_.pick(fig_2.layout, ['legend']))
    fig.layout.update(title=session.spec['name'], width=500, height=600)
    viz.plot(fig)
    return fig
Exemple #4
0
    def save(self, commit=True):
        user = super().save(commit)
        cd = self.cleaned_data

        user.save()

        if user.is_participant():
            for field, value in py_.pick(cd, *self.PROFILE_FIELDS).items():
                setattr(user.participant_profile, field, value)
        else:
            user.participant_profile = Participant(**py_.pick(cd, *self.PROFILE_FIELDS))

        user.participant_profile.save()

        return user
Exemple #5
0
def test_query_index_by(db, model_class, data, callback, expected):
    """Test Query.index_by."""
    db.save([model_class(item) for item in data])
    results = db.query(model_class).index_by(callback)

    for key, value in results.items():
        assert pyd.pick(dict(value), pyd.keys(expected[key])) == expected[key]
Exemple #6
0
def task_reports(name,
                 _id,
                 endpoint,
                 method="GET",
                 source='report',
                 args={},
                 chain=[]):
    msg = "Task Report - %s" % _id
    normalize = from_pairs(map_(args, lambda i: [i['key'], i['value']]))

    query = json.dumps({
        '_id': normalize.get('report_id'),
        'owner._id': normalize.get('owner_user'),
        'active': True
    })

    resource = ExternalMaestroScheduler(_id) \
        .post_request(path="reports", body={'query': query})

    if resource.get_status() >= 400:
        msg = resource.get_error()
        task_deplete.delay(msg, _id)

    if resource.get_status() < 400:
        result = resource.get_results()
        msg = "Report success - %s" % _id
        if result.get('found', 0) == 1:
            objs = pick(result.get('items')[0], ['filters', 'component'])
            transformParams(args, objs)
            task_webhook.delay(name, _id, endpoint, source, method, args,
                               chain)

    return {'msg': msg, 'status_code': resource.get_status()}
 def populate(self, **kwargs):
     """Extended ndb.Model populate method, so it can ignore properties, which are not
     defined in model class without throwing error
     """
     kwargs = _.omit(kwargs, Base.PUBLIC_PROPERTIES + ['key', 'id'])  # We don't want to populate those properties
     kwargs = _.pick(kwargs, _.keys(self._properties))  # We want to populate only real model properties
     super(Base, self).populate(**kwargs)
Exemple #8
0
 def init_algo_params(self):
     '''Initialize other algorithm parameters'''
     algorithm_spec = self.agent.spec['algorithm']
     net_spec = self.agent.spec['net']
     self.action_policy = act_fns[algorithm_spec['action_policy']]
     self.action_policy_update = act_update_fns[
         algorithm_spec['action_policy_update']]
     util.set_attr(
         self,
         _.pick(
             algorithm_spec,
             [
                 # explore_var is epsilon, tau or etc. depending on the action policy
                 # these control the trade off between exploration and exploitaton
                 'explore_var_start',
                 'explore_var_end',
                 'explore_anneal_epi',
                 'gamma',  # the discount factor
                 'training_epoch',  # how many batches to train each time
                 'training_frequency',  # how often to train (once a few timesteps)
                 'training_iters_per_batch',  # how many times to train each batch
                 'training_min_timestep',  # how long before starting training
             ]))
     self.nanflat_explore_var_a = [self.explore_var_start
                                   ] * self.agent.body_num
Exemple #9
0
 def init_nets(self):
     '''Initialize the neural network used to learn the Q function from the spec'''
     body = self.agent.nanflat_body_a[0]  # single-body algo
     state_dim = body.state_dim  # dimension of the environment state, e.g. 4
     action_dim = body.action_dim  # dimension of the environment actions, e.g. 2
     net_spec = self.agent.spec['net']
     net_kwargs = util.compact_dict(
         dict(
             hid_layers_activation=_.get(net_spec, 'hid_layers_activation'),
             optim_param=_.get(net_spec, 'optim'),
             loss_param=_.get(net_spec, 'loss'),
             clamp_grad=_.get(net_spec, 'clamp_grad'),
             clamp_grad_val=_.get(net_spec, 'clamp_grad_val'),
         ))
     self.net = getattr(net,
                        net_spec['type'])(state_dim, net_spec['hid_layers'],
                                          action_dim, **net_kwargs)
     util.set_attr(
         self,
         _.pick(
             net_spec,
             [
                 # how many examples to learn per training iteration
                 'batch_size',
                 'decay_lr',
                 'decay_lr_frequency',
                 'decay_lr_min_timestep',
             ]))
Exemple #10
0
 def load(self, train_df):
     '''Load clock from the last row of body.train_df'''
     last_row = train_df.iloc[-1]
     last_clock_vals = ps.pick(last_row,
                               *['epi', 't', 'wall_t', 'opt_step', 'frame'])
     util.set_attr(self, last_clock_vals)
     self.start_wall_t -= self.wall_t  # offset elapsed wall_t
Exemple #11
0
    def save(self, commit=True):
        user = super().save(commit)
        cd = self.cleaned_data
        current_grade = Grade.objects.get_current()

        user.is_active = False
        user.save()

        user.participant_profile = Participant(
            **py_.pick(
                cd,
                "phone",
                "street",
                "city",
                "zip_code",
                "country",
                "school",
                "school_year",
                "school_alt_name",
                "schole_alt_street",
                "school_alt_city",
                "school_alt_zip_code",
            )
        )
        user.participant_profile.save()

        if current_grade:
            user.participant_profile.applications.add(current_grade)

        return user
Exemple #12
0
def parse_form_args_for_model(model,
                              extra_args=None,
                              json=None,
                              validator=True):
    # get all editable properties
    editable_props = model.EDITABLE_PROPS + (extra_args or [])
    return _.pick(json or request.form, editable_props)
Exemple #13
0
def transform(device, with_description=False):
    item = pick(
        device,
        'id',
        'code',
        'name',
        'model',
        'brand',
        'tag_code',
        'status',
        'manufacturer_id',
        'manufacturer_date',
        'department_id',
    )
    if with_description:
        item['description'] = device.description
    manufacturer = Manufacturer.query.filter_by(
        id=device.manufacturer_id, record_status=const.RECORD_NORMAL).first()
    item['manufacturer_name'] = manufacturer.name

    department = Department.query.filter_by(
        id=device.department_id, record_status=const.RECORD_NORMAL).first()
    item['department_code'] = department.code
    item['department_name'] = department.name
    return item
Exemple #14
0
 def init_nets(self):
     '''Initialize nets with multi-task dimensions, and set net params'''
     # NOTE: Separate init from MultitaskDQN despite similarities so that this implementation can support arbitrary sized state and action heads (e.g. multiple layers)
     net_spec = self.agent.spec['net']
     if len(net_spec['hid_layers']) > 0:
         state_head_out_d = int(net_spec['hid_layers'][0] / 4)
     else:
         state_head_out_d = 16
     self.state_dims = [
         [body.state_dim, state_head_out_d] for body in self.agent.nanflat_body_a]
     self.action_dims = [
         [body.action_dim] for body in self.agent.nanflat_body_a]
     self.total_state_dim = sum([s[0] for s in self.state_dims])
     self.total_action_dim = sum([a[0] for a in self.action_dims])
     logger.debug(
         f'State dims: {self.state_dims}, total: {self.total_state_dim}')
     logger.debug(
         f'Action dims: {self.action_dims}, total: {self.total_action_dim}')
     net_kwargs = util.compact_dict(dict(
         hid_layers_activation=_.get(net_spec, 'hid_layers_activation'),
         optim_param=_.get(net_spec, 'optim'),
         loss_param=_.get(net_spec, 'loss'),
         clamp_grad=_.get(net_spec, 'clamp_grad'),
         clamp_grad_val=_.get(net_spec, 'clamp_grad_val'),
     ))
     self.net = getattr(net, net_spec['type'])(
         self.state_dims, net_spec['hid_layers'], self.action_dims, **net_kwargs)
     self.target_net = getattr(net, net_spec['type'])(
         self.state_dims, net_spec['hid_layers'], self.action_dims, **net_kwargs)
     self.online_net = self.target_net
     self.eval_net = self.target_net
     util.set_attr(self, _.pick(net_spec, [
         'batch_size', 'update_type', 'update_frequency', 'polyak_weight',
     ]))
Exemple #15
0
def set_attr(obj, attr_dict, keys=None):
    '''Set attribute of an object from a dict'''
    if keys is not None:
        attr_dict = ps.pick(attr_dict, keys)
    for attr, val in attr_dict.items():
        setattr(obj, attr, val)
    return obj
Exemple #16
0
 def init_nets(self):
     '''Initialize nets with multi-task dimensions, and set net params'''
     self.state_dims = [
         body.state_dim for body in self.agent.nanflat_body_a]
     self.action_dims = [
         body.action_dim for body in self.agent.nanflat_body_a]
     self.total_state_dim = sum(self.state_dims)
     self.total_action_dim = sum(self.action_dims)
     net_spec = self.agent.spec['net']
     net_kwargs = util.compact_dict(dict(
         hid_layers_activation=_.get(net_spec, 'hid_layers_activation'),
         optim_param=_.get(net_spec, 'optim'),
         loss_param=_.get(net_spec, 'loss'),
         clamp_grad=_.get(net_spec, 'clamp_grad'),
         clamp_grad_val=_.get(net_spec, 'clamp_grad_val'),
     ))
     self.net = getattr(net, net_spec['type'])(
         self.total_state_dim, net_spec['hid_layers'], self.total_action_dim, **net_kwargs)
     self.target_net = getattr(net, net_spec['type'])(
         self.total_state_dim, net_spec['hid_layers'], self.total_action_dim, **net_kwargs)
     self.online_net = self.target_net
     self.eval_net = self.target_net
     util.set_attr(self, _.pick(net_spec, [
         'batch_size', 'update_type', 'update_frequency', 'polyak_weight',
     ]))
Exemple #17
0
def plot_session(session_spec, info_space, session_data):
    '''Plot the session graph, 2 panes: reward, loss & explore_var. Each aeb_df gets its own color'''
    graph_x = session_spec['meta'].get('graph_x', 'epi')
    aeb_count = len(session_data)
    palette = viz.get_palette(aeb_count)
    fig = viz.tools.make_subplots(rows=3, cols=1, shared_xaxes=True)
    for idx, (a, e, b) in enumerate(session_data):
        aeb_str = f'{a}{e}{b}'
        aeb_df = session_data[(a, e, b)]
        aeb_df.fillna(0, inplace=True)  # for saving plot, cant have nan
        fig_1 = viz.plot_line(aeb_df, 'reward', graph_x, legend_name=aeb_str, draw=False, trace_kwargs={'legendgroup': aeb_str, 'line': {'color': palette[idx]}})
        fig.append_trace(fig_1.data[0], 1, 1)

        fig_2 = viz.plot_line(aeb_df, ['loss'], graph_x, y2_col=['explore_var'], trace_kwargs={'legendgroup': aeb_str, 'showlegend': False, 'line': {'color': palette[idx]}}, draw=False)
        fig.append_trace(fig_2.data[0], 2, 1)
        fig.append_trace(fig_2.data[1], 3, 1)

    fig.layout['xaxis1'].update(title=graph_x, zerolinewidth=1)
    fig.layout['yaxis1'].update(fig_1.layout['yaxis'])
    fig.layout['yaxis1'].update(domain=[0.55, 1])
    fig.layout['yaxis2'].update(fig_2.layout['yaxis'])
    fig.layout['yaxis2'].update(showgrid=False, domain=[0, 0.45])
    fig.layout['yaxis3'].update(fig_2.layout['yaxis2'])
    fig.layout['yaxis3'].update(overlaying='y2', anchor='x2')
    fig.layout.update(ps.pick(fig_1.layout, ['legend']))
    fig.layout.update(title=f'session graph: {session_spec["name"]} t{info_space.get("trial")} s{info_space.get("session")}', width=500, height=600)
    viz.plot(fig)
    return fig
Exemple #18
0
 def init_nets(self):
     '''Initialize networks'''
     body = self.agent.nanflat_body_a[0]  # single-body algo
     state_dim = body.state_dim
     action_dim = body.action_dim
     net_spec = self.agent.spec['net']
     net_kwargs = util.compact_dict(dict(
         hid_layers_activation=_.get(net_spec, 'hid_layers_activation'),
         optim_param=_.get(net_spec, 'optim'),
         loss_param=_.get(net_spec, 'loss'),
         clamp_grad=_.get(net_spec, 'clamp_grad'),
         clamp_grad_val=_.get(net_spec, 'clamp_grad_val'),
     ))
     self.net = getattr(net, net_spec['type'])(
         state_dim, net_spec['hid_layers'], action_dim, **net_kwargs)
     self.target_net = getattr(net, net_spec['type'])(
         state_dim, net_spec['hid_layers'], action_dim, **net_kwargs)
     self.online_net = self.target_net
     self.eval_net = self.target_net
     util.set_attr(self, _.pick(net_spec, [
         'batch_size',
     ]))
     # Default network update params for base
     self.update_type = 'replace'
     self.update_frequency = 1
     self.polyak_weight = 0.0
Exemple #19
0
def plot_session(session_spec, info_space, session_data):
    '''Plot the session graph, 2 panes: reward, loss & explore_var. Each aeb_df gets its own color'''
    aeb_count = len(session_data)
    palette = get_palette(aeb_count)
    fig = viz.tools.make_subplots(rows=3, cols=1, shared_xaxes=True)
    for idx, (a, e, b) in enumerate(session_data):
        aeb_str = f'{a}{e}{b}'
        aeb_df = session_data[(a, e, b)]
        aeb_df.fillna(0, inplace=True)  # for saving plot, cant have nan
        fig_1 = viz.plot_line(aeb_df, 'reward', 'epi', legend_name=aeb_str, draw=False, trace_kwargs={'legendgroup': aeb_str, 'line': {'color': palette[idx]}})
        fig.append_trace(fig_1.data[0], 1, 1)

        fig_2 = viz.plot_line(aeb_df, ['loss'], 'epi', y2_col=['explore_var'], trace_kwargs={'legendgroup': aeb_str, 'showlegend': False, 'line': {'color': palette[idx]}}, draw=False)
        fig.append_trace(fig_2.data[0], 2, 1)
        fig.append_trace(fig_2.data[1], 3, 1)

    fig.layout['xaxis1'].update(title='epi', zerolinewidth=1)
    fig.layout['yaxis1'].update(fig_1.layout['yaxis'])
    fig.layout['yaxis1'].update(domain=[0.55, 1])
    fig.layout['yaxis2'].update(fig_2.layout['yaxis'])
    fig.layout['yaxis2'].update(showgrid=False, domain=[0, 0.45])
    fig.layout['yaxis3'].update(fig_2.layout['yaxis2'])
    fig.layout['yaxis3'].update(overlaying='y2', anchor='x2')
    fig.layout.update(ps.pick(fig_1.layout, ['legend']))
    fig.layout.update(title=f'session graph: {session_spec["name"]} t{info_space.get("trial")} s{info_space.get("session")}', width=500, height=600)
    viz.plot(fig)
    return fig
Exemple #20
0
def test_query_key_by(db, model_class, data, iteratee, expected):
    """Test SQLQuery.key_by."""
    db.save([model_class(item) for item in data])
    results = db.query(model_class).key_by(iteratee)

    for key, value in results.items():
        assert pyd.pick(dict(value), pyd.keys(expected[key])) == expected[key]
Exemple #21
0
def transform(device_rent):
    item = pick(device_rent,
                'id',
                'device_code',
                'status',
                'borrower_member_code',
                'borrower_department_id',
                'borrow_date',
                'borrow_remark',
                'returner_member_code',
                'return_remark',
                'expect_return_date',
                'real_return_date',
                )
    device = Device.query.filter_by(
        code=device_rent.device_code,
        record_status=const.RECORD_NORMAL
    ).first()
    item['device_id'] = device.id
    item['device_name'] = device.name
    item['model'] = device.model
    item['brand'] = device.brand
    item['tag_code'] = device.tag_code
    item['description'] = device.description
    item['manufacturer_date'] = device.manufacturer_date
    item['department_id'] = device.department_id

    manufacturer = Manufacturer.query.filter_by(
        id=device.manufacturer_id,
        record_status=const.RECORD_NORMAL
    ).first()
    item['manufacturer'] = manufacturer.name

    device_department = Department.query.filter_by(
        id=device.department_id,
        record_status=const.RECORD_NORMAL
    ).first()
    item['department_code'] = device_department.code
    item['department_name'] = device_department.name

    borrower = Member.query.filter_by(
        code=device_rent.borrower_member_code,
        record_status=const.RECORD_NORMAL
    ).first()
    item['borrower_id'] = borrower.id
    item['borrower_name'] = borrower.name

    borrower_department = Department.query.filter_by(
        id=borrower.department_id,
        record_status=const.RECORD_NORMAL
    ).first()
    item['borrower_department_code'] = borrower_department.code
    item['borrower_department_name'] = borrower_department.name
    returner = Member.query.filter_by(
        code=device_rent.returner_member_code,
        record_status=const.RECORD_NORMAL
    ).first()
    item['returner_id'] = returner.id
    item['returner_name'] = returner.name
    return item
Exemple #22
0
    async def upsert(self, body):
        request = self._httpclient.request()
        req_body = pydash.pick(body, [
            'status', 'name', 'type_specific', 'release_version', 'type',
            'uuid'
        ])

        req_json_body = json.dumps(req_body)
        #req_json_body = req_body

        url = self._httpclient.scheduler_url('/workers/')
        try:

            headers = {'Content-type': 'application/json'}
            async with request.put(url, data=req_json_body) as r:
                #TODO error
                json_data = await r.json()
                #await request.close()

                print(r)
                print(json_data)
                print('upset done')
                return json_data

        except Exception as exc:

            print(exc)
            pass
Exemple #23
0
def run_around_tests():
  query = pick(sample_config, 'tenant', 'integration_type')
  collection.delete_one(query)

  yield # Run the test

  collection.delete_one(query)
Exemple #24
0
 def _map(item):
     values = [
         v for i, v in enumerate(pick(attr.asdict(item), fields).values())
     ]
     return DataRecord(formulation=item.formulation,
                       fields=fields,
                       values=values)
Exemple #25
0
 def init_nets(self):
     super(DQN, self).init_nets()
     # Network update params
     net_spec = self.agent.spec['net']
     util.set_attr(self, _.pick(net_spec, [
         'update_type', 'update_frequency', 'polyak_weight',
     ]))
Exemple #26
0
def test_get_config():
  query = pick(sample_config, 'tenant', 'integration_type')
  collection.update_one(query, {'$set': sample_config}, upsert=True)

  uri = '/config?tenant=acme&integration_type=flight-information-system'
  _, response = app.test_client.get(uri)
  assert response.status == 200
  assert response.json == sample_config
def transform(field):
    sportsfield_data = pick(field, 'id', 'court_name', 'description',
                            'court_type', 'court_count', 'court_fee',
                            'period_class_id', 'order_days')
    period_class_id = sportsfield_data['period_class_id']
    period_class_name = PeriodClass.query.filter_by(id=period_class_id).first()
    sportsfield_data['period_class_name'] = period_class_name.period_class_name
    return sportsfield_data
Exemple #28
0
def test_query_stack_by(db, model_class, data, iteratee, expected):
    """Test SQLQuery.stack_by."""
    db.save([model_class(item) for item in data])
    results = db.query(model_class).stack_by(iteratee)

    for key, items in results.items():
        items = [pyd.pick(dict(item), pyd.keys(expected[key][0]))
                 for item in items]
        assert items == expected[key]
Exemple #29
0
def plot_go(
        df, y_col=None, x_col='index', y2_col=None,
        title=None, y_title=None, x_title=None, x_type=None,
        legend_name=None, width=500, height=350, draw=True,
        save=False, filename=None,
        trace_class='Scatter', trace_kwargs=None, layout_kwargs=None):
    '''
    Quickly plot from df using trace_class, e.g. go.Scatter
    1. create_label() to auto-resolve labels
    2. create_layout() with go.Layout() and update(layout_kwargs)
    3. spread and create go.<trace_class>() and update(trace_kwargs)
    4. Create the figure and plot accordingly
    @returns figure
    '''
    df = df.copy()
    if x_col == 'index':
        df['index'] = df.index.tolist()

    label = create_label(y_col, x_col, title, y_title, x_title, legend_name)
    layout = create_layout(
        x_type=x_type, width=width, height=height, layout_kwargs=layout_kwargs,
        **ps.pick(label, ['title', 'y_title', 'x_title']))
    y_col_list, x_col_list = label['y_col_list'], label['x_col_list']

    if y2_col is not None:
        label2 = create_label(y2_col, x_col, title, y_title, x_title, legend_name)
        layout.update(dict(yaxis2=dict(
            rangemode='tozero', title=label2['y_title'],
            side='right', overlaying='y1', anchor='x1',
        )))
        y2_col_list, x_col_list = label2['y_col_list'], label2['x_col_list']
        label2_legend_name_list = label2['legend_name_list']
    else:
        y2_col_list = []
        label2_legend_name_list = []

    combo_y_col_list = y_col_list + y2_col_list
    combo_legend_name_list = label['legend_name_list'] + label2_legend_name_list
    y_col_num, x_col_num = len(combo_y_col_list), len(x_col_list)
    trace_num = max(y_col_num, x_col_num)
    data = []
    for idx in range(trace_num):
        y_c = ps.get(combo_y_col_list, idx % y_col_num)
        x_c = ps.get(x_col_list, idx % x_col_num)
        df_y, df_x = ps.get(df, y_c), ps.get(df, x_c)
        trace = ps.get(go, trace_class)(y=df_y, x=df_x, name=combo_legend_name_list[idx])
        trace.update(trace_kwargs)
        if idx >= len(y_col_list):
            trace.update(dict(yaxis='y2', xaxis='x1'))
        data.append(trace)

    figure = go.Figure(data=data, layout=layout)
    if draw:
        plot(figure)
    if save:
        save_image(figure, filename=filename)
    return figure
Exemple #30
0
 def set_net_attributes(self):
     '''Initializes additional parameters from the net spec. Called by init_nets'''
     net_spec = self.agent.spec['net']
     util.set_attr(self, _.pick(net_spec, [
         'decay_lr', 'decay_lr_frequency', 'decay_lr_min_timestep', 'gpu'
     ]))
     if not hasattr(self, 'gpu'):
         self.gpu = False
     logger.info(f'Training on gpu: {self.gpu}')
Exemple #31
0
def plot_go(
        df, y_col=None, x_col='index', y2_col=None,
        title=None, y_title=None, x_title=None, x_type=None,
        legend_name=None, width=500, height=350, draw=True,
        save=False, filename=None,
        trace_class='Scatter', trace_kwargs=None, layout_kwargs=None):
    '''
    Quickly plot from df using trace_class, e.g. go.Scatter
    1. create_label() to auto-resolve labels
    2. create_layout() with go.Layout() and update(layout_kwargs)
    3. spread and create go.<trace_class>() and update(trace_kwargs)
    4. Create the figure and plot accordingly
    @returns figure
    '''
    df = df.copy()
    if x_col == 'index':
        df['index'] = df.index.tolist()

    label = create_label(y_col, x_col, title, y_title, x_title, legend_name)
    layout = create_layout(
        x_type=x_type, width=width, height=height, layout_kwargs=layout_kwargs,
        **ps.pick(label, ['title', 'y_title', 'x_title']))
    y_col_list, x_col_list = label['y_col_list'], label['x_col_list']

    if y2_col is not None:
        label2 = create_label(y2_col, x_col, title, y_title, x_title, legend_name)
        layout.update(dict(yaxis2=dict(
            rangemode='tozero', title=label2['y_title'],
            side='right', overlaying='y1', anchor='x1',
        )))
        y2_col_list, x_col_list = label2['y_col_list'], label2['x_col_list']
        label2_legend_name_list = label2['legend_name_list']
    else:
        y2_col_list = []
        label2_legend_name_list = []

    combo_y_col_list = y_col_list + y2_col_list
    combo_legend_name_list = label['legend_name_list'] + label2_legend_name_list
    y_col_num, x_col_num = len(combo_y_col_list), len(x_col_list)
    trace_num = max(y_col_num, x_col_num)
    data = []
    for idx in range(trace_num):
        y_c = ps.get(combo_y_col_list, idx % y_col_num)
        x_c = ps.get(x_col_list, idx % x_col_num)
        df_y, df_x = ps.get(df, y_c), ps.get(df, x_c)
        trace = ps.get(go, trace_class)(y=df_y, x=df_x, name=combo_legend_name_list[idx])
        trace.update(trace_kwargs)
        if idx >= len(y_col_list):
            trace.update(dict(yaxis='y2', xaxis='x1'))
        data.append(trace)

    figure = go.Figure(data=data, layout=layout)
    if draw:
        plot(figure)
    if save:
        save_image(figure, filename=filename)
    return figure
Exemple #32
0
def appInfo():
    root_path = os.path.join(app.root_path, '..')
    file = open(root_path + '/package.json')
    json_data = file.read()
    data = json.loads(json_data)

    file.close()
    return pick(data,
                ['name', 'provider', 'description', 'version', 'license'])
Exemple #33
0
 def extract(s, i):
     d = _.pick(
         s['item'],
         ['id', 'mallProductId', 'productTitle', 'imageUrl', 'rank'])
     is_ad = isinstance(d['rank'], str)
     d['isAd'] = is_ad
     d['displayRank'] = i + 1
     if is_ad:
         d['rank'] = int(d['rank'])
     return d
 def get_auth_module(cls):
     if cls.json_auth in request.json:
         args = _.pick(request.json.get(cls.json_auth), [cls.json_email, cls.json_password,
                                                          cls.json_refresh_token, cls.json_google_token])
         if cls.json_password in args and cls.json_email in args: # authType is password
             return EmailAuth(args.get(cls.json_email), args.get(cls.json_password))
         elif cls.json_refresh_token in args: # refresh token time
             return RefreshTokenAuth(args.get(cls.json_refresh_token))
         elif cls.json_google_token in args:
             return GoogleAuth(args.get(cls.json_google_token))
     return None
    def post(self): # lists all the products
        args = _.pick(request.json, TransactionModel.PUBLIC_PROPERTIES + ['merchant_id', 'product_id'])

        merchant_db = product_db = None
        if args.get('merchant_id'): # do a look up
            url = "https://www.delivery.com/api/merchant/%s?client_id=brewhacks2016" % args.get('merchant_id')
            result = urlfetch.fetch(url)
            if result.status_code == 200:
                data = json.loads(result.content)
                merchant_db = scraper.parse_merchant(data.get('merchant'))
                args['merchant_key'] = merchant_db._key
        else:
            args['merchant_key'] = ndb.Key(urlsafe=args.get('merchant_key'))
            merchant_db = args.get('merchant_key').get()

        if not merchant_db: # look it up
            errors.create(404, message="merchant not found")

        # do products
        if args.get('product_id'): # do a look up
            url = "https://www.delivery.com/api/data/product/%(product)s?merchant_id=%(merchant)s" \
                  "&client_id=brewhacks2016" % dict(product=args.get('product_id'),
                                                    merchant=args.get('merchant_id'))
            result = urlfetch.fetch(url)
            if result.status_code == 200:
                data = json.loads(result.content)
                product_db = scraper.parse_product(data.get('item')[0], data.get('item')[0].get('id'))
                args['product_key'] = product_db._key
        else:
            args['product_key'] = ndb.Key(urlsafe=args.get('product_key'))
            product_db = args.get('product_key').get()

        if not product_db: # look it up
            errors.create(404, message="product not found")

        args['merchant'] = merchant_db
        args['product'] = product_db

        transaction_db = TransactionModel(**args)
        transaction_db.put()
        return transaction_db.to_dict(include=TransactionModel.get_public_properties())
Exemple #36
0
	def __on_battle_player_added(self, player):
		database.insert_battle_player(**_.pick(player, 'id', 'name'))
		database.insert_vehicle(id=player['vehicle_id'], player_id=player['id'], is_alive=player['is_alive'])
Exemple #37
0
def test_pick(case, expected):
    assert _.pick(*case) == expected