def reward(self, history_id, rewards):
"""Reward the previous action with reward.
Parameters
----------
history_id : int
The history id of the action to reward.
rewards : dictionary
The dictionary {action_id, reward}, where reward is a float.
"""
context = (self._historystorage
.get_unrewarded_history(history_id)
.context)
model = self._modelstorage.get_model()
w = model['w']
action_probs = model['action_probs']
action_ids = list(six.viewkeys(six.next(six.itervalues(context))))
# Update the model
for action_id, reward in six.viewitems(rewards):
y_hat = {}
v_hat = {}
for i in six.viewkeys(context):
y_hat[i] = (context[i][action_id] * reward
/ action_probs[action_id])
v_hat[i] = sum(
[context[i][k] / action_probs[k] for k in action_ids])
w[i] = w[i] * np.exp(
self.p_min / 2
* (y_hat[i] + v_hat[i]
* np.sqrt(np.log(len(context) / self.delta)
/ (len(action_ids) * self.max_rounds))))
self._modelstorage.save_model({
'action_probs': action_probs, 'w': w})
# Update the history
self._historystorage.add_reward(history_id, rewards)
python类viewkeys()的实例源码
def iterids(self):
r"""Return iterable of the Action ids.
Returns
-------
action_ids: iterable
Action ids.
"""
return six.viewkeys(self._actions)
def rebalance(context, data):
#############################################################################
# Pipeline data will be a dataframe with boolean columns named 'longs' and
# 'shorts'.
pipeline_data = context.pipeline_data
all_assets = pipeline_data.index
longs = all_assets[pipeline_data.longs]
shorts = all_assets[pipeline_data.shorts]
record(universe_size=len(all_assets))
# Build a 2x-leveraged, equal-weight, long-short portfolio.
one_third = 1.0 / context.optim_leveraged
for asset in longs:
order_target_percent(asset, one_third)
for asset in shorts:
order_target_percent(asset, -one_third)
# Remove any assets that should no longer be in our portfolio.
portfolio_assets = longs | shorts
positions = context.portfolio.positions
for asset in viewkeys(positions) - set(portfolio_assets):
# This will fail if the asset was removed from our portfolio because it
# was delisted.
if data.can_trade(asset):
order_target_percent(asset, 0)
def _normalize(d):
'''
The above parse function generates output of list in dict form
i.e. {'abc' : {0: 'xyz', 1: 'pqr'}}. This function normalize it and turn
them into proper data type, i.e. {'abc': ['xyz', 'pqr']}
Note: if dict has element starts with 10, 11 etc.. this function won't fill
blanks.
for eg: {'abc': {10: 'xyz', 12: 'pqr'}} will convert to
{'abc': ['xyz', 'pqr']}
'''
newd = {}
if isinstance(d, dict) == False:
return d
# if dictionary. iterate over each element and append to newd
for k, v in six.iteritems(d):
if isinstance(v, dict):
first_key = next(iter(six.viewkeys(v)))
if isinstance(first_key, int):
temp_new = []
for k1, v1 in v.items():
temp_new.append(_normalize(v1))
newd[k] = temp_new
elif first_key == '':
newd[k] = v.values()[0]
else:
newd[k] = _normalize(v)
else:
newd[k] = v
return newd
def synchronize(self):
modified = False
for app_unique_name in six.viewkeys(self._devices.copy()):
if not self._devices[app_unique_name].get('stale', False):
continue
modified = True
# This is a stale device, destroy it.
self.on_delete_request(app_unique_name)
if not modified:
return
# Read bridge status
self._bridge_mtu = netdev.dev_mtu(self._TMBR_DEV)
def viewkeys(self):
return self._dict.viewkeys()
def instantiate_plugins(config, **kwargs):
kwargs.setdefault('propagate_map_exceptions', True)
kwargs.setdefault('on_load_failure_callback', extension_load_failure_callback)
kwargs.setdefault('verify_requirements', True)
kwargs.setdefault('names', config.get('PLUGINS_ENABLED', six.viewkeys(config.get('PLUGINS', {}))))
plugins = stevedore.named.NamedExtensionManager(**kwargs)
logger.debug("'{!s}' plugins: loaded {!s}", kwargs['namespace'], plugins.names())
for plugin in plugins:
plugin_config = config.get('PLUGINS', {}).get(plugin.name, {})
plugin_config.setdefault('DEBUG', config.get('DEBUG', False))
plugin_config.setdefault('DEFAULT_TIMEOUT', config.get('DEFAULT_TIMEOUT', False))
plugin.obj = plugin.plugin(plugin_config)
plugin.obj.plugin_name = plugin.name
logger.debug("'{!s}' plugins: instantiated {!s}", kwargs['namespace'], plugins.names())
return plugins
def instantiate_practices(config, **kwargs):
kwargs.setdefault('propagate_map_exceptions', True)
kwargs.setdefault('on_load_failure_callback', extension_load_failure_callback)
kwargs.setdefault('verify_requirements', True)
kwargs.setdefault('names', config.get('PRACTICES_ENABLED',
six.viewkeys(config.get('PRACTICES', {}))))
practices = stevedore.named.NamedExtensionManager(**kwargs)
logger.debug("'{!s}' practices: loaded {!s}", kwargs['namespace'], practices.names())
for plugin in practices:
plugin_config = config.get('PRACTICES', {}).get(plugin.name, {})
plugin.obj = plugin.plugin(plugin_config)
plugin.obj.plugin_name = plugin.name
logger.debug("'{!s}' practices: instantiated {!s}", kwargs['namespace'], practices.names())
return practices
def adjust_attributes(token, replacements):
if PY3 or _utils.PY27:
needs_adjustment = viewkeys(token['data']) & viewkeys(replacements)
else:
needs_adjustment = frozenset(token['data']) & frozenset(replacements)
if needs_adjustment:
token['data'] = OrderedDict((replacements.get(k, k), v)
for k, v in token['data'].items())
def everything_else_is_wrong():
d = None # note: bugbear is no type checker
d.iterkeys()
d.itervalues()
d.iteritems()
d.iterlists() # Djangoism
d.viewkeys()
d.viewvalues()
d.viewitems()
d.viewlists() # Djangoism
d.next()
d.keys().next()
def as_cql_query(self, formatted=False):
"""
Returns a CQL query that can be used to recreate this function.
If `formatted` is set to :const:`True`, extra whitespace will
be added to make the query more readable.
"""
sep = '\n ' if formatted else ' '
keyspace = protect_name(self.keyspace_name)
name = protect_name(self.name)
selected_cols = '*' if self.include_all_columns else ', '.join(protect_name(col.name) for col in self.columns.values())
base_table = protect_name(self.base_table_name)
where_clause = self.where_clause
part_key = ', '.join(protect_name(col.name) for col in self.partition_key)
if len(self.partition_key) > 1:
pk = "((%s)" % part_key
else:
pk = "(%s" % part_key
if self.clustering_key:
pk += ", %s" % ', '.join(protect_name(col.name) for col in self.clustering_key)
pk += ")"
properties = TableMetadataV3._property_string(formatted, self.clustering_key, self.options)
ret = "CREATE MATERIALIZED VIEW %(keyspace)s.%(name)s AS%(sep)s" \
"SELECT %(selected_cols)s%(sep)s" \
"FROM %(keyspace)s.%(base_table)s%(sep)s" \
"WHERE %(where_clause)s%(sep)s" \
"PRIMARY KEY %(pk)s%(sep)s" \
"WITH %(properties)s" % locals()
if self.extensions:
registry = _RegisteredExtensionType._extension_registry
for k in six.viewkeys(registry) & self.extensions: # no viewkeys on OrderedMapSerializeKey
ext = registry[k]
cql = ext.after_table_cql(self, k, self.extensions[k])
if cql:
ret += "\n\n%s" % (cql,)
return ret
def _retrieve_assets(self, sids, asset_tbl, asset_type):
"""
Internal function for loading assets from a table.
This should be the only method of `AssetFinder` that writes Assets into
self._asset_cache.
Parameters
---------
sids : iterable of int
Asset ids to look up.
asset_tbl : sqlalchemy.Table
Table from which to query assets.
asset_type : type
Type of asset to be constructed.
Returns
-------
assets : dict[int -> Asset]
Dict mapping requested sids to the retrieved assets.
"""
# Fastpath for empty request.
if not sids:
return {}
cache = self._asset_cache
hits = {}
for assets in self._group_into_chunks(sids):
# Load misses from the db.
query = self._select_assets_by_sid(asset_tbl, assets)
for row in imap(dict, query.execute().fetchall()):
asset = asset_type(**_convert_asset_timestamp_fields(row))
sid = asset.sid
hits[sid] = cache[sid] = asset
# If we get here, it means something in our code thought that a
# particular sid was an equity/future and called this function with a
# concrete type, but we couldn't actually resolve the asset. This is
# an error in our code, not a user-input error.
misses = tuple(set(sids) - viewkeys(hits))
if misses:
if asset_type == Equity:
raise EquitiesNotFound(sids=misses)
else:
raise FutureContractsNotFound(sids=misses)
return hits
def from_geom(geom):
"""
Return an instantiated stat object
stats should not override this method.
Parameters
----------
geom : geom
`geom`
Returns
-------
out : stat
A stat object
Raises
------
:class:`PlotnineError` if unable to create a `stat`.
"""
name = geom.params['stat']
kwargs = geom._kwargs
# More stable when reloading modules than
# using issubclass
if (not isinstance(name, type) and
hasattr(name, 'compute_layer')):
return name
if isinstance(name, stat):
return name
elif isinstance(name, type) and issubclass(name, stat):
klass = name
elif is_string(name):
if not name.startswith('stat_'):
name = 'stat_{}'.format(name)
klass = Registry[name]
else:
raise PlotnineError(
'Unknown stat of type {}'.format(type(name)))
valid_kwargs = (
(klass.aesthetics() |
six.viewkeys(klass.DEFAULT_PARAMS)) &
six.viewkeys(kwargs))
params = {k: kwargs[k] for k in valid_kwargs}
return klass(geom=geom, **params)
def generate_for_host(host):
from . import util
rr = ''
rr += '''\n########## GENERATED DO NOT MODIFY #####################\n'''
sshport = 22 if util.is_localhost(host['name']) else 2222
if not util.is_localhost(host['name']):
if host.get('match'):
matches = host.get('match')
for hostname in [host['name'], host['name'] + '-ports']:
for match in matches:
rr += 'Match originalhost {hostname} exec "{match[condition]}"\n'.format(
hostname=hostname, match=match)
for key in six.viewkeys(match):
if not key in ssh_option_names:
continue
rr += ' {key} {value}\n'.format(key=key,
value=match[key])
rr += '\n'
rr += 'host {}\n'.format(host['name'])
for key, val in six.iteritems(host):
if not key in ssh_option_names:
continue
if not isinstance(val, (list, tuple)):
val = [val]
for vv in val:
rr += ' {key} {value}\n'.format(key=key, value=vv)
rr += '\n'
if host.get('containers'):
rr += 'host {}-ports\n'.format(host['name'])
if not 'HostName' in six.viewkeys(host):
host['HostName'] = host['name']
for key, val in six.iteritems(host):
if not key in ssh_option_names + ['LocalForward']:
continue
if not isinstance(val, (list, tuple)):
val = [val]
for vv in val:
rr += ' {key} {value}\n'.format(key=key, value=vv)
# rr += ' LocalForward {}-local:2375 localhost:2375\n'.format(host['name'])
for cont in host.get('containers', []):
ports = cont['image'].get('ports', [])
for port in ports + ["{}:22".format(sshport)]:
(p1, p2) = port.split(':')
rr += (
" LocalForward {0}:{1} {2}:{1}\n".format(
cont['name'], p1, cont['ip']))
rr += '\n'
for cont in host.get('containers', []):
rr += container_entry_template.format(**locals())
for key, val in six.iteritems(host):
if key in container_ssh_option_names:
rr += ' {} {}\n'.format(key, val)
return rr
def _retrieve_assets(self, sids, asset_tbl, asset_type):
"""
Internal function for loading assets from a table.
This should be the only method of `AssetFinder` that writes Assets into
self._asset_cache.
Parameters
---------
sids : iterable of int
Asset ids to look up.
asset_tbl : sqlalchemy.Table
Table from which to query assets.
asset_type : type
Type of asset to be constructed.
Returns
-------
assets : dict[int -> Asset]
Dict mapping requested sids to the retrieved assets.
"""
# Fastpath for empty request.
if not sids:
return {}
cache = self._asset_cache
hits = {}
querying_equities = issubclass(asset_type, Equity)
filter_kwargs = (
_filter_equity_kwargs
if querying_equities else
_filter_future_kwargs
)
rows = self._retrieve_asset_dicts(sids, asset_tbl, querying_equities)
for row in rows:
sid = row['sid']
asset = asset_type(**filter_kwargs(row))
hits[sid] = cache[sid] = asset
# If we get here, it means something in our code thought that a
# particular sid was an equity/future and called this function with a
# concrete type, but we couldn't actually resolve the asset. This is
# an error in our code, not a user-input error.
misses = tuple(set(sids) - viewkeys(hits))
if misses:
if querying_equities:
raise EquitiesNotFound(sids=misses)
else:
raise FutureContractsNotFound(sids=misses)
return hits
def _forward(self):
if self.fallback_to_local:
return self._local_forward()
responses = {}
errors = collections.defaultdict(lambda: [])
for sp in self.enabled_sps:
if sp == 'default':
r = self._do_request_on('default')
if 200 <= r.status_code < 300:
responses['default'] = r
if not self.aggregate:
return self._finalize(r)
else:
errors[r.status_code].append(r)
else:
for p in auth.get_projects_at_sp(sp, self.details.token):
r = self._do_request_on(sp, p)
if 200 <= r.status_code < 300:
responses[(sp, p)] = r
if not self.aggregate:
return self._finalize(r)
else:
errors[r.status_code].append(r)
# NOTE(knikolla): If we haven't returned yet, either we're aggregating
# or there are errors.
if not errors:
# TODO(knikolla): Plug this into _finalize to have a common path
# for everything that is returned.
return flask.Response(
services.aggregate(responses,
self.details.action[0],
self.details.service,
version=self.details.version,
params=self.details.args,
path=request.base_url,
strip_details=self.strip_details),
200,
content_type='application/json'
)
if six.viewkeys(errors) == {404}:
return self._finalize(errors[404][0])
else:
utils.safe_pop(errors, 404)
if len(errors.keys()) == 1:
return self._finalize(list(errors.values())[0][0])
# TODO(jfreud): log
return flask.Response("Something strange happened.\n", 500)
def get_action(self, context=None, n_actions=None):
"""Return the action to perform
Parameters
----------
context : {array-like, None}
The context of current state, None if no context available.
n_actions: int (default: None)
Number of actions wanted to recommend users. If None, only return
one action. If -1, get all actions.
Returns
-------
history_id : int
The history id of the action.
recommendations : list of dict
Each dict contains
{Action object, estimated_reward, uncertainty}.
"""
if self._action_storage.count() == 0:
return self._get_action_with_empty_action_storage(context,
n_actions)
probs = self._exp3_probs()
if n_actions == -1:
n_actions = self._action_storage.count()
action_ids = list(six.viewkeys(probs))
prob_array = np.asarray([probs[action_id]
for action_id in action_ids])
recommendation_ids = self.random_state.choice(
action_ids, size=n_actions, p=prob_array, replace=False)
if n_actions is None:
recommendations = self._recommendation_cls(
action=self._action_storage.get(recommendation_ids),
estimated_reward=probs[recommendation_ids],
uncertainty=probs[recommendation_ids],
score=probs[recommendation_ids],
)
else:
recommendations = [] # pylint: disable=redefined-variable-type
for action_id in recommendation_ids:
recommendations.append(self._recommendation_cls(
action=self._action_storage.get(action_id),
estimated_reward=probs[action_id],
uncertainty=probs[action_id],
score=probs[action_id],
))
history_id = self._history_storage.add_history(context, recommendations)
return history_id, recommendations
def __init__(self):
if context.GLOBAL.cell is not None:
zkclient = context.GLOBAL.zk.conn
cell_state = CellState()
_LOGGER.info('Initializing api.')
watch_running(zkclient, cell_state)
watch_placement(zkclient, cell_state)
watch_finished(zkclient, cell_state)
watch_finished_history(zkclient, cell_state)
def _list(match=None, finished=False, partition=None):
"""List instances state."""
if match is None:
match = '*'
if '#' not in match:
match += '#*'
filtered = [
{'name': name, 'state': item['state'], 'host': item['host']}
for name, item in six.viewitems(cell_state.placement.copy())
if fnmatch.fnmatch(name, match)
]
if finished:
for name in six.viewkeys(cell_state.finished.copy()):
if fnmatch.fnmatch(name, match):
state = cell_state.get_finished(name)
item = {'name': name}
item.update(state)
filtered.append(item)
if partition is not None:
hosts = [rec['_id'] for rec in
API._get_server_info()
if rec['partition'] == partition]
filtered = [item for item in filtered
if item['host'] in hosts]
return sorted(filtered, key=lambda item: item['name'])
@schema.schema({'$ref': 'instance.json#/resource_id'})
def get(rsrc_id):
"""Get instance state."""
if rsrc_id in cell_state.placement:
state = cell_state.placement[rsrc_id]
else:
state = cell_state.get_finished(rsrc_id)
if not state:
return None
res = {'name': rsrc_id}
res.update(state)
return res
self.list = _list
self.get = get