repo
stringlengths 7
55
| path
stringlengths 4
223
| func_name
stringlengths 1
134
| original_string
stringlengths 75
104k
| language
stringclasses 1
value | code
stringlengths 75
104k
| code_tokens
listlengths 19
28.4k
| docstring
stringlengths 1
46.9k
| docstring_tokens
listlengths 1
1.97k
| sha
stringlengths 40
40
| url
stringlengths 87
315
| partition
stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|
pypa/pipenv
|
pipenv/vendor/jinja2/compiler.py
|
CodeGenerator.pop_assign_tracking
|
def pop_assign_tracking(self, frame):
"""Pops the topmost level for assignment tracking and updates the
context variables if necessary.
"""
vars = self._assign_stack.pop()
if not frame.toplevel or not vars:
return
public_names = [x for x in vars if x[:1] != '_']
if len(vars) == 1:
name = next(iter(vars))
ref = frame.symbols.ref(name)
self.writeline('context.vars[%r] = %s' % (name, ref))
else:
self.writeline('context.vars.update({')
for idx, name in enumerate(vars):
if idx:
self.write(', ')
ref = frame.symbols.ref(name)
self.write('%r: %s' % (name, ref))
self.write('})')
if public_names:
if len(public_names) == 1:
self.writeline('context.exported_vars.add(%r)' %
public_names[0])
else:
self.writeline('context.exported_vars.update((%s))' %
', '.join(imap(repr, public_names)))
|
python
|
def pop_assign_tracking(self, frame):
"""Pops the topmost level for assignment tracking and updates the
context variables if necessary.
"""
vars = self._assign_stack.pop()
if not frame.toplevel or not vars:
return
public_names = [x for x in vars if x[:1] != '_']
if len(vars) == 1:
name = next(iter(vars))
ref = frame.symbols.ref(name)
self.writeline('context.vars[%r] = %s' % (name, ref))
else:
self.writeline('context.vars.update({')
for idx, name in enumerate(vars):
if idx:
self.write(', ')
ref = frame.symbols.ref(name)
self.write('%r: %s' % (name, ref))
self.write('})')
if public_names:
if len(public_names) == 1:
self.writeline('context.exported_vars.add(%r)' %
public_names[0])
else:
self.writeline('context.exported_vars.update((%s))' %
', '.join(imap(repr, public_names)))
|
[
"def",
"pop_assign_tracking",
"(",
"self",
",",
"frame",
")",
":",
"vars",
"=",
"self",
".",
"_assign_stack",
".",
"pop",
"(",
")",
"if",
"not",
"frame",
".",
"toplevel",
"or",
"not",
"vars",
":",
"return",
"public_names",
"=",
"[",
"x",
"for",
"x",
"in",
"vars",
"if",
"x",
"[",
":",
"1",
"]",
"!=",
"'_'",
"]",
"if",
"len",
"(",
"vars",
")",
"==",
"1",
":",
"name",
"=",
"next",
"(",
"iter",
"(",
"vars",
")",
")",
"ref",
"=",
"frame",
".",
"symbols",
".",
"ref",
"(",
"name",
")",
"self",
".",
"writeline",
"(",
"'context.vars[%r] = %s'",
"%",
"(",
"name",
",",
"ref",
")",
")",
"else",
":",
"self",
".",
"writeline",
"(",
"'context.vars.update({'",
")",
"for",
"idx",
",",
"name",
"in",
"enumerate",
"(",
"vars",
")",
":",
"if",
"idx",
":",
"self",
".",
"write",
"(",
"', '",
")",
"ref",
"=",
"frame",
".",
"symbols",
".",
"ref",
"(",
"name",
")",
"self",
".",
"write",
"(",
"'%r: %s'",
"%",
"(",
"name",
",",
"ref",
")",
")",
"self",
".",
"write",
"(",
"'})'",
")",
"if",
"public_names",
":",
"if",
"len",
"(",
"public_names",
")",
"==",
"1",
":",
"self",
".",
"writeline",
"(",
"'context.exported_vars.add(%r)'",
"%",
"public_names",
"[",
"0",
"]",
")",
"else",
":",
"self",
".",
"writeline",
"(",
"'context.exported_vars.update((%s))'",
"%",
"', '",
".",
"join",
"(",
"imap",
"(",
"repr",
",",
"public_names",
")",
")",
")"
] |
Pops the topmost level for assignment tracking and updates the
context variables if necessary.
|
[
"Pops",
"the",
"topmost",
"level",
"for",
"assignment",
"tracking",
"and",
"updates",
"the",
"context",
"variables",
"if",
"necessary",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/jinja2/compiler.py#L665-L691
|
train
|
pypa/pipenv
|
pipenv/vendor/jinja2/compiler.py
|
CodeGenerator.visit_Block
|
def visit_Block(self, node, frame):
"""Call a block and register it for the template."""
level = 0
if frame.toplevel:
# if we know that we are a child template, there is no need to
# check if we are one
if self.has_known_extends:
return
if self.extends_so_far > 0:
self.writeline('if parent_template is None:')
self.indent()
level += 1
if node.scoped:
context = self.derive_context(frame)
else:
context = self.get_context_ref()
if supports_yield_from and not self.environment.is_async and \
frame.buffer is None:
self.writeline('yield from context.blocks[%r][0](%s)' % (
node.name, context), node)
else:
loop = self.environment.is_async and 'async for' or 'for'
self.writeline('%s event in context.blocks[%r][0](%s):' % (
loop, node.name, context), node)
self.indent()
self.simple_write('event', frame)
self.outdent()
self.outdent(level)
|
python
|
def visit_Block(self, node, frame):
"""Call a block and register it for the template."""
level = 0
if frame.toplevel:
# if we know that we are a child template, there is no need to
# check if we are one
if self.has_known_extends:
return
if self.extends_so_far > 0:
self.writeline('if parent_template is None:')
self.indent()
level += 1
if node.scoped:
context = self.derive_context(frame)
else:
context = self.get_context_ref()
if supports_yield_from and not self.environment.is_async and \
frame.buffer is None:
self.writeline('yield from context.blocks[%r][0](%s)' % (
node.name, context), node)
else:
loop = self.environment.is_async and 'async for' or 'for'
self.writeline('%s event in context.blocks[%r][0](%s):' % (
loop, node.name, context), node)
self.indent()
self.simple_write('event', frame)
self.outdent()
self.outdent(level)
|
[
"def",
"visit_Block",
"(",
"self",
",",
"node",
",",
"frame",
")",
":",
"level",
"=",
"0",
"if",
"frame",
".",
"toplevel",
":",
"# if we know that we are a child template, there is no need to",
"# check if we are one",
"if",
"self",
".",
"has_known_extends",
":",
"return",
"if",
"self",
".",
"extends_so_far",
">",
"0",
":",
"self",
".",
"writeline",
"(",
"'if parent_template is None:'",
")",
"self",
".",
"indent",
"(",
")",
"level",
"+=",
"1",
"if",
"node",
".",
"scoped",
":",
"context",
"=",
"self",
".",
"derive_context",
"(",
"frame",
")",
"else",
":",
"context",
"=",
"self",
".",
"get_context_ref",
"(",
")",
"if",
"supports_yield_from",
"and",
"not",
"self",
".",
"environment",
".",
"is_async",
"and",
"frame",
".",
"buffer",
"is",
"None",
":",
"self",
".",
"writeline",
"(",
"'yield from context.blocks[%r][0](%s)'",
"%",
"(",
"node",
".",
"name",
",",
"context",
")",
",",
"node",
")",
"else",
":",
"loop",
"=",
"self",
".",
"environment",
".",
"is_async",
"and",
"'async for'",
"or",
"'for'",
"self",
".",
"writeline",
"(",
"'%s event in context.blocks[%r][0](%s):'",
"%",
"(",
"loop",
",",
"node",
".",
"name",
",",
"context",
")",
",",
"node",
")",
"self",
".",
"indent",
"(",
")",
"self",
".",
"simple_write",
"(",
"'event'",
",",
"frame",
")",
"self",
".",
"outdent",
"(",
")",
"self",
".",
"outdent",
"(",
"level",
")"
] |
Call a block and register it for the template.
|
[
"Call",
"a",
"block",
"and",
"register",
"it",
"for",
"the",
"template",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/jinja2/compiler.py#L811-L841
|
train
|
pypa/pipenv
|
pipenv/vendor/jinja2/compiler.py
|
CodeGenerator.visit_Extends
|
def visit_Extends(self, node, frame):
"""Calls the extender."""
if not frame.toplevel:
self.fail('cannot use extend from a non top-level scope',
node.lineno)
# if the number of extends statements in general is zero so
# far, we don't have to add a check if something extended
# the template before this one.
if self.extends_so_far > 0:
# if we have a known extends we just add a template runtime
# error into the generated code. We could catch that at compile
# time too, but i welcome it not to confuse users by throwing the
# same error at different times just "because we can".
if not self.has_known_extends:
self.writeline('if parent_template is not None:')
self.indent()
self.writeline('raise TemplateRuntimeError(%r)' %
'extended multiple times')
# if we have a known extends already we don't need that code here
# as we know that the template execution will end here.
if self.has_known_extends:
raise CompilerExit()
else:
self.outdent()
self.writeline('parent_template = environment.get_template(', node)
self.visit(node.template, frame)
self.write(', %r)' % self.name)
self.writeline('for name, parent_block in parent_template.'
'blocks.%s():' % dict_item_iter)
self.indent()
self.writeline('context.blocks.setdefault(name, []).'
'append(parent_block)')
self.outdent()
# if this extends statement was in the root level we can take
# advantage of that information and simplify the generated code
# in the top level from this point onwards
if frame.rootlevel:
self.has_known_extends = True
# and now we have one more
self.extends_so_far += 1
|
python
|
def visit_Extends(self, node, frame):
"""Calls the extender."""
if not frame.toplevel:
self.fail('cannot use extend from a non top-level scope',
node.lineno)
# if the number of extends statements in general is zero so
# far, we don't have to add a check if something extended
# the template before this one.
if self.extends_so_far > 0:
# if we have a known extends we just add a template runtime
# error into the generated code. We could catch that at compile
# time too, but i welcome it not to confuse users by throwing the
# same error at different times just "because we can".
if not self.has_known_extends:
self.writeline('if parent_template is not None:')
self.indent()
self.writeline('raise TemplateRuntimeError(%r)' %
'extended multiple times')
# if we have a known extends already we don't need that code here
# as we know that the template execution will end here.
if self.has_known_extends:
raise CompilerExit()
else:
self.outdent()
self.writeline('parent_template = environment.get_template(', node)
self.visit(node.template, frame)
self.write(', %r)' % self.name)
self.writeline('for name, parent_block in parent_template.'
'blocks.%s():' % dict_item_iter)
self.indent()
self.writeline('context.blocks.setdefault(name, []).'
'append(parent_block)')
self.outdent()
# if this extends statement was in the root level we can take
# advantage of that information and simplify the generated code
# in the top level from this point onwards
if frame.rootlevel:
self.has_known_extends = True
# and now we have one more
self.extends_so_far += 1
|
[
"def",
"visit_Extends",
"(",
"self",
",",
"node",
",",
"frame",
")",
":",
"if",
"not",
"frame",
".",
"toplevel",
":",
"self",
".",
"fail",
"(",
"'cannot use extend from a non top-level scope'",
",",
"node",
".",
"lineno",
")",
"# if the number of extends statements in general is zero so",
"# far, we don't have to add a check if something extended",
"# the template before this one.",
"if",
"self",
".",
"extends_so_far",
">",
"0",
":",
"# if we have a known extends we just add a template runtime",
"# error into the generated code. We could catch that at compile",
"# time too, but i welcome it not to confuse users by throwing the",
"# same error at different times just \"because we can\".",
"if",
"not",
"self",
".",
"has_known_extends",
":",
"self",
".",
"writeline",
"(",
"'if parent_template is not None:'",
")",
"self",
".",
"indent",
"(",
")",
"self",
".",
"writeline",
"(",
"'raise TemplateRuntimeError(%r)'",
"%",
"'extended multiple times'",
")",
"# if we have a known extends already we don't need that code here",
"# as we know that the template execution will end here.",
"if",
"self",
".",
"has_known_extends",
":",
"raise",
"CompilerExit",
"(",
")",
"else",
":",
"self",
".",
"outdent",
"(",
")",
"self",
".",
"writeline",
"(",
"'parent_template = environment.get_template('",
",",
"node",
")",
"self",
".",
"visit",
"(",
"node",
".",
"template",
",",
"frame",
")",
"self",
".",
"write",
"(",
"', %r)'",
"%",
"self",
".",
"name",
")",
"self",
".",
"writeline",
"(",
"'for name, parent_block in parent_template.'",
"'blocks.%s():'",
"%",
"dict_item_iter",
")",
"self",
".",
"indent",
"(",
")",
"self",
".",
"writeline",
"(",
"'context.blocks.setdefault(name, []).'",
"'append(parent_block)'",
")",
"self",
".",
"outdent",
"(",
")",
"# if this extends statement was in the root level we can take",
"# advantage of that information and simplify the generated code",
"# in the top level from this point onwards",
"if",
"frame",
".",
"rootlevel",
":",
"self",
".",
"has_known_extends",
"=",
"True",
"# and now we have one more",
"self",
".",
"extends_so_far",
"+=",
"1"
] |
Calls the extender.
|
[
"Calls",
"the",
"extender",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/jinja2/compiler.py#L843-L888
|
train
|
pypa/pipenv
|
pipenv/vendor/jinja2/compiler.py
|
CodeGenerator.visit_Include
|
def visit_Include(self, node, frame):
"""Handles includes."""
if node.ignore_missing:
self.writeline('try:')
self.indent()
func_name = 'get_or_select_template'
if isinstance(node.template, nodes.Const):
if isinstance(node.template.value, string_types):
func_name = 'get_template'
elif isinstance(node.template.value, (tuple, list)):
func_name = 'select_template'
elif isinstance(node.template, (nodes.Tuple, nodes.List)):
func_name = 'select_template'
self.writeline('template = environment.%s(' % func_name, node)
self.visit(node.template, frame)
self.write(', %r)' % self.name)
if node.ignore_missing:
self.outdent()
self.writeline('except TemplateNotFound:')
self.indent()
self.writeline('pass')
self.outdent()
self.writeline('else:')
self.indent()
skip_event_yield = False
if node.with_context:
loop = self.environment.is_async and 'async for' or 'for'
self.writeline('%s event in template.root_render_func('
'template.new_context(context.get_all(), True, '
'%s)):' % (loop, self.dump_local_context(frame)))
elif self.environment.is_async:
self.writeline('for event in (await '
'template._get_default_module_async())'
'._body_stream:')
else:
if supports_yield_from:
self.writeline('yield from template._get_default_module()'
'._body_stream')
skip_event_yield = True
else:
self.writeline('for event in template._get_default_module()'
'._body_stream:')
if not skip_event_yield:
self.indent()
self.simple_write('event', frame)
self.outdent()
if node.ignore_missing:
self.outdent()
|
python
|
def visit_Include(self, node, frame):
"""Handles includes."""
if node.ignore_missing:
self.writeline('try:')
self.indent()
func_name = 'get_or_select_template'
if isinstance(node.template, nodes.Const):
if isinstance(node.template.value, string_types):
func_name = 'get_template'
elif isinstance(node.template.value, (tuple, list)):
func_name = 'select_template'
elif isinstance(node.template, (nodes.Tuple, nodes.List)):
func_name = 'select_template'
self.writeline('template = environment.%s(' % func_name, node)
self.visit(node.template, frame)
self.write(', %r)' % self.name)
if node.ignore_missing:
self.outdent()
self.writeline('except TemplateNotFound:')
self.indent()
self.writeline('pass')
self.outdent()
self.writeline('else:')
self.indent()
skip_event_yield = False
if node.with_context:
loop = self.environment.is_async and 'async for' or 'for'
self.writeline('%s event in template.root_render_func('
'template.new_context(context.get_all(), True, '
'%s)):' % (loop, self.dump_local_context(frame)))
elif self.environment.is_async:
self.writeline('for event in (await '
'template._get_default_module_async())'
'._body_stream:')
else:
if supports_yield_from:
self.writeline('yield from template._get_default_module()'
'._body_stream')
skip_event_yield = True
else:
self.writeline('for event in template._get_default_module()'
'._body_stream:')
if not skip_event_yield:
self.indent()
self.simple_write('event', frame)
self.outdent()
if node.ignore_missing:
self.outdent()
|
[
"def",
"visit_Include",
"(",
"self",
",",
"node",
",",
"frame",
")",
":",
"if",
"node",
".",
"ignore_missing",
":",
"self",
".",
"writeline",
"(",
"'try:'",
")",
"self",
".",
"indent",
"(",
")",
"func_name",
"=",
"'get_or_select_template'",
"if",
"isinstance",
"(",
"node",
".",
"template",
",",
"nodes",
".",
"Const",
")",
":",
"if",
"isinstance",
"(",
"node",
".",
"template",
".",
"value",
",",
"string_types",
")",
":",
"func_name",
"=",
"'get_template'",
"elif",
"isinstance",
"(",
"node",
".",
"template",
".",
"value",
",",
"(",
"tuple",
",",
"list",
")",
")",
":",
"func_name",
"=",
"'select_template'",
"elif",
"isinstance",
"(",
"node",
".",
"template",
",",
"(",
"nodes",
".",
"Tuple",
",",
"nodes",
".",
"List",
")",
")",
":",
"func_name",
"=",
"'select_template'",
"self",
".",
"writeline",
"(",
"'template = environment.%s('",
"%",
"func_name",
",",
"node",
")",
"self",
".",
"visit",
"(",
"node",
".",
"template",
",",
"frame",
")",
"self",
".",
"write",
"(",
"', %r)'",
"%",
"self",
".",
"name",
")",
"if",
"node",
".",
"ignore_missing",
":",
"self",
".",
"outdent",
"(",
")",
"self",
".",
"writeline",
"(",
"'except TemplateNotFound:'",
")",
"self",
".",
"indent",
"(",
")",
"self",
".",
"writeline",
"(",
"'pass'",
")",
"self",
".",
"outdent",
"(",
")",
"self",
".",
"writeline",
"(",
"'else:'",
")",
"self",
".",
"indent",
"(",
")",
"skip_event_yield",
"=",
"False",
"if",
"node",
".",
"with_context",
":",
"loop",
"=",
"self",
".",
"environment",
".",
"is_async",
"and",
"'async for'",
"or",
"'for'",
"self",
".",
"writeline",
"(",
"'%s event in template.root_render_func('",
"'template.new_context(context.get_all(), True, '",
"'%s)):'",
"%",
"(",
"loop",
",",
"self",
".",
"dump_local_context",
"(",
"frame",
")",
")",
")",
"elif",
"self",
".",
"environment",
".",
"is_async",
":",
"self",
".",
"writeline",
"(",
"'for event in (await '",
"'template._get_default_module_async())'",
"'._body_stream:'",
")",
"else",
":",
"if",
"supports_yield_from",
":",
"self",
".",
"writeline",
"(",
"'yield from template._get_default_module()'",
"'._body_stream'",
")",
"skip_event_yield",
"=",
"True",
"else",
":",
"self",
".",
"writeline",
"(",
"'for event in template._get_default_module()'",
"'._body_stream:'",
")",
"if",
"not",
"skip_event_yield",
":",
"self",
".",
"indent",
"(",
")",
"self",
".",
"simple_write",
"(",
"'event'",
",",
"frame",
")",
"self",
".",
"outdent",
"(",
")",
"if",
"node",
".",
"ignore_missing",
":",
"self",
".",
"outdent",
"(",
")"
] |
Handles includes.
|
[
"Handles",
"includes",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/jinja2/compiler.py#L890-L942
|
train
|
pypa/pipenv
|
pipenv/vendor/jinja2/compiler.py
|
CodeGenerator.visit_Import
|
def visit_Import(self, node, frame):
"""Visit regular imports."""
self.writeline('%s = ' % frame.symbols.ref(node.target), node)
if frame.toplevel:
self.write('context.vars[%r] = ' % node.target)
if self.environment.is_async:
self.write('await ')
self.write('environment.get_template(')
self.visit(node.template, frame)
self.write(', %r).' % self.name)
if node.with_context:
self.write('make_module%s(context.get_all(), True, %s)'
% (self.environment.is_async and '_async' or '',
self.dump_local_context(frame)))
elif self.environment.is_async:
self.write('_get_default_module_async()')
else:
self.write('_get_default_module()')
if frame.toplevel and not node.target.startswith('_'):
self.writeline('context.exported_vars.discard(%r)' % node.target)
|
python
|
def visit_Import(self, node, frame):
"""Visit regular imports."""
self.writeline('%s = ' % frame.symbols.ref(node.target), node)
if frame.toplevel:
self.write('context.vars[%r] = ' % node.target)
if self.environment.is_async:
self.write('await ')
self.write('environment.get_template(')
self.visit(node.template, frame)
self.write(', %r).' % self.name)
if node.with_context:
self.write('make_module%s(context.get_all(), True, %s)'
% (self.environment.is_async and '_async' or '',
self.dump_local_context(frame)))
elif self.environment.is_async:
self.write('_get_default_module_async()')
else:
self.write('_get_default_module()')
if frame.toplevel and not node.target.startswith('_'):
self.writeline('context.exported_vars.discard(%r)' % node.target)
|
[
"def",
"visit_Import",
"(",
"self",
",",
"node",
",",
"frame",
")",
":",
"self",
".",
"writeline",
"(",
"'%s = '",
"%",
"frame",
".",
"symbols",
".",
"ref",
"(",
"node",
".",
"target",
")",
",",
"node",
")",
"if",
"frame",
".",
"toplevel",
":",
"self",
".",
"write",
"(",
"'context.vars[%r] = '",
"%",
"node",
".",
"target",
")",
"if",
"self",
".",
"environment",
".",
"is_async",
":",
"self",
".",
"write",
"(",
"'await '",
")",
"self",
".",
"write",
"(",
"'environment.get_template('",
")",
"self",
".",
"visit",
"(",
"node",
".",
"template",
",",
"frame",
")",
"self",
".",
"write",
"(",
"', %r).'",
"%",
"self",
".",
"name",
")",
"if",
"node",
".",
"with_context",
":",
"self",
".",
"write",
"(",
"'make_module%s(context.get_all(), True, %s)'",
"%",
"(",
"self",
".",
"environment",
".",
"is_async",
"and",
"'_async'",
"or",
"''",
",",
"self",
".",
"dump_local_context",
"(",
"frame",
")",
")",
")",
"elif",
"self",
".",
"environment",
".",
"is_async",
":",
"self",
".",
"write",
"(",
"'_get_default_module_async()'",
")",
"else",
":",
"self",
".",
"write",
"(",
"'_get_default_module()'",
")",
"if",
"frame",
".",
"toplevel",
"and",
"not",
"node",
".",
"target",
".",
"startswith",
"(",
"'_'",
")",
":",
"self",
".",
"writeline",
"(",
"'context.exported_vars.discard(%r)'",
"%",
"node",
".",
"target",
")"
] |
Visit regular imports.
|
[
"Visit",
"regular",
"imports",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/jinja2/compiler.py#L944-L963
|
train
|
pypa/pipenv
|
pipenv/vendor/jinja2/compiler.py
|
CodeGenerator.visit_FromImport
|
def visit_FromImport(self, node, frame):
"""Visit named imports."""
self.newline(node)
self.write('included_template = %senvironment.get_template('
% (self.environment.is_async and 'await ' or ''))
self.visit(node.template, frame)
self.write(', %r).' % self.name)
if node.with_context:
self.write('make_module%s(context.get_all(), True, %s)'
% (self.environment.is_async and '_async' or '',
self.dump_local_context(frame)))
elif self.environment.is_async:
self.write('_get_default_module_async()')
else:
self.write('_get_default_module()')
var_names = []
discarded_names = []
for name in node.names:
if isinstance(name, tuple):
name, alias = name
else:
alias = name
self.writeline('%s = getattr(included_template, '
'%r, missing)' % (frame.symbols.ref(alias), name))
self.writeline('if %s is missing:' % frame.symbols.ref(alias))
self.indent()
self.writeline('%s = undefined(%r %% '
'included_template.__name__, '
'name=%r)' %
(frame.symbols.ref(alias),
'the template %%r (imported on %s) does '
'not export the requested name %s' % (
self.position(node),
repr(name)
), name))
self.outdent()
if frame.toplevel:
var_names.append(alias)
if not alias.startswith('_'):
discarded_names.append(alias)
if var_names:
if len(var_names) == 1:
name = var_names[0]
self.writeline('context.vars[%r] = %s' %
(name, frame.symbols.ref(name)))
else:
self.writeline('context.vars.update({%s})' % ', '.join(
'%r: %s' % (name, frame.symbols.ref(name)) for name in var_names
))
if discarded_names:
if len(discarded_names) == 1:
self.writeline('context.exported_vars.discard(%r)' %
discarded_names[0])
else:
self.writeline('context.exported_vars.difference_'
'update((%s))' % ', '.join(imap(repr, discarded_names)))
|
python
|
def visit_FromImport(self, node, frame):
"""Visit named imports."""
self.newline(node)
self.write('included_template = %senvironment.get_template('
% (self.environment.is_async and 'await ' or ''))
self.visit(node.template, frame)
self.write(', %r).' % self.name)
if node.with_context:
self.write('make_module%s(context.get_all(), True, %s)'
% (self.environment.is_async and '_async' or '',
self.dump_local_context(frame)))
elif self.environment.is_async:
self.write('_get_default_module_async()')
else:
self.write('_get_default_module()')
var_names = []
discarded_names = []
for name in node.names:
if isinstance(name, tuple):
name, alias = name
else:
alias = name
self.writeline('%s = getattr(included_template, '
'%r, missing)' % (frame.symbols.ref(alias), name))
self.writeline('if %s is missing:' % frame.symbols.ref(alias))
self.indent()
self.writeline('%s = undefined(%r %% '
'included_template.__name__, '
'name=%r)' %
(frame.symbols.ref(alias),
'the template %%r (imported on %s) does '
'not export the requested name %s' % (
self.position(node),
repr(name)
), name))
self.outdent()
if frame.toplevel:
var_names.append(alias)
if not alias.startswith('_'):
discarded_names.append(alias)
if var_names:
if len(var_names) == 1:
name = var_names[0]
self.writeline('context.vars[%r] = %s' %
(name, frame.symbols.ref(name)))
else:
self.writeline('context.vars.update({%s})' % ', '.join(
'%r: %s' % (name, frame.symbols.ref(name)) for name in var_names
))
if discarded_names:
if len(discarded_names) == 1:
self.writeline('context.exported_vars.discard(%r)' %
discarded_names[0])
else:
self.writeline('context.exported_vars.difference_'
'update((%s))' % ', '.join(imap(repr, discarded_names)))
|
[
"def",
"visit_FromImport",
"(",
"self",
",",
"node",
",",
"frame",
")",
":",
"self",
".",
"newline",
"(",
"node",
")",
"self",
".",
"write",
"(",
"'included_template = %senvironment.get_template('",
"%",
"(",
"self",
".",
"environment",
".",
"is_async",
"and",
"'await '",
"or",
"''",
")",
")",
"self",
".",
"visit",
"(",
"node",
".",
"template",
",",
"frame",
")",
"self",
".",
"write",
"(",
"', %r).'",
"%",
"self",
".",
"name",
")",
"if",
"node",
".",
"with_context",
":",
"self",
".",
"write",
"(",
"'make_module%s(context.get_all(), True, %s)'",
"%",
"(",
"self",
".",
"environment",
".",
"is_async",
"and",
"'_async'",
"or",
"''",
",",
"self",
".",
"dump_local_context",
"(",
"frame",
")",
")",
")",
"elif",
"self",
".",
"environment",
".",
"is_async",
":",
"self",
".",
"write",
"(",
"'_get_default_module_async()'",
")",
"else",
":",
"self",
".",
"write",
"(",
"'_get_default_module()'",
")",
"var_names",
"=",
"[",
"]",
"discarded_names",
"=",
"[",
"]",
"for",
"name",
"in",
"node",
".",
"names",
":",
"if",
"isinstance",
"(",
"name",
",",
"tuple",
")",
":",
"name",
",",
"alias",
"=",
"name",
"else",
":",
"alias",
"=",
"name",
"self",
".",
"writeline",
"(",
"'%s = getattr(included_template, '",
"'%r, missing)'",
"%",
"(",
"frame",
".",
"symbols",
".",
"ref",
"(",
"alias",
")",
",",
"name",
")",
")",
"self",
".",
"writeline",
"(",
"'if %s is missing:'",
"%",
"frame",
".",
"symbols",
".",
"ref",
"(",
"alias",
")",
")",
"self",
".",
"indent",
"(",
")",
"self",
".",
"writeline",
"(",
"'%s = undefined(%r %% '",
"'included_template.__name__, '",
"'name=%r)'",
"%",
"(",
"frame",
".",
"symbols",
".",
"ref",
"(",
"alias",
")",
",",
"'the template %%r (imported on %s) does '",
"'not export the requested name %s'",
"%",
"(",
"self",
".",
"position",
"(",
"node",
")",
",",
"repr",
"(",
"name",
")",
")",
",",
"name",
")",
")",
"self",
".",
"outdent",
"(",
")",
"if",
"frame",
".",
"toplevel",
":",
"var_names",
".",
"append",
"(",
"alias",
")",
"if",
"not",
"alias",
".",
"startswith",
"(",
"'_'",
")",
":",
"discarded_names",
".",
"append",
"(",
"alias",
")",
"if",
"var_names",
":",
"if",
"len",
"(",
"var_names",
")",
"==",
"1",
":",
"name",
"=",
"var_names",
"[",
"0",
"]",
"self",
".",
"writeline",
"(",
"'context.vars[%r] = %s'",
"%",
"(",
"name",
",",
"frame",
".",
"symbols",
".",
"ref",
"(",
"name",
")",
")",
")",
"else",
":",
"self",
".",
"writeline",
"(",
"'context.vars.update({%s})'",
"%",
"', '",
".",
"join",
"(",
"'%r: %s'",
"%",
"(",
"name",
",",
"frame",
".",
"symbols",
".",
"ref",
"(",
"name",
")",
")",
"for",
"name",
"in",
"var_names",
")",
")",
"if",
"discarded_names",
":",
"if",
"len",
"(",
"discarded_names",
")",
"==",
"1",
":",
"self",
".",
"writeline",
"(",
"'context.exported_vars.discard(%r)'",
"%",
"discarded_names",
"[",
"0",
"]",
")",
"else",
":",
"self",
".",
"writeline",
"(",
"'context.exported_vars.difference_'",
"'update((%s))'",
"%",
"', '",
".",
"join",
"(",
"imap",
"(",
"repr",
",",
"discarded_names",
")",
")",
")"
] |
Visit named imports.
|
[
"Visit",
"named",
"imports",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/jinja2/compiler.py#L965-L1022
|
train
|
pypa/pipenv
|
pipenv/vendor/backports/weakref.py
|
finalize.detach
|
def detach(self):
"""If alive then mark as dead and return (obj, func, args, kwargs);
otherwise return None"""
info = self._registry.get(self)
obj = info and info.weakref()
if obj is not None and self._registry.pop(self, None):
return (obj, info.func, info.args, info.kwargs or {})
|
python
|
def detach(self):
"""If alive then mark as dead and return (obj, func, args, kwargs);
otherwise return None"""
info = self._registry.get(self)
obj = info and info.weakref()
if obj is not None and self._registry.pop(self, None):
return (obj, info.func, info.args, info.kwargs or {})
|
[
"def",
"detach",
"(",
"self",
")",
":",
"info",
"=",
"self",
".",
"_registry",
".",
"get",
"(",
"self",
")",
"obj",
"=",
"info",
"and",
"info",
".",
"weakref",
"(",
")",
"if",
"obj",
"is",
"not",
"None",
"and",
"self",
".",
"_registry",
".",
"pop",
"(",
"self",
",",
"None",
")",
":",
"return",
"(",
"obj",
",",
"info",
".",
"func",
",",
"info",
".",
"args",
",",
"info",
".",
"kwargs",
"or",
"{",
"}",
")"
] |
If alive then mark as dead and return (obj, func, args, kwargs);
otherwise return None
|
[
"If",
"alive",
"then",
"mark",
"as",
"dead",
"and",
"return",
"(",
"obj",
"func",
"args",
"kwargs",
")",
";",
"otherwise",
"return",
"None"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/backports/weakref.py#L69-L75
|
train
|
pypa/pipenv
|
pipenv/vendor/backports/weakref.py
|
finalize.peek
|
def peek(self):
"""If alive then return (obj, func, args, kwargs);
otherwise return None"""
info = self._registry.get(self)
obj = info and info.weakref()
if obj is not None:
return (obj, info.func, info.args, info.kwargs or {})
|
python
|
def peek(self):
"""If alive then return (obj, func, args, kwargs);
otherwise return None"""
info = self._registry.get(self)
obj = info and info.weakref()
if obj is not None:
return (obj, info.func, info.args, info.kwargs or {})
|
[
"def",
"peek",
"(",
"self",
")",
":",
"info",
"=",
"self",
".",
"_registry",
".",
"get",
"(",
"self",
")",
"obj",
"=",
"info",
"and",
"info",
".",
"weakref",
"(",
")",
"if",
"obj",
"is",
"not",
"None",
":",
"return",
"(",
"obj",
",",
"info",
".",
"func",
",",
"info",
".",
"args",
",",
"info",
".",
"kwargs",
"or",
"{",
"}",
")"
] |
If alive then return (obj, func, args, kwargs);
otherwise return None
|
[
"If",
"alive",
"then",
"return",
"(",
"obj",
"func",
"args",
"kwargs",
")",
";",
"otherwise",
"return",
"None"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/backports/weakref.py#L77-L83
|
train
|
pypa/pipenv
|
pipenv/vendor/backports/weakref.py
|
finalize.atexit
|
def atexit(self):
"""Whether finalizer should be called at exit"""
info = self._registry.get(self)
return bool(info) and info.atexit
|
python
|
def atexit(self):
"""Whether finalizer should be called at exit"""
info = self._registry.get(self)
return bool(info) and info.atexit
|
[
"def",
"atexit",
"(",
"self",
")",
":",
"info",
"=",
"self",
".",
"_registry",
".",
"get",
"(",
"self",
")",
"return",
"bool",
"(",
"info",
")",
"and",
"info",
".",
"atexit"
] |
Whether finalizer should be called at exit
|
[
"Whether",
"finalizer",
"should",
"be",
"called",
"at",
"exit"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/backports/weakref.py#L91-L94
|
train
|
pypa/pipenv
|
pipenv/patched/notpip/_vendor/html5lib/treebuilders/etree_lxml.py
|
tostring
|
def tostring(element):
"""Serialize an element and its child nodes to a string"""
rv = []
def serializeElement(element):
if not hasattr(element, "tag"):
if element.docinfo.internalDTD:
if element.docinfo.doctype:
dtd_str = element.docinfo.doctype
else:
dtd_str = "<!DOCTYPE %s>" % element.docinfo.root_name
rv.append(dtd_str)
serializeElement(element.getroot())
elif element.tag == comment_type:
rv.append("<!--%s-->" % (element.text,))
else:
# This is assumed to be an ordinary element
if not element.attrib:
rv.append("<%s>" % (element.tag,))
else:
attr = " ".join(["%s=\"%s\"" % (name, value)
for name, value in element.attrib.items()])
rv.append("<%s %s>" % (element.tag, attr))
if element.text:
rv.append(element.text)
for child in element:
serializeElement(child)
rv.append("</%s>" % (element.tag,))
if hasattr(element, "tail") and element.tail:
rv.append(element.tail)
serializeElement(element)
return "".join(rv)
|
python
|
def tostring(element):
"""Serialize an element and its child nodes to a string"""
rv = []
def serializeElement(element):
if not hasattr(element, "tag"):
if element.docinfo.internalDTD:
if element.docinfo.doctype:
dtd_str = element.docinfo.doctype
else:
dtd_str = "<!DOCTYPE %s>" % element.docinfo.root_name
rv.append(dtd_str)
serializeElement(element.getroot())
elif element.tag == comment_type:
rv.append("<!--%s-->" % (element.text,))
else:
# This is assumed to be an ordinary element
if not element.attrib:
rv.append("<%s>" % (element.tag,))
else:
attr = " ".join(["%s=\"%s\"" % (name, value)
for name, value in element.attrib.items()])
rv.append("<%s %s>" % (element.tag, attr))
if element.text:
rv.append(element.text)
for child in element:
serializeElement(child)
rv.append("</%s>" % (element.tag,))
if hasattr(element, "tail") and element.tail:
rv.append(element.tail)
serializeElement(element)
return "".join(rv)
|
[
"def",
"tostring",
"(",
"element",
")",
":",
"rv",
"=",
"[",
"]",
"def",
"serializeElement",
"(",
"element",
")",
":",
"if",
"not",
"hasattr",
"(",
"element",
",",
"\"tag\"",
")",
":",
"if",
"element",
".",
"docinfo",
".",
"internalDTD",
":",
"if",
"element",
".",
"docinfo",
".",
"doctype",
":",
"dtd_str",
"=",
"element",
".",
"docinfo",
".",
"doctype",
"else",
":",
"dtd_str",
"=",
"\"<!DOCTYPE %s>\"",
"%",
"element",
".",
"docinfo",
".",
"root_name",
"rv",
".",
"append",
"(",
"dtd_str",
")",
"serializeElement",
"(",
"element",
".",
"getroot",
"(",
")",
")",
"elif",
"element",
".",
"tag",
"==",
"comment_type",
":",
"rv",
".",
"append",
"(",
"\"<!--%s-->\"",
"%",
"(",
"element",
".",
"text",
",",
")",
")",
"else",
":",
"# This is assumed to be an ordinary element",
"if",
"not",
"element",
".",
"attrib",
":",
"rv",
".",
"append",
"(",
"\"<%s>\"",
"%",
"(",
"element",
".",
"tag",
",",
")",
")",
"else",
":",
"attr",
"=",
"\" \"",
".",
"join",
"(",
"[",
"\"%s=\\\"%s\\\"\"",
"%",
"(",
"name",
",",
"value",
")",
"for",
"name",
",",
"value",
"in",
"element",
".",
"attrib",
".",
"items",
"(",
")",
"]",
")",
"rv",
".",
"append",
"(",
"\"<%s %s>\"",
"%",
"(",
"element",
".",
"tag",
",",
"attr",
")",
")",
"if",
"element",
".",
"text",
":",
"rv",
".",
"append",
"(",
"element",
".",
"text",
")",
"for",
"child",
"in",
"element",
":",
"serializeElement",
"(",
"child",
")",
"rv",
".",
"append",
"(",
"\"</%s>\"",
"%",
"(",
"element",
".",
"tag",
",",
")",
")",
"if",
"hasattr",
"(",
"element",
",",
"\"tail\"",
")",
"and",
"element",
".",
"tail",
":",
"rv",
".",
"append",
"(",
"element",
".",
"tail",
")",
"serializeElement",
"(",
"element",
")",
"return",
"\"\"",
".",
"join",
"(",
"rv",
")"
] |
Serialize an element and its child nodes to a string
|
[
"Serialize",
"an",
"element",
"and",
"its",
"child",
"nodes",
"to",
"a",
"string"
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/patched/notpip/_vendor/html5lib/treebuilders/etree_lxml.py#L134-L172
|
train
|
pypa/pipenv
|
pipenv/vendor/jinja2/visitor.py
|
NodeVisitor.get_visitor
|
def get_visitor(self, node):
"""Return the visitor function for this node or `None` if no visitor
exists for this node. In that case the generic visit function is
used instead.
"""
method = 'visit_' + node.__class__.__name__
return getattr(self, method, None)
|
python
|
def get_visitor(self, node):
"""Return the visitor function for this node or `None` if no visitor
exists for this node. In that case the generic visit function is
used instead.
"""
method = 'visit_' + node.__class__.__name__
return getattr(self, method, None)
|
[
"def",
"get_visitor",
"(",
"self",
",",
"node",
")",
":",
"method",
"=",
"'visit_'",
"+",
"node",
".",
"__class__",
".",
"__name__",
"return",
"getattr",
"(",
"self",
",",
"method",
",",
"None",
")"
] |
Return the visitor function for this node or `None` if no visitor
exists for this node. In that case the generic visit function is
used instead.
|
[
"Return",
"the",
"visitor",
"function",
"for",
"this",
"node",
"or",
"None",
"if",
"no",
"visitor",
"exists",
"for",
"this",
"node",
".",
"In",
"that",
"case",
"the",
"generic",
"visit",
"function",
"is",
"used",
"instead",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/jinja2/visitor.py#L26-L32
|
train
|
pypa/pipenv
|
pipenv/vendor/jinja2/visitor.py
|
NodeVisitor.visit
|
def visit(self, node, *args, **kwargs):
"""Visit a node."""
f = self.get_visitor(node)
if f is not None:
return f(node, *args, **kwargs)
return self.generic_visit(node, *args, **kwargs)
|
python
|
def visit(self, node, *args, **kwargs):
"""Visit a node."""
f = self.get_visitor(node)
if f is not None:
return f(node, *args, **kwargs)
return self.generic_visit(node, *args, **kwargs)
|
[
"def",
"visit",
"(",
"self",
",",
"node",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"f",
"=",
"self",
".",
"get_visitor",
"(",
"node",
")",
"if",
"f",
"is",
"not",
"None",
":",
"return",
"f",
"(",
"node",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"return",
"self",
".",
"generic_visit",
"(",
"node",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")"
] |
Visit a node.
|
[
"Visit",
"a",
"node",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/jinja2/visitor.py#L34-L39
|
train
|
pypa/pipenv
|
pipenv/vendor/jinja2/visitor.py
|
NodeVisitor.generic_visit
|
def generic_visit(self, node, *args, **kwargs):
"""Called if no explicit visitor function exists for a node."""
for node in node.iter_child_nodes():
self.visit(node, *args, **kwargs)
|
python
|
def generic_visit(self, node, *args, **kwargs):
"""Called if no explicit visitor function exists for a node."""
for node in node.iter_child_nodes():
self.visit(node, *args, **kwargs)
|
[
"def",
"generic_visit",
"(",
"self",
",",
"node",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"for",
"node",
"in",
"node",
".",
"iter_child_nodes",
"(",
")",
":",
"self",
".",
"visit",
"(",
"node",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")"
] |
Called if no explicit visitor function exists for a node.
|
[
"Called",
"if",
"no",
"explicit",
"visitor",
"function",
"exists",
"for",
"a",
"node",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/jinja2/visitor.py#L41-L44
|
train
|
pypa/pipenv
|
pipenv/vendor/jinja2/visitor.py
|
NodeTransformer.visit_list
|
def visit_list(self, node, *args, **kwargs):
"""As transformers may return lists in some places this method
can be used to enforce a list as return value.
"""
rv = self.visit(node, *args, **kwargs)
if not isinstance(rv, list):
rv = [rv]
return rv
|
python
|
def visit_list(self, node, *args, **kwargs):
"""As transformers may return lists in some places this method
can be used to enforce a list as return value.
"""
rv = self.visit(node, *args, **kwargs)
if not isinstance(rv, list):
rv = [rv]
return rv
|
[
"def",
"visit_list",
"(",
"self",
",",
"node",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"rv",
"=",
"self",
".",
"visit",
"(",
"node",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"if",
"not",
"isinstance",
"(",
"rv",
",",
"list",
")",
":",
"rv",
"=",
"[",
"rv",
"]",
"return",
"rv"
] |
As transformers may return lists in some places this method
can be used to enforce a list as return value.
|
[
"As",
"transformers",
"may",
"return",
"lists",
"in",
"some",
"places",
"this",
"method",
"can",
"be",
"used",
"to",
"enforce",
"a",
"list",
"as",
"return",
"value",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/jinja2/visitor.py#L80-L87
|
train
|
pypa/pipenv
|
pipenv/vendor/pep517/wrappers.py
|
default_subprocess_runner
|
def default_subprocess_runner(cmd, cwd=None, extra_environ=None):
"""The default method of calling the wrapper subprocess."""
env = os.environ.copy()
if extra_environ:
env.update(extra_environ)
check_call(cmd, cwd=cwd, env=env)
|
python
|
def default_subprocess_runner(cmd, cwd=None, extra_environ=None):
"""The default method of calling the wrapper subprocess."""
env = os.environ.copy()
if extra_environ:
env.update(extra_environ)
check_call(cmd, cwd=cwd, env=env)
|
[
"def",
"default_subprocess_runner",
"(",
"cmd",
",",
"cwd",
"=",
"None",
",",
"extra_environ",
"=",
"None",
")",
":",
"env",
"=",
"os",
".",
"environ",
".",
"copy",
"(",
")",
"if",
"extra_environ",
":",
"env",
".",
"update",
"(",
"extra_environ",
")",
"check_call",
"(",
"cmd",
",",
"cwd",
"=",
"cwd",
",",
"env",
"=",
"env",
")"
] |
The default method of calling the wrapper subprocess.
|
[
"The",
"default",
"method",
"of",
"calling",
"the",
"wrapper",
"subprocess",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pep517/wrappers.py#L31-L37
|
train
|
pypa/pipenv
|
pipenv/vendor/pep517/wrappers.py
|
Pep517HookCaller.build_wheel
|
def build_wheel(
self, wheel_directory, config_settings=None,
metadata_directory=None):
"""Build a wheel from this project.
Returns the name of the newly created file.
In general, this will call the 'build_wheel' hook in the backend.
However, if that was previously called by
'prepare_metadata_for_build_wheel', and the same metadata_directory is
used, the previously built wheel will be copied to wheel_directory.
"""
if metadata_directory is not None:
metadata_directory = abspath(metadata_directory)
return self._call_hook('build_wheel', {
'wheel_directory': abspath(wheel_directory),
'config_settings': config_settings,
'metadata_directory': metadata_directory,
})
|
python
|
def build_wheel(
self, wheel_directory, config_settings=None,
metadata_directory=None):
"""Build a wheel from this project.
Returns the name of the newly created file.
In general, this will call the 'build_wheel' hook in the backend.
However, if that was previously called by
'prepare_metadata_for_build_wheel', and the same metadata_directory is
used, the previously built wheel will be copied to wheel_directory.
"""
if metadata_directory is not None:
metadata_directory = abspath(metadata_directory)
return self._call_hook('build_wheel', {
'wheel_directory': abspath(wheel_directory),
'config_settings': config_settings,
'metadata_directory': metadata_directory,
})
|
[
"def",
"build_wheel",
"(",
"self",
",",
"wheel_directory",
",",
"config_settings",
"=",
"None",
",",
"metadata_directory",
"=",
"None",
")",
":",
"if",
"metadata_directory",
"is",
"not",
"None",
":",
"metadata_directory",
"=",
"abspath",
"(",
"metadata_directory",
")",
"return",
"self",
".",
"_call_hook",
"(",
"'build_wheel'",
",",
"{",
"'wheel_directory'",
":",
"abspath",
"(",
"wheel_directory",
")",
",",
"'config_settings'",
":",
"config_settings",
",",
"'metadata_directory'",
":",
"metadata_directory",
",",
"}",
")"
] |
Build a wheel from this project.
Returns the name of the newly created file.
In general, this will call the 'build_wheel' hook in the backend.
However, if that was previously called by
'prepare_metadata_for_build_wheel', and the same metadata_directory is
used, the previously built wheel will be copied to wheel_directory.
|
[
"Build",
"a",
"wheel",
"from",
"this",
"project",
"."
] |
cae8d76c210b9777e90aab76e9c4b0e53bb19cde
|
https://github.com/pypa/pipenv/blob/cae8d76c210b9777e90aab76e9c4b0e53bb19cde/pipenv/vendor/pep517/wrappers.py#L89-L107
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/bninception.py
|
bninception
|
def bninception(num_classes=1000, pretrained='imagenet'):
r"""BNInception model architecture from <https://arxiv.org/pdf/1502.03167.pdf>`_ paper.
"""
model = BNInception(num_classes=num_classes)
if pretrained is not None:
settings = pretrained_settings['bninception'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
model.load_state_dict(model_zoo.load_url(settings['url']))
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
return model
|
python
|
def bninception(num_classes=1000, pretrained='imagenet'):
r"""BNInception model architecture from <https://arxiv.org/pdf/1502.03167.pdf>`_ paper.
"""
model = BNInception(num_classes=num_classes)
if pretrained is not None:
settings = pretrained_settings['bninception'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
model.load_state_dict(model_zoo.load_url(settings['url']))
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
return model
|
[
"def",
"bninception",
"(",
"num_classes",
"=",
"1000",
",",
"pretrained",
"=",
"'imagenet'",
")",
":",
"model",
"=",
"BNInception",
"(",
"num_classes",
"=",
"num_classes",
")",
"if",
"pretrained",
"is",
"not",
"None",
":",
"settings",
"=",
"pretrained_settings",
"[",
"'bninception'",
"]",
"[",
"pretrained",
"]",
"assert",
"num_classes",
"==",
"settings",
"[",
"'num_classes'",
"]",
",",
"\"num_classes should be {}, but is {}\"",
".",
"format",
"(",
"settings",
"[",
"'num_classes'",
"]",
",",
"num_classes",
")",
"model",
".",
"load_state_dict",
"(",
"model_zoo",
".",
"load_url",
"(",
"settings",
"[",
"'url'",
"]",
")",
")",
"model",
".",
"input_space",
"=",
"settings",
"[",
"'input_space'",
"]",
"model",
".",
"input_size",
"=",
"settings",
"[",
"'input_size'",
"]",
"model",
".",
"input_range",
"=",
"settings",
"[",
"'input_range'",
"]",
"model",
".",
"mean",
"=",
"settings",
"[",
"'mean'",
"]",
"model",
".",
"std",
"=",
"settings",
"[",
"'std'",
"]",
"return",
"model"
] |
r"""BNInception model architecture from <https://arxiv.org/pdf/1502.03167.pdf>`_ paper.
|
[
"r",
"BNInception",
"model",
"architecture",
"from",
"<https",
":",
"//",
"arxiv",
".",
"org",
"/",
"pdf",
"/",
"1502",
".",
"03167",
".",
"pdf",
">",
"_",
"paper",
"."
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/bninception.py#L497-L511
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/fbresnet/resnet152_load.py
|
conv3x3
|
def conv3x3(in_planes, out_planes, stride=1):
"3x3 convolution with padding"
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=True)
|
python
|
def conv3x3(in_planes, out_planes, stride=1):
"3x3 convolution with padding"
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=True)
|
[
"def",
"conv3x3",
"(",
"in_planes",
",",
"out_planes",
",",
"stride",
"=",
"1",
")",
":",
"return",
"nn",
".",
"Conv2d",
"(",
"in_planes",
",",
"out_planes",
",",
"kernel_size",
"=",
"3",
",",
"stride",
"=",
"stride",
",",
"padding",
"=",
"1",
",",
"bias",
"=",
"True",
")"
] |
3x3 convolution with padding
|
[
"3x3",
"convolution",
"with",
"padding"
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/fbresnet/resnet152_load.py#L20-L23
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/fbresnet/resnet152_load.py
|
resnet18
|
def resnet18(pretrained=False, **kwargs):
"""Constructs a ResNet-18 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet18']))
return model
|
python
|
def resnet18(pretrained=False, **kwargs):
"""Constructs a ResNet-18 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet18']))
return model
|
[
"def",
"resnet18",
"(",
"pretrained",
"=",
"False",
",",
"*",
"*",
"kwargs",
")",
":",
"model",
"=",
"ResNet",
"(",
"BasicBlock",
",",
"[",
"2",
",",
"2",
",",
"2",
",",
"2",
"]",
",",
"*",
"*",
"kwargs",
")",
"if",
"pretrained",
":",
"model",
".",
"load_state_dict",
"(",
"model_zoo",
".",
"load_url",
"(",
"model_urls",
"[",
"'resnet18'",
"]",
")",
")",
"return",
"model"
] |
Constructs a ResNet-18 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
[
"Constructs",
"a",
"ResNet",
"-",
"18",
"model",
"."
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/fbresnet/resnet152_load.py#L160-L169
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/fbresnet/resnet152_load.py
|
resnet50
|
def resnet50(pretrained=False, **kwargs):
"""Constructs a ResNet-50 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet50']))
return model
|
python
|
def resnet50(pretrained=False, **kwargs):
"""Constructs a ResNet-50 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet50']))
return model
|
[
"def",
"resnet50",
"(",
"pretrained",
"=",
"False",
",",
"*",
"*",
"kwargs",
")",
":",
"model",
"=",
"ResNet",
"(",
"Bottleneck",
",",
"[",
"3",
",",
"4",
",",
"6",
",",
"3",
"]",
",",
"*",
"*",
"kwargs",
")",
"if",
"pretrained",
":",
"model",
".",
"load_state_dict",
"(",
"model_zoo",
".",
"load_url",
"(",
"model_urls",
"[",
"'resnet50'",
"]",
")",
")",
"return",
"model"
] |
Constructs a ResNet-50 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
[
"Constructs",
"a",
"ResNet",
"-",
"50",
"model",
"."
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/fbresnet/resnet152_load.py#L184-L193
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/nasnet_mobile.py
|
nasnetamobile
|
def nasnetamobile(num_classes=1000, pretrained='imagenet'):
r"""NASNetALarge model architecture from the
`"NASNet" <https://arxiv.org/abs/1707.07012>`_ paper.
"""
if pretrained:
settings = pretrained_settings['nasnetamobile'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
# both 'imagenet'&'imagenet+background' are loaded from same parameters
model = NASNetAMobile(num_classes=num_classes)
model.load_state_dict(model_zoo.load_url(settings['url'], map_location=None))
# if pretrained == 'imagenet':
# new_last_linear = nn.Linear(model.last_linear.in_features, 1000)
# new_last_linear.weight.data = model.last_linear.weight.data[1:]
# new_last_linear.bias.data = model.last_linear.bias.data[1:]
# model.last_linear = new_last_linear
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
else:
settings = pretrained_settings['nasnetamobile']['imagenet']
model = NASNetAMobile(num_classes=num_classes)
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
return model
|
python
|
def nasnetamobile(num_classes=1000, pretrained='imagenet'):
r"""NASNetALarge model architecture from the
`"NASNet" <https://arxiv.org/abs/1707.07012>`_ paper.
"""
if pretrained:
settings = pretrained_settings['nasnetamobile'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
# both 'imagenet'&'imagenet+background' are loaded from same parameters
model = NASNetAMobile(num_classes=num_classes)
model.load_state_dict(model_zoo.load_url(settings['url'], map_location=None))
# if pretrained == 'imagenet':
# new_last_linear = nn.Linear(model.last_linear.in_features, 1000)
# new_last_linear.weight.data = model.last_linear.weight.data[1:]
# new_last_linear.bias.data = model.last_linear.bias.data[1:]
# model.last_linear = new_last_linear
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
else:
settings = pretrained_settings['nasnetamobile']['imagenet']
model = NASNetAMobile(num_classes=num_classes)
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
return model
|
[
"def",
"nasnetamobile",
"(",
"num_classes",
"=",
"1000",
",",
"pretrained",
"=",
"'imagenet'",
")",
":",
"if",
"pretrained",
":",
"settings",
"=",
"pretrained_settings",
"[",
"'nasnetamobile'",
"]",
"[",
"pretrained",
"]",
"assert",
"num_classes",
"==",
"settings",
"[",
"'num_classes'",
"]",
",",
"\"num_classes should be {}, but is {}\"",
".",
"format",
"(",
"settings",
"[",
"'num_classes'",
"]",
",",
"num_classes",
")",
"# both 'imagenet'&'imagenet+background' are loaded from same parameters",
"model",
"=",
"NASNetAMobile",
"(",
"num_classes",
"=",
"num_classes",
")",
"model",
".",
"load_state_dict",
"(",
"model_zoo",
".",
"load_url",
"(",
"settings",
"[",
"'url'",
"]",
",",
"map_location",
"=",
"None",
")",
")",
"# if pretrained == 'imagenet':",
"# new_last_linear = nn.Linear(model.last_linear.in_features, 1000)",
"# new_last_linear.weight.data = model.last_linear.weight.data[1:]",
"# new_last_linear.bias.data = model.last_linear.bias.data[1:]",
"# model.last_linear = new_last_linear",
"model",
".",
"input_space",
"=",
"settings",
"[",
"'input_space'",
"]",
"model",
".",
"input_size",
"=",
"settings",
"[",
"'input_size'",
"]",
"model",
".",
"input_range",
"=",
"settings",
"[",
"'input_range'",
"]",
"model",
".",
"mean",
"=",
"settings",
"[",
"'mean'",
"]",
"model",
".",
"std",
"=",
"settings",
"[",
"'std'",
"]",
"else",
":",
"settings",
"=",
"pretrained_settings",
"[",
"'nasnetamobile'",
"]",
"[",
"'imagenet'",
"]",
"model",
"=",
"NASNetAMobile",
"(",
"num_classes",
"=",
"num_classes",
")",
"model",
".",
"input_space",
"=",
"settings",
"[",
"'input_space'",
"]",
"model",
".",
"input_size",
"=",
"settings",
"[",
"'input_size'",
"]",
"model",
".",
"input_range",
"=",
"settings",
"[",
"'input_range'",
"]",
"model",
".",
"mean",
"=",
"settings",
"[",
"'mean'",
"]",
"model",
".",
"std",
"=",
"settings",
"[",
"'std'",
"]",
"return",
"model"
] |
r"""NASNetALarge model architecture from the
`"NASNet" <https://arxiv.org/abs/1707.07012>`_ paper.
|
[
"r",
"NASNetALarge",
"model",
"architecture",
"from",
"the",
"NASNet",
"<https",
":",
"//",
"arxiv",
".",
"org",
"/",
"abs",
"/",
"1707",
".",
"07012",
">",
"_",
"paper",
"."
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/nasnet_mobile.py#L618-L652
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/cafferesnet.py
|
cafferesnet101
|
def cafferesnet101(num_classes=1000, pretrained='imagenet'):
"""Constructs a ResNet-101 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(Bottleneck, [3, 4, 23, 3], num_classes=num_classes)
if pretrained is not None:
settings = pretrained_settings['cafferesnet101'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
model.load_state_dict(model_zoo.load_url(settings['url']))
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
return model
|
python
|
def cafferesnet101(num_classes=1000, pretrained='imagenet'):
"""Constructs a ResNet-101 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(Bottleneck, [3, 4, 23, 3], num_classes=num_classes)
if pretrained is not None:
settings = pretrained_settings['cafferesnet101'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
model.load_state_dict(model_zoo.load_url(settings['url']))
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
return model
|
[
"def",
"cafferesnet101",
"(",
"num_classes",
"=",
"1000",
",",
"pretrained",
"=",
"'imagenet'",
")",
":",
"model",
"=",
"ResNet",
"(",
"Bottleneck",
",",
"[",
"3",
",",
"4",
",",
"23",
",",
"3",
"]",
",",
"num_classes",
"=",
"num_classes",
")",
"if",
"pretrained",
"is",
"not",
"None",
":",
"settings",
"=",
"pretrained_settings",
"[",
"'cafferesnet101'",
"]",
"[",
"pretrained",
"]",
"assert",
"num_classes",
"==",
"settings",
"[",
"'num_classes'",
"]",
",",
"\"num_classes should be {}, but is {}\"",
".",
"format",
"(",
"settings",
"[",
"'num_classes'",
"]",
",",
"num_classes",
")",
"model",
".",
"load_state_dict",
"(",
"model_zoo",
".",
"load_url",
"(",
"settings",
"[",
"'url'",
"]",
")",
")",
"model",
".",
"input_space",
"=",
"settings",
"[",
"'input_space'",
"]",
"model",
".",
"input_size",
"=",
"settings",
"[",
"'input_size'",
"]",
"model",
".",
"input_range",
"=",
"settings",
"[",
"'input_range'",
"]",
"model",
".",
"mean",
"=",
"settings",
"[",
"'mean'",
"]",
"model",
".",
"std",
"=",
"settings",
"[",
"'std'",
"]",
"return",
"model"
] |
Constructs a ResNet-101 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
[
"Constructs",
"a",
"ResNet",
"-",
"101",
"model",
".",
"Args",
":",
"pretrained",
"(",
"bool",
")",
":",
"If",
"True",
"returns",
"a",
"model",
"pre",
"-",
"trained",
"on",
"ImageNet"
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/cafferesnet.py#L168-L184
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/fbresnet.py
|
fbresnet152
|
def fbresnet152(num_classes=1000, pretrained='imagenet'):
"""Constructs a ResNet-152 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = FBResNet(Bottleneck, [3, 8, 36, 3], num_classes=num_classes)
if pretrained is not None:
settings = pretrained_settings['fbresnet152'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
model.load_state_dict(model_zoo.load_url(settings['url']))
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
return model
|
python
|
def fbresnet152(num_classes=1000, pretrained='imagenet'):
"""Constructs a ResNet-152 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = FBResNet(Bottleneck, [3, 8, 36, 3], num_classes=num_classes)
if pretrained is not None:
settings = pretrained_settings['fbresnet152'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
model.load_state_dict(model_zoo.load_url(settings['url']))
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
return model
|
[
"def",
"fbresnet152",
"(",
"num_classes",
"=",
"1000",
",",
"pretrained",
"=",
"'imagenet'",
")",
":",
"model",
"=",
"FBResNet",
"(",
"Bottleneck",
",",
"[",
"3",
",",
"8",
",",
"36",
",",
"3",
"]",
",",
"num_classes",
"=",
"num_classes",
")",
"if",
"pretrained",
"is",
"not",
"None",
":",
"settings",
"=",
"pretrained_settings",
"[",
"'fbresnet152'",
"]",
"[",
"pretrained",
"]",
"assert",
"num_classes",
"==",
"settings",
"[",
"'num_classes'",
"]",
",",
"\"num_classes should be {}, but is {}\"",
".",
"format",
"(",
"settings",
"[",
"'num_classes'",
"]",
",",
"num_classes",
")",
"model",
".",
"load_state_dict",
"(",
"model_zoo",
".",
"load_url",
"(",
"settings",
"[",
"'url'",
"]",
")",
")",
"model",
".",
"input_space",
"=",
"settings",
"[",
"'input_space'",
"]",
"model",
".",
"input_size",
"=",
"settings",
"[",
"'input_size'",
"]",
"model",
".",
"input_range",
"=",
"settings",
"[",
"'input_range'",
"]",
"model",
".",
"mean",
"=",
"settings",
"[",
"'mean'",
"]",
"model",
".",
"std",
"=",
"settings",
"[",
"'std'",
"]",
"return",
"model"
] |
Constructs a ResNet-152 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
[
"Constructs",
"a",
"ResNet",
"-",
"152",
"model",
"."
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/fbresnet.py#L216-L233
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/torchvision_models.py
|
alexnet
|
def alexnet(num_classes=1000, pretrained='imagenet'):
r"""AlexNet model architecture from the
`"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper.
"""
# https://github.com/pytorch/vision/blob/master/torchvision/models/alexnet.py
model = models.alexnet(pretrained=False)
if pretrained is not None:
settings = pretrained_settings['alexnet'][pretrained]
model = load_pretrained(model, num_classes, settings)
model = modify_alexnet(model)
return model
|
python
|
def alexnet(num_classes=1000, pretrained='imagenet'):
r"""AlexNet model architecture from the
`"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper.
"""
# https://github.com/pytorch/vision/blob/master/torchvision/models/alexnet.py
model = models.alexnet(pretrained=False)
if pretrained is not None:
settings = pretrained_settings['alexnet'][pretrained]
model = load_pretrained(model, num_classes, settings)
model = modify_alexnet(model)
return model
|
[
"def",
"alexnet",
"(",
"num_classes",
"=",
"1000",
",",
"pretrained",
"=",
"'imagenet'",
")",
":",
"# https://github.com/pytorch/vision/blob/master/torchvision/models/alexnet.py",
"model",
"=",
"models",
".",
"alexnet",
"(",
"pretrained",
"=",
"False",
")",
"if",
"pretrained",
"is",
"not",
"None",
":",
"settings",
"=",
"pretrained_settings",
"[",
"'alexnet'",
"]",
"[",
"pretrained",
"]",
"model",
"=",
"load_pretrained",
"(",
"model",
",",
"num_classes",
",",
"settings",
")",
"model",
"=",
"modify_alexnet",
"(",
"model",
")",
"return",
"model"
] |
r"""AlexNet model architecture from the
`"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper.
|
[
"r",
"AlexNet",
"model",
"architecture",
"from",
"the",
"One",
"weird",
"trick",
"...",
"<https",
":",
"//",
"arxiv",
".",
"org",
"/",
"abs",
"/",
"1404",
".",
"5997",
">",
"_",
"paper",
"."
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/torchvision_models.py#L168-L178
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/torchvision_models.py
|
densenet121
|
def densenet121(num_classes=1000, pretrained='imagenet'):
r"""Densenet-121 model from
`"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`
"""
model = models.densenet121(pretrained=False)
if pretrained is not None:
settings = pretrained_settings['densenet121'][pretrained]
model = load_pretrained(model, num_classes, settings)
model = modify_densenets(model)
return model
|
python
|
def densenet121(num_classes=1000, pretrained='imagenet'):
r"""Densenet-121 model from
`"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`
"""
model = models.densenet121(pretrained=False)
if pretrained is not None:
settings = pretrained_settings['densenet121'][pretrained]
model = load_pretrained(model, num_classes, settings)
model = modify_densenets(model)
return model
|
[
"def",
"densenet121",
"(",
"num_classes",
"=",
"1000",
",",
"pretrained",
"=",
"'imagenet'",
")",
":",
"model",
"=",
"models",
".",
"densenet121",
"(",
"pretrained",
"=",
"False",
")",
"if",
"pretrained",
"is",
"not",
"None",
":",
"settings",
"=",
"pretrained_settings",
"[",
"'densenet121'",
"]",
"[",
"pretrained",
"]",
"model",
"=",
"load_pretrained",
"(",
"model",
",",
"num_classes",
",",
"settings",
")",
"model",
"=",
"modify_densenets",
"(",
"model",
")",
"return",
"model"
] |
r"""Densenet-121 model from
`"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`
|
[
"r",
"Densenet",
"-",
"121",
"model",
"from",
"Densely",
"Connected",
"Convolutional",
"Networks",
"<https",
":",
"//",
"arxiv",
".",
"org",
"/",
"pdf",
"/",
"1608",
".",
"06993",
".",
"pdf",
">"
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/torchvision_models.py#L205-L214
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/torchvision_models.py
|
inceptionv3
|
def inceptionv3(num_classes=1000, pretrained='imagenet'):
r"""Inception v3 model architecture from
`"Rethinking the Inception Architecture for Computer Vision" <http://arxiv.org/abs/1512.00567>`_.
"""
model = models.inception_v3(pretrained=False)
if pretrained is not None:
settings = pretrained_settings['inceptionv3'][pretrained]
model = load_pretrained(model, num_classes, settings)
# Modify attributs
model.last_linear = model.fc
del model.fc
def features(self, input):
# 299 x 299 x 3
x = self.Conv2d_1a_3x3(input) # 149 x 149 x 32
x = self.Conv2d_2a_3x3(x) # 147 x 147 x 32
x = self.Conv2d_2b_3x3(x) # 147 x 147 x 64
x = F.max_pool2d(x, kernel_size=3, stride=2) # 73 x 73 x 64
x = self.Conv2d_3b_1x1(x) # 73 x 73 x 80
x = self.Conv2d_4a_3x3(x) # 71 x 71 x 192
x = F.max_pool2d(x, kernel_size=3, stride=2) # 35 x 35 x 192
x = self.Mixed_5b(x) # 35 x 35 x 256
x = self.Mixed_5c(x) # 35 x 35 x 288
x = self.Mixed_5d(x) # 35 x 35 x 288
x = self.Mixed_6a(x) # 17 x 17 x 768
x = self.Mixed_6b(x) # 17 x 17 x 768
x = self.Mixed_6c(x) # 17 x 17 x 768
x = self.Mixed_6d(x) # 17 x 17 x 768
x = self.Mixed_6e(x) # 17 x 17 x 768
if self.training and self.aux_logits:
self._out_aux = self.AuxLogits(x) # 17 x 17 x 768
x = self.Mixed_7a(x) # 8 x 8 x 1280
x = self.Mixed_7b(x) # 8 x 8 x 2048
x = self.Mixed_7c(x) # 8 x 8 x 2048
return x
def logits(self, features):
x = F.avg_pool2d(features, kernel_size=8) # 1 x 1 x 2048
x = F.dropout(x, training=self.training) # 1 x 1 x 2048
x = x.view(x.size(0), -1) # 2048
x = self.last_linear(x) # 1000 (num_classes)
if self.training and self.aux_logits:
aux = self._out_aux
self._out_aux = None
return x, aux
return x
def forward(self, input):
x = self.features(input)
x = self.logits(x)
return x
# Modify methods
model.features = types.MethodType(features, model)
model.logits = types.MethodType(logits, model)
model.forward = types.MethodType(forward, model)
return model
|
python
|
def inceptionv3(num_classes=1000, pretrained='imagenet'):
r"""Inception v3 model architecture from
`"Rethinking the Inception Architecture for Computer Vision" <http://arxiv.org/abs/1512.00567>`_.
"""
model = models.inception_v3(pretrained=False)
if pretrained is not None:
settings = pretrained_settings['inceptionv3'][pretrained]
model = load_pretrained(model, num_classes, settings)
# Modify attributs
model.last_linear = model.fc
del model.fc
def features(self, input):
# 299 x 299 x 3
x = self.Conv2d_1a_3x3(input) # 149 x 149 x 32
x = self.Conv2d_2a_3x3(x) # 147 x 147 x 32
x = self.Conv2d_2b_3x3(x) # 147 x 147 x 64
x = F.max_pool2d(x, kernel_size=3, stride=2) # 73 x 73 x 64
x = self.Conv2d_3b_1x1(x) # 73 x 73 x 80
x = self.Conv2d_4a_3x3(x) # 71 x 71 x 192
x = F.max_pool2d(x, kernel_size=3, stride=2) # 35 x 35 x 192
x = self.Mixed_5b(x) # 35 x 35 x 256
x = self.Mixed_5c(x) # 35 x 35 x 288
x = self.Mixed_5d(x) # 35 x 35 x 288
x = self.Mixed_6a(x) # 17 x 17 x 768
x = self.Mixed_6b(x) # 17 x 17 x 768
x = self.Mixed_6c(x) # 17 x 17 x 768
x = self.Mixed_6d(x) # 17 x 17 x 768
x = self.Mixed_6e(x) # 17 x 17 x 768
if self.training and self.aux_logits:
self._out_aux = self.AuxLogits(x) # 17 x 17 x 768
x = self.Mixed_7a(x) # 8 x 8 x 1280
x = self.Mixed_7b(x) # 8 x 8 x 2048
x = self.Mixed_7c(x) # 8 x 8 x 2048
return x
def logits(self, features):
x = F.avg_pool2d(features, kernel_size=8) # 1 x 1 x 2048
x = F.dropout(x, training=self.training) # 1 x 1 x 2048
x = x.view(x.size(0), -1) # 2048
x = self.last_linear(x) # 1000 (num_classes)
if self.training and self.aux_logits:
aux = self._out_aux
self._out_aux = None
return x, aux
return x
def forward(self, input):
x = self.features(input)
x = self.logits(x)
return x
# Modify methods
model.features = types.MethodType(features, model)
model.logits = types.MethodType(logits, model)
model.forward = types.MethodType(forward, model)
return model
|
[
"def",
"inceptionv3",
"(",
"num_classes",
"=",
"1000",
",",
"pretrained",
"=",
"'imagenet'",
")",
":",
"model",
"=",
"models",
".",
"inception_v3",
"(",
"pretrained",
"=",
"False",
")",
"if",
"pretrained",
"is",
"not",
"None",
":",
"settings",
"=",
"pretrained_settings",
"[",
"'inceptionv3'",
"]",
"[",
"pretrained",
"]",
"model",
"=",
"load_pretrained",
"(",
"model",
",",
"num_classes",
",",
"settings",
")",
"# Modify attributs",
"model",
".",
"last_linear",
"=",
"model",
".",
"fc",
"del",
"model",
".",
"fc",
"def",
"features",
"(",
"self",
",",
"input",
")",
":",
"# 299 x 299 x 3",
"x",
"=",
"self",
".",
"Conv2d_1a_3x3",
"(",
"input",
")",
"# 149 x 149 x 32",
"x",
"=",
"self",
".",
"Conv2d_2a_3x3",
"(",
"x",
")",
"# 147 x 147 x 32",
"x",
"=",
"self",
".",
"Conv2d_2b_3x3",
"(",
"x",
")",
"# 147 x 147 x 64",
"x",
"=",
"F",
".",
"max_pool2d",
"(",
"x",
",",
"kernel_size",
"=",
"3",
",",
"stride",
"=",
"2",
")",
"# 73 x 73 x 64",
"x",
"=",
"self",
".",
"Conv2d_3b_1x1",
"(",
"x",
")",
"# 73 x 73 x 80",
"x",
"=",
"self",
".",
"Conv2d_4a_3x3",
"(",
"x",
")",
"# 71 x 71 x 192",
"x",
"=",
"F",
".",
"max_pool2d",
"(",
"x",
",",
"kernel_size",
"=",
"3",
",",
"stride",
"=",
"2",
")",
"# 35 x 35 x 192",
"x",
"=",
"self",
".",
"Mixed_5b",
"(",
"x",
")",
"# 35 x 35 x 256",
"x",
"=",
"self",
".",
"Mixed_5c",
"(",
"x",
")",
"# 35 x 35 x 288",
"x",
"=",
"self",
".",
"Mixed_5d",
"(",
"x",
")",
"# 35 x 35 x 288",
"x",
"=",
"self",
".",
"Mixed_6a",
"(",
"x",
")",
"# 17 x 17 x 768",
"x",
"=",
"self",
".",
"Mixed_6b",
"(",
"x",
")",
"# 17 x 17 x 768",
"x",
"=",
"self",
".",
"Mixed_6c",
"(",
"x",
")",
"# 17 x 17 x 768",
"x",
"=",
"self",
".",
"Mixed_6d",
"(",
"x",
")",
"# 17 x 17 x 768",
"x",
"=",
"self",
".",
"Mixed_6e",
"(",
"x",
")",
"# 17 x 17 x 768",
"if",
"self",
".",
"training",
"and",
"self",
".",
"aux_logits",
":",
"self",
".",
"_out_aux",
"=",
"self",
".",
"AuxLogits",
"(",
"x",
")",
"# 17 x 17 x 768",
"x",
"=",
"self",
".",
"Mixed_7a",
"(",
"x",
")",
"# 8 x 8 x 1280",
"x",
"=",
"self",
".",
"Mixed_7b",
"(",
"x",
")",
"# 8 x 8 x 2048",
"x",
"=",
"self",
".",
"Mixed_7c",
"(",
"x",
")",
"# 8 x 8 x 2048",
"return",
"x",
"def",
"logits",
"(",
"self",
",",
"features",
")",
":",
"x",
"=",
"F",
".",
"avg_pool2d",
"(",
"features",
",",
"kernel_size",
"=",
"8",
")",
"# 1 x 1 x 2048",
"x",
"=",
"F",
".",
"dropout",
"(",
"x",
",",
"training",
"=",
"self",
".",
"training",
")",
"# 1 x 1 x 2048",
"x",
"=",
"x",
".",
"view",
"(",
"x",
".",
"size",
"(",
"0",
")",
",",
"-",
"1",
")",
"# 2048",
"x",
"=",
"self",
".",
"last_linear",
"(",
"x",
")",
"# 1000 (num_classes)",
"if",
"self",
".",
"training",
"and",
"self",
".",
"aux_logits",
":",
"aux",
"=",
"self",
".",
"_out_aux",
"self",
".",
"_out_aux",
"=",
"None",
"return",
"x",
",",
"aux",
"return",
"x",
"def",
"forward",
"(",
"self",
",",
"input",
")",
":",
"x",
"=",
"self",
".",
"features",
"(",
"input",
")",
"x",
"=",
"self",
".",
"logits",
"(",
"x",
")",
"return",
"x",
"# Modify methods",
"model",
".",
"features",
"=",
"types",
".",
"MethodType",
"(",
"features",
",",
"model",
")",
"model",
".",
"logits",
"=",
"types",
".",
"MethodType",
"(",
"logits",
",",
"model",
")",
"model",
".",
"forward",
"=",
"types",
".",
"MethodType",
"(",
"forward",
",",
"model",
")",
"return",
"model"
] |
r"""Inception v3 model architecture from
`"Rethinking the Inception Architecture for Computer Vision" <http://arxiv.org/abs/1512.00567>`_.
|
[
"r",
"Inception",
"v3",
"model",
"architecture",
"from",
"Rethinking",
"the",
"Inception",
"Architecture",
"for",
"Computer",
"Vision",
"<http",
":",
"//",
"arxiv",
".",
"org",
"/",
"abs",
"/",
"1512",
".",
"00567",
">",
"_",
"."
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/torchvision_models.py#L252-L309
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/torchvision_models.py
|
resnet50
|
def resnet50(num_classes=1000, pretrained='imagenet'):
"""Constructs a ResNet-50 model.
"""
model = models.resnet50(pretrained=False)
if pretrained is not None:
settings = pretrained_settings['resnet50'][pretrained]
model = load_pretrained(model, num_classes, settings)
model = modify_resnets(model)
return model
|
python
|
def resnet50(num_classes=1000, pretrained='imagenet'):
"""Constructs a ResNet-50 model.
"""
model = models.resnet50(pretrained=False)
if pretrained is not None:
settings = pretrained_settings['resnet50'][pretrained]
model = load_pretrained(model, num_classes, settings)
model = modify_resnets(model)
return model
|
[
"def",
"resnet50",
"(",
"num_classes",
"=",
"1000",
",",
"pretrained",
"=",
"'imagenet'",
")",
":",
"model",
"=",
"models",
".",
"resnet50",
"(",
"pretrained",
"=",
"False",
")",
"if",
"pretrained",
"is",
"not",
"None",
":",
"settings",
"=",
"pretrained_settings",
"[",
"'resnet50'",
"]",
"[",
"pretrained",
"]",
"model",
"=",
"load_pretrained",
"(",
"model",
",",
"num_classes",
",",
"settings",
")",
"model",
"=",
"modify_resnets",
"(",
"model",
")",
"return",
"model"
] |
Constructs a ResNet-50 model.
|
[
"Constructs",
"a",
"ResNet",
"-",
"50",
"model",
"."
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/torchvision_models.py#L368-L376
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/torchvision_models.py
|
squeezenet1_0
|
def squeezenet1_0(num_classes=1000, pretrained='imagenet'):
r"""SqueezeNet model architecture from the `"SqueezeNet: AlexNet-level
accuracy with 50x fewer parameters and <0.5MB model size"
<https://arxiv.org/abs/1602.07360>`_ paper.
"""
model = models.squeezenet1_0(pretrained=False)
if pretrained is not None:
settings = pretrained_settings['squeezenet1_0'][pretrained]
model = load_pretrained(model, num_classes, settings)
model = modify_squeezenets(model)
return model
|
python
|
def squeezenet1_0(num_classes=1000, pretrained='imagenet'):
r"""SqueezeNet model architecture from the `"SqueezeNet: AlexNet-level
accuracy with 50x fewer parameters and <0.5MB model size"
<https://arxiv.org/abs/1602.07360>`_ paper.
"""
model = models.squeezenet1_0(pretrained=False)
if pretrained is not None:
settings = pretrained_settings['squeezenet1_0'][pretrained]
model = load_pretrained(model, num_classes, settings)
model = modify_squeezenets(model)
return model
|
[
"def",
"squeezenet1_0",
"(",
"num_classes",
"=",
"1000",
",",
"pretrained",
"=",
"'imagenet'",
")",
":",
"model",
"=",
"models",
".",
"squeezenet1_0",
"(",
"pretrained",
"=",
"False",
")",
"if",
"pretrained",
"is",
"not",
"None",
":",
"settings",
"=",
"pretrained_settings",
"[",
"'squeezenet1_0'",
"]",
"[",
"pretrained",
"]",
"model",
"=",
"load_pretrained",
"(",
"model",
",",
"num_classes",
",",
"settings",
")",
"model",
"=",
"modify_squeezenets",
"(",
"model",
")",
"return",
"model"
] |
r"""SqueezeNet model architecture from the `"SqueezeNet: AlexNet-level
accuracy with 50x fewer parameters and <0.5MB model size"
<https://arxiv.org/abs/1602.07360>`_ paper.
|
[
"r",
"SqueezeNet",
"model",
"architecture",
"from",
"the",
"SqueezeNet",
":",
"AlexNet",
"-",
"level",
"accuracy",
"with",
"50x",
"fewer",
"parameters",
"and",
"<0",
".",
"5MB",
"model",
"size",
"<https",
":",
"//",
"arxiv",
".",
"org",
"/",
"abs",
"/",
"1602",
".",
"07360",
">",
"_",
"paper",
"."
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/torchvision_models.py#L428-L438
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/torchvision_models.py
|
vgg11
|
def vgg11(num_classes=1000, pretrained='imagenet'):
"""VGG 11-layer model (configuration "A")
"""
model = models.vgg11(pretrained=False)
if pretrained is not None:
settings = pretrained_settings['vgg11'][pretrained]
model = load_pretrained(model, num_classes, settings)
model = modify_vggs(model)
return model
|
python
|
def vgg11(num_classes=1000, pretrained='imagenet'):
"""VGG 11-layer model (configuration "A")
"""
model = models.vgg11(pretrained=False)
if pretrained is not None:
settings = pretrained_settings['vgg11'][pretrained]
model = load_pretrained(model, num_classes, settings)
model = modify_vggs(model)
return model
|
[
"def",
"vgg11",
"(",
"num_classes",
"=",
"1000",
",",
"pretrained",
"=",
"'imagenet'",
")",
":",
"model",
"=",
"models",
".",
"vgg11",
"(",
"pretrained",
"=",
"False",
")",
"if",
"pretrained",
"is",
"not",
"None",
":",
"settings",
"=",
"pretrained_settings",
"[",
"'vgg11'",
"]",
"[",
"pretrained",
"]",
"model",
"=",
"load_pretrained",
"(",
"model",
",",
"num_classes",
",",
"settings",
")",
"model",
"=",
"modify_vggs",
"(",
"model",
")",
"return",
"model"
] |
VGG 11-layer model (configuration "A")
|
[
"VGG",
"11",
"-",
"layer",
"model",
"(",
"configuration",
"A",
")"
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/torchvision_models.py#L495-L503
|
train
|
Cadene/pretrained-models.pytorch
|
examples/imagenet_eval.py
|
adjust_learning_rate
|
def adjust_learning_rate(optimizer, epoch):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
lr = args.lr * (0.1 ** (epoch // 30))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
|
python
|
def adjust_learning_rate(optimizer, epoch):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
lr = args.lr * (0.1 ** (epoch // 30))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
|
[
"def",
"adjust_learning_rate",
"(",
"optimizer",
",",
"epoch",
")",
":",
"lr",
"=",
"args",
".",
"lr",
"*",
"(",
"0.1",
"**",
"(",
"epoch",
"//",
"30",
")",
")",
"for",
"param_group",
"in",
"optimizer",
".",
"param_groups",
":",
"param_group",
"[",
"'lr'",
"]",
"=",
"lr"
] |
Sets the learning rate to the initial LR decayed by 10 every 30 epochs
|
[
"Sets",
"the",
"learning",
"rate",
"to",
"the",
"initial",
"LR",
"decayed",
"by",
"10",
"every",
"30",
"epochs"
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/examples/imagenet_eval.py#L280-L284
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/nasnet.py
|
nasnetalarge
|
def nasnetalarge(num_classes=1001, pretrained='imagenet'):
r"""NASNetALarge model architecture from the
`"NASNet" <https://arxiv.org/abs/1707.07012>`_ paper.
"""
if pretrained:
settings = pretrained_settings['nasnetalarge'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
# both 'imagenet'&'imagenet+background' are loaded from same parameters
model = NASNetALarge(num_classes=1001)
model.load_state_dict(model_zoo.load_url(settings['url']))
if pretrained == 'imagenet':
new_last_linear = nn.Linear(model.last_linear.in_features, 1000)
new_last_linear.weight.data = model.last_linear.weight.data[1:]
new_last_linear.bias.data = model.last_linear.bias.data[1:]
model.last_linear = new_last_linear
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
else:
model = NASNetALarge(num_classes=num_classes)
return model
|
python
|
def nasnetalarge(num_classes=1001, pretrained='imagenet'):
r"""NASNetALarge model architecture from the
`"NASNet" <https://arxiv.org/abs/1707.07012>`_ paper.
"""
if pretrained:
settings = pretrained_settings['nasnetalarge'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
# both 'imagenet'&'imagenet+background' are loaded from same parameters
model = NASNetALarge(num_classes=1001)
model.load_state_dict(model_zoo.load_url(settings['url']))
if pretrained == 'imagenet':
new_last_linear = nn.Linear(model.last_linear.in_features, 1000)
new_last_linear.weight.data = model.last_linear.weight.data[1:]
new_last_linear.bias.data = model.last_linear.bias.data[1:]
model.last_linear = new_last_linear
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
else:
model = NASNetALarge(num_classes=num_classes)
return model
|
[
"def",
"nasnetalarge",
"(",
"num_classes",
"=",
"1001",
",",
"pretrained",
"=",
"'imagenet'",
")",
":",
"if",
"pretrained",
":",
"settings",
"=",
"pretrained_settings",
"[",
"'nasnetalarge'",
"]",
"[",
"pretrained",
"]",
"assert",
"num_classes",
"==",
"settings",
"[",
"'num_classes'",
"]",
",",
"\"num_classes should be {}, but is {}\"",
".",
"format",
"(",
"settings",
"[",
"'num_classes'",
"]",
",",
"num_classes",
")",
"# both 'imagenet'&'imagenet+background' are loaded from same parameters",
"model",
"=",
"NASNetALarge",
"(",
"num_classes",
"=",
"1001",
")",
"model",
".",
"load_state_dict",
"(",
"model_zoo",
".",
"load_url",
"(",
"settings",
"[",
"'url'",
"]",
")",
")",
"if",
"pretrained",
"==",
"'imagenet'",
":",
"new_last_linear",
"=",
"nn",
".",
"Linear",
"(",
"model",
".",
"last_linear",
".",
"in_features",
",",
"1000",
")",
"new_last_linear",
".",
"weight",
".",
"data",
"=",
"model",
".",
"last_linear",
".",
"weight",
".",
"data",
"[",
"1",
":",
"]",
"new_last_linear",
".",
"bias",
".",
"data",
"=",
"model",
".",
"last_linear",
".",
"bias",
".",
"data",
"[",
"1",
":",
"]",
"model",
".",
"last_linear",
"=",
"new_last_linear",
"model",
".",
"input_space",
"=",
"settings",
"[",
"'input_space'",
"]",
"model",
".",
"input_size",
"=",
"settings",
"[",
"'input_size'",
"]",
"model",
".",
"input_range",
"=",
"settings",
"[",
"'input_range'",
"]",
"model",
".",
"mean",
"=",
"settings",
"[",
"'mean'",
"]",
"model",
".",
"std",
"=",
"settings",
"[",
"'std'",
"]",
"else",
":",
"model",
"=",
"NASNetALarge",
"(",
"num_classes",
"=",
"num_classes",
")",
"return",
"model"
] |
r"""NASNetALarge model architecture from the
`"NASNet" <https://arxiv.org/abs/1707.07012>`_ paper.
|
[
"r",
"NASNetALarge",
"model",
"architecture",
"from",
"the",
"NASNet",
"<https",
":",
"//",
"arxiv",
".",
"org",
"/",
"abs",
"/",
"1707",
".",
"07012",
">",
"_",
"paper",
"."
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/nasnet.py#L608-L635
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/dpn.py
|
adaptive_avgmax_pool2d
|
def adaptive_avgmax_pool2d(x, pool_type='avg', padding=0, count_include_pad=False):
"""Selectable global pooling function with dynamic input kernel size
"""
if pool_type == 'avgmaxc':
x = torch.cat([
F.avg_pool2d(
x, kernel_size=(x.size(2), x.size(3)), padding=padding, count_include_pad=count_include_pad),
F.max_pool2d(x, kernel_size=(x.size(2), x.size(3)), padding=padding)
], dim=1)
elif pool_type == 'avgmax':
x_avg = F.avg_pool2d(
x, kernel_size=(x.size(2), x.size(3)), padding=padding, count_include_pad=count_include_pad)
x_max = F.max_pool2d(x, kernel_size=(x.size(2), x.size(3)), padding=padding)
x = 0.5 * (x_avg + x_max)
elif pool_type == 'max':
x = F.max_pool2d(x, kernel_size=(x.size(2), x.size(3)), padding=padding)
else:
if pool_type != 'avg':
print('Invalid pool type %s specified. Defaulting to average pooling.' % pool_type)
x = F.avg_pool2d(
x, kernel_size=(x.size(2), x.size(3)), padding=padding, count_include_pad=count_include_pad)
return x
|
python
|
def adaptive_avgmax_pool2d(x, pool_type='avg', padding=0, count_include_pad=False):
"""Selectable global pooling function with dynamic input kernel size
"""
if pool_type == 'avgmaxc':
x = torch.cat([
F.avg_pool2d(
x, kernel_size=(x.size(2), x.size(3)), padding=padding, count_include_pad=count_include_pad),
F.max_pool2d(x, kernel_size=(x.size(2), x.size(3)), padding=padding)
], dim=1)
elif pool_type == 'avgmax':
x_avg = F.avg_pool2d(
x, kernel_size=(x.size(2), x.size(3)), padding=padding, count_include_pad=count_include_pad)
x_max = F.max_pool2d(x, kernel_size=(x.size(2), x.size(3)), padding=padding)
x = 0.5 * (x_avg + x_max)
elif pool_type == 'max':
x = F.max_pool2d(x, kernel_size=(x.size(2), x.size(3)), padding=padding)
else:
if pool_type != 'avg':
print('Invalid pool type %s specified. Defaulting to average pooling.' % pool_type)
x = F.avg_pool2d(
x, kernel_size=(x.size(2), x.size(3)), padding=padding, count_include_pad=count_include_pad)
return x
|
[
"def",
"adaptive_avgmax_pool2d",
"(",
"x",
",",
"pool_type",
"=",
"'avg'",
",",
"padding",
"=",
"0",
",",
"count_include_pad",
"=",
"False",
")",
":",
"if",
"pool_type",
"==",
"'avgmaxc'",
":",
"x",
"=",
"torch",
".",
"cat",
"(",
"[",
"F",
".",
"avg_pool2d",
"(",
"x",
",",
"kernel_size",
"=",
"(",
"x",
".",
"size",
"(",
"2",
")",
",",
"x",
".",
"size",
"(",
"3",
")",
")",
",",
"padding",
"=",
"padding",
",",
"count_include_pad",
"=",
"count_include_pad",
")",
",",
"F",
".",
"max_pool2d",
"(",
"x",
",",
"kernel_size",
"=",
"(",
"x",
".",
"size",
"(",
"2",
")",
",",
"x",
".",
"size",
"(",
"3",
")",
")",
",",
"padding",
"=",
"padding",
")",
"]",
",",
"dim",
"=",
"1",
")",
"elif",
"pool_type",
"==",
"'avgmax'",
":",
"x_avg",
"=",
"F",
".",
"avg_pool2d",
"(",
"x",
",",
"kernel_size",
"=",
"(",
"x",
".",
"size",
"(",
"2",
")",
",",
"x",
".",
"size",
"(",
"3",
")",
")",
",",
"padding",
"=",
"padding",
",",
"count_include_pad",
"=",
"count_include_pad",
")",
"x_max",
"=",
"F",
".",
"max_pool2d",
"(",
"x",
",",
"kernel_size",
"=",
"(",
"x",
".",
"size",
"(",
"2",
")",
",",
"x",
".",
"size",
"(",
"3",
")",
")",
",",
"padding",
"=",
"padding",
")",
"x",
"=",
"0.5",
"*",
"(",
"x_avg",
"+",
"x_max",
")",
"elif",
"pool_type",
"==",
"'max'",
":",
"x",
"=",
"F",
".",
"max_pool2d",
"(",
"x",
",",
"kernel_size",
"=",
"(",
"x",
".",
"size",
"(",
"2",
")",
",",
"x",
".",
"size",
"(",
"3",
")",
")",
",",
"padding",
"=",
"padding",
")",
"else",
":",
"if",
"pool_type",
"!=",
"'avg'",
":",
"print",
"(",
"'Invalid pool type %s specified. Defaulting to average pooling.'",
"%",
"pool_type",
")",
"x",
"=",
"F",
".",
"avg_pool2d",
"(",
"x",
",",
"kernel_size",
"=",
"(",
"x",
".",
"size",
"(",
"2",
")",
",",
"x",
".",
"size",
"(",
"3",
")",
")",
",",
"padding",
"=",
"padding",
",",
"count_include_pad",
"=",
"count_include_pad",
")",
"return",
"x"
] |
Selectable global pooling function with dynamic input kernel size
|
[
"Selectable",
"global",
"pooling",
"function",
"with",
"dynamic",
"input",
"kernel",
"size"
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/dpn.py#L407-L428
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/datasets/utils.py
|
download_url
|
def download_url(url, destination=None, progress_bar=True):
"""Download a URL to a local file.
Parameters
----------
url : str
The URL to download.
destination : str, None
The destination of the file. If None is given the file is saved to a temporary directory.
progress_bar : bool
Whether to show a command-line progress bar while downloading.
Returns
-------
filename : str
The location of the downloaded file.
Notes
-----
Progress bar use/example adapted from tqdm documentation: https://github.com/tqdm/tqdm
"""
def my_hook(t):
last_b = [0]
def inner(b=1, bsize=1, tsize=None):
if tsize is not None:
t.total = tsize
if b > 0:
t.update((b - last_b[0]) * bsize)
last_b[0] = b
return inner
if progress_bar:
with tqdm(unit='B', unit_scale=True, miniters=1, desc=url.split('/')[-1]) as t:
filename, _ = urlretrieve(url, filename=destination, reporthook=my_hook(t))
else:
filename, _ = urlretrieve(url, filename=destination)
|
python
|
def download_url(url, destination=None, progress_bar=True):
"""Download a URL to a local file.
Parameters
----------
url : str
The URL to download.
destination : str, None
The destination of the file. If None is given the file is saved to a temporary directory.
progress_bar : bool
Whether to show a command-line progress bar while downloading.
Returns
-------
filename : str
The location of the downloaded file.
Notes
-----
Progress bar use/example adapted from tqdm documentation: https://github.com/tqdm/tqdm
"""
def my_hook(t):
last_b = [0]
def inner(b=1, bsize=1, tsize=None):
if tsize is not None:
t.total = tsize
if b > 0:
t.update((b - last_b[0]) * bsize)
last_b[0] = b
return inner
if progress_bar:
with tqdm(unit='B', unit_scale=True, miniters=1, desc=url.split('/')[-1]) as t:
filename, _ = urlretrieve(url, filename=destination, reporthook=my_hook(t))
else:
filename, _ = urlretrieve(url, filename=destination)
|
[
"def",
"download_url",
"(",
"url",
",",
"destination",
"=",
"None",
",",
"progress_bar",
"=",
"True",
")",
":",
"def",
"my_hook",
"(",
"t",
")",
":",
"last_b",
"=",
"[",
"0",
"]",
"def",
"inner",
"(",
"b",
"=",
"1",
",",
"bsize",
"=",
"1",
",",
"tsize",
"=",
"None",
")",
":",
"if",
"tsize",
"is",
"not",
"None",
":",
"t",
".",
"total",
"=",
"tsize",
"if",
"b",
">",
"0",
":",
"t",
".",
"update",
"(",
"(",
"b",
"-",
"last_b",
"[",
"0",
"]",
")",
"*",
"bsize",
")",
"last_b",
"[",
"0",
"]",
"=",
"b",
"return",
"inner",
"if",
"progress_bar",
":",
"with",
"tqdm",
"(",
"unit",
"=",
"'B'",
",",
"unit_scale",
"=",
"True",
",",
"miniters",
"=",
"1",
",",
"desc",
"=",
"url",
".",
"split",
"(",
"'/'",
")",
"[",
"-",
"1",
"]",
")",
"as",
"t",
":",
"filename",
",",
"_",
"=",
"urlretrieve",
"(",
"url",
",",
"filename",
"=",
"destination",
",",
"reporthook",
"=",
"my_hook",
"(",
"t",
")",
")",
"else",
":",
"filename",
",",
"_",
"=",
"urlretrieve",
"(",
"url",
",",
"filename",
"=",
"destination",
")"
] |
Download a URL to a local file.
Parameters
----------
url : str
The URL to download.
destination : str, None
The destination of the file. If None is given the file is saved to a temporary directory.
progress_bar : bool
Whether to show a command-line progress bar while downloading.
Returns
-------
filename : str
The location of the downloaded file.
Notes
-----
Progress bar use/example adapted from tqdm documentation: https://github.com/tqdm/tqdm
|
[
"Download",
"a",
"URL",
"to",
"a",
"local",
"file",
"."
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/datasets/utils.py#L45-L83
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/datasets/utils.py
|
AveragePrecisionMeter.add
|
def add(self, output, target):
"""
Args:
output (Tensor): NxK tensor that for each of the N examples
indicates the probability of the example belonging to each of
the K classes, according to the model. The probabilities should
sum to one over all classes
target (Tensor): binary NxK tensort that encodes which of the K
classes are associated with the N-th input
(eg: a row [0, 1, 0, 1] indicates that the example is
associated with classes 2 and 4)
weight (optional, Tensor): Nx1 tensor representing the weight for
each example (each weight > 0)
"""
if not torch.is_tensor(output):
output = torch.from_numpy(output)
if not torch.is_tensor(target):
target = torch.from_numpy(target)
if output.dim() == 1:
output = output.view(-1, 1)
else:
assert output.dim() == 2, \
'wrong output size (should be 1D or 2D with one column \
per class)'
if target.dim() == 1:
target = target.view(-1, 1)
else:
assert target.dim() == 2, \
'wrong target size (should be 1D or 2D with one column \
per class)'
if self.scores.numel() > 0:
assert target.size(1) == self.targets.size(1), \
'dimensions for output should match previously added examples.'
# make sure storage is of sufficient size
if self.scores.storage().size() < self.scores.numel() + output.numel():
new_size = math.ceil(self.scores.storage().size() * 1.5)
self.scores.storage().resize_(int(new_size + output.numel()))
self.targets.storage().resize_(int(new_size + output.numel()))
# store scores and targets
offset = self.scores.size(0) if self.scores.dim() > 0 else 0
self.scores.resize_(offset + output.size(0), output.size(1))
self.targets.resize_(offset + target.size(0), target.size(1))
self.scores.narrow(0, offset, output.size(0)).copy_(output)
self.targets.narrow(0, offset, target.size(0)).copy_(target)
|
python
|
def add(self, output, target):
"""
Args:
output (Tensor): NxK tensor that for each of the N examples
indicates the probability of the example belonging to each of
the K classes, according to the model. The probabilities should
sum to one over all classes
target (Tensor): binary NxK tensort that encodes which of the K
classes are associated with the N-th input
(eg: a row [0, 1, 0, 1] indicates that the example is
associated with classes 2 and 4)
weight (optional, Tensor): Nx1 tensor representing the weight for
each example (each weight > 0)
"""
if not torch.is_tensor(output):
output = torch.from_numpy(output)
if not torch.is_tensor(target):
target = torch.from_numpy(target)
if output.dim() == 1:
output = output.view(-1, 1)
else:
assert output.dim() == 2, \
'wrong output size (should be 1D or 2D with one column \
per class)'
if target.dim() == 1:
target = target.view(-1, 1)
else:
assert target.dim() == 2, \
'wrong target size (should be 1D or 2D with one column \
per class)'
if self.scores.numel() > 0:
assert target.size(1) == self.targets.size(1), \
'dimensions for output should match previously added examples.'
# make sure storage is of sufficient size
if self.scores.storage().size() < self.scores.numel() + output.numel():
new_size = math.ceil(self.scores.storage().size() * 1.5)
self.scores.storage().resize_(int(new_size + output.numel()))
self.targets.storage().resize_(int(new_size + output.numel()))
# store scores and targets
offset = self.scores.size(0) if self.scores.dim() > 0 else 0
self.scores.resize_(offset + output.size(0), output.size(1))
self.targets.resize_(offset + target.size(0), target.size(1))
self.scores.narrow(0, offset, output.size(0)).copy_(output)
self.targets.narrow(0, offset, target.size(0)).copy_(target)
|
[
"def",
"add",
"(",
"self",
",",
"output",
",",
"target",
")",
":",
"if",
"not",
"torch",
".",
"is_tensor",
"(",
"output",
")",
":",
"output",
"=",
"torch",
".",
"from_numpy",
"(",
"output",
")",
"if",
"not",
"torch",
".",
"is_tensor",
"(",
"target",
")",
":",
"target",
"=",
"torch",
".",
"from_numpy",
"(",
"target",
")",
"if",
"output",
".",
"dim",
"(",
")",
"==",
"1",
":",
"output",
"=",
"output",
".",
"view",
"(",
"-",
"1",
",",
"1",
")",
"else",
":",
"assert",
"output",
".",
"dim",
"(",
")",
"==",
"2",
",",
"'wrong output size (should be 1D or 2D with one column \\\n per class)'",
"if",
"target",
".",
"dim",
"(",
")",
"==",
"1",
":",
"target",
"=",
"target",
".",
"view",
"(",
"-",
"1",
",",
"1",
")",
"else",
":",
"assert",
"target",
".",
"dim",
"(",
")",
"==",
"2",
",",
"'wrong target size (should be 1D or 2D with one column \\\n per class)'",
"if",
"self",
".",
"scores",
".",
"numel",
"(",
")",
">",
"0",
":",
"assert",
"target",
".",
"size",
"(",
"1",
")",
"==",
"self",
".",
"targets",
".",
"size",
"(",
"1",
")",
",",
"'dimensions for output should match previously added examples.'",
"# make sure storage is of sufficient size",
"if",
"self",
".",
"scores",
".",
"storage",
"(",
")",
".",
"size",
"(",
")",
"<",
"self",
".",
"scores",
".",
"numel",
"(",
")",
"+",
"output",
".",
"numel",
"(",
")",
":",
"new_size",
"=",
"math",
".",
"ceil",
"(",
"self",
".",
"scores",
".",
"storage",
"(",
")",
".",
"size",
"(",
")",
"*",
"1.5",
")",
"self",
".",
"scores",
".",
"storage",
"(",
")",
".",
"resize_",
"(",
"int",
"(",
"new_size",
"+",
"output",
".",
"numel",
"(",
")",
")",
")",
"self",
".",
"targets",
".",
"storage",
"(",
")",
".",
"resize_",
"(",
"int",
"(",
"new_size",
"+",
"output",
".",
"numel",
"(",
")",
")",
")",
"# store scores and targets",
"offset",
"=",
"self",
".",
"scores",
".",
"size",
"(",
"0",
")",
"if",
"self",
".",
"scores",
".",
"dim",
"(",
")",
">",
"0",
"else",
"0",
"self",
".",
"scores",
".",
"resize_",
"(",
"offset",
"+",
"output",
".",
"size",
"(",
"0",
")",
",",
"output",
".",
"size",
"(",
"1",
")",
")",
"self",
".",
"targets",
".",
"resize_",
"(",
"offset",
"+",
"target",
".",
"size",
"(",
"0",
")",
",",
"target",
".",
"size",
"(",
"1",
")",
")",
"self",
".",
"scores",
".",
"narrow",
"(",
"0",
",",
"offset",
",",
"output",
".",
"size",
"(",
"0",
")",
")",
".",
"copy_",
"(",
"output",
")",
"self",
".",
"targets",
".",
"narrow",
"(",
"0",
",",
"offset",
",",
"target",
".",
"size",
"(",
"0",
")",
")",
".",
"copy_",
"(",
"target",
")"
] |
Args:
output (Tensor): NxK tensor that for each of the N examples
indicates the probability of the example belonging to each of
the K classes, according to the model. The probabilities should
sum to one over all classes
target (Tensor): binary NxK tensort that encodes which of the K
classes are associated with the N-th input
(eg: a row [0, 1, 0, 1] indicates that the example is
associated with classes 2 and 4)
weight (optional, Tensor): Nx1 tensor representing the weight for
each example (each weight > 0)
|
[
"Args",
":",
"output",
"(",
"Tensor",
")",
":",
"NxK",
"tensor",
"that",
"for",
"each",
"of",
"the",
"N",
"examples",
"indicates",
"the",
"probability",
"of",
"the",
"example",
"belonging",
"to",
"each",
"of",
"the",
"K",
"classes",
"according",
"to",
"the",
"model",
".",
"The",
"probabilities",
"should",
"sum",
"to",
"one",
"over",
"all",
"classes",
"target",
"(",
"Tensor",
")",
":",
"binary",
"NxK",
"tensort",
"that",
"encodes",
"which",
"of",
"the",
"K",
"classes",
"are",
"associated",
"with",
"the",
"N",
"-",
"th",
"input",
"(",
"eg",
":",
"a",
"row",
"[",
"0",
"1",
"0",
"1",
"]",
"indicates",
"that",
"the",
"example",
"is",
"associated",
"with",
"classes",
"2",
"and",
"4",
")",
"weight",
"(",
"optional",
"Tensor",
")",
":",
"Nx1",
"tensor",
"representing",
"the",
"weight",
"for",
"each",
"example",
"(",
"each",
"weight",
">",
"0",
")"
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/datasets/utils.py#L110-L156
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/datasets/utils.py
|
AveragePrecisionMeter.value
|
def value(self):
"""Returns the model's average precision for each class
Return:
ap (FloatTensor): 1xK tensor, with avg precision for each class k
"""
if self.scores.numel() == 0:
return 0
ap = torch.zeros(self.scores.size(1))
rg = torch.arange(1, self.scores.size(0)).float()
# compute average precision for each class
for k in range(self.scores.size(1)):
# sort scores
scores = self.scores[:, k]
targets = self.targets[:, k]
# compute average precision
ap[k] = AveragePrecisionMeter.average_precision(scores, targets, self.difficult_examples)
return ap
|
python
|
def value(self):
"""Returns the model's average precision for each class
Return:
ap (FloatTensor): 1xK tensor, with avg precision for each class k
"""
if self.scores.numel() == 0:
return 0
ap = torch.zeros(self.scores.size(1))
rg = torch.arange(1, self.scores.size(0)).float()
# compute average precision for each class
for k in range(self.scores.size(1)):
# sort scores
scores = self.scores[:, k]
targets = self.targets[:, k]
# compute average precision
ap[k] = AveragePrecisionMeter.average_precision(scores, targets, self.difficult_examples)
return ap
|
[
"def",
"value",
"(",
"self",
")",
":",
"if",
"self",
".",
"scores",
".",
"numel",
"(",
")",
"==",
"0",
":",
"return",
"0",
"ap",
"=",
"torch",
".",
"zeros",
"(",
"self",
".",
"scores",
".",
"size",
"(",
"1",
")",
")",
"rg",
"=",
"torch",
".",
"arange",
"(",
"1",
",",
"self",
".",
"scores",
".",
"size",
"(",
"0",
")",
")",
".",
"float",
"(",
")",
"# compute average precision for each class",
"for",
"k",
"in",
"range",
"(",
"self",
".",
"scores",
".",
"size",
"(",
"1",
")",
")",
":",
"# sort scores",
"scores",
"=",
"self",
".",
"scores",
"[",
":",
",",
"k",
"]",
"targets",
"=",
"self",
".",
"targets",
"[",
":",
",",
"k",
"]",
"# compute average precision",
"ap",
"[",
"k",
"]",
"=",
"AveragePrecisionMeter",
".",
"average_precision",
"(",
"scores",
",",
"targets",
",",
"self",
".",
"difficult_examples",
")",
"return",
"ap"
] |
Returns the model's average precision for each class
Return:
ap (FloatTensor): 1xK tensor, with avg precision for each class k
|
[
"Returns",
"the",
"model",
"s",
"average",
"precision",
"for",
"each",
"class",
"Return",
":",
"ap",
"(",
"FloatTensor",
")",
":",
"1xK",
"tensor",
"with",
"avg",
"precision",
"for",
"each",
"class",
"k"
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/datasets/utils.py#L158-L177
|
train
|
Cadene/pretrained-models.pytorch
|
pretrainedmodels/models/polynet.py
|
polynet
|
def polynet(num_classes=1000, pretrained='imagenet'):
"""PolyNet architecture from the paper
'PolyNet: A Pursuit of Structural Diversity in Very Deep Networks'
https://arxiv.org/abs/1611.05725
"""
if pretrained:
settings = pretrained_settings['polynet'][pretrained]
assert num_classes == settings['num_classes'], \
'num_classes should be {}, but is {}'.format(
settings['num_classes'], num_classes)
model = PolyNet(num_classes=num_classes)
model.load_state_dict(model_zoo.load_url(settings['url']))
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
else:
model = PolyNet(num_classes=num_classes)
return model
|
python
|
def polynet(num_classes=1000, pretrained='imagenet'):
"""PolyNet architecture from the paper
'PolyNet: A Pursuit of Structural Diversity in Very Deep Networks'
https://arxiv.org/abs/1611.05725
"""
if pretrained:
settings = pretrained_settings['polynet'][pretrained]
assert num_classes == settings['num_classes'], \
'num_classes should be {}, but is {}'.format(
settings['num_classes'], num_classes)
model = PolyNet(num_classes=num_classes)
model.load_state_dict(model_zoo.load_url(settings['url']))
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
else:
model = PolyNet(num_classes=num_classes)
return model
|
[
"def",
"polynet",
"(",
"num_classes",
"=",
"1000",
",",
"pretrained",
"=",
"'imagenet'",
")",
":",
"if",
"pretrained",
":",
"settings",
"=",
"pretrained_settings",
"[",
"'polynet'",
"]",
"[",
"pretrained",
"]",
"assert",
"num_classes",
"==",
"settings",
"[",
"'num_classes'",
"]",
",",
"'num_classes should be {}, but is {}'",
".",
"format",
"(",
"settings",
"[",
"'num_classes'",
"]",
",",
"num_classes",
")",
"model",
"=",
"PolyNet",
"(",
"num_classes",
"=",
"num_classes",
")",
"model",
".",
"load_state_dict",
"(",
"model_zoo",
".",
"load_url",
"(",
"settings",
"[",
"'url'",
"]",
")",
")",
"model",
".",
"input_space",
"=",
"settings",
"[",
"'input_space'",
"]",
"model",
".",
"input_size",
"=",
"settings",
"[",
"'input_size'",
"]",
"model",
".",
"input_range",
"=",
"settings",
"[",
"'input_range'",
"]",
"model",
".",
"mean",
"=",
"settings",
"[",
"'mean'",
"]",
"model",
".",
"std",
"=",
"settings",
"[",
"'std'",
"]",
"else",
":",
"model",
"=",
"PolyNet",
"(",
"num_classes",
"=",
"num_classes",
")",
"return",
"model"
] |
PolyNet architecture from the paper
'PolyNet: A Pursuit of Structural Diversity in Very Deep Networks'
https://arxiv.org/abs/1611.05725
|
[
"PolyNet",
"architecture",
"from",
"the",
"paper",
"PolyNet",
":",
"A",
"Pursuit",
"of",
"Structural",
"Diversity",
"in",
"Very",
"Deep",
"Networks",
"https",
":",
"//",
"arxiv",
".",
"org",
"/",
"abs",
"/",
"1611",
".",
"05725"
] |
021d97897c9aa76ec759deff43d341c4fd45d7ba
|
https://github.com/Cadene/pretrained-models.pytorch/blob/021d97897c9aa76ec759deff43d341c4fd45d7ba/pretrainedmodels/models/polynet.py#L461-L480
|
train
|
quantopian/zipline
|
zipline/utils/cache.py
|
CachedObject.unwrap
|
def unwrap(self, dt):
"""
Get the cached value.
Returns
-------
value : object
The cached value.
Raises
------
Expired
Raised when `dt` is greater than self.expires.
"""
expires = self._expires
if expires is AlwaysExpired or expires < dt:
raise Expired(self._expires)
return self._value
|
python
|
def unwrap(self, dt):
"""
Get the cached value.
Returns
-------
value : object
The cached value.
Raises
------
Expired
Raised when `dt` is greater than self.expires.
"""
expires = self._expires
if expires is AlwaysExpired or expires < dt:
raise Expired(self._expires)
return self._value
|
[
"def",
"unwrap",
"(",
"self",
",",
"dt",
")",
":",
"expires",
"=",
"self",
".",
"_expires",
"if",
"expires",
"is",
"AlwaysExpired",
"or",
"expires",
"<",
"dt",
":",
"raise",
"Expired",
"(",
"self",
".",
"_expires",
")",
"return",
"self",
".",
"_value"
] |
Get the cached value.
Returns
-------
value : object
The cached value.
Raises
------
Expired
Raised when `dt` is greater than self.expires.
|
[
"Get",
"the",
"cached",
"value",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/cache.py#L67-L84
|
train
|
quantopian/zipline
|
zipline/utils/cache.py
|
ExpiringCache.get
|
def get(self, key, dt):
"""Get the value of a cached object.
Parameters
----------
key : any
The key to lookup.
dt : datetime
The time of the lookup.
Returns
-------
result : any
The value for ``key``.
Raises
------
KeyError
Raised if the key is not in the cache or the value for the key
has expired.
"""
try:
return self._cache[key].unwrap(dt)
except Expired:
self.cleanup(self._cache[key]._unsafe_get_value())
del self._cache[key]
raise KeyError(key)
|
python
|
def get(self, key, dt):
"""Get the value of a cached object.
Parameters
----------
key : any
The key to lookup.
dt : datetime
The time of the lookup.
Returns
-------
result : any
The value for ``key``.
Raises
------
KeyError
Raised if the key is not in the cache or the value for the key
has expired.
"""
try:
return self._cache[key].unwrap(dt)
except Expired:
self.cleanup(self._cache[key]._unsafe_get_value())
del self._cache[key]
raise KeyError(key)
|
[
"def",
"get",
"(",
"self",
",",
"key",
",",
"dt",
")",
":",
"try",
":",
"return",
"self",
".",
"_cache",
"[",
"key",
"]",
".",
"unwrap",
"(",
"dt",
")",
"except",
"Expired",
":",
"self",
".",
"cleanup",
"(",
"self",
".",
"_cache",
"[",
"key",
"]",
".",
"_unsafe_get_value",
"(",
")",
")",
"del",
"self",
".",
"_cache",
"[",
"key",
"]",
"raise",
"KeyError",
"(",
"key",
")"
] |
Get the value of a cached object.
Parameters
----------
key : any
The key to lookup.
dt : datetime
The time of the lookup.
Returns
-------
result : any
The value for ``key``.
Raises
------
KeyError
Raised if the key is not in the cache or the value for the key
has expired.
|
[
"Get",
"the",
"value",
"of",
"a",
"cached",
"object",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/cache.py#L131-L157
|
train
|
quantopian/zipline
|
zipline/utils/cache.py
|
ExpiringCache.set
|
def set(self, key, value, expiration_dt):
"""Adds a new key value pair to the cache.
Parameters
----------
key : any
The key to use for the pair.
value : any
The value to store under the name ``key``.
expiration_dt : datetime
When should this mapping expire? The cache is considered invalid
for dates **strictly greater** than ``expiration_dt``.
"""
self._cache[key] = CachedObject(value, expiration_dt)
|
python
|
def set(self, key, value, expiration_dt):
"""Adds a new key value pair to the cache.
Parameters
----------
key : any
The key to use for the pair.
value : any
The value to store under the name ``key``.
expiration_dt : datetime
When should this mapping expire? The cache is considered invalid
for dates **strictly greater** than ``expiration_dt``.
"""
self._cache[key] = CachedObject(value, expiration_dt)
|
[
"def",
"set",
"(",
"self",
",",
"key",
",",
"value",
",",
"expiration_dt",
")",
":",
"self",
".",
"_cache",
"[",
"key",
"]",
"=",
"CachedObject",
"(",
"value",
",",
"expiration_dt",
")"
] |
Adds a new key value pair to the cache.
Parameters
----------
key : any
The key to use for the pair.
value : any
The value to store under the name ``key``.
expiration_dt : datetime
When should this mapping expire? The cache is considered invalid
for dates **strictly greater** than ``expiration_dt``.
|
[
"Adds",
"a",
"new",
"key",
"value",
"pair",
"to",
"the",
"cache",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/cache.py#L159-L172
|
train
|
quantopian/zipline
|
zipline/utils/cache.py
|
working_dir.ensure_dir
|
def ensure_dir(self, *path_parts):
"""Ensures a subdirectory of the working directory.
Parameters
----------
path_parts : iterable[str]
The parts of the path after the working directory.
"""
path = self.getpath(*path_parts)
ensure_directory(path)
return path
|
python
|
def ensure_dir(self, *path_parts):
"""Ensures a subdirectory of the working directory.
Parameters
----------
path_parts : iterable[str]
The parts of the path after the working directory.
"""
path = self.getpath(*path_parts)
ensure_directory(path)
return path
|
[
"def",
"ensure_dir",
"(",
"self",
",",
"*",
"path_parts",
")",
":",
"path",
"=",
"self",
".",
"getpath",
"(",
"*",
"path_parts",
")",
"ensure_directory",
"(",
"path",
")",
"return",
"path"
] |
Ensures a subdirectory of the working directory.
Parameters
----------
path_parts : iterable[str]
The parts of the path after the working directory.
|
[
"Ensures",
"a",
"subdirectory",
"of",
"the",
"working",
"directory",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/cache.py#L358-L368
|
train
|
quantopian/zipline
|
zipline/data/in_memory_daily_bars.py
|
verify_frames_aligned
|
def verify_frames_aligned(frames, calendar):
"""
Verify that DataFrames in ``frames`` have the same indexing scheme and are
aligned to ``calendar``.
Parameters
----------
frames : list[pd.DataFrame]
calendar : trading_calendars.TradingCalendar
Raises
------
ValueError
If frames have different indexes/columns, or if frame indexes do not
match a contiguous region of ``calendar``.
"""
indexes = [f.index for f in frames]
check_indexes_all_same(indexes, message="DataFrame indexes don't match:")
columns = [f.columns for f in frames]
check_indexes_all_same(columns, message="DataFrame columns don't match:")
start, end = indexes[0][[0, -1]]
cal_sessions = calendar.sessions_in_range(start, end)
check_indexes_all_same(
[indexes[0], cal_sessions],
"DataFrame index doesn't match {} calendar:".format(calendar.name),
)
|
python
|
def verify_frames_aligned(frames, calendar):
"""
Verify that DataFrames in ``frames`` have the same indexing scheme and are
aligned to ``calendar``.
Parameters
----------
frames : list[pd.DataFrame]
calendar : trading_calendars.TradingCalendar
Raises
------
ValueError
If frames have different indexes/columns, or if frame indexes do not
match a contiguous region of ``calendar``.
"""
indexes = [f.index for f in frames]
check_indexes_all_same(indexes, message="DataFrame indexes don't match:")
columns = [f.columns for f in frames]
check_indexes_all_same(columns, message="DataFrame columns don't match:")
start, end = indexes[0][[0, -1]]
cal_sessions = calendar.sessions_in_range(start, end)
check_indexes_all_same(
[indexes[0], cal_sessions],
"DataFrame index doesn't match {} calendar:".format(calendar.name),
)
|
[
"def",
"verify_frames_aligned",
"(",
"frames",
",",
"calendar",
")",
":",
"indexes",
"=",
"[",
"f",
".",
"index",
"for",
"f",
"in",
"frames",
"]",
"check_indexes_all_same",
"(",
"indexes",
",",
"message",
"=",
"\"DataFrame indexes don't match:\"",
")",
"columns",
"=",
"[",
"f",
".",
"columns",
"for",
"f",
"in",
"frames",
"]",
"check_indexes_all_same",
"(",
"columns",
",",
"message",
"=",
"\"DataFrame columns don't match:\"",
")",
"start",
",",
"end",
"=",
"indexes",
"[",
"0",
"]",
"[",
"[",
"0",
",",
"-",
"1",
"]",
"]",
"cal_sessions",
"=",
"calendar",
".",
"sessions_in_range",
"(",
"start",
",",
"end",
")",
"check_indexes_all_same",
"(",
"[",
"indexes",
"[",
"0",
"]",
",",
"cal_sessions",
"]",
",",
"\"DataFrame index doesn't match {} calendar:\"",
".",
"format",
"(",
"calendar",
".",
"name",
")",
",",
")"
] |
Verify that DataFrames in ``frames`` have the same indexing scheme and are
aligned to ``calendar``.
Parameters
----------
frames : list[pd.DataFrame]
calendar : trading_calendars.TradingCalendar
Raises
------
ValueError
If frames have different indexes/columns, or if frame indexes do not
match a contiguous region of ``calendar``.
|
[
"Verify",
"that",
"DataFrames",
"in",
"frames",
"have",
"the",
"same",
"indexing",
"scheme",
"and",
"are",
"aligned",
"to",
"calendar",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/in_memory_daily_bars.py#L124-L152
|
train
|
quantopian/zipline
|
zipline/data/in_memory_daily_bars.py
|
InMemoryDailyBarReader.get_value
|
def get_value(self, sid, dt, field):
"""
Parameters
----------
sid : int
The asset identifier.
day : datetime64-like
Midnight of the day for which data is requested.
field : string
The price field. e.g. ('open', 'high', 'low', 'close', 'volume')
Returns
-------
float
The spot price for colname of the given sid on the given day.
Raises a NoDataOnDate exception if the given day and sid is before
or after the date range of the equity.
Returns -1 if the day is within the date range, but the price is
0.
"""
return self.frames[field].loc[dt, sid]
|
python
|
def get_value(self, sid, dt, field):
"""
Parameters
----------
sid : int
The asset identifier.
day : datetime64-like
Midnight of the day for which data is requested.
field : string
The price field. e.g. ('open', 'high', 'low', 'close', 'volume')
Returns
-------
float
The spot price for colname of the given sid on the given day.
Raises a NoDataOnDate exception if the given day and sid is before
or after the date range of the equity.
Returns -1 if the day is within the date range, but the price is
0.
"""
return self.frames[field].loc[dt, sid]
|
[
"def",
"get_value",
"(",
"self",
",",
"sid",
",",
"dt",
",",
"field",
")",
":",
"return",
"self",
".",
"frames",
"[",
"field",
"]",
".",
"loc",
"[",
"dt",
",",
"sid",
"]"
] |
Parameters
----------
sid : int
The asset identifier.
day : datetime64-like
Midnight of the day for which data is requested.
field : string
The price field. e.g. ('open', 'high', 'low', 'close', 'volume')
Returns
-------
float
The spot price for colname of the given sid on the given day.
Raises a NoDataOnDate exception if the given day and sid is before
or after the date range of the equity.
Returns -1 if the day is within the date range, but the price is
0.
|
[
"Parameters",
"----------",
"sid",
":",
"int",
"The",
"asset",
"identifier",
".",
"day",
":",
"datetime64",
"-",
"like",
"Midnight",
"of",
"the",
"day",
"for",
"which",
"data",
"is",
"requested",
".",
"field",
":",
"string",
"The",
"price",
"field",
".",
"e",
".",
"g",
".",
"(",
"open",
"high",
"low",
"close",
"volume",
")"
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/in_memory_daily_bars.py#L78-L98
|
train
|
quantopian/zipline
|
zipline/data/in_memory_daily_bars.py
|
InMemoryDailyBarReader.get_last_traded_dt
|
def get_last_traded_dt(self, asset, dt):
"""
Parameters
----------
asset : zipline.asset.Asset
The asset identifier.
dt : datetime64-like
Midnight of the day for which data is requested.
Returns
-------
pd.Timestamp : The last know dt for the asset and dt;
NaT if no trade is found before the given dt.
"""
try:
return self.frames['close'].loc[:, asset.sid].last_valid_index()
except IndexError:
return NaT
|
python
|
def get_last_traded_dt(self, asset, dt):
"""
Parameters
----------
asset : zipline.asset.Asset
The asset identifier.
dt : datetime64-like
Midnight of the day for which data is requested.
Returns
-------
pd.Timestamp : The last know dt for the asset and dt;
NaT if no trade is found before the given dt.
"""
try:
return self.frames['close'].loc[:, asset.sid].last_valid_index()
except IndexError:
return NaT
|
[
"def",
"get_last_traded_dt",
"(",
"self",
",",
"asset",
",",
"dt",
")",
":",
"try",
":",
"return",
"self",
".",
"frames",
"[",
"'close'",
"]",
".",
"loc",
"[",
":",
",",
"asset",
".",
"sid",
"]",
".",
"last_valid_index",
"(",
")",
"except",
"IndexError",
":",
"return",
"NaT"
] |
Parameters
----------
asset : zipline.asset.Asset
The asset identifier.
dt : datetime64-like
Midnight of the day for which data is requested.
Returns
-------
pd.Timestamp : The last know dt for the asset and dt;
NaT if no trade is found before the given dt.
|
[
"Parameters",
"----------",
"asset",
":",
"zipline",
".",
"asset",
".",
"Asset",
"The",
"asset",
"identifier",
".",
"dt",
":",
"datetime64",
"-",
"like",
"Midnight",
"of",
"the",
"day",
"for",
"which",
"data",
"is",
"requested",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/in_memory_daily_bars.py#L100-L117
|
train
|
quantopian/zipline
|
zipline/utils/functional.py
|
same
|
def same(*values):
"""
Check if all values in a sequence are equal.
Returns True on empty sequences.
Examples
--------
>>> same(1, 1, 1, 1)
True
>>> same(1, 2, 1)
False
>>> same()
True
"""
if not values:
return True
first, rest = values[0], values[1:]
return all(value == first for value in rest)
|
python
|
def same(*values):
"""
Check if all values in a sequence are equal.
Returns True on empty sequences.
Examples
--------
>>> same(1, 1, 1, 1)
True
>>> same(1, 2, 1)
False
>>> same()
True
"""
if not values:
return True
first, rest = values[0], values[1:]
return all(value == first for value in rest)
|
[
"def",
"same",
"(",
"*",
"values",
")",
":",
"if",
"not",
"values",
":",
"return",
"True",
"first",
",",
"rest",
"=",
"values",
"[",
"0",
"]",
",",
"values",
"[",
"1",
":",
"]",
"return",
"all",
"(",
"value",
"==",
"first",
"for",
"value",
"in",
"rest",
")"
] |
Check if all values in a sequence are equal.
Returns True on empty sequences.
Examples
--------
>>> same(1, 1, 1, 1)
True
>>> same(1, 2, 1)
False
>>> same()
True
|
[
"Check",
"if",
"all",
"values",
"in",
"a",
"sequence",
"are",
"equal",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/functional.py#L88-L106
|
train
|
quantopian/zipline
|
zipline/utils/functional.py
|
dzip_exact
|
def dzip_exact(*dicts):
"""
Parameters
----------
*dicts : iterable[dict]
A sequence of dicts all sharing the same keys.
Returns
-------
zipped : dict
A dict whose keys are the union of all keys in *dicts, and whose values
are tuples of length len(dicts) containing the result of looking up
each key in each dict.
Raises
------
ValueError
If dicts don't all have the same keys.
Examples
--------
>>> result = dzip_exact({'a': 1, 'b': 2}, {'a': 3, 'b': 4})
>>> result == {'a': (1, 3), 'b': (2, 4)}
True
"""
if not same(*map(viewkeys, dicts)):
raise ValueError(
"dict keys not all equal:\n\n%s" % _format_unequal_keys(dicts)
)
return {k: tuple(d[k] for d in dicts) for k in dicts[0]}
|
python
|
def dzip_exact(*dicts):
"""
Parameters
----------
*dicts : iterable[dict]
A sequence of dicts all sharing the same keys.
Returns
-------
zipped : dict
A dict whose keys are the union of all keys in *dicts, and whose values
are tuples of length len(dicts) containing the result of looking up
each key in each dict.
Raises
------
ValueError
If dicts don't all have the same keys.
Examples
--------
>>> result = dzip_exact({'a': 1, 'b': 2}, {'a': 3, 'b': 4})
>>> result == {'a': (1, 3), 'b': (2, 4)}
True
"""
if not same(*map(viewkeys, dicts)):
raise ValueError(
"dict keys not all equal:\n\n%s" % _format_unequal_keys(dicts)
)
return {k: tuple(d[k] for d in dicts) for k in dicts[0]}
|
[
"def",
"dzip_exact",
"(",
"*",
"dicts",
")",
":",
"if",
"not",
"same",
"(",
"*",
"map",
"(",
"viewkeys",
",",
"dicts",
")",
")",
":",
"raise",
"ValueError",
"(",
"\"dict keys not all equal:\\n\\n%s\"",
"%",
"_format_unequal_keys",
"(",
"dicts",
")",
")",
"return",
"{",
"k",
":",
"tuple",
"(",
"d",
"[",
"k",
"]",
"for",
"d",
"in",
"dicts",
")",
"for",
"k",
"in",
"dicts",
"[",
"0",
"]",
"}"
] |
Parameters
----------
*dicts : iterable[dict]
A sequence of dicts all sharing the same keys.
Returns
-------
zipped : dict
A dict whose keys are the union of all keys in *dicts, and whose values
are tuples of length len(dicts) containing the result of looking up
each key in each dict.
Raises
------
ValueError
If dicts don't all have the same keys.
Examples
--------
>>> result = dzip_exact({'a': 1, 'b': 2}, {'a': 3, 'b': 4})
>>> result == {'a': (1, 3), 'b': (2, 4)}
True
|
[
"Parameters",
"----------",
"*",
"dicts",
":",
"iterable",
"[",
"dict",
"]",
"A",
"sequence",
"of",
"dicts",
"all",
"sharing",
"the",
"same",
"keys",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/functional.py#L113-L142
|
train
|
quantopian/zipline
|
zipline/utils/functional.py
|
_gen_unzip
|
def _gen_unzip(it, elem_len):
"""Helper for unzip which checks the lengths of each element in it.
Parameters
----------
it : iterable[tuple]
An iterable of tuples. ``unzip`` should map ensure that these are
already tuples.
elem_len : int or None
The expected element length. If this is None it is infered from the
length of the first element.
Yields
------
elem : tuple
Each element of ``it``.
Raises
------
ValueError
Raised when the lengths do not match the ``elem_len``.
"""
elem = next(it)
first_elem_len = len(elem)
if elem_len is not None and elem_len != first_elem_len:
raise ValueError(
'element at index 0 was length %d, expected %d' % (
first_elem_len,
elem_len,
)
)
else:
elem_len = first_elem_len
yield elem
for n, elem in enumerate(it, 1):
if len(elem) != elem_len:
raise ValueError(
'element at index %d was length %d, expected %d' % (
n,
len(elem),
elem_len,
),
)
yield elem
|
python
|
def _gen_unzip(it, elem_len):
"""Helper for unzip which checks the lengths of each element in it.
Parameters
----------
it : iterable[tuple]
An iterable of tuples. ``unzip`` should map ensure that these are
already tuples.
elem_len : int or None
The expected element length. If this is None it is infered from the
length of the first element.
Yields
------
elem : tuple
Each element of ``it``.
Raises
------
ValueError
Raised when the lengths do not match the ``elem_len``.
"""
elem = next(it)
first_elem_len = len(elem)
if elem_len is not None and elem_len != first_elem_len:
raise ValueError(
'element at index 0 was length %d, expected %d' % (
first_elem_len,
elem_len,
)
)
else:
elem_len = first_elem_len
yield elem
for n, elem in enumerate(it, 1):
if len(elem) != elem_len:
raise ValueError(
'element at index %d was length %d, expected %d' % (
n,
len(elem),
elem_len,
),
)
yield elem
|
[
"def",
"_gen_unzip",
"(",
"it",
",",
"elem_len",
")",
":",
"elem",
"=",
"next",
"(",
"it",
")",
"first_elem_len",
"=",
"len",
"(",
"elem",
")",
"if",
"elem_len",
"is",
"not",
"None",
"and",
"elem_len",
"!=",
"first_elem_len",
":",
"raise",
"ValueError",
"(",
"'element at index 0 was length %d, expected %d'",
"%",
"(",
"first_elem_len",
",",
"elem_len",
",",
")",
")",
"else",
":",
"elem_len",
"=",
"first_elem_len",
"yield",
"elem",
"for",
"n",
",",
"elem",
"in",
"enumerate",
"(",
"it",
",",
"1",
")",
":",
"if",
"len",
"(",
"elem",
")",
"!=",
"elem_len",
":",
"raise",
"ValueError",
"(",
"'element at index %d was length %d, expected %d'",
"%",
"(",
"n",
",",
"len",
"(",
"elem",
")",
",",
"elem_len",
",",
")",
",",
")",
"yield",
"elem"
] |
Helper for unzip which checks the lengths of each element in it.
Parameters
----------
it : iterable[tuple]
An iterable of tuples. ``unzip`` should map ensure that these are
already tuples.
elem_len : int or None
The expected element length. If this is None it is infered from the
length of the first element.
Yields
------
elem : tuple
Each element of ``it``.
Raises
------
ValueError
Raised when the lengths do not match the ``elem_len``.
|
[
"Helper",
"for",
"unzip",
"which",
"checks",
"the",
"lengths",
"of",
"each",
"element",
"in",
"it",
".",
"Parameters",
"----------",
"it",
":",
"iterable",
"[",
"tuple",
"]",
"An",
"iterable",
"of",
"tuples",
".",
"unzip",
"should",
"map",
"ensure",
"that",
"these",
"are",
"already",
"tuples",
".",
"elem_len",
":",
"int",
"or",
"None",
"The",
"expected",
"element",
"length",
".",
"If",
"this",
"is",
"None",
"it",
"is",
"infered",
"from",
"the",
"length",
"of",
"the",
"first",
"element",
".",
"Yields",
"------",
"elem",
":",
"tuple",
"Each",
"element",
"of",
"it",
".",
"Raises",
"------",
"ValueError",
"Raised",
"when",
"the",
"lengths",
"do",
"not",
"match",
"the",
"elem_len",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/functional.py#L145-L187
|
train
|
quantopian/zipline
|
zipline/utils/functional.py
|
unzip
|
def unzip(seq, elem_len=None):
"""Unzip a length n sequence of length m sequences into m seperate length
n sequences.
Parameters
----------
seq : iterable[iterable]
The sequence to unzip.
elem_len : int, optional
The expected length of each element of ``seq``. If not provided this
will be infered from the length of the first element of ``seq``. This
can be used to ensure that code like: ``a, b = unzip(seq)`` does not
fail even when ``seq`` is empty.
Returns
-------
seqs : iterable[iterable]
The new sequences pulled out of the first iterable.
Raises
------
ValueError
Raised when ``seq`` is empty and ``elem_len`` is not provided.
Raised when elements of ``seq`` do not match the given ``elem_len`` or
the length of the first element of ``seq``.
Examples
--------
>>> seq = [('a', 1), ('b', 2), ('c', 3)]
>>> cs, ns = unzip(seq)
>>> cs
('a', 'b', 'c')
>>> ns
(1, 2, 3)
# checks that the elements are the same length
>>> seq = [('a', 1), ('b', 2), ('c', 3, 'extra')]
>>> cs, ns = unzip(seq)
Traceback (most recent call last):
...
ValueError: element at index 2 was length 3, expected 2
# allows an explicit element length instead of infering
>>> seq = [('a', 1, 'extra'), ('b', 2), ('c', 3)]
>>> cs, ns = unzip(seq, 2)
Traceback (most recent call last):
...
ValueError: element at index 0 was length 3, expected 2
# handles empty sequences when a length is given
>>> cs, ns = unzip([], elem_len=2)
>>> cs == ns == ()
True
Notes
-----
This function will force ``seq`` to completion.
"""
ret = tuple(zip(*_gen_unzip(map(tuple, seq), elem_len)))
if ret:
return ret
if elem_len is None:
raise ValueError("cannot unzip empty sequence without 'elem_len'")
return ((),) * elem_len
|
python
|
def unzip(seq, elem_len=None):
"""Unzip a length n sequence of length m sequences into m seperate length
n sequences.
Parameters
----------
seq : iterable[iterable]
The sequence to unzip.
elem_len : int, optional
The expected length of each element of ``seq``. If not provided this
will be infered from the length of the first element of ``seq``. This
can be used to ensure that code like: ``a, b = unzip(seq)`` does not
fail even when ``seq`` is empty.
Returns
-------
seqs : iterable[iterable]
The new sequences pulled out of the first iterable.
Raises
------
ValueError
Raised when ``seq`` is empty and ``elem_len`` is not provided.
Raised when elements of ``seq`` do not match the given ``elem_len`` or
the length of the first element of ``seq``.
Examples
--------
>>> seq = [('a', 1), ('b', 2), ('c', 3)]
>>> cs, ns = unzip(seq)
>>> cs
('a', 'b', 'c')
>>> ns
(1, 2, 3)
# checks that the elements are the same length
>>> seq = [('a', 1), ('b', 2), ('c', 3, 'extra')]
>>> cs, ns = unzip(seq)
Traceback (most recent call last):
...
ValueError: element at index 2 was length 3, expected 2
# allows an explicit element length instead of infering
>>> seq = [('a', 1, 'extra'), ('b', 2), ('c', 3)]
>>> cs, ns = unzip(seq, 2)
Traceback (most recent call last):
...
ValueError: element at index 0 was length 3, expected 2
# handles empty sequences when a length is given
>>> cs, ns = unzip([], elem_len=2)
>>> cs == ns == ()
True
Notes
-----
This function will force ``seq`` to completion.
"""
ret = tuple(zip(*_gen_unzip(map(tuple, seq), elem_len)))
if ret:
return ret
if elem_len is None:
raise ValueError("cannot unzip empty sequence without 'elem_len'")
return ((),) * elem_len
|
[
"def",
"unzip",
"(",
"seq",
",",
"elem_len",
"=",
"None",
")",
":",
"ret",
"=",
"tuple",
"(",
"zip",
"(",
"*",
"_gen_unzip",
"(",
"map",
"(",
"tuple",
",",
"seq",
")",
",",
"elem_len",
")",
")",
")",
"if",
"ret",
":",
"return",
"ret",
"if",
"elem_len",
"is",
"None",
":",
"raise",
"ValueError",
"(",
"\"cannot unzip empty sequence without 'elem_len'\"",
")",
"return",
"(",
"(",
")",
",",
")",
"*",
"elem_len"
] |
Unzip a length n sequence of length m sequences into m seperate length
n sequences.
Parameters
----------
seq : iterable[iterable]
The sequence to unzip.
elem_len : int, optional
The expected length of each element of ``seq``. If not provided this
will be infered from the length of the first element of ``seq``. This
can be used to ensure that code like: ``a, b = unzip(seq)`` does not
fail even when ``seq`` is empty.
Returns
-------
seqs : iterable[iterable]
The new sequences pulled out of the first iterable.
Raises
------
ValueError
Raised when ``seq`` is empty and ``elem_len`` is not provided.
Raised when elements of ``seq`` do not match the given ``elem_len`` or
the length of the first element of ``seq``.
Examples
--------
>>> seq = [('a', 1), ('b', 2), ('c', 3)]
>>> cs, ns = unzip(seq)
>>> cs
('a', 'b', 'c')
>>> ns
(1, 2, 3)
# checks that the elements are the same length
>>> seq = [('a', 1), ('b', 2), ('c', 3, 'extra')]
>>> cs, ns = unzip(seq)
Traceback (most recent call last):
...
ValueError: element at index 2 was length 3, expected 2
# allows an explicit element length instead of infering
>>> seq = [('a', 1, 'extra'), ('b', 2), ('c', 3)]
>>> cs, ns = unzip(seq, 2)
Traceback (most recent call last):
...
ValueError: element at index 0 was length 3, expected 2
# handles empty sequences when a length is given
>>> cs, ns = unzip([], elem_len=2)
>>> cs == ns == ()
True
Notes
-----
This function will force ``seq`` to completion.
|
[
"Unzip",
"a",
"length",
"n",
"sequence",
"of",
"length",
"m",
"sequences",
"into",
"m",
"seperate",
"length",
"n",
"sequences",
".",
"Parameters",
"----------",
"seq",
":",
"iterable",
"[",
"iterable",
"]",
"The",
"sequence",
"to",
"unzip",
".",
"elem_len",
":",
"int",
"optional",
"The",
"expected",
"length",
"of",
"each",
"element",
"of",
"seq",
".",
"If",
"not",
"provided",
"this",
"will",
"be",
"infered",
"from",
"the",
"length",
"of",
"the",
"first",
"element",
"of",
"seq",
".",
"This",
"can",
"be",
"used",
"to",
"ensure",
"that",
"code",
"like",
":",
"a",
"b",
"=",
"unzip",
"(",
"seq",
")",
"does",
"not",
"fail",
"even",
"when",
"seq",
"is",
"empty",
".",
"Returns",
"-------",
"seqs",
":",
"iterable",
"[",
"iterable",
"]",
"The",
"new",
"sequences",
"pulled",
"out",
"of",
"the",
"first",
"iterable",
".",
"Raises",
"------",
"ValueError",
"Raised",
"when",
"seq",
"is",
"empty",
"and",
"elem_len",
"is",
"not",
"provided",
".",
"Raised",
"when",
"elements",
"of",
"seq",
"do",
"not",
"match",
"the",
"given",
"elem_len",
"or",
"the",
"length",
"of",
"the",
"first",
"element",
"of",
"seq",
".",
"Examples",
"--------",
">>>",
"seq",
"=",
"[",
"(",
"a",
"1",
")",
"(",
"b",
"2",
")",
"(",
"c",
"3",
")",
"]",
">>>",
"cs",
"ns",
"=",
"unzip",
"(",
"seq",
")",
">>>",
"cs",
"(",
"a",
"b",
"c",
")",
">>>",
"ns",
"(",
"1",
"2",
"3",
")"
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/functional.py#L190-L250
|
train
|
quantopian/zipline
|
zipline/utils/functional.py
|
getattrs
|
def getattrs(value, attrs, default=_no_default):
"""
Perform a chained application of ``getattr`` on ``value`` with the values
in ``attrs``.
If ``default`` is supplied, return it if any of the attribute lookups fail.
Parameters
----------
value : object
Root of the lookup chain.
attrs : iterable[str]
Sequence of attributes to look up.
default : object, optional
Value to return if any of the lookups fail.
Returns
-------
result : object
Result of the lookup sequence.
Examples
--------
>>> class EmptyObject(object):
... pass
...
>>> obj = EmptyObject()
>>> obj.foo = EmptyObject()
>>> obj.foo.bar = "value"
>>> getattrs(obj, ('foo', 'bar'))
'value'
>>> getattrs(obj, ('foo', 'buzz'))
Traceback (most recent call last):
...
AttributeError: 'EmptyObject' object has no attribute 'buzz'
>>> getattrs(obj, ('foo', 'buzz'), 'default')
'default'
"""
try:
for attr in attrs:
value = getattr(value, attr)
except AttributeError:
if default is _no_default:
raise
value = default
return value
|
python
|
def getattrs(value, attrs, default=_no_default):
"""
Perform a chained application of ``getattr`` on ``value`` with the values
in ``attrs``.
If ``default`` is supplied, return it if any of the attribute lookups fail.
Parameters
----------
value : object
Root of the lookup chain.
attrs : iterable[str]
Sequence of attributes to look up.
default : object, optional
Value to return if any of the lookups fail.
Returns
-------
result : object
Result of the lookup sequence.
Examples
--------
>>> class EmptyObject(object):
... pass
...
>>> obj = EmptyObject()
>>> obj.foo = EmptyObject()
>>> obj.foo.bar = "value"
>>> getattrs(obj, ('foo', 'bar'))
'value'
>>> getattrs(obj, ('foo', 'buzz'))
Traceback (most recent call last):
...
AttributeError: 'EmptyObject' object has no attribute 'buzz'
>>> getattrs(obj, ('foo', 'buzz'), 'default')
'default'
"""
try:
for attr in attrs:
value = getattr(value, attr)
except AttributeError:
if default is _no_default:
raise
value = default
return value
|
[
"def",
"getattrs",
"(",
"value",
",",
"attrs",
",",
"default",
"=",
"_no_default",
")",
":",
"try",
":",
"for",
"attr",
"in",
"attrs",
":",
"value",
"=",
"getattr",
"(",
"value",
",",
"attr",
")",
"except",
"AttributeError",
":",
"if",
"default",
"is",
"_no_default",
":",
"raise",
"value",
"=",
"default",
"return",
"value"
] |
Perform a chained application of ``getattr`` on ``value`` with the values
in ``attrs``.
If ``default`` is supplied, return it if any of the attribute lookups fail.
Parameters
----------
value : object
Root of the lookup chain.
attrs : iterable[str]
Sequence of attributes to look up.
default : object, optional
Value to return if any of the lookups fail.
Returns
-------
result : object
Result of the lookup sequence.
Examples
--------
>>> class EmptyObject(object):
... pass
...
>>> obj = EmptyObject()
>>> obj.foo = EmptyObject()
>>> obj.foo.bar = "value"
>>> getattrs(obj, ('foo', 'bar'))
'value'
>>> getattrs(obj, ('foo', 'buzz'))
Traceback (most recent call last):
...
AttributeError: 'EmptyObject' object has no attribute 'buzz'
>>> getattrs(obj, ('foo', 'buzz'), 'default')
'default'
|
[
"Perform",
"a",
"chained",
"application",
"of",
"getattr",
"on",
"value",
"with",
"the",
"values",
"in",
"attrs",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/functional.py#L256-L303
|
train
|
quantopian/zipline
|
zipline/utils/functional.py
|
set_attribute
|
def set_attribute(name, value):
"""
Decorator factory for setting attributes on a function.
Doesn't change the behavior of the wrapped function.
Examples
--------
>>> @set_attribute('__name__', 'foo')
... def bar():
... return 3
...
>>> bar()
3
>>> bar.__name__
'foo'
"""
def decorator(f):
setattr(f, name, value)
return f
return decorator
|
python
|
def set_attribute(name, value):
"""
Decorator factory for setting attributes on a function.
Doesn't change the behavior of the wrapped function.
Examples
--------
>>> @set_attribute('__name__', 'foo')
... def bar():
... return 3
...
>>> bar()
3
>>> bar.__name__
'foo'
"""
def decorator(f):
setattr(f, name, value)
return f
return decorator
|
[
"def",
"set_attribute",
"(",
"name",
",",
"value",
")",
":",
"def",
"decorator",
"(",
"f",
")",
":",
"setattr",
"(",
"f",
",",
"name",
",",
"value",
")",
"return",
"f",
"return",
"decorator"
] |
Decorator factory for setting attributes on a function.
Doesn't change the behavior of the wrapped function.
Examples
--------
>>> @set_attribute('__name__', 'foo')
... def bar():
... return 3
...
>>> bar()
3
>>> bar.__name__
'foo'
|
[
"Decorator",
"factory",
"for",
"setting",
"attributes",
"on",
"a",
"function",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/functional.py#L307-L327
|
train
|
quantopian/zipline
|
zipline/utils/functional.py
|
foldr
|
def foldr(f, seq, default=_no_default):
"""Fold a function over a sequence with right associativity.
Parameters
----------
f : callable[any, any]
The function to reduce the sequence with.
The first argument will be the element of the sequence; the second
argument will be the accumulator.
seq : iterable[any]
The sequence to reduce.
default : any, optional
The starting value to reduce with. If not provided, the sequence
cannot be empty, and the last value of the sequence will be used.
Returns
-------
folded : any
The folded value.
Notes
-----
This functions works by reducing the list in a right associative way.
For example, imagine we are folding with ``operator.add`` or ``+``:
.. code-block:: python
foldr(add, seq) -> seq[0] + (seq[1] + (seq[2] + (...seq[-1], default)))
In the more general case with an arbitrary function, ``foldr`` will expand
like so:
.. code-block:: python
foldr(f, seq) -> f(seq[0], f(seq[1], f(seq[2], ...f(seq[-1], default))))
For a more in depth discussion of left and right folds, see:
`https://en.wikipedia.org/wiki/Fold_(higher-order_function)`_
The images in that page are very good for showing the differences between
``foldr`` and ``foldl`` (``reduce``).
.. note::
For performance reasons is is best to pass a strict (non-lazy) sequence,
for example, a list.
See Also
--------
:func:`functools.reduce`
:func:`sum`
"""
return reduce(
flip(f),
reversed(seq),
*(default,) if default is not _no_default else ()
)
|
python
|
def foldr(f, seq, default=_no_default):
"""Fold a function over a sequence with right associativity.
Parameters
----------
f : callable[any, any]
The function to reduce the sequence with.
The first argument will be the element of the sequence; the second
argument will be the accumulator.
seq : iterable[any]
The sequence to reduce.
default : any, optional
The starting value to reduce with. If not provided, the sequence
cannot be empty, and the last value of the sequence will be used.
Returns
-------
folded : any
The folded value.
Notes
-----
This functions works by reducing the list in a right associative way.
For example, imagine we are folding with ``operator.add`` or ``+``:
.. code-block:: python
foldr(add, seq) -> seq[0] + (seq[1] + (seq[2] + (...seq[-1], default)))
In the more general case with an arbitrary function, ``foldr`` will expand
like so:
.. code-block:: python
foldr(f, seq) -> f(seq[0], f(seq[1], f(seq[2], ...f(seq[-1], default))))
For a more in depth discussion of left and right folds, see:
`https://en.wikipedia.org/wiki/Fold_(higher-order_function)`_
The images in that page are very good for showing the differences between
``foldr`` and ``foldl`` (``reduce``).
.. note::
For performance reasons is is best to pass a strict (non-lazy) sequence,
for example, a list.
See Also
--------
:func:`functools.reduce`
:func:`sum`
"""
return reduce(
flip(f),
reversed(seq),
*(default,) if default is not _no_default else ()
)
|
[
"def",
"foldr",
"(",
"f",
",",
"seq",
",",
"default",
"=",
"_no_default",
")",
":",
"return",
"reduce",
"(",
"flip",
"(",
"f",
")",
",",
"reversed",
"(",
"seq",
")",
",",
"*",
"(",
"default",
",",
")",
"if",
"default",
"is",
"not",
"_no_default",
"else",
"(",
")",
")"
] |
Fold a function over a sequence with right associativity.
Parameters
----------
f : callable[any, any]
The function to reduce the sequence with.
The first argument will be the element of the sequence; the second
argument will be the accumulator.
seq : iterable[any]
The sequence to reduce.
default : any, optional
The starting value to reduce with. If not provided, the sequence
cannot be empty, and the last value of the sequence will be used.
Returns
-------
folded : any
The folded value.
Notes
-----
This functions works by reducing the list in a right associative way.
For example, imagine we are folding with ``operator.add`` or ``+``:
.. code-block:: python
foldr(add, seq) -> seq[0] + (seq[1] + (seq[2] + (...seq[-1], default)))
In the more general case with an arbitrary function, ``foldr`` will expand
like so:
.. code-block:: python
foldr(f, seq) -> f(seq[0], f(seq[1], f(seq[2], ...f(seq[-1], default))))
For a more in depth discussion of left and right folds, see:
`https://en.wikipedia.org/wiki/Fold_(higher-order_function)`_
The images in that page are very good for showing the differences between
``foldr`` and ``foldl`` (``reduce``).
.. note::
For performance reasons is is best to pass a strict (non-lazy) sequence,
for example, a list.
See Also
--------
:func:`functools.reduce`
:func:`sum`
|
[
"Fold",
"a",
"function",
"over",
"a",
"sequence",
"with",
"right",
"associativity",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/functional.py#L337-L393
|
train
|
quantopian/zipline
|
zipline/utils/functional.py
|
invert
|
def invert(d):
"""
Invert a dictionary into a dictionary of sets.
>>> invert({'a': 1, 'b': 2, 'c': 1}) # doctest: +SKIP
{1: {'a', 'c'}, 2: {'b'}}
"""
out = {}
for k, v in iteritems(d):
try:
out[v].add(k)
except KeyError:
out[v] = {k}
return out
|
python
|
def invert(d):
"""
Invert a dictionary into a dictionary of sets.
>>> invert({'a': 1, 'b': 2, 'c': 1}) # doctest: +SKIP
{1: {'a', 'c'}, 2: {'b'}}
"""
out = {}
for k, v in iteritems(d):
try:
out[v].add(k)
except KeyError:
out[v] = {k}
return out
|
[
"def",
"invert",
"(",
"d",
")",
":",
"out",
"=",
"{",
"}",
"for",
"k",
",",
"v",
"in",
"iteritems",
"(",
"d",
")",
":",
"try",
":",
"out",
"[",
"v",
"]",
".",
"add",
"(",
"k",
")",
"except",
"KeyError",
":",
"out",
"[",
"v",
"]",
"=",
"{",
"k",
"}",
"return",
"out"
] |
Invert a dictionary into a dictionary of sets.
>>> invert({'a': 1, 'b': 2, 'c': 1}) # doctest: +SKIP
{1: {'a', 'c'}, 2: {'b'}}
|
[
"Invert",
"a",
"dictionary",
"into",
"a",
"dictionary",
"of",
"sets",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/functional.py#L396-L409
|
train
|
quantopian/zipline
|
zipline/examples/olmar.py
|
simplex_projection
|
def simplex_projection(v, b=1):
r"""Projection vectors to the simplex domain
Implemented according to the paper: Efficient projections onto the
l1-ball for learning in high dimensions, John Duchi, et al. ICML 2008.
Implementation Time: 2011 June 17 by Bin@libin AT pmail.ntu.edu.sg
Optimization Problem: min_{w}\| w - v \|_{2}^{2}
s.t. sum_{i=1}^{m}=z, w_{i}\geq 0
Input: A vector v \in R^{m}, and a scalar z > 0 (default=1)
Output: Projection vector w
:Example:
>>> proj = simplex_projection([.4 ,.3, -.4, .5])
>>> proj # doctest: +NORMALIZE_WHITESPACE
array([ 0.33333333, 0.23333333, 0. , 0.43333333])
>>> print(proj.sum())
1.0
Original matlab implementation: John Duchi (jduchi@cs.berkeley.edu)
Python-port: Copyright 2013 by Thomas Wiecki (thomas.wiecki@gmail.com).
"""
v = np.asarray(v)
p = len(v)
# Sort v into u in descending order
v = (v > 0) * v
u = np.sort(v)[::-1]
sv = np.cumsum(u)
rho = np.where(u > (sv - b) / np.arange(1, p + 1))[0][-1]
theta = np.max([0, (sv[rho] - b) / (rho + 1)])
w = (v - theta)
w[w < 0] = 0
return w
|
python
|
def simplex_projection(v, b=1):
r"""Projection vectors to the simplex domain
Implemented according to the paper: Efficient projections onto the
l1-ball for learning in high dimensions, John Duchi, et al. ICML 2008.
Implementation Time: 2011 June 17 by Bin@libin AT pmail.ntu.edu.sg
Optimization Problem: min_{w}\| w - v \|_{2}^{2}
s.t. sum_{i=1}^{m}=z, w_{i}\geq 0
Input: A vector v \in R^{m}, and a scalar z > 0 (default=1)
Output: Projection vector w
:Example:
>>> proj = simplex_projection([.4 ,.3, -.4, .5])
>>> proj # doctest: +NORMALIZE_WHITESPACE
array([ 0.33333333, 0.23333333, 0. , 0.43333333])
>>> print(proj.sum())
1.0
Original matlab implementation: John Duchi (jduchi@cs.berkeley.edu)
Python-port: Copyright 2013 by Thomas Wiecki (thomas.wiecki@gmail.com).
"""
v = np.asarray(v)
p = len(v)
# Sort v into u in descending order
v = (v > 0) * v
u = np.sort(v)[::-1]
sv = np.cumsum(u)
rho = np.where(u > (sv - b) / np.arange(1, p + 1))[0][-1]
theta = np.max([0, (sv[rho] - b) / (rho + 1)])
w = (v - theta)
w[w < 0] = 0
return w
|
[
"def",
"simplex_projection",
"(",
"v",
",",
"b",
"=",
"1",
")",
":",
"v",
"=",
"np",
".",
"asarray",
"(",
"v",
")",
"p",
"=",
"len",
"(",
"v",
")",
"# Sort v into u in descending order",
"v",
"=",
"(",
"v",
">",
"0",
")",
"*",
"v",
"u",
"=",
"np",
".",
"sort",
"(",
"v",
")",
"[",
":",
":",
"-",
"1",
"]",
"sv",
"=",
"np",
".",
"cumsum",
"(",
"u",
")",
"rho",
"=",
"np",
".",
"where",
"(",
"u",
">",
"(",
"sv",
"-",
"b",
")",
"/",
"np",
".",
"arange",
"(",
"1",
",",
"p",
"+",
"1",
")",
")",
"[",
"0",
"]",
"[",
"-",
"1",
"]",
"theta",
"=",
"np",
".",
"max",
"(",
"[",
"0",
",",
"(",
"sv",
"[",
"rho",
"]",
"-",
"b",
")",
"/",
"(",
"rho",
"+",
"1",
")",
"]",
")",
"w",
"=",
"(",
"v",
"-",
"theta",
")",
"w",
"[",
"w",
"<",
"0",
"]",
"=",
"0",
"return",
"w"
] |
r"""Projection vectors to the simplex domain
Implemented according to the paper: Efficient projections onto the
l1-ball for learning in high dimensions, John Duchi, et al. ICML 2008.
Implementation Time: 2011 June 17 by Bin@libin AT pmail.ntu.edu.sg
Optimization Problem: min_{w}\| w - v \|_{2}^{2}
s.t. sum_{i=1}^{m}=z, w_{i}\geq 0
Input: A vector v \in R^{m}, and a scalar z > 0 (default=1)
Output: Projection vector w
:Example:
>>> proj = simplex_projection([.4 ,.3, -.4, .5])
>>> proj # doctest: +NORMALIZE_WHITESPACE
array([ 0.33333333, 0.23333333, 0. , 0.43333333])
>>> print(proj.sum())
1.0
Original matlab implementation: John Duchi (jduchi@cs.berkeley.edu)
Python-port: Copyright 2013 by Thomas Wiecki (thomas.wiecki@gmail.com).
|
[
"r",
"Projection",
"vectors",
"to",
"the",
"simplex",
"domain"
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/examples/olmar.py#L111-L146
|
train
|
quantopian/zipline
|
zipline/examples/__init__.py
|
run_example
|
def run_example(example_name, environ):
"""
Run an example module from zipline.examples.
"""
mod = EXAMPLE_MODULES[example_name]
register_calendar("YAHOO", get_calendar("NYSE"), force=True)
return run_algorithm(
initialize=getattr(mod, 'initialize', None),
handle_data=getattr(mod, 'handle_data', None),
before_trading_start=getattr(mod, 'before_trading_start', None),
analyze=getattr(mod, 'analyze', None),
bundle='test',
environ=environ,
# Provide a default capital base, but allow the test to override.
**merge({'capital_base': 1e7}, mod._test_args())
)
|
python
|
def run_example(example_name, environ):
"""
Run an example module from zipline.examples.
"""
mod = EXAMPLE_MODULES[example_name]
register_calendar("YAHOO", get_calendar("NYSE"), force=True)
return run_algorithm(
initialize=getattr(mod, 'initialize', None),
handle_data=getattr(mod, 'handle_data', None),
before_trading_start=getattr(mod, 'before_trading_start', None),
analyze=getattr(mod, 'analyze', None),
bundle='test',
environ=environ,
# Provide a default capital base, but allow the test to override.
**merge({'capital_base': 1e7}, mod._test_args())
)
|
[
"def",
"run_example",
"(",
"example_name",
",",
"environ",
")",
":",
"mod",
"=",
"EXAMPLE_MODULES",
"[",
"example_name",
"]",
"register_calendar",
"(",
"\"YAHOO\"",
",",
"get_calendar",
"(",
"\"NYSE\"",
")",
",",
"force",
"=",
"True",
")",
"return",
"run_algorithm",
"(",
"initialize",
"=",
"getattr",
"(",
"mod",
",",
"'initialize'",
",",
"None",
")",
",",
"handle_data",
"=",
"getattr",
"(",
"mod",
",",
"'handle_data'",
",",
"None",
")",
",",
"before_trading_start",
"=",
"getattr",
"(",
"mod",
",",
"'before_trading_start'",
",",
"None",
")",
",",
"analyze",
"=",
"getattr",
"(",
"mod",
",",
"'analyze'",
",",
"None",
")",
",",
"bundle",
"=",
"'test'",
",",
"environ",
"=",
"environ",
",",
"# Provide a default capital base, but allow the test to override.",
"*",
"*",
"merge",
"(",
"{",
"'capital_base'",
":",
"1e7",
"}",
",",
"mod",
".",
"_test_args",
"(",
")",
")",
")"
] |
Run an example module from zipline.examples.
|
[
"Run",
"an",
"example",
"module",
"from",
"zipline",
".",
"examples",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/examples/__init__.py#L64-L81
|
train
|
quantopian/zipline
|
zipline/pipeline/factors/statistical.py
|
vectorized_beta
|
def vectorized_beta(dependents, independent, allowed_missing, out=None):
"""
Compute slopes of linear regressions between columns of ``dependents`` and
``independent``.
Parameters
----------
dependents : np.array[N, M]
Array with columns of data to be regressed against ``independent``.
independent : np.array[N, 1]
Independent variable of the regression
allowed_missing : int
Number of allowed missing (NaN) observations per column. Columns with
more than this many non-nan observations in both ``dependents`` and
``independents`` will output NaN as the regression coefficient.
Returns
-------
slopes : np.array[M]
Linear regression coefficients for each column of ``dependents``.
"""
# Cache these as locals since we're going to call them multiple times.
nan = np.nan
isnan = np.isnan
N, M = dependents.shape
if out is None:
out = np.full(M, nan)
# Copy N times as a column vector and fill with nans to have the same
# missing value pattern as the dependent variable.
#
# PERF_TODO: We could probably avoid the space blowup by doing this in
# Cython.
# shape: (N, M)
independent = np.where(
isnan(dependents),
nan,
independent,
)
# Calculate beta as Cov(X, Y) / Cov(X, X).
# https://en.wikipedia.org/wiki/Simple_linear_regression#Fitting_the_regression_line # noqa
#
# NOTE: The usual formula for covariance is::
#
# mean((X - mean(X)) * (Y - mean(Y)))
#
# However, we don't actually need to take the mean of both sides of the
# product, because of the folllowing equivalence::
#
# Let X_res = (X - mean(X)).
# We have:
#
# mean(X_res * (Y - mean(Y))) = mean(X_res * (Y - mean(Y)))
# (1) = mean((X_res * Y) - (X_res * mean(Y)))
# (2) = mean(X_res * Y) - mean(X_res * mean(Y))
# (3) = mean(X_res * Y) - mean(X_res) * mean(Y)
# (4) = mean(X_res * Y) - 0 * mean(Y)
# (5) = mean(X_res * Y)
#
#
# The tricky step in the above derivation is step (4). We know that
# mean(X_res) is zero because, for any X:
#
# mean(X - mean(X)) = mean(X) - mean(X) = 0.
#
# The upshot of this is that we only have to center one of `independent`
# and `dependent` when calculating covariances. Since we need the centered
# `independent` to calculate its variance in the next step, we choose to
# center `independent`.
# shape: (N, M)
ind_residual = independent - nanmean(independent, axis=0)
# shape: (M,)
covariances = nanmean(ind_residual * dependents, axis=0)
# We end up with different variances in each column here because each
# column may have a different subset of the data dropped due to missing
# data in the corresponding dependent column.
# shape: (M,)
independent_variances = nanmean(ind_residual ** 2, axis=0)
# shape: (M,)
np.divide(covariances, independent_variances, out=out)
# Write nans back to locations where we have more then allowed number of
# missing entries.
nanlocs = isnan(independent).sum(axis=0) > allowed_missing
out[nanlocs] = nan
return out
|
python
|
def vectorized_beta(dependents, independent, allowed_missing, out=None):
"""
Compute slopes of linear regressions between columns of ``dependents`` and
``independent``.
Parameters
----------
dependents : np.array[N, M]
Array with columns of data to be regressed against ``independent``.
independent : np.array[N, 1]
Independent variable of the regression
allowed_missing : int
Number of allowed missing (NaN) observations per column. Columns with
more than this many non-nan observations in both ``dependents`` and
``independents`` will output NaN as the regression coefficient.
Returns
-------
slopes : np.array[M]
Linear regression coefficients for each column of ``dependents``.
"""
# Cache these as locals since we're going to call them multiple times.
nan = np.nan
isnan = np.isnan
N, M = dependents.shape
if out is None:
out = np.full(M, nan)
# Copy N times as a column vector and fill with nans to have the same
# missing value pattern as the dependent variable.
#
# PERF_TODO: We could probably avoid the space blowup by doing this in
# Cython.
# shape: (N, M)
independent = np.where(
isnan(dependents),
nan,
independent,
)
# Calculate beta as Cov(X, Y) / Cov(X, X).
# https://en.wikipedia.org/wiki/Simple_linear_regression#Fitting_the_regression_line # noqa
#
# NOTE: The usual formula for covariance is::
#
# mean((X - mean(X)) * (Y - mean(Y)))
#
# However, we don't actually need to take the mean of both sides of the
# product, because of the folllowing equivalence::
#
# Let X_res = (X - mean(X)).
# We have:
#
# mean(X_res * (Y - mean(Y))) = mean(X_res * (Y - mean(Y)))
# (1) = mean((X_res * Y) - (X_res * mean(Y)))
# (2) = mean(X_res * Y) - mean(X_res * mean(Y))
# (3) = mean(X_res * Y) - mean(X_res) * mean(Y)
# (4) = mean(X_res * Y) - 0 * mean(Y)
# (5) = mean(X_res * Y)
#
#
# The tricky step in the above derivation is step (4). We know that
# mean(X_res) is zero because, for any X:
#
# mean(X - mean(X)) = mean(X) - mean(X) = 0.
#
# The upshot of this is that we only have to center one of `independent`
# and `dependent` when calculating covariances. Since we need the centered
# `independent` to calculate its variance in the next step, we choose to
# center `independent`.
# shape: (N, M)
ind_residual = independent - nanmean(independent, axis=0)
# shape: (M,)
covariances = nanmean(ind_residual * dependents, axis=0)
# We end up with different variances in each column here because each
# column may have a different subset of the data dropped due to missing
# data in the corresponding dependent column.
# shape: (M,)
independent_variances = nanmean(ind_residual ** 2, axis=0)
# shape: (M,)
np.divide(covariances, independent_variances, out=out)
# Write nans back to locations where we have more then allowed number of
# missing entries.
nanlocs = isnan(independent).sum(axis=0) > allowed_missing
out[nanlocs] = nan
return out
|
[
"def",
"vectorized_beta",
"(",
"dependents",
",",
"independent",
",",
"allowed_missing",
",",
"out",
"=",
"None",
")",
":",
"# Cache these as locals since we're going to call them multiple times.",
"nan",
"=",
"np",
".",
"nan",
"isnan",
"=",
"np",
".",
"isnan",
"N",
",",
"M",
"=",
"dependents",
".",
"shape",
"if",
"out",
"is",
"None",
":",
"out",
"=",
"np",
".",
"full",
"(",
"M",
",",
"nan",
")",
"# Copy N times as a column vector and fill with nans to have the same",
"# missing value pattern as the dependent variable.",
"#",
"# PERF_TODO: We could probably avoid the space blowup by doing this in",
"# Cython.",
"# shape: (N, M)",
"independent",
"=",
"np",
".",
"where",
"(",
"isnan",
"(",
"dependents",
")",
",",
"nan",
",",
"independent",
",",
")",
"# Calculate beta as Cov(X, Y) / Cov(X, X).",
"# https://en.wikipedia.org/wiki/Simple_linear_regression#Fitting_the_regression_line # noqa",
"#",
"# NOTE: The usual formula for covariance is::",
"#",
"# mean((X - mean(X)) * (Y - mean(Y)))",
"#",
"# However, we don't actually need to take the mean of both sides of the",
"# product, because of the folllowing equivalence::",
"#",
"# Let X_res = (X - mean(X)).",
"# We have:",
"#",
"# mean(X_res * (Y - mean(Y))) = mean(X_res * (Y - mean(Y)))",
"# (1) = mean((X_res * Y) - (X_res * mean(Y)))",
"# (2) = mean(X_res * Y) - mean(X_res * mean(Y))",
"# (3) = mean(X_res * Y) - mean(X_res) * mean(Y)",
"# (4) = mean(X_res * Y) - 0 * mean(Y)",
"# (5) = mean(X_res * Y)",
"#",
"#",
"# The tricky step in the above derivation is step (4). We know that",
"# mean(X_res) is zero because, for any X:",
"#",
"# mean(X - mean(X)) = mean(X) - mean(X) = 0.",
"#",
"# The upshot of this is that we only have to center one of `independent`",
"# and `dependent` when calculating covariances. Since we need the centered",
"# `independent` to calculate its variance in the next step, we choose to",
"# center `independent`.",
"# shape: (N, M)",
"ind_residual",
"=",
"independent",
"-",
"nanmean",
"(",
"independent",
",",
"axis",
"=",
"0",
")",
"# shape: (M,)",
"covariances",
"=",
"nanmean",
"(",
"ind_residual",
"*",
"dependents",
",",
"axis",
"=",
"0",
")",
"# We end up with different variances in each column here because each",
"# column may have a different subset of the data dropped due to missing",
"# data in the corresponding dependent column.",
"# shape: (M,)",
"independent_variances",
"=",
"nanmean",
"(",
"ind_residual",
"**",
"2",
",",
"axis",
"=",
"0",
")",
"# shape: (M,)",
"np",
".",
"divide",
"(",
"covariances",
",",
"independent_variances",
",",
"out",
"=",
"out",
")",
"# Write nans back to locations where we have more then allowed number of",
"# missing entries.",
"nanlocs",
"=",
"isnan",
"(",
"independent",
")",
".",
"sum",
"(",
"axis",
"=",
"0",
")",
">",
"allowed_missing",
"out",
"[",
"nanlocs",
"]",
"=",
"nan",
"return",
"out"
] |
Compute slopes of linear regressions between columns of ``dependents`` and
``independent``.
Parameters
----------
dependents : np.array[N, M]
Array with columns of data to be regressed against ``independent``.
independent : np.array[N, 1]
Independent variable of the regression
allowed_missing : int
Number of allowed missing (NaN) observations per column. Columns with
more than this many non-nan observations in both ``dependents`` and
``independents`` will output NaN as the regression coefficient.
Returns
-------
slopes : np.array[M]
Linear regression coefficients for each column of ``dependents``.
|
[
"Compute",
"slopes",
"of",
"linear",
"regressions",
"between",
"columns",
"of",
"dependents",
"and",
"independent",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/factors/statistical.py#L572-L665
|
train
|
quantopian/zipline
|
zipline/data/treasuries_can.py
|
_format_url
|
def _format_url(instrument_type,
instrument_ids,
start_date,
end_date,
earliest_allowed_date):
"""
Format a URL for loading data from Bank of Canada.
"""
return (
"http://www.bankofcanada.ca/stats/results/csv"
"?lP=lookup_{instrument_type}_yields.php"
"&sR={restrict}"
"&se={instrument_ids}"
"&dF={start}"
"&dT={end}".format(
instrument_type=instrument_type,
instrument_ids='-'.join(map(prepend("L_"), instrument_ids)),
restrict=earliest_allowed_date.strftime("%Y-%m-%d"),
start=start_date.strftime("%Y-%m-%d"),
end=end_date.strftime("%Y-%m-%d"),
)
)
|
python
|
def _format_url(instrument_type,
instrument_ids,
start_date,
end_date,
earliest_allowed_date):
"""
Format a URL for loading data from Bank of Canada.
"""
return (
"http://www.bankofcanada.ca/stats/results/csv"
"?lP=lookup_{instrument_type}_yields.php"
"&sR={restrict}"
"&se={instrument_ids}"
"&dF={start}"
"&dT={end}".format(
instrument_type=instrument_type,
instrument_ids='-'.join(map(prepend("L_"), instrument_ids)),
restrict=earliest_allowed_date.strftime("%Y-%m-%d"),
start=start_date.strftime("%Y-%m-%d"),
end=end_date.strftime("%Y-%m-%d"),
)
)
|
[
"def",
"_format_url",
"(",
"instrument_type",
",",
"instrument_ids",
",",
"start_date",
",",
"end_date",
",",
"earliest_allowed_date",
")",
":",
"return",
"(",
"\"http://www.bankofcanada.ca/stats/results/csv\"",
"\"?lP=lookup_{instrument_type}_yields.php\"",
"\"&sR={restrict}\"",
"\"&se={instrument_ids}\"",
"\"&dF={start}\"",
"\"&dT={end}\"",
".",
"format",
"(",
"instrument_type",
"=",
"instrument_type",
",",
"instrument_ids",
"=",
"'-'",
".",
"join",
"(",
"map",
"(",
"prepend",
"(",
"\"L_\"",
")",
",",
"instrument_ids",
")",
")",
",",
"restrict",
"=",
"earliest_allowed_date",
".",
"strftime",
"(",
"\"%Y-%m-%d\"",
")",
",",
"start",
"=",
"start_date",
".",
"strftime",
"(",
"\"%Y-%m-%d\"",
")",
",",
"end",
"=",
"end_date",
".",
"strftime",
"(",
"\"%Y-%m-%d\"",
")",
",",
")",
")"
] |
Format a URL for loading data from Bank of Canada.
|
[
"Format",
"a",
"URL",
"for",
"loading",
"data",
"from",
"Bank",
"of",
"Canada",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/treasuries_can.py#L39-L60
|
train
|
quantopian/zipline
|
zipline/data/treasuries_can.py
|
load_frame
|
def load_frame(url, skiprows):
"""
Load a DataFrame of data from a Bank of Canada site.
"""
return pd.read_csv(
url,
skiprows=skiprows,
skipinitialspace=True,
na_values=["Bank holiday", "Not available"],
parse_dates=["Date"],
index_col="Date",
).dropna(how='all') \
.tz_localize('UTC') \
.rename(columns=COLUMN_NAMES)
|
python
|
def load_frame(url, skiprows):
"""
Load a DataFrame of data from a Bank of Canada site.
"""
return pd.read_csv(
url,
skiprows=skiprows,
skipinitialspace=True,
na_values=["Bank holiday", "Not available"],
parse_dates=["Date"],
index_col="Date",
).dropna(how='all') \
.tz_localize('UTC') \
.rename(columns=COLUMN_NAMES)
|
[
"def",
"load_frame",
"(",
"url",
",",
"skiprows",
")",
":",
"return",
"pd",
".",
"read_csv",
"(",
"url",
",",
"skiprows",
"=",
"skiprows",
",",
"skipinitialspace",
"=",
"True",
",",
"na_values",
"=",
"[",
"\"Bank holiday\"",
",",
"\"Not available\"",
"]",
",",
"parse_dates",
"=",
"[",
"\"Date\"",
"]",
",",
"index_col",
"=",
"\"Date\"",
",",
")",
".",
"dropna",
"(",
"how",
"=",
"'all'",
")",
".",
"tz_localize",
"(",
"'UTC'",
")",
".",
"rename",
"(",
"columns",
"=",
"COLUMN_NAMES",
")"
] |
Load a DataFrame of data from a Bank of Canada site.
|
[
"Load",
"a",
"DataFrame",
"of",
"data",
"from",
"a",
"Bank",
"of",
"Canada",
"site",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/treasuries_can.py#L67-L80
|
train
|
quantopian/zipline
|
zipline/data/treasuries_can.py
|
check_known_inconsistencies
|
def check_known_inconsistencies(bill_data, bond_data):
"""
There are a couple quirks in the data provided by Bank of Canada.
Check that no new quirks have been introduced in the latest download.
"""
inconsistent_dates = bill_data.index.sym_diff(bond_data.index)
known_inconsistencies = [
# bill_data has an entry for 2010-02-15, which bond_data doesn't.
# bond_data has an entry for 2006-09-04, which bill_data doesn't.
# Both of these dates are bank holidays (Flag Day and Labor Day,
# respectively).
pd.Timestamp('2006-09-04', tz='UTC'),
pd.Timestamp('2010-02-15', tz='UTC'),
# 2013-07-25 comes back as "Not available" from the bills endpoint.
# This date doesn't seem to be a bank holiday, but the previous
# calendar implementation dropped this entry, so we drop it as well.
# If someone cares deeply about the integrity of the Canadian trading
# calendar, they may want to consider forward-filling here rather than
# dropping the row.
pd.Timestamp('2013-07-25', tz='UTC'),
]
unexpected_inconsistences = inconsistent_dates.drop(known_inconsistencies)
if len(unexpected_inconsistences):
in_bills = bill_data.index.difference(bond_data.index).difference(
known_inconsistencies
)
in_bonds = bond_data.index.difference(bill_data.index).difference(
known_inconsistencies
)
raise ValueError(
"Inconsistent dates for Canadian treasury bills vs bonds. \n"
"Dates with bills but not bonds: {in_bills}.\n"
"Dates with bonds but not bills: {in_bonds}.".format(
in_bills=in_bills,
in_bonds=in_bonds,
)
)
|
python
|
def check_known_inconsistencies(bill_data, bond_data):
"""
There are a couple quirks in the data provided by Bank of Canada.
Check that no new quirks have been introduced in the latest download.
"""
inconsistent_dates = bill_data.index.sym_diff(bond_data.index)
known_inconsistencies = [
# bill_data has an entry for 2010-02-15, which bond_data doesn't.
# bond_data has an entry for 2006-09-04, which bill_data doesn't.
# Both of these dates are bank holidays (Flag Day and Labor Day,
# respectively).
pd.Timestamp('2006-09-04', tz='UTC'),
pd.Timestamp('2010-02-15', tz='UTC'),
# 2013-07-25 comes back as "Not available" from the bills endpoint.
# This date doesn't seem to be a bank holiday, but the previous
# calendar implementation dropped this entry, so we drop it as well.
# If someone cares deeply about the integrity of the Canadian trading
# calendar, they may want to consider forward-filling here rather than
# dropping the row.
pd.Timestamp('2013-07-25', tz='UTC'),
]
unexpected_inconsistences = inconsistent_dates.drop(known_inconsistencies)
if len(unexpected_inconsistences):
in_bills = bill_data.index.difference(bond_data.index).difference(
known_inconsistencies
)
in_bonds = bond_data.index.difference(bill_data.index).difference(
known_inconsistencies
)
raise ValueError(
"Inconsistent dates for Canadian treasury bills vs bonds. \n"
"Dates with bills but not bonds: {in_bills}.\n"
"Dates with bonds but not bills: {in_bonds}.".format(
in_bills=in_bills,
in_bonds=in_bonds,
)
)
|
[
"def",
"check_known_inconsistencies",
"(",
"bill_data",
",",
"bond_data",
")",
":",
"inconsistent_dates",
"=",
"bill_data",
".",
"index",
".",
"sym_diff",
"(",
"bond_data",
".",
"index",
")",
"known_inconsistencies",
"=",
"[",
"# bill_data has an entry for 2010-02-15, which bond_data doesn't.",
"# bond_data has an entry for 2006-09-04, which bill_data doesn't.",
"# Both of these dates are bank holidays (Flag Day and Labor Day,",
"# respectively).",
"pd",
".",
"Timestamp",
"(",
"'2006-09-04'",
",",
"tz",
"=",
"'UTC'",
")",
",",
"pd",
".",
"Timestamp",
"(",
"'2010-02-15'",
",",
"tz",
"=",
"'UTC'",
")",
",",
"# 2013-07-25 comes back as \"Not available\" from the bills endpoint.",
"# This date doesn't seem to be a bank holiday, but the previous",
"# calendar implementation dropped this entry, so we drop it as well.",
"# If someone cares deeply about the integrity of the Canadian trading",
"# calendar, they may want to consider forward-filling here rather than",
"# dropping the row.",
"pd",
".",
"Timestamp",
"(",
"'2013-07-25'",
",",
"tz",
"=",
"'UTC'",
")",
",",
"]",
"unexpected_inconsistences",
"=",
"inconsistent_dates",
".",
"drop",
"(",
"known_inconsistencies",
")",
"if",
"len",
"(",
"unexpected_inconsistences",
")",
":",
"in_bills",
"=",
"bill_data",
".",
"index",
".",
"difference",
"(",
"bond_data",
".",
"index",
")",
".",
"difference",
"(",
"known_inconsistencies",
")",
"in_bonds",
"=",
"bond_data",
".",
"index",
".",
"difference",
"(",
"bill_data",
".",
"index",
")",
".",
"difference",
"(",
"known_inconsistencies",
")",
"raise",
"ValueError",
"(",
"\"Inconsistent dates for Canadian treasury bills vs bonds. \\n\"",
"\"Dates with bills but not bonds: {in_bills}.\\n\"",
"\"Dates with bonds but not bills: {in_bonds}.\"",
".",
"format",
"(",
"in_bills",
"=",
"in_bills",
",",
"in_bonds",
"=",
"in_bonds",
",",
")",
")"
] |
There are a couple quirks in the data provided by Bank of Canada.
Check that no new quirks have been introduced in the latest download.
|
[
"There",
"are",
"a",
"couple",
"quirks",
"in",
"the",
"data",
"provided",
"by",
"Bank",
"of",
"Canada",
".",
"Check",
"that",
"no",
"new",
"quirks",
"have",
"been",
"introduced",
"in",
"the",
"latest",
"download",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/treasuries_can.py#L83-L119
|
train
|
quantopian/zipline
|
zipline/data/treasuries_can.py
|
earliest_possible_date
|
def earliest_possible_date():
"""
The earliest date for which we can load data from this module.
"""
today = pd.Timestamp('now', tz='UTC').normalize()
# Bank of Canada only has the last 10 years of data at any given time.
return today.replace(year=today.year - 10)
|
python
|
def earliest_possible_date():
"""
The earliest date for which we can load data from this module.
"""
today = pd.Timestamp('now', tz='UTC').normalize()
# Bank of Canada only has the last 10 years of data at any given time.
return today.replace(year=today.year - 10)
|
[
"def",
"earliest_possible_date",
"(",
")",
":",
"today",
"=",
"pd",
".",
"Timestamp",
"(",
"'now'",
",",
"tz",
"=",
"'UTC'",
")",
".",
"normalize",
"(",
")",
"# Bank of Canada only has the last 10 years of data at any given time.",
"return",
"today",
".",
"replace",
"(",
"year",
"=",
"today",
".",
"year",
"-",
"10",
")"
] |
The earliest date for which we can load data from this module.
|
[
"The",
"earliest",
"date",
"for",
"which",
"we",
"can",
"load",
"data",
"from",
"this",
"module",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/treasuries_can.py#L122-L128
|
train
|
quantopian/zipline
|
zipline/finance/slippage.py
|
fill_price_worse_than_limit_price
|
def fill_price_worse_than_limit_price(fill_price, order):
"""
Checks whether the fill price is worse than the order's limit price.
Parameters
----------
fill_price: float
The price to check.
order: zipline.finance.order.Order
The order whose limit price to check.
Returns
-------
bool: Whether the fill price is above the limit price (for a buy) or below
the limit price (for a sell).
"""
if order.limit:
# this is tricky! if an order with a limit price has reached
# the limit price, we will try to fill the order. do not fill
# these shares if the impacted price is worse than the limit
# price. return early to avoid creating the transaction.
# buy order is worse if the impacted price is greater than
# the limit price. sell order is worse if the impacted price
# is less than the limit price
if (order.direction > 0 and fill_price > order.limit) or \
(order.direction < 0 and fill_price < order.limit):
return True
return False
|
python
|
def fill_price_worse_than_limit_price(fill_price, order):
"""
Checks whether the fill price is worse than the order's limit price.
Parameters
----------
fill_price: float
The price to check.
order: zipline.finance.order.Order
The order whose limit price to check.
Returns
-------
bool: Whether the fill price is above the limit price (for a buy) or below
the limit price (for a sell).
"""
if order.limit:
# this is tricky! if an order with a limit price has reached
# the limit price, we will try to fill the order. do not fill
# these shares if the impacted price is worse than the limit
# price. return early to avoid creating the transaction.
# buy order is worse if the impacted price is greater than
# the limit price. sell order is worse if the impacted price
# is less than the limit price
if (order.direction > 0 and fill_price > order.limit) or \
(order.direction < 0 and fill_price < order.limit):
return True
return False
|
[
"def",
"fill_price_worse_than_limit_price",
"(",
"fill_price",
",",
"order",
")",
":",
"if",
"order",
".",
"limit",
":",
"# this is tricky! if an order with a limit price has reached",
"# the limit price, we will try to fill the order. do not fill",
"# these shares if the impacted price is worse than the limit",
"# price. return early to avoid creating the transaction.",
"# buy order is worse if the impacted price is greater than",
"# the limit price. sell order is worse if the impacted price",
"# is less than the limit price",
"if",
"(",
"order",
".",
"direction",
">",
"0",
"and",
"fill_price",
">",
"order",
".",
"limit",
")",
"or",
"(",
"order",
".",
"direction",
"<",
"0",
"and",
"fill_price",
"<",
"order",
".",
"limit",
")",
":",
"return",
"True",
"return",
"False"
] |
Checks whether the fill price is worse than the order's limit price.
Parameters
----------
fill_price: float
The price to check.
order: zipline.finance.order.Order
The order whose limit price to check.
Returns
-------
bool: Whether the fill price is above the limit price (for a buy) or below
the limit price (for a sell).
|
[
"Checks",
"whether",
"the",
"fill",
"price",
"is",
"worse",
"than",
"the",
"order",
"s",
"limit",
"price",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/slippage.py#L50-L80
|
train
|
quantopian/zipline
|
zipline/finance/slippage.py
|
MarketImpactBase._get_window_data
|
def _get_window_data(self, data, asset, window_length):
"""
Internal utility method to return the trailing mean volume over the
past 'window_length' days, and volatility of close prices for a
specific asset.
Parameters
----------
data : The BarData from which to fetch the daily windows.
asset : The Asset whose data we are fetching.
window_length : Number of days of history used to calculate the mean
volume and close price volatility.
Returns
-------
(mean volume, volatility)
"""
try:
values = self._window_data_cache.get(asset, data.current_session)
except KeyError:
try:
# Add a day because we want 'window_length' complete days,
# excluding the current day.
volume_history = data.history(
asset, 'volume', window_length + 1, '1d',
)
close_history = data.history(
asset, 'close', window_length + 1, '1d',
)
except HistoryWindowStartsBeforeData:
# If there is not enough data to do a full history call, return
# values as if there was no data.
return 0, np.NaN
# Exclude the first value of the percent change array because it is
# always just NaN.
close_volatility = close_history[:-1].pct_change()[1:].std(
skipna=False,
)
values = {
'volume': volume_history[:-1].mean(),
'close': close_volatility * SQRT_252,
}
self._window_data_cache.set(asset, values, data.current_session)
return values['volume'], values['close']
|
python
|
def _get_window_data(self, data, asset, window_length):
"""
Internal utility method to return the trailing mean volume over the
past 'window_length' days, and volatility of close prices for a
specific asset.
Parameters
----------
data : The BarData from which to fetch the daily windows.
asset : The Asset whose data we are fetching.
window_length : Number of days of history used to calculate the mean
volume and close price volatility.
Returns
-------
(mean volume, volatility)
"""
try:
values = self._window_data_cache.get(asset, data.current_session)
except KeyError:
try:
# Add a day because we want 'window_length' complete days,
# excluding the current day.
volume_history = data.history(
asset, 'volume', window_length + 1, '1d',
)
close_history = data.history(
asset, 'close', window_length + 1, '1d',
)
except HistoryWindowStartsBeforeData:
# If there is not enough data to do a full history call, return
# values as if there was no data.
return 0, np.NaN
# Exclude the first value of the percent change array because it is
# always just NaN.
close_volatility = close_history[:-1].pct_change()[1:].std(
skipna=False,
)
values = {
'volume': volume_history[:-1].mean(),
'close': close_volatility * SQRT_252,
}
self._window_data_cache.set(asset, values, data.current_session)
return values['volume'], values['close']
|
[
"def",
"_get_window_data",
"(",
"self",
",",
"data",
",",
"asset",
",",
"window_length",
")",
":",
"try",
":",
"values",
"=",
"self",
".",
"_window_data_cache",
".",
"get",
"(",
"asset",
",",
"data",
".",
"current_session",
")",
"except",
"KeyError",
":",
"try",
":",
"# Add a day because we want 'window_length' complete days,",
"# excluding the current day.",
"volume_history",
"=",
"data",
".",
"history",
"(",
"asset",
",",
"'volume'",
",",
"window_length",
"+",
"1",
",",
"'1d'",
",",
")",
"close_history",
"=",
"data",
".",
"history",
"(",
"asset",
",",
"'close'",
",",
"window_length",
"+",
"1",
",",
"'1d'",
",",
")",
"except",
"HistoryWindowStartsBeforeData",
":",
"# If there is not enough data to do a full history call, return",
"# values as if there was no data.",
"return",
"0",
",",
"np",
".",
"NaN",
"# Exclude the first value of the percent change array because it is",
"# always just NaN.",
"close_volatility",
"=",
"close_history",
"[",
":",
"-",
"1",
"]",
".",
"pct_change",
"(",
")",
"[",
"1",
":",
"]",
".",
"std",
"(",
"skipna",
"=",
"False",
",",
")",
"values",
"=",
"{",
"'volume'",
":",
"volume_history",
"[",
":",
"-",
"1",
"]",
".",
"mean",
"(",
")",
",",
"'close'",
":",
"close_volatility",
"*",
"SQRT_252",
",",
"}",
"self",
".",
"_window_data_cache",
".",
"set",
"(",
"asset",
",",
"values",
",",
"data",
".",
"current_session",
")",
"return",
"values",
"[",
"'volume'",
"]",
",",
"values",
"[",
"'close'",
"]"
] |
Internal utility method to return the trailing mean volume over the
past 'window_length' days, and volatility of close prices for a
specific asset.
Parameters
----------
data : The BarData from which to fetch the daily windows.
asset : The Asset whose data we are fetching.
window_length : Number of days of history used to calculate the mean
volume and close price volatility.
Returns
-------
(mean volume, volatility)
|
[
"Internal",
"utility",
"method",
"to",
"return",
"the",
"trailing",
"mean",
"volume",
"over",
"the",
"past",
"window_length",
"days",
"and",
"volatility",
"of",
"close",
"prices",
"for",
"a",
"specific",
"asset",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/slippage.py#L399-L444
|
train
|
quantopian/zipline
|
zipline/pipeline/term.py
|
validate_dtype
|
def validate_dtype(termname, dtype, missing_value):
"""
Validate a `dtype` and `missing_value` passed to Term.__new__.
Ensures that we know how to represent ``dtype``, and that missing_value
is specified for types without default missing values.
Returns
-------
validated_dtype, validated_missing_value : np.dtype, any
The dtype and missing_value to use for the new term.
Raises
------
DTypeNotSpecified
When no dtype was passed to the instance, and the class doesn't
provide a default.
NotDType
When either the class or the instance provides a value not
coercible to a numpy dtype.
NoDefaultMissingValue
When dtype requires an explicit missing_value, but
``missing_value`` is NotSpecified.
"""
if dtype is NotSpecified:
raise DTypeNotSpecified(termname=termname)
try:
dtype = dtype_class(dtype)
except TypeError:
raise NotDType(dtype=dtype, termname=termname)
if not can_represent_dtype(dtype):
raise UnsupportedDType(dtype=dtype, termname=termname)
if missing_value is NotSpecified:
missing_value = default_missing_value_for_dtype(dtype)
try:
if (dtype == categorical_dtype):
# This check is necessary because we use object dtype for
# categoricals, and numpy will allow us to promote numerical
# values to object even though we don't support them.
_assert_valid_categorical_missing_value(missing_value)
# For any other type, we can check if the missing_value is safe by
# making an array of that value and trying to safely convert it to
# the desired type.
# 'same_kind' allows casting between things like float32 and
# float64, but not str and int.
array([missing_value]).astype(dtype=dtype, casting='same_kind')
except TypeError as e:
raise TypeError(
"Missing value {value!r} is not a valid choice "
"for term {termname} with dtype {dtype}.\n\n"
"Coercion attempt failed with: {error}".format(
termname=termname,
value=missing_value,
dtype=dtype,
error=e,
)
)
return dtype, missing_value
|
python
|
def validate_dtype(termname, dtype, missing_value):
"""
Validate a `dtype` and `missing_value` passed to Term.__new__.
Ensures that we know how to represent ``dtype``, and that missing_value
is specified for types without default missing values.
Returns
-------
validated_dtype, validated_missing_value : np.dtype, any
The dtype and missing_value to use for the new term.
Raises
------
DTypeNotSpecified
When no dtype was passed to the instance, and the class doesn't
provide a default.
NotDType
When either the class or the instance provides a value not
coercible to a numpy dtype.
NoDefaultMissingValue
When dtype requires an explicit missing_value, but
``missing_value`` is NotSpecified.
"""
if dtype is NotSpecified:
raise DTypeNotSpecified(termname=termname)
try:
dtype = dtype_class(dtype)
except TypeError:
raise NotDType(dtype=dtype, termname=termname)
if not can_represent_dtype(dtype):
raise UnsupportedDType(dtype=dtype, termname=termname)
if missing_value is NotSpecified:
missing_value = default_missing_value_for_dtype(dtype)
try:
if (dtype == categorical_dtype):
# This check is necessary because we use object dtype for
# categoricals, and numpy will allow us to promote numerical
# values to object even though we don't support them.
_assert_valid_categorical_missing_value(missing_value)
# For any other type, we can check if the missing_value is safe by
# making an array of that value and trying to safely convert it to
# the desired type.
# 'same_kind' allows casting between things like float32 and
# float64, but not str and int.
array([missing_value]).astype(dtype=dtype, casting='same_kind')
except TypeError as e:
raise TypeError(
"Missing value {value!r} is not a valid choice "
"for term {termname} with dtype {dtype}.\n\n"
"Coercion attempt failed with: {error}".format(
termname=termname,
value=missing_value,
dtype=dtype,
error=e,
)
)
return dtype, missing_value
|
[
"def",
"validate_dtype",
"(",
"termname",
",",
"dtype",
",",
"missing_value",
")",
":",
"if",
"dtype",
"is",
"NotSpecified",
":",
"raise",
"DTypeNotSpecified",
"(",
"termname",
"=",
"termname",
")",
"try",
":",
"dtype",
"=",
"dtype_class",
"(",
"dtype",
")",
"except",
"TypeError",
":",
"raise",
"NotDType",
"(",
"dtype",
"=",
"dtype",
",",
"termname",
"=",
"termname",
")",
"if",
"not",
"can_represent_dtype",
"(",
"dtype",
")",
":",
"raise",
"UnsupportedDType",
"(",
"dtype",
"=",
"dtype",
",",
"termname",
"=",
"termname",
")",
"if",
"missing_value",
"is",
"NotSpecified",
":",
"missing_value",
"=",
"default_missing_value_for_dtype",
"(",
"dtype",
")",
"try",
":",
"if",
"(",
"dtype",
"==",
"categorical_dtype",
")",
":",
"# This check is necessary because we use object dtype for",
"# categoricals, and numpy will allow us to promote numerical",
"# values to object even though we don't support them.",
"_assert_valid_categorical_missing_value",
"(",
"missing_value",
")",
"# For any other type, we can check if the missing_value is safe by",
"# making an array of that value and trying to safely convert it to",
"# the desired type.",
"# 'same_kind' allows casting between things like float32 and",
"# float64, but not str and int.",
"array",
"(",
"[",
"missing_value",
"]",
")",
".",
"astype",
"(",
"dtype",
"=",
"dtype",
",",
"casting",
"=",
"'same_kind'",
")",
"except",
"TypeError",
"as",
"e",
":",
"raise",
"TypeError",
"(",
"\"Missing value {value!r} is not a valid choice \"",
"\"for term {termname} with dtype {dtype}.\\n\\n\"",
"\"Coercion attempt failed with: {error}\"",
".",
"format",
"(",
"termname",
"=",
"termname",
",",
"value",
"=",
"missing_value",
",",
"dtype",
"=",
"dtype",
",",
"error",
"=",
"e",
",",
")",
")",
"return",
"dtype",
",",
"missing_value"
] |
Validate a `dtype` and `missing_value` passed to Term.__new__.
Ensures that we know how to represent ``dtype``, and that missing_value
is specified for types without default missing values.
Returns
-------
validated_dtype, validated_missing_value : np.dtype, any
The dtype and missing_value to use for the new term.
Raises
------
DTypeNotSpecified
When no dtype was passed to the instance, and the class doesn't
provide a default.
NotDType
When either the class or the instance provides a value not
coercible to a numpy dtype.
NoDefaultMissingValue
When dtype requires an explicit missing_value, but
``missing_value`` is NotSpecified.
|
[
"Validate",
"a",
"dtype",
"and",
"missing_value",
"passed",
"to",
"Term",
".",
"__new__",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/term.py#L795-L858
|
train
|
quantopian/zipline
|
zipline/pipeline/term.py
|
_assert_valid_categorical_missing_value
|
def _assert_valid_categorical_missing_value(value):
"""
Check that value is a valid categorical missing_value.
Raises a TypeError if the value is cannot be used as the missing_value for
a categorical_dtype Term.
"""
label_types = LabelArray.SUPPORTED_SCALAR_TYPES
if not isinstance(value, label_types):
raise TypeError(
"Categorical terms must have missing values of type "
"{types}.".format(
types=' or '.join([t.__name__ for t in label_types]),
)
)
|
python
|
def _assert_valid_categorical_missing_value(value):
"""
Check that value is a valid categorical missing_value.
Raises a TypeError if the value is cannot be used as the missing_value for
a categorical_dtype Term.
"""
label_types = LabelArray.SUPPORTED_SCALAR_TYPES
if not isinstance(value, label_types):
raise TypeError(
"Categorical terms must have missing values of type "
"{types}.".format(
types=' or '.join([t.__name__ for t in label_types]),
)
)
|
[
"def",
"_assert_valid_categorical_missing_value",
"(",
"value",
")",
":",
"label_types",
"=",
"LabelArray",
".",
"SUPPORTED_SCALAR_TYPES",
"if",
"not",
"isinstance",
"(",
"value",
",",
"label_types",
")",
":",
"raise",
"TypeError",
"(",
"\"Categorical terms must have missing values of type \"",
"\"{types}.\"",
".",
"format",
"(",
"types",
"=",
"' or '",
".",
"join",
"(",
"[",
"t",
".",
"__name__",
"for",
"t",
"in",
"label_types",
"]",
")",
",",
")",
")"
] |
Check that value is a valid categorical missing_value.
Raises a TypeError if the value is cannot be used as the missing_value for
a categorical_dtype Term.
|
[
"Check",
"that",
"value",
"is",
"a",
"valid",
"categorical",
"missing_value",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/term.py#L861-L875
|
train
|
quantopian/zipline
|
zipline/pipeline/term.py
|
Term._pop_params
|
def _pop_params(cls, kwargs):
"""
Pop entries from the `kwargs` passed to cls.__new__ based on the values
in `cls.params`.
Parameters
----------
kwargs : dict
The kwargs passed to cls.__new__.
Returns
-------
params : list[(str, object)]
A list of string, value pairs containing the entries in cls.params.
Raises
------
TypeError
Raised if any parameter values are not passed or not hashable.
"""
params = cls.params
if not isinstance(params, Mapping):
params = {k: NotSpecified for k in params}
param_values = []
for key, default_value in params.items():
try:
value = kwargs.pop(key, default_value)
if value is NotSpecified:
raise KeyError(key)
# Check here that the value is hashable so that we fail here
# instead of trying to hash the param values tuple later.
hash(value)
except KeyError:
raise TypeError(
"{typename} expected a keyword parameter {name!r}.".format(
typename=cls.__name__,
name=key
)
)
except TypeError:
# Value wasn't hashable.
raise TypeError(
"{typename} expected a hashable value for parameter "
"{name!r}, but got {value!r} instead.".format(
typename=cls.__name__,
name=key,
value=value,
)
)
param_values.append((key, value))
return tuple(param_values)
|
python
|
def _pop_params(cls, kwargs):
"""
Pop entries from the `kwargs` passed to cls.__new__ based on the values
in `cls.params`.
Parameters
----------
kwargs : dict
The kwargs passed to cls.__new__.
Returns
-------
params : list[(str, object)]
A list of string, value pairs containing the entries in cls.params.
Raises
------
TypeError
Raised if any parameter values are not passed or not hashable.
"""
params = cls.params
if not isinstance(params, Mapping):
params = {k: NotSpecified for k in params}
param_values = []
for key, default_value in params.items():
try:
value = kwargs.pop(key, default_value)
if value is NotSpecified:
raise KeyError(key)
# Check here that the value is hashable so that we fail here
# instead of trying to hash the param values tuple later.
hash(value)
except KeyError:
raise TypeError(
"{typename} expected a keyword parameter {name!r}.".format(
typename=cls.__name__,
name=key
)
)
except TypeError:
# Value wasn't hashable.
raise TypeError(
"{typename} expected a hashable value for parameter "
"{name!r}, but got {value!r} instead.".format(
typename=cls.__name__,
name=key,
value=value,
)
)
param_values.append((key, value))
return tuple(param_values)
|
[
"def",
"_pop_params",
"(",
"cls",
",",
"kwargs",
")",
":",
"params",
"=",
"cls",
".",
"params",
"if",
"not",
"isinstance",
"(",
"params",
",",
"Mapping",
")",
":",
"params",
"=",
"{",
"k",
":",
"NotSpecified",
"for",
"k",
"in",
"params",
"}",
"param_values",
"=",
"[",
"]",
"for",
"key",
",",
"default_value",
"in",
"params",
".",
"items",
"(",
")",
":",
"try",
":",
"value",
"=",
"kwargs",
".",
"pop",
"(",
"key",
",",
"default_value",
")",
"if",
"value",
"is",
"NotSpecified",
":",
"raise",
"KeyError",
"(",
"key",
")",
"# Check here that the value is hashable so that we fail here",
"# instead of trying to hash the param values tuple later.",
"hash",
"(",
"value",
")",
"except",
"KeyError",
":",
"raise",
"TypeError",
"(",
"\"{typename} expected a keyword parameter {name!r}.\"",
".",
"format",
"(",
"typename",
"=",
"cls",
".",
"__name__",
",",
"name",
"=",
"key",
")",
")",
"except",
"TypeError",
":",
"# Value wasn't hashable.",
"raise",
"TypeError",
"(",
"\"{typename} expected a hashable value for parameter \"",
"\"{name!r}, but got {value!r} instead.\"",
".",
"format",
"(",
"typename",
"=",
"cls",
".",
"__name__",
",",
"name",
"=",
"key",
",",
"value",
"=",
"value",
",",
")",
")",
"param_values",
".",
"append",
"(",
"(",
"key",
",",
"value",
")",
")",
"return",
"tuple",
"(",
"param_values",
")"
] |
Pop entries from the `kwargs` passed to cls.__new__ based on the values
in `cls.params`.
Parameters
----------
kwargs : dict
The kwargs passed to cls.__new__.
Returns
-------
params : list[(str, object)]
A list of string, value pairs containing the entries in cls.params.
Raises
------
TypeError
Raised if any parameter values are not passed or not hashable.
|
[
"Pop",
"entries",
"from",
"the",
"kwargs",
"passed",
"to",
"cls",
".",
"__new__",
"based",
"on",
"the",
"values",
"in",
"cls",
".",
"params",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/term.py#L140-L192
|
train
|
quantopian/zipline
|
zipline/pipeline/term.py
|
Term._static_identity
|
def _static_identity(cls,
domain,
dtype,
missing_value,
window_safe,
ndim,
params):
"""
Return the identity of the Term that would be constructed from the
given arguments.
Identities that compare equal will cause us to return a cached instance
rather than constructing a new one. We do this primarily because it
makes dependency resolution easier.
This is a classmethod so that it can be called from Term.__new__ to
determine whether to produce a new instance.
"""
return (cls, domain, dtype, missing_value, window_safe, ndim, params)
|
python
|
def _static_identity(cls,
domain,
dtype,
missing_value,
window_safe,
ndim,
params):
"""
Return the identity of the Term that would be constructed from the
given arguments.
Identities that compare equal will cause us to return a cached instance
rather than constructing a new one. We do this primarily because it
makes dependency resolution easier.
This is a classmethod so that it can be called from Term.__new__ to
determine whether to produce a new instance.
"""
return (cls, domain, dtype, missing_value, window_safe, ndim, params)
|
[
"def",
"_static_identity",
"(",
"cls",
",",
"domain",
",",
"dtype",
",",
"missing_value",
",",
"window_safe",
",",
"ndim",
",",
"params",
")",
":",
"return",
"(",
"cls",
",",
"domain",
",",
"dtype",
",",
"missing_value",
",",
"window_safe",
",",
"ndim",
",",
"params",
")"
] |
Return the identity of the Term that would be constructed from the
given arguments.
Identities that compare equal will cause us to return a cached instance
rather than constructing a new one. We do this primarily because it
makes dependency resolution easier.
This is a classmethod so that it can be called from Term.__new__ to
determine whether to produce a new instance.
|
[
"Return",
"the",
"identity",
"of",
"the",
"Term",
"that",
"would",
"be",
"constructed",
"from",
"the",
"given",
"arguments",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/term.py#L217-L235
|
train
|
quantopian/zipline
|
zipline/pipeline/term.py
|
Term._init
|
def _init(self, domain, dtype, missing_value, window_safe, ndim, params):
"""
Parameters
----------
domain : zipline.pipeline.domain.Domain
The domain of this term.
dtype : np.dtype
Dtype of this term's output.
missing_value : object
Missing value for this term.
ndim : 1 or 2
The dimensionality of this term.
params : tuple[(str, hashable)]
Tuple of key/value pairs of additional parameters.
"""
self.domain = domain
self.dtype = dtype
self.missing_value = missing_value
self.window_safe = window_safe
self.ndim = ndim
for name, value in params:
if hasattr(self, name):
raise TypeError(
"Parameter {name!r} conflicts with already-present"
" attribute with value {value!r}.".format(
name=name,
value=getattr(self, name),
)
)
# TODO: Consider setting these values as attributes and replacing
# the boilerplate in NumericalExpression, Rank, and
# PercentileFilter.
self.params = dict(params)
# Make sure that subclasses call super() in their _validate() methods
# by setting this flag. The base class implementation of _validate
# should set this flag to True.
self._subclass_called_super_validate = False
self._validate()
assert self._subclass_called_super_validate, (
"Term._validate() was not called.\n"
"This probably means that you overrode _validate"
" without calling super()."
)
del self._subclass_called_super_validate
return self
|
python
|
def _init(self, domain, dtype, missing_value, window_safe, ndim, params):
"""
Parameters
----------
domain : zipline.pipeline.domain.Domain
The domain of this term.
dtype : np.dtype
Dtype of this term's output.
missing_value : object
Missing value for this term.
ndim : 1 or 2
The dimensionality of this term.
params : tuple[(str, hashable)]
Tuple of key/value pairs of additional parameters.
"""
self.domain = domain
self.dtype = dtype
self.missing_value = missing_value
self.window_safe = window_safe
self.ndim = ndim
for name, value in params:
if hasattr(self, name):
raise TypeError(
"Parameter {name!r} conflicts with already-present"
" attribute with value {value!r}.".format(
name=name,
value=getattr(self, name),
)
)
# TODO: Consider setting these values as attributes and replacing
# the boilerplate in NumericalExpression, Rank, and
# PercentileFilter.
self.params = dict(params)
# Make sure that subclasses call super() in their _validate() methods
# by setting this flag. The base class implementation of _validate
# should set this flag to True.
self._subclass_called_super_validate = False
self._validate()
assert self._subclass_called_super_validate, (
"Term._validate() was not called.\n"
"This probably means that you overrode _validate"
" without calling super()."
)
del self._subclass_called_super_validate
return self
|
[
"def",
"_init",
"(",
"self",
",",
"domain",
",",
"dtype",
",",
"missing_value",
",",
"window_safe",
",",
"ndim",
",",
"params",
")",
":",
"self",
".",
"domain",
"=",
"domain",
"self",
".",
"dtype",
"=",
"dtype",
"self",
".",
"missing_value",
"=",
"missing_value",
"self",
".",
"window_safe",
"=",
"window_safe",
"self",
".",
"ndim",
"=",
"ndim",
"for",
"name",
",",
"value",
"in",
"params",
":",
"if",
"hasattr",
"(",
"self",
",",
"name",
")",
":",
"raise",
"TypeError",
"(",
"\"Parameter {name!r} conflicts with already-present\"",
"\" attribute with value {value!r}.\"",
".",
"format",
"(",
"name",
"=",
"name",
",",
"value",
"=",
"getattr",
"(",
"self",
",",
"name",
")",
",",
")",
")",
"# TODO: Consider setting these values as attributes and replacing",
"# the boilerplate in NumericalExpression, Rank, and",
"# PercentileFilter.",
"self",
".",
"params",
"=",
"dict",
"(",
"params",
")",
"# Make sure that subclasses call super() in their _validate() methods",
"# by setting this flag. The base class implementation of _validate",
"# should set this flag to True.",
"self",
".",
"_subclass_called_super_validate",
"=",
"False",
"self",
".",
"_validate",
"(",
")",
"assert",
"self",
".",
"_subclass_called_super_validate",
",",
"(",
"\"Term._validate() was not called.\\n\"",
"\"This probably means that you overrode _validate\"",
"\" without calling super().\"",
")",
"del",
"self",
".",
"_subclass_called_super_validate",
"return",
"self"
] |
Parameters
----------
domain : zipline.pipeline.domain.Domain
The domain of this term.
dtype : np.dtype
Dtype of this term's output.
missing_value : object
Missing value for this term.
ndim : 1 or 2
The dimensionality of this term.
params : tuple[(str, hashable)]
Tuple of key/value pairs of additional parameters.
|
[
"Parameters",
"----------",
"domain",
":",
"zipline",
".",
"pipeline",
".",
"domain",
".",
"Domain",
"The",
"domain",
"of",
"this",
"term",
".",
"dtype",
":",
"np",
".",
"dtype",
"Dtype",
"of",
"this",
"term",
"s",
"output",
".",
"missing_value",
":",
"object",
"Missing",
"value",
"for",
"this",
"term",
".",
"ndim",
":",
"1",
"or",
"2",
"The",
"dimensionality",
"of",
"this",
"term",
".",
"params",
":",
"tuple",
"[",
"(",
"str",
"hashable",
")",
"]",
"Tuple",
"of",
"key",
"/",
"value",
"pairs",
"of",
"additional",
"parameters",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/term.py#L237-L285
|
train
|
quantopian/zipline
|
zipline/pipeline/term.py
|
ComputableTerm.dependencies
|
def dependencies(self):
"""
The number of extra rows needed for each of our inputs to compute this
term.
"""
extra_input_rows = max(0, self.window_length - 1)
out = {}
for term in self.inputs:
out[term] = extra_input_rows
out[self.mask] = 0
return out
|
python
|
def dependencies(self):
"""
The number of extra rows needed for each of our inputs to compute this
term.
"""
extra_input_rows = max(0, self.window_length - 1)
out = {}
for term in self.inputs:
out[term] = extra_input_rows
out[self.mask] = 0
return out
|
[
"def",
"dependencies",
"(",
"self",
")",
":",
"extra_input_rows",
"=",
"max",
"(",
"0",
",",
"self",
".",
"window_length",
"-",
"1",
")",
"out",
"=",
"{",
"}",
"for",
"term",
"in",
"self",
".",
"inputs",
":",
"out",
"[",
"term",
"]",
"=",
"extra_input_rows",
"out",
"[",
"self",
".",
"mask",
"]",
"=",
"0",
"return",
"out"
] |
The number of extra rows needed for each of our inputs to compute this
term.
|
[
"The",
"number",
"of",
"extra",
"rows",
"needed",
"for",
"each",
"of",
"our",
"inputs",
"to",
"compute",
"this",
"term",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/term.py#L613-L623
|
train
|
quantopian/zipline
|
zipline/pipeline/term.py
|
ComputableTerm.to_workspace_value
|
def to_workspace_value(self, result, assets):
"""
Called with a column of the result of a pipeline. This needs to put
the data into a format that can be used in a workspace to continue
doing computations.
Parameters
----------
result : pd.Series
A multiindexed series with (dates, assets) whose values are the
results of running this pipeline term over the dates.
assets : pd.Index
All of the assets being requested. This allows us to correctly
shape the workspace value.
Returns
-------
workspace_value : array-like
An array like value that the engine can consume.
"""
return result.unstack().fillna(self.missing_value).reindex(
columns=assets,
fill_value=self.missing_value,
).values
|
python
|
def to_workspace_value(self, result, assets):
"""
Called with a column of the result of a pipeline. This needs to put
the data into a format that can be used in a workspace to continue
doing computations.
Parameters
----------
result : pd.Series
A multiindexed series with (dates, assets) whose values are the
results of running this pipeline term over the dates.
assets : pd.Index
All of the assets being requested. This allows us to correctly
shape the workspace value.
Returns
-------
workspace_value : array-like
An array like value that the engine can consume.
"""
return result.unstack().fillna(self.missing_value).reindex(
columns=assets,
fill_value=self.missing_value,
).values
|
[
"def",
"to_workspace_value",
"(",
"self",
",",
"result",
",",
"assets",
")",
":",
"return",
"result",
".",
"unstack",
"(",
")",
".",
"fillna",
"(",
"self",
".",
"missing_value",
")",
".",
"reindex",
"(",
"columns",
"=",
"assets",
",",
"fill_value",
"=",
"self",
".",
"missing_value",
",",
")",
".",
"values"
] |
Called with a column of the result of a pipeline. This needs to put
the data into a format that can be used in a workspace to continue
doing computations.
Parameters
----------
result : pd.Series
A multiindexed series with (dates, assets) whose values are the
results of running this pipeline term over the dates.
assets : pd.Index
All of the assets being requested. This allows us to correctly
shape the workspace value.
Returns
-------
workspace_value : array-like
An array like value that the engine can consume.
|
[
"Called",
"with",
"a",
"column",
"of",
"the",
"result",
"of",
"a",
"pipeline",
".",
"This",
"needs",
"to",
"put",
"the",
"data",
"into",
"a",
"format",
"that",
"can",
"be",
"used",
"in",
"a",
"workspace",
"to",
"continue",
"doing",
"computations",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/term.py#L638-L661
|
train
|
quantopian/zipline
|
zipline/finance/position.py
|
Position.earn_stock_dividend
|
def earn_stock_dividend(self, stock_dividend):
"""
Register the number of shares we held at this dividend's ex date so
that we can pay out the correct amount on the dividend's pay date.
"""
return {
'payment_asset': stock_dividend.payment_asset,
'share_count': np.floor(
self.amount * float(stock_dividend.ratio)
)
}
|
python
|
def earn_stock_dividend(self, stock_dividend):
"""
Register the number of shares we held at this dividend's ex date so
that we can pay out the correct amount on the dividend's pay date.
"""
return {
'payment_asset': stock_dividend.payment_asset,
'share_count': np.floor(
self.amount * float(stock_dividend.ratio)
)
}
|
[
"def",
"earn_stock_dividend",
"(",
"self",
",",
"stock_dividend",
")",
":",
"return",
"{",
"'payment_asset'",
":",
"stock_dividend",
".",
"payment_asset",
",",
"'share_count'",
":",
"np",
".",
"floor",
"(",
"self",
".",
"amount",
"*",
"float",
"(",
"stock_dividend",
".",
"ratio",
")",
")",
"}"
] |
Register the number of shares we held at this dividend's ex date so
that we can pay out the correct amount on the dividend's pay date.
|
[
"Register",
"the",
"number",
"of",
"shares",
"we",
"held",
"at",
"this",
"dividend",
"s",
"ex",
"date",
"so",
"that",
"we",
"can",
"pay",
"out",
"the",
"correct",
"amount",
"on",
"the",
"dividend",
"s",
"pay",
"date",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/position.py#L79-L89
|
train
|
quantopian/zipline
|
zipline/finance/position.py
|
Position.handle_split
|
def handle_split(self, asset, ratio):
"""
Update the position by the split ratio, and return the resulting
fractional share that will be converted into cash.
Returns the unused cash.
"""
if self.asset != asset:
raise Exception("updating split with the wrong asset!")
# adjust the # of shares by the ratio
# (if we had 100 shares, and the ratio is 3,
# we now have 33 shares)
# (old_share_count / ratio = new_share_count)
# (old_price * ratio = new_price)
# e.g., 33.333
raw_share_count = self.amount / float(ratio)
# e.g., 33
full_share_count = np.floor(raw_share_count)
# e.g., 0.333
fractional_share_count = raw_share_count - full_share_count
# adjust the cost basis to the nearest cent, e.g., 60.0
new_cost_basis = round(self.cost_basis * ratio, 2)
self.cost_basis = new_cost_basis
self.amount = full_share_count
return_cash = round(float(fractional_share_count * new_cost_basis), 2)
log.info("after split: " + str(self))
log.info("returning cash: " + str(return_cash))
# return the leftover cash, which will be converted into cash
# (rounded to the nearest cent)
return return_cash
|
python
|
def handle_split(self, asset, ratio):
"""
Update the position by the split ratio, and return the resulting
fractional share that will be converted into cash.
Returns the unused cash.
"""
if self.asset != asset:
raise Exception("updating split with the wrong asset!")
# adjust the # of shares by the ratio
# (if we had 100 shares, and the ratio is 3,
# we now have 33 shares)
# (old_share_count / ratio = new_share_count)
# (old_price * ratio = new_price)
# e.g., 33.333
raw_share_count = self.amount / float(ratio)
# e.g., 33
full_share_count = np.floor(raw_share_count)
# e.g., 0.333
fractional_share_count = raw_share_count - full_share_count
# adjust the cost basis to the nearest cent, e.g., 60.0
new_cost_basis = round(self.cost_basis * ratio, 2)
self.cost_basis = new_cost_basis
self.amount = full_share_count
return_cash = round(float(fractional_share_count * new_cost_basis), 2)
log.info("after split: " + str(self))
log.info("returning cash: " + str(return_cash))
# return the leftover cash, which will be converted into cash
# (rounded to the nearest cent)
return return_cash
|
[
"def",
"handle_split",
"(",
"self",
",",
"asset",
",",
"ratio",
")",
":",
"if",
"self",
".",
"asset",
"!=",
"asset",
":",
"raise",
"Exception",
"(",
"\"updating split with the wrong asset!\"",
")",
"# adjust the # of shares by the ratio",
"# (if we had 100 shares, and the ratio is 3,",
"# we now have 33 shares)",
"# (old_share_count / ratio = new_share_count)",
"# (old_price * ratio = new_price)",
"# e.g., 33.333",
"raw_share_count",
"=",
"self",
".",
"amount",
"/",
"float",
"(",
"ratio",
")",
"# e.g., 33",
"full_share_count",
"=",
"np",
".",
"floor",
"(",
"raw_share_count",
")",
"# e.g., 0.333",
"fractional_share_count",
"=",
"raw_share_count",
"-",
"full_share_count",
"# adjust the cost basis to the nearest cent, e.g., 60.0",
"new_cost_basis",
"=",
"round",
"(",
"self",
".",
"cost_basis",
"*",
"ratio",
",",
"2",
")",
"self",
".",
"cost_basis",
"=",
"new_cost_basis",
"self",
".",
"amount",
"=",
"full_share_count",
"return_cash",
"=",
"round",
"(",
"float",
"(",
"fractional_share_count",
"*",
"new_cost_basis",
")",
",",
"2",
")",
"log",
".",
"info",
"(",
"\"after split: \"",
"+",
"str",
"(",
"self",
")",
")",
"log",
".",
"info",
"(",
"\"returning cash: \"",
"+",
"str",
"(",
"return_cash",
")",
")",
"# return the leftover cash, which will be converted into cash",
"# (rounded to the nearest cent)",
"return",
"return_cash"
] |
Update the position by the split ratio, and return the resulting
fractional share that will be converted into cash.
Returns the unused cash.
|
[
"Update",
"the",
"position",
"by",
"the",
"split",
"ratio",
"and",
"return",
"the",
"resulting",
"fractional",
"share",
"that",
"will",
"be",
"converted",
"into",
"cash",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/position.py#L91-L129
|
train
|
quantopian/zipline
|
zipline/finance/position.py
|
Position.adjust_commission_cost_basis
|
def adjust_commission_cost_basis(self, asset, cost):
"""
A note about cost-basis in zipline: all positions are considered
to share a cost basis, even if they were executed in different
transactions with different commission costs, different prices, etc.
Due to limitations about how zipline handles positions, zipline will
currently spread an externally-delivered commission charge across
all shares in a position.
"""
if asset != self.asset:
raise Exception('Updating a commission for a different asset?')
if cost == 0.0:
return
# If we no longer hold this position, there is no cost basis to
# adjust.
if self.amount == 0:
return
# We treat cost basis as the share price where we have broken even.
# For longs, commissions cause a relatively straight forward increase
# in the cost basis.
#
# For shorts, you actually want to decrease the cost basis because you
# break even and earn a profit when the share price decreases.
#
# Shorts are represented as having a negative `amount`.
#
# The multiplication and division by `amount` cancel out leaving the
# cost_basis positive, while subtracting the commission.
prev_cost = self.cost_basis * self.amount
if isinstance(asset, Future):
cost_to_use = cost / asset.price_multiplier
else:
cost_to_use = cost
new_cost = prev_cost + cost_to_use
self.cost_basis = new_cost / self.amount
|
python
|
def adjust_commission_cost_basis(self, asset, cost):
"""
A note about cost-basis in zipline: all positions are considered
to share a cost basis, even if they were executed in different
transactions with different commission costs, different prices, etc.
Due to limitations about how zipline handles positions, zipline will
currently spread an externally-delivered commission charge across
all shares in a position.
"""
if asset != self.asset:
raise Exception('Updating a commission for a different asset?')
if cost == 0.0:
return
# If we no longer hold this position, there is no cost basis to
# adjust.
if self.amount == 0:
return
# We treat cost basis as the share price where we have broken even.
# For longs, commissions cause a relatively straight forward increase
# in the cost basis.
#
# For shorts, you actually want to decrease the cost basis because you
# break even and earn a profit when the share price decreases.
#
# Shorts are represented as having a negative `amount`.
#
# The multiplication and division by `amount` cancel out leaving the
# cost_basis positive, while subtracting the commission.
prev_cost = self.cost_basis * self.amount
if isinstance(asset, Future):
cost_to_use = cost / asset.price_multiplier
else:
cost_to_use = cost
new_cost = prev_cost + cost_to_use
self.cost_basis = new_cost / self.amount
|
[
"def",
"adjust_commission_cost_basis",
"(",
"self",
",",
"asset",
",",
"cost",
")",
":",
"if",
"asset",
"!=",
"self",
".",
"asset",
":",
"raise",
"Exception",
"(",
"'Updating a commission for a different asset?'",
")",
"if",
"cost",
"==",
"0.0",
":",
"return",
"# If we no longer hold this position, there is no cost basis to",
"# adjust.",
"if",
"self",
".",
"amount",
"==",
"0",
":",
"return",
"# We treat cost basis as the share price where we have broken even.",
"# For longs, commissions cause a relatively straight forward increase",
"# in the cost basis.",
"#",
"# For shorts, you actually want to decrease the cost basis because you",
"# break even and earn a profit when the share price decreases.",
"#",
"# Shorts are represented as having a negative `amount`.",
"#",
"# The multiplication and division by `amount` cancel out leaving the",
"# cost_basis positive, while subtracting the commission.",
"prev_cost",
"=",
"self",
".",
"cost_basis",
"*",
"self",
".",
"amount",
"if",
"isinstance",
"(",
"asset",
",",
"Future",
")",
":",
"cost_to_use",
"=",
"cost",
"/",
"asset",
".",
"price_multiplier",
"else",
":",
"cost_to_use",
"=",
"cost",
"new_cost",
"=",
"prev_cost",
"+",
"cost_to_use",
"self",
".",
"cost_basis",
"=",
"new_cost",
"/",
"self",
".",
"amount"
] |
A note about cost-basis in zipline: all positions are considered
to share a cost basis, even if they were executed in different
transactions with different commission costs, different prices, etc.
Due to limitations about how zipline handles positions, zipline will
currently spread an externally-delivered commission charge across
all shares in a position.
|
[
"A",
"note",
"about",
"cost",
"-",
"basis",
"in",
"zipline",
":",
"all",
"positions",
"are",
"considered",
"to",
"share",
"a",
"cost",
"basis",
"even",
"if",
"they",
"were",
"executed",
"in",
"different",
"transactions",
"with",
"different",
"commission",
"costs",
"different",
"prices",
"etc",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/position.py#L164-L203
|
train
|
quantopian/zipline
|
zipline/finance/position.py
|
Position.to_dict
|
def to_dict(self):
"""
Creates a dictionary representing the state of this position.
Returns a dict object of the form:
"""
return {
'sid': self.asset,
'amount': self.amount,
'cost_basis': self.cost_basis,
'last_sale_price': self.last_sale_price
}
|
python
|
def to_dict(self):
"""
Creates a dictionary representing the state of this position.
Returns a dict object of the form:
"""
return {
'sid': self.asset,
'amount': self.amount,
'cost_basis': self.cost_basis,
'last_sale_price': self.last_sale_price
}
|
[
"def",
"to_dict",
"(",
"self",
")",
":",
"return",
"{",
"'sid'",
":",
"self",
".",
"asset",
",",
"'amount'",
":",
"self",
".",
"amount",
",",
"'cost_basis'",
":",
"self",
".",
"cost_basis",
",",
"'last_sale_price'",
":",
"self",
".",
"last_sale_price",
"}"
] |
Creates a dictionary representing the state of this position.
Returns a dict object of the form:
|
[
"Creates",
"a",
"dictionary",
"representing",
"the",
"state",
"of",
"this",
"position",
".",
"Returns",
"a",
"dict",
"object",
"of",
"the",
"form",
":"
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/position.py#L215-L225
|
train
|
quantopian/zipline
|
zipline/data/bundles/core.py
|
_make_bundle_core
|
def _make_bundle_core():
"""Create a family of data bundle functions that read from the same
bundle mapping.
Returns
-------
bundles : mappingproxy
The mapping of bundles to bundle payloads.
register : callable
The function which registers new bundles in the ``bundles`` mapping.
unregister : callable
The function which deregisters bundles from the ``bundles`` mapping.
ingest : callable
The function which downloads and write data for a given data bundle.
load : callable
The function which loads the ingested bundles back into memory.
clean : callable
The function which cleans up data written with ``ingest``.
"""
_bundles = {} # the registered bundles
# Expose _bundles through a proxy so that users cannot mutate this
# accidentally. Users may go through `register` to update this which will
# warn when trampling another bundle.
bundles = mappingproxy(_bundles)
@curry
def register(name,
f,
calendar_name='NYSE',
start_session=None,
end_session=None,
minutes_per_day=390,
create_writers=True):
"""Register a data bundle ingest function.
Parameters
----------
name : str
The name of the bundle.
f : callable
The ingest function. This function will be passed:
environ : mapping
The environment this is being run with.
asset_db_writer : AssetDBWriter
The asset db writer to write into.
minute_bar_writer : BcolzMinuteBarWriter
The minute bar writer to write into.
daily_bar_writer : BcolzDailyBarWriter
The daily bar writer to write into.
adjustment_writer : SQLiteAdjustmentWriter
The adjustment db writer to write into.
calendar : trading_calendars.TradingCalendar
The trading calendar to ingest for.
start_session : pd.Timestamp
The first session of data to ingest.
end_session : pd.Timestamp
The last session of data to ingest.
cache : DataFrameCache
A mapping object to temporarily store dataframes.
This should be used to cache intermediates in case the load
fails. This will be automatically cleaned up after a
successful load.
show_progress : bool
Show the progress for the current load where possible.
calendar_name : str, optional
The name of a calendar used to align bundle data.
Default is 'NYSE'.
start_session : pd.Timestamp, optional
The first session for which we want data. If not provided,
or if the date lies outside the range supported by the
calendar, the first_session of the calendar is used.
end_session : pd.Timestamp, optional
The last session for which we want data. If not provided,
or if the date lies outside the range supported by the
calendar, the last_session of the calendar is used.
minutes_per_day : int, optional
The number of minutes in each normal trading day.
create_writers : bool, optional
Should the ingest machinery create the writers for the ingest
function. This can be disabled as an optimization for cases where
they are not needed, like the ``quantopian-quandl`` bundle.
Notes
-----
This function my be used as a decorator, for example:
.. code-block:: python
@register('quandl')
def quandl_ingest_function(...):
...
See Also
--------
zipline.data.bundles.bundles
"""
if name in bundles:
warnings.warn(
'Overwriting bundle with name %r' % name,
stacklevel=3,
)
# NOTE: We don't eagerly compute calendar values here because
# `register` is called at module scope in zipline, and creating a
# calendar currently takes between 0.5 and 1 seconds, which causes a
# noticeable delay on the zipline CLI.
_bundles[name] = RegisteredBundle(
calendar_name=calendar_name,
start_session=start_session,
end_session=end_session,
minutes_per_day=minutes_per_day,
ingest=f,
create_writers=create_writers,
)
return f
def unregister(name):
"""Unregister a bundle.
Parameters
----------
name : str
The name of the bundle to unregister.
Raises
------
UnknownBundle
Raised when no bundle has been registered with the given name.
See Also
--------
zipline.data.bundles.bundles
"""
try:
del _bundles[name]
except KeyError:
raise UnknownBundle(name)
def ingest(name,
environ=os.environ,
timestamp=None,
assets_versions=(),
show_progress=False):
"""Ingest data for a given bundle.
Parameters
----------
name : str
The name of the bundle.
environ : mapping, optional
The environment variables. By default this is os.environ.
timestamp : datetime, optional
The timestamp to use for the load.
By default this is the current time.
assets_versions : Iterable[int], optional
Versions of the assets db to which to downgrade.
show_progress : bool, optional
Tell the ingest function to display the progress where possible.
"""
try:
bundle = bundles[name]
except KeyError:
raise UnknownBundle(name)
calendar = get_calendar(bundle.calendar_name)
start_session = bundle.start_session
end_session = bundle.end_session
if start_session is None or start_session < calendar.first_session:
start_session = calendar.first_session
if end_session is None or end_session > calendar.last_session:
end_session = calendar.last_session
if timestamp is None:
timestamp = pd.Timestamp.utcnow()
timestamp = timestamp.tz_convert('utc').tz_localize(None)
timestr = to_bundle_ingest_dirname(timestamp)
cachepath = cache_path(name, environ=environ)
pth.ensure_directory(pth.data_path([name, timestr], environ=environ))
pth.ensure_directory(cachepath)
with dataframe_cache(cachepath, clean_on_failure=False) as cache, \
ExitStack() as stack:
# we use `cleanup_on_failure=False` so that we don't purge the
# cache directory if the load fails in the middle
if bundle.create_writers:
wd = stack.enter_context(working_dir(
pth.data_path([], environ=environ))
)
daily_bars_path = wd.ensure_dir(
*daily_equity_relative(
name, timestr, environ=environ,
)
)
daily_bar_writer = BcolzDailyBarWriter(
daily_bars_path,
calendar,
start_session,
end_session,
)
# Do an empty write to ensure that the daily ctables exist
# when we create the SQLiteAdjustmentWriter below. The
# SQLiteAdjustmentWriter needs to open the daily ctables so
# that it can compute the adjustment ratios for the dividends.
daily_bar_writer.write(())
minute_bar_writer = BcolzMinuteBarWriter(
wd.ensure_dir(*minute_equity_relative(
name, timestr, environ=environ)
),
calendar,
start_session,
end_session,
minutes_per_day=bundle.minutes_per_day,
)
assets_db_path = wd.getpath(*asset_db_relative(
name, timestr, environ=environ,
))
asset_db_writer = AssetDBWriter(assets_db_path)
adjustment_db_writer = stack.enter_context(
SQLiteAdjustmentWriter(
wd.getpath(*adjustment_db_relative(
name, timestr, environ=environ)),
BcolzDailyBarReader(daily_bars_path),
overwrite=True,
)
)
else:
daily_bar_writer = None
minute_bar_writer = None
asset_db_writer = None
adjustment_db_writer = None
if assets_versions:
raise ValueError('Need to ingest a bundle that creates '
'writers in order to downgrade the assets'
' db.')
bundle.ingest(
environ,
asset_db_writer,
minute_bar_writer,
daily_bar_writer,
adjustment_db_writer,
calendar,
start_session,
end_session,
cache,
show_progress,
pth.data_path([name, timestr], environ=environ),
)
for version in sorted(set(assets_versions), reverse=True):
version_path = wd.getpath(*asset_db_relative(
name, timestr, environ=environ, db_version=version,
))
with working_file(version_path) as wf:
shutil.copy2(assets_db_path, wf.path)
downgrade(wf.path, version)
def most_recent_data(bundle_name, timestamp, environ=None):
"""Get the path to the most recent data after ``date``for the
given bundle.
Parameters
----------
bundle_name : str
The name of the bundle to lookup.
timestamp : datetime
The timestamp to begin searching on or before.
environ : dict, optional
An environment dict to forward to zipline_root.
"""
if bundle_name not in bundles:
raise UnknownBundle(bundle_name)
try:
candidates = os.listdir(
pth.data_path([bundle_name], environ=environ),
)
return pth.data_path(
[bundle_name,
max(
filter(complement(pth.hidden), candidates),
key=from_bundle_ingest_dirname,
)],
environ=environ,
)
except (ValueError, OSError) as e:
if getattr(e, 'errno', errno.ENOENT) != errno.ENOENT:
raise
raise ValueError(
'no data for bundle {bundle!r} on or before {timestamp}\n'
'maybe you need to run: $ zipline ingest -b {bundle}'.format(
bundle=bundle_name,
timestamp=timestamp,
),
)
def load(name, environ=os.environ, timestamp=None):
"""Loads a previously ingested bundle.
Parameters
----------
name : str
The name of the bundle.
environ : mapping, optional
The environment variables. Defaults of os.environ.
timestamp : datetime, optional
The timestamp of the data to lookup.
Defaults to the current time.
Returns
-------
bundle_data : BundleData
The raw data readers for this bundle.
"""
if timestamp is None:
timestamp = pd.Timestamp.utcnow()
timestr = most_recent_data(name, timestamp, environ=environ)
return BundleData(
asset_finder=AssetFinder(
asset_db_path(name, timestr, environ=environ),
),
equity_minute_bar_reader=BcolzMinuteBarReader(
minute_equity_path(name, timestr, environ=environ),
),
equity_daily_bar_reader=BcolzDailyBarReader(
daily_equity_path(name, timestr, environ=environ),
),
adjustment_reader=SQLiteAdjustmentReader(
adjustment_db_path(name, timestr, environ=environ),
),
)
@preprocess(
before=optionally(ensure_timestamp),
after=optionally(ensure_timestamp),
)
def clean(name,
before=None,
after=None,
keep_last=None,
environ=os.environ):
"""Clean up data that was created with ``ingest`` or
``$ python -m zipline ingest``
Parameters
----------
name : str
The name of the bundle to remove data for.
before : datetime, optional
Remove data ingested before this date.
This argument is mutually exclusive with: keep_last
after : datetime, optional
Remove data ingested after this date.
This argument is mutually exclusive with: keep_last
keep_last : int, optional
Remove all but the last ``keep_last`` ingestions.
This argument is mutually exclusive with:
before
after
environ : mapping, optional
The environment variables. Defaults of os.environ.
Returns
-------
cleaned : set[str]
The names of the runs that were removed.
Raises
------
BadClean
Raised when ``before`` and or ``after`` are passed with
``keep_last``. This is a subclass of ``ValueError``.
"""
try:
all_runs = sorted(
filter(
complement(pth.hidden),
os.listdir(pth.data_path([name], environ=environ)),
),
key=from_bundle_ingest_dirname,
)
except OSError as e:
if e.errno != errno.ENOENT:
raise
raise UnknownBundle(name)
if ((before is not None or after is not None) and
keep_last is not None):
raise BadClean(before, after, keep_last)
if keep_last is None:
def should_clean(name):
dt = from_bundle_ingest_dirname(name)
return (
(before is not None and dt < before) or
(after is not None and dt > after)
)
elif keep_last >= 0:
last_n_dts = set(take(keep_last, reversed(all_runs)))
def should_clean(name):
return name not in last_n_dts
else:
raise BadClean(before, after, keep_last)
cleaned = set()
for run in all_runs:
if should_clean(run):
path = pth.data_path([name, run], environ=environ)
shutil.rmtree(path)
cleaned.add(path)
return cleaned
return BundleCore(bundles, register, unregister, ingest, load, clean)
|
python
|
def _make_bundle_core():
"""Create a family of data bundle functions that read from the same
bundle mapping.
Returns
-------
bundles : mappingproxy
The mapping of bundles to bundle payloads.
register : callable
The function which registers new bundles in the ``bundles`` mapping.
unregister : callable
The function which deregisters bundles from the ``bundles`` mapping.
ingest : callable
The function which downloads and write data for a given data bundle.
load : callable
The function which loads the ingested bundles back into memory.
clean : callable
The function which cleans up data written with ``ingest``.
"""
_bundles = {} # the registered bundles
# Expose _bundles through a proxy so that users cannot mutate this
# accidentally. Users may go through `register` to update this which will
# warn when trampling another bundle.
bundles = mappingproxy(_bundles)
@curry
def register(name,
f,
calendar_name='NYSE',
start_session=None,
end_session=None,
minutes_per_day=390,
create_writers=True):
"""Register a data bundle ingest function.
Parameters
----------
name : str
The name of the bundle.
f : callable
The ingest function. This function will be passed:
environ : mapping
The environment this is being run with.
asset_db_writer : AssetDBWriter
The asset db writer to write into.
minute_bar_writer : BcolzMinuteBarWriter
The minute bar writer to write into.
daily_bar_writer : BcolzDailyBarWriter
The daily bar writer to write into.
adjustment_writer : SQLiteAdjustmentWriter
The adjustment db writer to write into.
calendar : trading_calendars.TradingCalendar
The trading calendar to ingest for.
start_session : pd.Timestamp
The first session of data to ingest.
end_session : pd.Timestamp
The last session of data to ingest.
cache : DataFrameCache
A mapping object to temporarily store dataframes.
This should be used to cache intermediates in case the load
fails. This will be automatically cleaned up after a
successful load.
show_progress : bool
Show the progress for the current load where possible.
calendar_name : str, optional
The name of a calendar used to align bundle data.
Default is 'NYSE'.
start_session : pd.Timestamp, optional
The first session for which we want data. If not provided,
or if the date lies outside the range supported by the
calendar, the first_session of the calendar is used.
end_session : pd.Timestamp, optional
The last session for which we want data. If not provided,
or if the date lies outside the range supported by the
calendar, the last_session of the calendar is used.
minutes_per_day : int, optional
The number of minutes in each normal trading day.
create_writers : bool, optional
Should the ingest machinery create the writers for the ingest
function. This can be disabled as an optimization for cases where
they are not needed, like the ``quantopian-quandl`` bundle.
Notes
-----
This function my be used as a decorator, for example:
.. code-block:: python
@register('quandl')
def quandl_ingest_function(...):
...
See Also
--------
zipline.data.bundles.bundles
"""
if name in bundles:
warnings.warn(
'Overwriting bundle with name %r' % name,
stacklevel=3,
)
# NOTE: We don't eagerly compute calendar values here because
# `register` is called at module scope in zipline, and creating a
# calendar currently takes between 0.5 and 1 seconds, which causes a
# noticeable delay on the zipline CLI.
_bundles[name] = RegisteredBundle(
calendar_name=calendar_name,
start_session=start_session,
end_session=end_session,
minutes_per_day=minutes_per_day,
ingest=f,
create_writers=create_writers,
)
return f
def unregister(name):
"""Unregister a bundle.
Parameters
----------
name : str
The name of the bundle to unregister.
Raises
------
UnknownBundle
Raised when no bundle has been registered with the given name.
See Also
--------
zipline.data.bundles.bundles
"""
try:
del _bundles[name]
except KeyError:
raise UnknownBundle(name)
def ingest(name,
environ=os.environ,
timestamp=None,
assets_versions=(),
show_progress=False):
"""Ingest data for a given bundle.
Parameters
----------
name : str
The name of the bundle.
environ : mapping, optional
The environment variables. By default this is os.environ.
timestamp : datetime, optional
The timestamp to use for the load.
By default this is the current time.
assets_versions : Iterable[int], optional
Versions of the assets db to which to downgrade.
show_progress : bool, optional
Tell the ingest function to display the progress where possible.
"""
try:
bundle = bundles[name]
except KeyError:
raise UnknownBundle(name)
calendar = get_calendar(bundle.calendar_name)
start_session = bundle.start_session
end_session = bundle.end_session
if start_session is None or start_session < calendar.first_session:
start_session = calendar.first_session
if end_session is None or end_session > calendar.last_session:
end_session = calendar.last_session
if timestamp is None:
timestamp = pd.Timestamp.utcnow()
timestamp = timestamp.tz_convert('utc').tz_localize(None)
timestr = to_bundle_ingest_dirname(timestamp)
cachepath = cache_path(name, environ=environ)
pth.ensure_directory(pth.data_path([name, timestr], environ=environ))
pth.ensure_directory(cachepath)
with dataframe_cache(cachepath, clean_on_failure=False) as cache, \
ExitStack() as stack:
# we use `cleanup_on_failure=False` so that we don't purge the
# cache directory if the load fails in the middle
if bundle.create_writers:
wd = stack.enter_context(working_dir(
pth.data_path([], environ=environ))
)
daily_bars_path = wd.ensure_dir(
*daily_equity_relative(
name, timestr, environ=environ,
)
)
daily_bar_writer = BcolzDailyBarWriter(
daily_bars_path,
calendar,
start_session,
end_session,
)
# Do an empty write to ensure that the daily ctables exist
# when we create the SQLiteAdjustmentWriter below. The
# SQLiteAdjustmentWriter needs to open the daily ctables so
# that it can compute the adjustment ratios for the dividends.
daily_bar_writer.write(())
minute_bar_writer = BcolzMinuteBarWriter(
wd.ensure_dir(*minute_equity_relative(
name, timestr, environ=environ)
),
calendar,
start_session,
end_session,
minutes_per_day=bundle.minutes_per_day,
)
assets_db_path = wd.getpath(*asset_db_relative(
name, timestr, environ=environ,
))
asset_db_writer = AssetDBWriter(assets_db_path)
adjustment_db_writer = stack.enter_context(
SQLiteAdjustmentWriter(
wd.getpath(*adjustment_db_relative(
name, timestr, environ=environ)),
BcolzDailyBarReader(daily_bars_path),
overwrite=True,
)
)
else:
daily_bar_writer = None
minute_bar_writer = None
asset_db_writer = None
adjustment_db_writer = None
if assets_versions:
raise ValueError('Need to ingest a bundle that creates '
'writers in order to downgrade the assets'
' db.')
bundle.ingest(
environ,
asset_db_writer,
minute_bar_writer,
daily_bar_writer,
adjustment_db_writer,
calendar,
start_session,
end_session,
cache,
show_progress,
pth.data_path([name, timestr], environ=environ),
)
for version in sorted(set(assets_versions), reverse=True):
version_path = wd.getpath(*asset_db_relative(
name, timestr, environ=environ, db_version=version,
))
with working_file(version_path) as wf:
shutil.copy2(assets_db_path, wf.path)
downgrade(wf.path, version)
def most_recent_data(bundle_name, timestamp, environ=None):
"""Get the path to the most recent data after ``date``for the
given bundle.
Parameters
----------
bundle_name : str
The name of the bundle to lookup.
timestamp : datetime
The timestamp to begin searching on or before.
environ : dict, optional
An environment dict to forward to zipline_root.
"""
if bundle_name not in bundles:
raise UnknownBundle(bundle_name)
try:
candidates = os.listdir(
pth.data_path([bundle_name], environ=environ),
)
return pth.data_path(
[bundle_name,
max(
filter(complement(pth.hidden), candidates),
key=from_bundle_ingest_dirname,
)],
environ=environ,
)
except (ValueError, OSError) as e:
if getattr(e, 'errno', errno.ENOENT) != errno.ENOENT:
raise
raise ValueError(
'no data for bundle {bundle!r} on or before {timestamp}\n'
'maybe you need to run: $ zipline ingest -b {bundle}'.format(
bundle=bundle_name,
timestamp=timestamp,
),
)
def load(name, environ=os.environ, timestamp=None):
"""Loads a previously ingested bundle.
Parameters
----------
name : str
The name of the bundle.
environ : mapping, optional
The environment variables. Defaults of os.environ.
timestamp : datetime, optional
The timestamp of the data to lookup.
Defaults to the current time.
Returns
-------
bundle_data : BundleData
The raw data readers for this bundle.
"""
if timestamp is None:
timestamp = pd.Timestamp.utcnow()
timestr = most_recent_data(name, timestamp, environ=environ)
return BundleData(
asset_finder=AssetFinder(
asset_db_path(name, timestr, environ=environ),
),
equity_minute_bar_reader=BcolzMinuteBarReader(
minute_equity_path(name, timestr, environ=environ),
),
equity_daily_bar_reader=BcolzDailyBarReader(
daily_equity_path(name, timestr, environ=environ),
),
adjustment_reader=SQLiteAdjustmentReader(
adjustment_db_path(name, timestr, environ=environ),
),
)
@preprocess(
before=optionally(ensure_timestamp),
after=optionally(ensure_timestamp),
)
def clean(name,
before=None,
after=None,
keep_last=None,
environ=os.environ):
"""Clean up data that was created with ``ingest`` or
``$ python -m zipline ingest``
Parameters
----------
name : str
The name of the bundle to remove data for.
before : datetime, optional
Remove data ingested before this date.
This argument is mutually exclusive with: keep_last
after : datetime, optional
Remove data ingested after this date.
This argument is mutually exclusive with: keep_last
keep_last : int, optional
Remove all but the last ``keep_last`` ingestions.
This argument is mutually exclusive with:
before
after
environ : mapping, optional
The environment variables. Defaults of os.environ.
Returns
-------
cleaned : set[str]
The names of the runs that were removed.
Raises
------
BadClean
Raised when ``before`` and or ``after`` are passed with
``keep_last``. This is a subclass of ``ValueError``.
"""
try:
all_runs = sorted(
filter(
complement(pth.hidden),
os.listdir(pth.data_path([name], environ=environ)),
),
key=from_bundle_ingest_dirname,
)
except OSError as e:
if e.errno != errno.ENOENT:
raise
raise UnknownBundle(name)
if ((before is not None or after is not None) and
keep_last is not None):
raise BadClean(before, after, keep_last)
if keep_last is None:
def should_clean(name):
dt = from_bundle_ingest_dirname(name)
return (
(before is not None and dt < before) or
(after is not None and dt > after)
)
elif keep_last >= 0:
last_n_dts = set(take(keep_last, reversed(all_runs)))
def should_clean(name):
return name not in last_n_dts
else:
raise BadClean(before, after, keep_last)
cleaned = set()
for run in all_runs:
if should_clean(run):
path = pth.data_path([name, run], environ=environ)
shutil.rmtree(path)
cleaned.add(path)
return cleaned
return BundleCore(bundles, register, unregister, ingest, load, clean)
|
[
"def",
"_make_bundle_core",
"(",
")",
":",
"_bundles",
"=",
"{",
"}",
"# the registered bundles",
"# Expose _bundles through a proxy so that users cannot mutate this",
"# accidentally. Users may go through `register` to update this which will",
"# warn when trampling another bundle.",
"bundles",
"=",
"mappingproxy",
"(",
"_bundles",
")",
"@",
"curry",
"def",
"register",
"(",
"name",
",",
"f",
",",
"calendar_name",
"=",
"'NYSE'",
",",
"start_session",
"=",
"None",
",",
"end_session",
"=",
"None",
",",
"minutes_per_day",
"=",
"390",
",",
"create_writers",
"=",
"True",
")",
":",
"\"\"\"Register a data bundle ingest function.\n\n Parameters\n ----------\n name : str\n The name of the bundle.\n f : callable\n The ingest function. This function will be passed:\n\n environ : mapping\n The environment this is being run with.\n asset_db_writer : AssetDBWriter\n The asset db writer to write into.\n minute_bar_writer : BcolzMinuteBarWriter\n The minute bar writer to write into.\n daily_bar_writer : BcolzDailyBarWriter\n The daily bar writer to write into.\n adjustment_writer : SQLiteAdjustmentWriter\n The adjustment db writer to write into.\n calendar : trading_calendars.TradingCalendar\n The trading calendar to ingest for.\n start_session : pd.Timestamp\n The first session of data to ingest.\n end_session : pd.Timestamp\n The last session of data to ingest.\n cache : DataFrameCache\n A mapping object to temporarily store dataframes.\n This should be used to cache intermediates in case the load\n fails. This will be automatically cleaned up after a\n successful load.\n show_progress : bool\n Show the progress for the current load where possible.\n calendar_name : str, optional\n The name of a calendar used to align bundle data.\n Default is 'NYSE'.\n start_session : pd.Timestamp, optional\n The first session for which we want data. If not provided,\n or if the date lies outside the range supported by the\n calendar, the first_session of the calendar is used.\n end_session : pd.Timestamp, optional\n The last session for which we want data. If not provided,\n or if the date lies outside the range supported by the\n calendar, the last_session of the calendar is used.\n minutes_per_day : int, optional\n The number of minutes in each normal trading day.\n create_writers : bool, optional\n Should the ingest machinery create the writers for the ingest\n function. This can be disabled as an optimization for cases where\n they are not needed, like the ``quantopian-quandl`` bundle.\n\n Notes\n -----\n This function my be used as a decorator, for example:\n\n .. code-block:: python\n\n @register('quandl')\n def quandl_ingest_function(...):\n ...\n\n See Also\n --------\n zipline.data.bundles.bundles\n \"\"\"",
"if",
"name",
"in",
"bundles",
":",
"warnings",
".",
"warn",
"(",
"'Overwriting bundle with name %r'",
"%",
"name",
",",
"stacklevel",
"=",
"3",
",",
")",
"# NOTE: We don't eagerly compute calendar values here because",
"# `register` is called at module scope in zipline, and creating a",
"# calendar currently takes between 0.5 and 1 seconds, which causes a",
"# noticeable delay on the zipline CLI.",
"_bundles",
"[",
"name",
"]",
"=",
"RegisteredBundle",
"(",
"calendar_name",
"=",
"calendar_name",
",",
"start_session",
"=",
"start_session",
",",
"end_session",
"=",
"end_session",
",",
"minutes_per_day",
"=",
"minutes_per_day",
",",
"ingest",
"=",
"f",
",",
"create_writers",
"=",
"create_writers",
",",
")",
"return",
"f",
"def",
"unregister",
"(",
"name",
")",
":",
"\"\"\"Unregister a bundle.\n\n Parameters\n ----------\n name : str\n The name of the bundle to unregister.\n\n Raises\n ------\n UnknownBundle\n Raised when no bundle has been registered with the given name.\n\n See Also\n --------\n zipline.data.bundles.bundles\n \"\"\"",
"try",
":",
"del",
"_bundles",
"[",
"name",
"]",
"except",
"KeyError",
":",
"raise",
"UnknownBundle",
"(",
"name",
")",
"def",
"ingest",
"(",
"name",
",",
"environ",
"=",
"os",
".",
"environ",
",",
"timestamp",
"=",
"None",
",",
"assets_versions",
"=",
"(",
")",
",",
"show_progress",
"=",
"False",
")",
":",
"\"\"\"Ingest data for a given bundle.\n\n Parameters\n ----------\n name : str\n The name of the bundle.\n environ : mapping, optional\n The environment variables. By default this is os.environ.\n timestamp : datetime, optional\n The timestamp to use for the load.\n By default this is the current time.\n assets_versions : Iterable[int], optional\n Versions of the assets db to which to downgrade.\n show_progress : bool, optional\n Tell the ingest function to display the progress where possible.\n \"\"\"",
"try",
":",
"bundle",
"=",
"bundles",
"[",
"name",
"]",
"except",
"KeyError",
":",
"raise",
"UnknownBundle",
"(",
"name",
")",
"calendar",
"=",
"get_calendar",
"(",
"bundle",
".",
"calendar_name",
")",
"start_session",
"=",
"bundle",
".",
"start_session",
"end_session",
"=",
"bundle",
".",
"end_session",
"if",
"start_session",
"is",
"None",
"or",
"start_session",
"<",
"calendar",
".",
"first_session",
":",
"start_session",
"=",
"calendar",
".",
"first_session",
"if",
"end_session",
"is",
"None",
"or",
"end_session",
">",
"calendar",
".",
"last_session",
":",
"end_session",
"=",
"calendar",
".",
"last_session",
"if",
"timestamp",
"is",
"None",
":",
"timestamp",
"=",
"pd",
".",
"Timestamp",
".",
"utcnow",
"(",
")",
"timestamp",
"=",
"timestamp",
".",
"tz_convert",
"(",
"'utc'",
")",
".",
"tz_localize",
"(",
"None",
")",
"timestr",
"=",
"to_bundle_ingest_dirname",
"(",
"timestamp",
")",
"cachepath",
"=",
"cache_path",
"(",
"name",
",",
"environ",
"=",
"environ",
")",
"pth",
".",
"ensure_directory",
"(",
"pth",
".",
"data_path",
"(",
"[",
"name",
",",
"timestr",
"]",
",",
"environ",
"=",
"environ",
")",
")",
"pth",
".",
"ensure_directory",
"(",
"cachepath",
")",
"with",
"dataframe_cache",
"(",
"cachepath",
",",
"clean_on_failure",
"=",
"False",
")",
"as",
"cache",
",",
"ExitStack",
"(",
")",
"as",
"stack",
":",
"# we use `cleanup_on_failure=False` so that we don't purge the",
"# cache directory if the load fails in the middle",
"if",
"bundle",
".",
"create_writers",
":",
"wd",
"=",
"stack",
".",
"enter_context",
"(",
"working_dir",
"(",
"pth",
".",
"data_path",
"(",
"[",
"]",
",",
"environ",
"=",
"environ",
")",
")",
")",
"daily_bars_path",
"=",
"wd",
".",
"ensure_dir",
"(",
"*",
"daily_equity_relative",
"(",
"name",
",",
"timestr",
",",
"environ",
"=",
"environ",
",",
")",
")",
"daily_bar_writer",
"=",
"BcolzDailyBarWriter",
"(",
"daily_bars_path",
",",
"calendar",
",",
"start_session",
",",
"end_session",
",",
")",
"# Do an empty write to ensure that the daily ctables exist",
"# when we create the SQLiteAdjustmentWriter below. The",
"# SQLiteAdjustmentWriter needs to open the daily ctables so",
"# that it can compute the adjustment ratios for the dividends.",
"daily_bar_writer",
".",
"write",
"(",
"(",
")",
")",
"minute_bar_writer",
"=",
"BcolzMinuteBarWriter",
"(",
"wd",
".",
"ensure_dir",
"(",
"*",
"minute_equity_relative",
"(",
"name",
",",
"timestr",
",",
"environ",
"=",
"environ",
")",
")",
",",
"calendar",
",",
"start_session",
",",
"end_session",
",",
"minutes_per_day",
"=",
"bundle",
".",
"minutes_per_day",
",",
")",
"assets_db_path",
"=",
"wd",
".",
"getpath",
"(",
"*",
"asset_db_relative",
"(",
"name",
",",
"timestr",
",",
"environ",
"=",
"environ",
",",
")",
")",
"asset_db_writer",
"=",
"AssetDBWriter",
"(",
"assets_db_path",
")",
"adjustment_db_writer",
"=",
"stack",
".",
"enter_context",
"(",
"SQLiteAdjustmentWriter",
"(",
"wd",
".",
"getpath",
"(",
"*",
"adjustment_db_relative",
"(",
"name",
",",
"timestr",
",",
"environ",
"=",
"environ",
")",
")",
",",
"BcolzDailyBarReader",
"(",
"daily_bars_path",
")",
",",
"overwrite",
"=",
"True",
",",
")",
")",
"else",
":",
"daily_bar_writer",
"=",
"None",
"minute_bar_writer",
"=",
"None",
"asset_db_writer",
"=",
"None",
"adjustment_db_writer",
"=",
"None",
"if",
"assets_versions",
":",
"raise",
"ValueError",
"(",
"'Need to ingest a bundle that creates '",
"'writers in order to downgrade the assets'",
"' db.'",
")",
"bundle",
".",
"ingest",
"(",
"environ",
",",
"asset_db_writer",
",",
"minute_bar_writer",
",",
"daily_bar_writer",
",",
"adjustment_db_writer",
",",
"calendar",
",",
"start_session",
",",
"end_session",
",",
"cache",
",",
"show_progress",
",",
"pth",
".",
"data_path",
"(",
"[",
"name",
",",
"timestr",
"]",
",",
"environ",
"=",
"environ",
")",
",",
")",
"for",
"version",
"in",
"sorted",
"(",
"set",
"(",
"assets_versions",
")",
",",
"reverse",
"=",
"True",
")",
":",
"version_path",
"=",
"wd",
".",
"getpath",
"(",
"*",
"asset_db_relative",
"(",
"name",
",",
"timestr",
",",
"environ",
"=",
"environ",
",",
"db_version",
"=",
"version",
",",
")",
")",
"with",
"working_file",
"(",
"version_path",
")",
"as",
"wf",
":",
"shutil",
".",
"copy2",
"(",
"assets_db_path",
",",
"wf",
".",
"path",
")",
"downgrade",
"(",
"wf",
".",
"path",
",",
"version",
")",
"def",
"most_recent_data",
"(",
"bundle_name",
",",
"timestamp",
",",
"environ",
"=",
"None",
")",
":",
"\"\"\"Get the path to the most recent data after ``date``for the\n given bundle.\n\n Parameters\n ----------\n bundle_name : str\n The name of the bundle to lookup.\n timestamp : datetime\n The timestamp to begin searching on or before.\n environ : dict, optional\n An environment dict to forward to zipline_root.\n \"\"\"",
"if",
"bundle_name",
"not",
"in",
"bundles",
":",
"raise",
"UnknownBundle",
"(",
"bundle_name",
")",
"try",
":",
"candidates",
"=",
"os",
".",
"listdir",
"(",
"pth",
".",
"data_path",
"(",
"[",
"bundle_name",
"]",
",",
"environ",
"=",
"environ",
")",
",",
")",
"return",
"pth",
".",
"data_path",
"(",
"[",
"bundle_name",
",",
"max",
"(",
"filter",
"(",
"complement",
"(",
"pth",
".",
"hidden",
")",
",",
"candidates",
")",
",",
"key",
"=",
"from_bundle_ingest_dirname",
",",
")",
"]",
",",
"environ",
"=",
"environ",
",",
")",
"except",
"(",
"ValueError",
",",
"OSError",
")",
"as",
"e",
":",
"if",
"getattr",
"(",
"e",
",",
"'errno'",
",",
"errno",
".",
"ENOENT",
")",
"!=",
"errno",
".",
"ENOENT",
":",
"raise",
"raise",
"ValueError",
"(",
"'no data for bundle {bundle!r} on or before {timestamp}\\n'",
"'maybe you need to run: $ zipline ingest -b {bundle}'",
".",
"format",
"(",
"bundle",
"=",
"bundle_name",
",",
"timestamp",
"=",
"timestamp",
",",
")",
",",
")",
"def",
"load",
"(",
"name",
",",
"environ",
"=",
"os",
".",
"environ",
",",
"timestamp",
"=",
"None",
")",
":",
"\"\"\"Loads a previously ingested bundle.\n\n Parameters\n ----------\n name : str\n The name of the bundle.\n environ : mapping, optional\n The environment variables. Defaults of os.environ.\n timestamp : datetime, optional\n The timestamp of the data to lookup.\n Defaults to the current time.\n\n Returns\n -------\n bundle_data : BundleData\n The raw data readers for this bundle.\n \"\"\"",
"if",
"timestamp",
"is",
"None",
":",
"timestamp",
"=",
"pd",
".",
"Timestamp",
".",
"utcnow",
"(",
")",
"timestr",
"=",
"most_recent_data",
"(",
"name",
",",
"timestamp",
",",
"environ",
"=",
"environ",
")",
"return",
"BundleData",
"(",
"asset_finder",
"=",
"AssetFinder",
"(",
"asset_db_path",
"(",
"name",
",",
"timestr",
",",
"environ",
"=",
"environ",
")",
",",
")",
",",
"equity_minute_bar_reader",
"=",
"BcolzMinuteBarReader",
"(",
"minute_equity_path",
"(",
"name",
",",
"timestr",
",",
"environ",
"=",
"environ",
")",
",",
")",
",",
"equity_daily_bar_reader",
"=",
"BcolzDailyBarReader",
"(",
"daily_equity_path",
"(",
"name",
",",
"timestr",
",",
"environ",
"=",
"environ",
")",
",",
")",
",",
"adjustment_reader",
"=",
"SQLiteAdjustmentReader",
"(",
"adjustment_db_path",
"(",
"name",
",",
"timestr",
",",
"environ",
"=",
"environ",
")",
",",
")",
",",
")",
"@",
"preprocess",
"(",
"before",
"=",
"optionally",
"(",
"ensure_timestamp",
")",
",",
"after",
"=",
"optionally",
"(",
"ensure_timestamp",
")",
",",
")",
"def",
"clean",
"(",
"name",
",",
"before",
"=",
"None",
",",
"after",
"=",
"None",
",",
"keep_last",
"=",
"None",
",",
"environ",
"=",
"os",
".",
"environ",
")",
":",
"\"\"\"Clean up data that was created with ``ingest`` or\n ``$ python -m zipline ingest``\n\n Parameters\n ----------\n name : str\n The name of the bundle to remove data for.\n before : datetime, optional\n Remove data ingested before this date.\n This argument is mutually exclusive with: keep_last\n after : datetime, optional\n Remove data ingested after this date.\n This argument is mutually exclusive with: keep_last\n keep_last : int, optional\n Remove all but the last ``keep_last`` ingestions.\n This argument is mutually exclusive with:\n before\n after\n environ : mapping, optional\n The environment variables. Defaults of os.environ.\n\n Returns\n -------\n cleaned : set[str]\n The names of the runs that were removed.\n\n Raises\n ------\n BadClean\n Raised when ``before`` and or ``after`` are passed with\n ``keep_last``. This is a subclass of ``ValueError``.\n \"\"\"",
"try",
":",
"all_runs",
"=",
"sorted",
"(",
"filter",
"(",
"complement",
"(",
"pth",
".",
"hidden",
")",
",",
"os",
".",
"listdir",
"(",
"pth",
".",
"data_path",
"(",
"[",
"name",
"]",
",",
"environ",
"=",
"environ",
")",
")",
",",
")",
",",
"key",
"=",
"from_bundle_ingest_dirname",
",",
")",
"except",
"OSError",
"as",
"e",
":",
"if",
"e",
".",
"errno",
"!=",
"errno",
".",
"ENOENT",
":",
"raise",
"raise",
"UnknownBundle",
"(",
"name",
")",
"if",
"(",
"(",
"before",
"is",
"not",
"None",
"or",
"after",
"is",
"not",
"None",
")",
"and",
"keep_last",
"is",
"not",
"None",
")",
":",
"raise",
"BadClean",
"(",
"before",
",",
"after",
",",
"keep_last",
")",
"if",
"keep_last",
"is",
"None",
":",
"def",
"should_clean",
"(",
"name",
")",
":",
"dt",
"=",
"from_bundle_ingest_dirname",
"(",
"name",
")",
"return",
"(",
"(",
"before",
"is",
"not",
"None",
"and",
"dt",
"<",
"before",
")",
"or",
"(",
"after",
"is",
"not",
"None",
"and",
"dt",
">",
"after",
")",
")",
"elif",
"keep_last",
">=",
"0",
":",
"last_n_dts",
"=",
"set",
"(",
"take",
"(",
"keep_last",
",",
"reversed",
"(",
"all_runs",
")",
")",
")",
"def",
"should_clean",
"(",
"name",
")",
":",
"return",
"name",
"not",
"in",
"last_n_dts",
"else",
":",
"raise",
"BadClean",
"(",
"before",
",",
"after",
",",
"keep_last",
")",
"cleaned",
"=",
"set",
"(",
")",
"for",
"run",
"in",
"all_runs",
":",
"if",
"should_clean",
"(",
"run",
")",
":",
"path",
"=",
"pth",
".",
"data_path",
"(",
"[",
"name",
",",
"run",
"]",
",",
"environ",
"=",
"environ",
")",
"shutil",
".",
"rmtree",
"(",
"path",
")",
"cleaned",
".",
"add",
"(",
"path",
")",
"return",
"cleaned",
"return",
"BundleCore",
"(",
"bundles",
",",
"register",
",",
"unregister",
",",
"ingest",
",",
"load",
",",
"clean",
")"
] |
Create a family of data bundle functions that read from the same
bundle mapping.
Returns
-------
bundles : mappingproxy
The mapping of bundles to bundle payloads.
register : callable
The function which registers new bundles in the ``bundles`` mapping.
unregister : callable
The function which deregisters bundles from the ``bundles`` mapping.
ingest : callable
The function which downloads and write data for a given data bundle.
load : callable
The function which loads the ingested bundles back into memory.
clean : callable
The function which cleans up data written with ``ingest``.
|
[
"Create",
"a",
"family",
"of",
"data",
"bundle",
"functions",
"that",
"read",
"from",
"the",
"same",
"bundle",
"mapping",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/bundles/core.py#L195-L614
|
train
|
quantopian/zipline
|
zipline/utils/deprecate.py
|
deprecated
|
def deprecated(msg=None, stacklevel=2):
"""
Used to mark a function as deprecated.
Parameters
----------
msg : str
The message to display in the deprecation warning.
stacklevel : int
How far up the stack the warning needs to go, before
showing the relevant calling lines.
Examples
--------
@deprecated(msg='function_a is deprecated! Use function_b instead.')
def function_a(*args, **kwargs):
"""
def deprecated_dec(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
warnings.warn(
msg or "Function %s is deprecated." % fn.__name__,
category=DeprecationWarning,
stacklevel=stacklevel
)
return fn(*args, **kwargs)
return wrapper
return deprecated_dec
|
python
|
def deprecated(msg=None, stacklevel=2):
"""
Used to mark a function as deprecated.
Parameters
----------
msg : str
The message to display in the deprecation warning.
stacklevel : int
How far up the stack the warning needs to go, before
showing the relevant calling lines.
Examples
--------
@deprecated(msg='function_a is deprecated! Use function_b instead.')
def function_a(*args, **kwargs):
"""
def deprecated_dec(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
warnings.warn(
msg or "Function %s is deprecated." % fn.__name__,
category=DeprecationWarning,
stacklevel=stacklevel
)
return fn(*args, **kwargs)
return wrapper
return deprecated_dec
|
[
"def",
"deprecated",
"(",
"msg",
"=",
"None",
",",
"stacklevel",
"=",
"2",
")",
":",
"def",
"deprecated_dec",
"(",
"fn",
")",
":",
"@",
"wraps",
"(",
"fn",
")",
"def",
"wrapper",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"warnings",
".",
"warn",
"(",
"msg",
"or",
"\"Function %s is deprecated.\"",
"%",
"fn",
".",
"__name__",
",",
"category",
"=",
"DeprecationWarning",
",",
"stacklevel",
"=",
"stacklevel",
")",
"return",
"fn",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"return",
"wrapper",
"return",
"deprecated_dec"
] |
Used to mark a function as deprecated.
Parameters
----------
msg : str
The message to display in the deprecation warning.
stacklevel : int
How far up the stack the warning needs to go, before
showing the relevant calling lines.
Examples
--------
@deprecated(msg='function_a is deprecated! Use function_b instead.')
def function_a(*args, **kwargs):
|
[
"Used",
"to",
"mark",
"a",
"function",
"as",
"deprecated",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/deprecate.py#L20-L47
|
train
|
quantopian/zipline
|
zipline/data/history_loader.py
|
HistoryCompatibleUSEquityAdjustmentReader.load_pricing_adjustments
|
def load_pricing_adjustments(self, columns, dts, assets):
"""
Returns
-------
adjustments : list[dict[int -> Adjustment]]
A list, where each element corresponds to the `columns`, of
mappings from index to adjustment objects to apply at that index.
"""
out = [None] * len(columns)
for i, column in enumerate(columns):
adjs = {}
for asset in assets:
adjs.update(self._get_adjustments_in_range(
asset, dts, column))
out[i] = adjs
return out
|
python
|
def load_pricing_adjustments(self, columns, dts, assets):
"""
Returns
-------
adjustments : list[dict[int -> Adjustment]]
A list, where each element corresponds to the `columns`, of
mappings from index to adjustment objects to apply at that index.
"""
out = [None] * len(columns)
for i, column in enumerate(columns):
adjs = {}
for asset in assets:
adjs.update(self._get_adjustments_in_range(
asset, dts, column))
out[i] = adjs
return out
|
[
"def",
"load_pricing_adjustments",
"(",
"self",
",",
"columns",
",",
"dts",
",",
"assets",
")",
":",
"out",
"=",
"[",
"None",
"]",
"*",
"len",
"(",
"columns",
")",
"for",
"i",
",",
"column",
"in",
"enumerate",
"(",
"columns",
")",
":",
"adjs",
"=",
"{",
"}",
"for",
"asset",
"in",
"assets",
":",
"adjs",
".",
"update",
"(",
"self",
".",
"_get_adjustments_in_range",
"(",
"asset",
",",
"dts",
",",
"column",
")",
")",
"out",
"[",
"i",
"]",
"=",
"adjs",
"return",
"out"
] |
Returns
-------
adjustments : list[dict[int -> Adjustment]]
A list, where each element corresponds to the `columns`, of
mappings from index to adjustment objects to apply at that index.
|
[
"Returns",
"-------",
"adjustments",
":",
"list",
"[",
"dict",
"[",
"int",
"-",
">",
"Adjustment",
"]]",
"A",
"list",
"where",
"each",
"element",
"corresponds",
"to",
"the",
"columns",
"of",
"mappings",
"from",
"index",
"to",
"adjustment",
"objects",
"to",
"apply",
"at",
"that",
"index",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/history_loader.py#L48-L63
|
train
|
quantopian/zipline
|
zipline/data/history_loader.py
|
HistoryCompatibleUSEquityAdjustmentReader._get_adjustments_in_range
|
def _get_adjustments_in_range(self, asset, dts, field):
"""
Get the Float64Multiply objects to pass to an AdjustedArrayWindow.
For the use of AdjustedArrayWindow in the loader, which looks back
from current simulation time back to a window of data the dictionary is
structured with:
- the key into the dictionary for adjustments is the location of the
day from which the window is being viewed.
- the start of all multiply objects is always 0 (in each window all
adjustments are overlapping)
- the end of the multiply object is the location before the calendar
location of the adjustment action, making all days before the event
adjusted.
Parameters
----------
asset : Asset
The assets for which to get adjustments.
dts : iterable of datetime64-like
The dts for which adjustment data is needed.
field : str
OHLCV field for which to get the adjustments.
Returns
-------
out : dict[loc -> Float64Multiply]
The adjustments as a dict of loc -> Float64Multiply
"""
sid = int(asset)
start = normalize_date(dts[0])
end = normalize_date(dts[-1])
adjs = {}
if field != 'volume':
mergers = self._adjustments_reader.get_adjustments_for_sid(
'mergers', sid)
for m in mergers:
dt = m[0]
if start < dt <= end:
end_loc = dts.searchsorted(dt)
adj_loc = end_loc
mult = Float64Multiply(0,
end_loc - 1,
0,
0,
m[1])
try:
adjs[adj_loc].append(mult)
except KeyError:
adjs[adj_loc] = [mult]
divs = self._adjustments_reader.get_adjustments_for_sid(
'dividends', sid)
for d in divs:
dt = d[0]
if start < dt <= end:
end_loc = dts.searchsorted(dt)
adj_loc = end_loc
mult = Float64Multiply(0,
end_loc - 1,
0,
0,
d[1])
try:
adjs[adj_loc].append(mult)
except KeyError:
adjs[adj_loc] = [mult]
splits = self._adjustments_reader.get_adjustments_for_sid(
'splits', sid)
for s in splits:
dt = s[0]
if start < dt <= end:
if field == 'volume':
ratio = 1.0 / s[1]
else:
ratio = s[1]
end_loc = dts.searchsorted(dt)
adj_loc = end_loc
mult = Float64Multiply(0,
end_loc - 1,
0,
0,
ratio)
try:
adjs[adj_loc].append(mult)
except KeyError:
adjs[adj_loc] = [mult]
return adjs
|
python
|
def _get_adjustments_in_range(self, asset, dts, field):
"""
Get the Float64Multiply objects to pass to an AdjustedArrayWindow.
For the use of AdjustedArrayWindow in the loader, which looks back
from current simulation time back to a window of data the dictionary is
structured with:
- the key into the dictionary for adjustments is the location of the
day from which the window is being viewed.
- the start of all multiply objects is always 0 (in each window all
adjustments are overlapping)
- the end of the multiply object is the location before the calendar
location of the adjustment action, making all days before the event
adjusted.
Parameters
----------
asset : Asset
The assets for which to get adjustments.
dts : iterable of datetime64-like
The dts for which adjustment data is needed.
field : str
OHLCV field for which to get the adjustments.
Returns
-------
out : dict[loc -> Float64Multiply]
The adjustments as a dict of loc -> Float64Multiply
"""
sid = int(asset)
start = normalize_date(dts[0])
end = normalize_date(dts[-1])
adjs = {}
if field != 'volume':
mergers = self._adjustments_reader.get_adjustments_for_sid(
'mergers', sid)
for m in mergers:
dt = m[0]
if start < dt <= end:
end_loc = dts.searchsorted(dt)
adj_loc = end_loc
mult = Float64Multiply(0,
end_loc - 1,
0,
0,
m[1])
try:
adjs[adj_loc].append(mult)
except KeyError:
adjs[adj_loc] = [mult]
divs = self._adjustments_reader.get_adjustments_for_sid(
'dividends', sid)
for d in divs:
dt = d[0]
if start < dt <= end:
end_loc = dts.searchsorted(dt)
adj_loc = end_loc
mult = Float64Multiply(0,
end_loc - 1,
0,
0,
d[1])
try:
adjs[adj_loc].append(mult)
except KeyError:
adjs[adj_loc] = [mult]
splits = self._adjustments_reader.get_adjustments_for_sid(
'splits', sid)
for s in splits:
dt = s[0]
if start < dt <= end:
if field == 'volume':
ratio = 1.0 / s[1]
else:
ratio = s[1]
end_loc = dts.searchsorted(dt)
adj_loc = end_loc
mult = Float64Multiply(0,
end_loc - 1,
0,
0,
ratio)
try:
adjs[adj_loc].append(mult)
except KeyError:
adjs[adj_loc] = [mult]
return adjs
|
[
"def",
"_get_adjustments_in_range",
"(",
"self",
",",
"asset",
",",
"dts",
",",
"field",
")",
":",
"sid",
"=",
"int",
"(",
"asset",
")",
"start",
"=",
"normalize_date",
"(",
"dts",
"[",
"0",
"]",
")",
"end",
"=",
"normalize_date",
"(",
"dts",
"[",
"-",
"1",
"]",
")",
"adjs",
"=",
"{",
"}",
"if",
"field",
"!=",
"'volume'",
":",
"mergers",
"=",
"self",
".",
"_adjustments_reader",
".",
"get_adjustments_for_sid",
"(",
"'mergers'",
",",
"sid",
")",
"for",
"m",
"in",
"mergers",
":",
"dt",
"=",
"m",
"[",
"0",
"]",
"if",
"start",
"<",
"dt",
"<=",
"end",
":",
"end_loc",
"=",
"dts",
".",
"searchsorted",
"(",
"dt",
")",
"adj_loc",
"=",
"end_loc",
"mult",
"=",
"Float64Multiply",
"(",
"0",
",",
"end_loc",
"-",
"1",
",",
"0",
",",
"0",
",",
"m",
"[",
"1",
"]",
")",
"try",
":",
"adjs",
"[",
"adj_loc",
"]",
".",
"append",
"(",
"mult",
")",
"except",
"KeyError",
":",
"adjs",
"[",
"adj_loc",
"]",
"=",
"[",
"mult",
"]",
"divs",
"=",
"self",
".",
"_adjustments_reader",
".",
"get_adjustments_for_sid",
"(",
"'dividends'",
",",
"sid",
")",
"for",
"d",
"in",
"divs",
":",
"dt",
"=",
"d",
"[",
"0",
"]",
"if",
"start",
"<",
"dt",
"<=",
"end",
":",
"end_loc",
"=",
"dts",
".",
"searchsorted",
"(",
"dt",
")",
"adj_loc",
"=",
"end_loc",
"mult",
"=",
"Float64Multiply",
"(",
"0",
",",
"end_loc",
"-",
"1",
",",
"0",
",",
"0",
",",
"d",
"[",
"1",
"]",
")",
"try",
":",
"adjs",
"[",
"adj_loc",
"]",
".",
"append",
"(",
"mult",
")",
"except",
"KeyError",
":",
"adjs",
"[",
"adj_loc",
"]",
"=",
"[",
"mult",
"]",
"splits",
"=",
"self",
".",
"_adjustments_reader",
".",
"get_adjustments_for_sid",
"(",
"'splits'",
",",
"sid",
")",
"for",
"s",
"in",
"splits",
":",
"dt",
"=",
"s",
"[",
"0",
"]",
"if",
"start",
"<",
"dt",
"<=",
"end",
":",
"if",
"field",
"==",
"'volume'",
":",
"ratio",
"=",
"1.0",
"/",
"s",
"[",
"1",
"]",
"else",
":",
"ratio",
"=",
"s",
"[",
"1",
"]",
"end_loc",
"=",
"dts",
".",
"searchsorted",
"(",
"dt",
")",
"adj_loc",
"=",
"end_loc",
"mult",
"=",
"Float64Multiply",
"(",
"0",
",",
"end_loc",
"-",
"1",
",",
"0",
",",
"0",
",",
"ratio",
")",
"try",
":",
"adjs",
"[",
"adj_loc",
"]",
".",
"append",
"(",
"mult",
")",
"except",
"KeyError",
":",
"adjs",
"[",
"adj_loc",
"]",
"=",
"[",
"mult",
"]",
"return",
"adjs"
] |
Get the Float64Multiply objects to pass to an AdjustedArrayWindow.
For the use of AdjustedArrayWindow in the loader, which looks back
from current simulation time back to a window of data the dictionary is
structured with:
- the key into the dictionary for adjustments is the location of the
day from which the window is being viewed.
- the start of all multiply objects is always 0 (in each window all
adjustments are overlapping)
- the end of the multiply object is the location before the calendar
location of the adjustment action, making all days before the event
adjusted.
Parameters
----------
asset : Asset
The assets for which to get adjustments.
dts : iterable of datetime64-like
The dts for which adjustment data is needed.
field : str
OHLCV field for which to get the adjustments.
Returns
-------
out : dict[loc -> Float64Multiply]
The adjustments as a dict of loc -> Float64Multiply
|
[
"Get",
"the",
"Float64Multiply",
"objects",
"to",
"pass",
"to",
"an",
"AdjustedArrayWindow",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/history_loader.py#L65-L151
|
train
|
quantopian/zipline
|
zipline/data/history_loader.py
|
SlidingWindow.get
|
def get(self, end_ix):
"""
Returns
-------
out : A np.ndarray of the equity pricing up to end_ix after adjustments
and rounding have been applied.
"""
if self.most_recent_ix == end_ix:
return self.current
target = end_ix - self.cal_start - self.offset + 1
self.current = self.window.seek(target)
self.most_recent_ix = end_ix
return self.current
|
python
|
def get(self, end_ix):
"""
Returns
-------
out : A np.ndarray of the equity pricing up to end_ix after adjustments
and rounding have been applied.
"""
if self.most_recent_ix == end_ix:
return self.current
target = end_ix - self.cal_start - self.offset + 1
self.current = self.window.seek(target)
self.most_recent_ix = end_ix
return self.current
|
[
"def",
"get",
"(",
"self",
",",
"end_ix",
")",
":",
"if",
"self",
".",
"most_recent_ix",
"==",
"end_ix",
":",
"return",
"self",
".",
"current",
"target",
"=",
"end_ix",
"-",
"self",
".",
"cal_start",
"-",
"self",
".",
"offset",
"+",
"1",
"self",
".",
"current",
"=",
"self",
".",
"window",
".",
"seek",
"(",
"target",
")",
"self",
".",
"most_recent_ix",
"=",
"end_ix",
"return",
"self",
".",
"current"
] |
Returns
-------
out : A np.ndarray of the equity pricing up to end_ix after adjustments
and rounding have been applied.
|
[
"Returns",
"-------",
"out",
":",
"A",
"np",
".",
"ndarray",
"of",
"the",
"equity",
"pricing",
"up",
"to",
"end_ix",
"after",
"adjustments",
"and",
"rounding",
"have",
"been",
"applied",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/history_loader.py#L279-L293
|
train
|
quantopian/zipline
|
zipline/data/history_loader.py
|
HistoryLoader._ensure_sliding_windows
|
def _ensure_sliding_windows(self, assets, dts, field,
is_perspective_after):
"""
Ensure that there is a Float64Multiply window for each asset that can
provide data for the given parameters.
If the corresponding window for the (assets, len(dts), field) does not
exist, then create a new one.
If a corresponding window does exist for (assets, len(dts), field), but
can not provide data for the current dts range, then create a new
one and replace the expired window.
Parameters
----------
assets : iterable of Assets
The assets in the window
dts : iterable of datetime64-like
The datetimes for which to fetch data.
Makes an assumption that all dts are present and contiguous,
in the calendar.
field : str
The OHLCV field for which to retrieve data.
is_perspective_after : bool
see: `PricingHistoryLoader.history`
Returns
-------
out : list of Float64Window with sufficient data so that each asset's
window can provide `get` for the index corresponding with the last
value in `dts`
"""
end = dts[-1]
size = len(dts)
asset_windows = {}
needed_assets = []
cal = self._calendar
assets = self._asset_finder.retrieve_all(assets)
end_ix = find_in_sorted_index(cal, end)
for asset in assets:
try:
window = self._window_blocks[field].get(
(asset, size, is_perspective_after), end)
except KeyError:
needed_assets.append(asset)
else:
if end_ix < window.most_recent_ix:
# Window needs reset. Requested end index occurs before the
# end index from the previous history call for this window.
# Grab new window instead of rewinding adjustments.
needed_assets.append(asset)
else:
asset_windows[asset] = window
if needed_assets:
offset = 0
start_ix = find_in_sorted_index(cal, dts[0])
prefetch_end_ix = min(end_ix + self._prefetch_length, len(cal) - 1)
prefetch_end = cal[prefetch_end_ix]
prefetch_dts = cal[start_ix:prefetch_end_ix + 1]
if is_perspective_after:
adj_end_ix = min(prefetch_end_ix + 1, len(cal) - 1)
adj_dts = cal[start_ix:adj_end_ix + 1]
else:
adj_dts = prefetch_dts
prefetch_len = len(prefetch_dts)
array = self._array(prefetch_dts, needed_assets, field)
if field == 'sid':
window_type = Int64Window
else:
window_type = Float64Window
view_kwargs = {}
if field == 'volume':
array = array.astype(float64_dtype)
for i, asset in enumerate(needed_assets):
adj_reader = None
try:
adj_reader = self._adjustment_readers[type(asset)]
except KeyError:
adj_reader = None
if adj_reader is not None:
adjs = adj_reader.load_pricing_adjustments(
[field], adj_dts, [asset])[0]
else:
adjs = {}
window = window_type(
array[:, i].reshape(prefetch_len, 1),
view_kwargs,
adjs,
offset,
size,
int(is_perspective_after),
self._decimal_places_for_asset(asset, dts[-1]),
)
sliding_window = SlidingWindow(window, size, start_ix, offset)
asset_windows[asset] = sliding_window
self._window_blocks[field].set(
(asset, size, is_perspective_after),
sliding_window,
prefetch_end)
return [asset_windows[asset] for asset in assets]
|
python
|
def _ensure_sliding_windows(self, assets, dts, field,
is_perspective_after):
"""
Ensure that there is a Float64Multiply window for each asset that can
provide data for the given parameters.
If the corresponding window for the (assets, len(dts), field) does not
exist, then create a new one.
If a corresponding window does exist for (assets, len(dts), field), but
can not provide data for the current dts range, then create a new
one and replace the expired window.
Parameters
----------
assets : iterable of Assets
The assets in the window
dts : iterable of datetime64-like
The datetimes for which to fetch data.
Makes an assumption that all dts are present and contiguous,
in the calendar.
field : str
The OHLCV field for which to retrieve data.
is_perspective_after : bool
see: `PricingHistoryLoader.history`
Returns
-------
out : list of Float64Window with sufficient data so that each asset's
window can provide `get` for the index corresponding with the last
value in `dts`
"""
end = dts[-1]
size = len(dts)
asset_windows = {}
needed_assets = []
cal = self._calendar
assets = self._asset_finder.retrieve_all(assets)
end_ix = find_in_sorted_index(cal, end)
for asset in assets:
try:
window = self._window_blocks[field].get(
(asset, size, is_perspective_after), end)
except KeyError:
needed_assets.append(asset)
else:
if end_ix < window.most_recent_ix:
# Window needs reset. Requested end index occurs before the
# end index from the previous history call for this window.
# Grab new window instead of rewinding adjustments.
needed_assets.append(asset)
else:
asset_windows[asset] = window
if needed_assets:
offset = 0
start_ix = find_in_sorted_index(cal, dts[0])
prefetch_end_ix = min(end_ix + self._prefetch_length, len(cal) - 1)
prefetch_end = cal[prefetch_end_ix]
prefetch_dts = cal[start_ix:prefetch_end_ix + 1]
if is_perspective_after:
adj_end_ix = min(prefetch_end_ix + 1, len(cal) - 1)
adj_dts = cal[start_ix:adj_end_ix + 1]
else:
adj_dts = prefetch_dts
prefetch_len = len(prefetch_dts)
array = self._array(prefetch_dts, needed_assets, field)
if field == 'sid':
window_type = Int64Window
else:
window_type = Float64Window
view_kwargs = {}
if field == 'volume':
array = array.astype(float64_dtype)
for i, asset in enumerate(needed_assets):
adj_reader = None
try:
adj_reader = self._adjustment_readers[type(asset)]
except KeyError:
adj_reader = None
if adj_reader is not None:
adjs = adj_reader.load_pricing_adjustments(
[field], adj_dts, [asset])[0]
else:
adjs = {}
window = window_type(
array[:, i].reshape(prefetch_len, 1),
view_kwargs,
adjs,
offset,
size,
int(is_perspective_after),
self._decimal_places_for_asset(asset, dts[-1]),
)
sliding_window = SlidingWindow(window, size, start_ix, offset)
asset_windows[asset] = sliding_window
self._window_blocks[field].set(
(asset, size, is_perspective_after),
sliding_window,
prefetch_end)
return [asset_windows[asset] for asset in assets]
|
[
"def",
"_ensure_sliding_windows",
"(",
"self",
",",
"assets",
",",
"dts",
",",
"field",
",",
"is_perspective_after",
")",
":",
"end",
"=",
"dts",
"[",
"-",
"1",
"]",
"size",
"=",
"len",
"(",
"dts",
")",
"asset_windows",
"=",
"{",
"}",
"needed_assets",
"=",
"[",
"]",
"cal",
"=",
"self",
".",
"_calendar",
"assets",
"=",
"self",
".",
"_asset_finder",
".",
"retrieve_all",
"(",
"assets",
")",
"end_ix",
"=",
"find_in_sorted_index",
"(",
"cal",
",",
"end",
")",
"for",
"asset",
"in",
"assets",
":",
"try",
":",
"window",
"=",
"self",
".",
"_window_blocks",
"[",
"field",
"]",
".",
"get",
"(",
"(",
"asset",
",",
"size",
",",
"is_perspective_after",
")",
",",
"end",
")",
"except",
"KeyError",
":",
"needed_assets",
".",
"append",
"(",
"asset",
")",
"else",
":",
"if",
"end_ix",
"<",
"window",
".",
"most_recent_ix",
":",
"# Window needs reset. Requested end index occurs before the",
"# end index from the previous history call for this window.",
"# Grab new window instead of rewinding adjustments.",
"needed_assets",
".",
"append",
"(",
"asset",
")",
"else",
":",
"asset_windows",
"[",
"asset",
"]",
"=",
"window",
"if",
"needed_assets",
":",
"offset",
"=",
"0",
"start_ix",
"=",
"find_in_sorted_index",
"(",
"cal",
",",
"dts",
"[",
"0",
"]",
")",
"prefetch_end_ix",
"=",
"min",
"(",
"end_ix",
"+",
"self",
".",
"_prefetch_length",
",",
"len",
"(",
"cal",
")",
"-",
"1",
")",
"prefetch_end",
"=",
"cal",
"[",
"prefetch_end_ix",
"]",
"prefetch_dts",
"=",
"cal",
"[",
"start_ix",
":",
"prefetch_end_ix",
"+",
"1",
"]",
"if",
"is_perspective_after",
":",
"adj_end_ix",
"=",
"min",
"(",
"prefetch_end_ix",
"+",
"1",
",",
"len",
"(",
"cal",
")",
"-",
"1",
")",
"adj_dts",
"=",
"cal",
"[",
"start_ix",
":",
"adj_end_ix",
"+",
"1",
"]",
"else",
":",
"adj_dts",
"=",
"prefetch_dts",
"prefetch_len",
"=",
"len",
"(",
"prefetch_dts",
")",
"array",
"=",
"self",
".",
"_array",
"(",
"prefetch_dts",
",",
"needed_assets",
",",
"field",
")",
"if",
"field",
"==",
"'sid'",
":",
"window_type",
"=",
"Int64Window",
"else",
":",
"window_type",
"=",
"Float64Window",
"view_kwargs",
"=",
"{",
"}",
"if",
"field",
"==",
"'volume'",
":",
"array",
"=",
"array",
".",
"astype",
"(",
"float64_dtype",
")",
"for",
"i",
",",
"asset",
"in",
"enumerate",
"(",
"needed_assets",
")",
":",
"adj_reader",
"=",
"None",
"try",
":",
"adj_reader",
"=",
"self",
".",
"_adjustment_readers",
"[",
"type",
"(",
"asset",
")",
"]",
"except",
"KeyError",
":",
"adj_reader",
"=",
"None",
"if",
"adj_reader",
"is",
"not",
"None",
":",
"adjs",
"=",
"adj_reader",
".",
"load_pricing_adjustments",
"(",
"[",
"field",
"]",
",",
"adj_dts",
",",
"[",
"asset",
"]",
")",
"[",
"0",
"]",
"else",
":",
"adjs",
"=",
"{",
"}",
"window",
"=",
"window_type",
"(",
"array",
"[",
":",
",",
"i",
"]",
".",
"reshape",
"(",
"prefetch_len",
",",
"1",
")",
",",
"view_kwargs",
",",
"adjs",
",",
"offset",
",",
"size",
",",
"int",
"(",
"is_perspective_after",
")",
",",
"self",
".",
"_decimal_places_for_asset",
"(",
"asset",
",",
"dts",
"[",
"-",
"1",
"]",
")",
",",
")",
"sliding_window",
"=",
"SlidingWindow",
"(",
"window",
",",
"size",
",",
"start_ix",
",",
"offset",
")",
"asset_windows",
"[",
"asset",
"]",
"=",
"sliding_window",
"self",
".",
"_window_blocks",
"[",
"field",
"]",
".",
"set",
"(",
"(",
"asset",
",",
"size",
",",
"is_perspective_after",
")",
",",
"sliding_window",
",",
"prefetch_end",
")",
"return",
"[",
"asset_windows",
"[",
"asset",
"]",
"for",
"asset",
"in",
"assets",
"]"
] |
Ensure that there is a Float64Multiply window for each asset that can
provide data for the given parameters.
If the corresponding window for the (assets, len(dts), field) does not
exist, then create a new one.
If a corresponding window does exist for (assets, len(dts), field), but
can not provide data for the current dts range, then create a new
one and replace the expired window.
Parameters
----------
assets : iterable of Assets
The assets in the window
dts : iterable of datetime64-like
The datetimes for which to fetch data.
Makes an assumption that all dts are present and contiguous,
in the calendar.
field : str
The OHLCV field for which to retrieve data.
is_perspective_after : bool
see: `PricingHistoryLoader.history`
Returns
-------
out : list of Float64Window with sufficient data so that each asset's
window can provide `get` for the index corresponding with the last
value in `dts`
|
[
"Ensure",
"that",
"there",
"is",
"a",
"Float64Multiply",
"window",
"for",
"each",
"asset",
"that",
"can",
"provide",
"data",
"for",
"the",
"given",
"parameters",
".",
"If",
"the",
"corresponding",
"window",
"for",
"the",
"(",
"assets",
"len",
"(",
"dts",
")",
"field",
")",
"does",
"not",
"exist",
"then",
"create",
"a",
"new",
"one",
".",
"If",
"a",
"corresponding",
"window",
"does",
"exist",
"for",
"(",
"assets",
"len",
"(",
"dts",
")",
"field",
")",
"but",
"can",
"not",
"provide",
"data",
"for",
"the",
"current",
"dts",
"range",
"then",
"create",
"a",
"new",
"one",
"and",
"replace",
"the",
"expired",
"window",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/history_loader.py#L364-L469
|
train
|
quantopian/zipline
|
zipline/data/history_loader.py
|
HistoryLoader.history
|
def history(self, assets, dts, field, is_perspective_after):
"""
A window of pricing data with adjustments applied assuming that the
end of the window is the day before the current simulation time.
Parameters
----------
assets : iterable of Assets
The assets in the window.
dts : iterable of datetime64-like
The datetimes for which to fetch data.
Makes an assumption that all dts are present and contiguous,
in the calendar.
field : str
The OHLCV field for which to retrieve data.
is_perspective_after : bool
True, if the window is being viewed immediately after the last dt
in the sliding window.
False, if the window is viewed on the last dt.
This flag is used for handling the case where the last dt in the
requested window immediately precedes a corporate action, e.g.:
- is_perspective_after is True
When the viewpoint is after the last dt in the window, as when a
daily history window is accessed from a simulation that uses a
minute data frequency, the history call to this loader will not
include the current simulation dt. At that point in time, the raw
data for the last day in the window will require adjustment, so the
most recent adjustment with respect to the simulation time is
applied to the last dt in the requested window.
An example equity which has a 0.5 split ratio dated for 05-27,
with the dts for a history call of 5 bars with a '1d' frequency at
05-27 9:31. Simulation frequency is 'minute'.
(In this case this function is called with 4 daily dts, and the
calling function is responsible for stitching back on the
'current' dt)
| | | | | last dt | <-- viewer is here |
| | 05-23 | 05-24 | 05-25 | 05-26 | 05-27 9:31 |
| raw | 10.10 | 10.20 | 10.30 | 10.40 | |
| adj | 5.05 | 5.10 | 5.15 | 5.25 | |
The adjustment is applied to the last dt, 05-26, and all previous
dts.
- is_perspective_after is False, daily
When the viewpoint is the same point in time as the last dt in the
window, as when a daily history window is accessed from a
simulation that uses a daily data frequency, the history call will
include the current dt. At that point in time, the raw data for the
last day in the window will be post-adjustment, so no adjustment
is applied to the last dt.
An example equity which has a 0.5 split ratio dated for 05-27,
with the dts for a history call of 5 bars with a '1d' frequency at
05-27 0:00. Simulation frequency is 'daily'.
| | | | | | <-- viewer is here |
| | | | | | last dt |
| | 05-23 | 05-24 | 05-25 | 05-26 | 05-27 |
| raw | 10.10 | 10.20 | 10.30 | 10.40 | 5.25 |
| adj | 5.05 | 5.10 | 5.15 | 5.20 | 5.25 |
Adjustments are applied 05-23 through 05-26 but not to the last dt,
05-27
Returns
-------
out : np.ndarray with shape(len(days between start, end), len(assets))
"""
block = self._ensure_sliding_windows(assets,
dts,
field,
is_perspective_after)
end_ix = self._calendar.searchsorted(dts[-1])
return concatenate(
[window.get(end_ix) for window in block],
axis=1,
)
|
python
|
def history(self, assets, dts, field, is_perspective_after):
"""
A window of pricing data with adjustments applied assuming that the
end of the window is the day before the current simulation time.
Parameters
----------
assets : iterable of Assets
The assets in the window.
dts : iterable of datetime64-like
The datetimes for which to fetch data.
Makes an assumption that all dts are present and contiguous,
in the calendar.
field : str
The OHLCV field for which to retrieve data.
is_perspective_after : bool
True, if the window is being viewed immediately after the last dt
in the sliding window.
False, if the window is viewed on the last dt.
This flag is used for handling the case where the last dt in the
requested window immediately precedes a corporate action, e.g.:
- is_perspective_after is True
When the viewpoint is after the last dt in the window, as when a
daily history window is accessed from a simulation that uses a
minute data frequency, the history call to this loader will not
include the current simulation dt. At that point in time, the raw
data for the last day in the window will require adjustment, so the
most recent adjustment with respect to the simulation time is
applied to the last dt in the requested window.
An example equity which has a 0.5 split ratio dated for 05-27,
with the dts for a history call of 5 bars with a '1d' frequency at
05-27 9:31. Simulation frequency is 'minute'.
(In this case this function is called with 4 daily dts, and the
calling function is responsible for stitching back on the
'current' dt)
| | | | | last dt | <-- viewer is here |
| | 05-23 | 05-24 | 05-25 | 05-26 | 05-27 9:31 |
| raw | 10.10 | 10.20 | 10.30 | 10.40 | |
| adj | 5.05 | 5.10 | 5.15 | 5.25 | |
The adjustment is applied to the last dt, 05-26, and all previous
dts.
- is_perspective_after is False, daily
When the viewpoint is the same point in time as the last dt in the
window, as when a daily history window is accessed from a
simulation that uses a daily data frequency, the history call will
include the current dt. At that point in time, the raw data for the
last day in the window will be post-adjustment, so no adjustment
is applied to the last dt.
An example equity which has a 0.5 split ratio dated for 05-27,
with the dts for a history call of 5 bars with a '1d' frequency at
05-27 0:00. Simulation frequency is 'daily'.
| | | | | | <-- viewer is here |
| | | | | | last dt |
| | 05-23 | 05-24 | 05-25 | 05-26 | 05-27 |
| raw | 10.10 | 10.20 | 10.30 | 10.40 | 5.25 |
| adj | 5.05 | 5.10 | 5.15 | 5.20 | 5.25 |
Adjustments are applied 05-23 through 05-26 but not to the last dt,
05-27
Returns
-------
out : np.ndarray with shape(len(days between start, end), len(assets))
"""
block = self._ensure_sliding_windows(assets,
dts,
field,
is_perspective_after)
end_ix = self._calendar.searchsorted(dts[-1])
return concatenate(
[window.get(end_ix) for window in block],
axis=1,
)
|
[
"def",
"history",
"(",
"self",
",",
"assets",
",",
"dts",
",",
"field",
",",
"is_perspective_after",
")",
":",
"block",
"=",
"self",
".",
"_ensure_sliding_windows",
"(",
"assets",
",",
"dts",
",",
"field",
",",
"is_perspective_after",
")",
"end_ix",
"=",
"self",
".",
"_calendar",
".",
"searchsorted",
"(",
"dts",
"[",
"-",
"1",
"]",
")",
"return",
"concatenate",
"(",
"[",
"window",
".",
"get",
"(",
"end_ix",
")",
"for",
"window",
"in",
"block",
"]",
",",
"axis",
"=",
"1",
",",
")"
] |
A window of pricing data with adjustments applied assuming that the
end of the window is the day before the current simulation time.
Parameters
----------
assets : iterable of Assets
The assets in the window.
dts : iterable of datetime64-like
The datetimes for which to fetch data.
Makes an assumption that all dts are present and contiguous,
in the calendar.
field : str
The OHLCV field for which to retrieve data.
is_perspective_after : bool
True, if the window is being viewed immediately after the last dt
in the sliding window.
False, if the window is viewed on the last dt.
This flag is used for handling the case where the last dt in the
requested window immediately precedes a corporate action, e.g.:
- is_perspective_after is True
When the viewpoint is after the last dt in the window, as when a
daily history window is accessed from a simulation that uses a
minute data frequency, the history call to this loader will not
include the current simulation dt. At that point in time, the raw
data for the last day in the window will require adjustment, so the
most recent adjustment with respect to the simulation time is
applied to the last dt in the requested window.
An example equity which has a 0.5 split ratio dated for 05-27,
with the dts for a history call of 5 bars with a '1d' frequency at
05-27 9:31. Simulation frequency is 'minute'.
(In this case this function is called with 4 daily dts, and the
calling function is responsible for stitching back on the
'current' dt)
| | | | | last dt | <-- viewer is here |
| | 05-23 | 05-24 | 05-25 | 05-26 | 05-27 9:31 |
| raw | 10.10 | 10.20 | 10.30 | 10.40 | |
| adj | 5.05 | 5.10 | 5.15 | 5.25 | |
The adjustment is applied to the last dt, 05-26, and all previous
dts.
- is_perspective_after is False, daily
When the viewpoint is the same point in time as the last dt in the
window, as when a daily history window is accessed from a
simulation that uses a daily data frequency, the history call will
include the current dt. At that point in time, the raw data for the
last day in the window will be post-adjustment, so no adjustment
is applied to the last dt.
An example equity which has a 0.5 split ratio dated for 05-27,
with the dts for a history call of 5 bars with a '1d' frequency at
05-27 0:00. Simulation frequency is 'daily'.
| | | | | | <-- viewer is here |
| | | | | | last dt |
| | 05-23 | 05-24 | 05-25 | 05-26 | 05-27 |
| raw | 10.10 | 10.20 | 10.30 | 10.40 | 5.25 |
| adj | 5.05 | 5.10 | 5.15 | 5.20 | 5.25 |
Adjustments are applied 05-23 through 05-26 but not to the last dt,
05-27
Returns
-------
out : np.ndarray with shape(len(days between start, end), len(assets))
|
[
"A",
"window",
"of",
"pricing",
"data",
"with",
"adjustments",
"applied",
"assuming",
"that",
"the",
"end",
"of",
"the",
"window",
"is",
"the",
"day",
"before",
"the",
"current",
"simulation",
"time",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/history_loader.py#L471-L555
|
train
|
quantopian/zipline
|
zipline/sources/requests_csv.py
|
PandasCSV.parse_date_str_series
|
def parse_date_str_series(format_str, tz, date_str_series, data_frequency,
trading_day):
"""
Efficient parsing for a 1d Pandas/numpy object containing string
representations of dates.
Note: pd.to_datetime is significantly faster when no format string is
passed, and in pandas 0.12.0 the %p strptime directive is not correctly
handled if a format string is explicitly passed, but AM/PM is handled
properly if format=None.
Moreover, we were previously ignoring this parameter unintentionally
because we were incorrectly passing it as a positional. For all these
reasons, we ignore the format_str parameter when parsing datetimes.
"""
# Explicitly ignoring this parameter. See note above.
if format_str is not None:
logger.warn(
"The 'format_str' parameter to fetch_csv is deprecated. "
"Ignoring and defaulting to pandas default date parsing."
)
format_str = None
tz_str = str(tz)
if tz_str == pytz.utc.zone:
parsed = pd.to_datetime(
date_str_series.values,
format=format_str,
utc=True,
errors='coerce',
)
else:
parsed = pd.to_datetime(
date_str_series.values,
format=format_str,
errors='coerce',
).tz_localize(tz_str).tz_convert('UTC')
if data_frequency == 'daily':
parsed = roll_dts_to_midnight(parsed, trading_day)
return parsed
|
python
|
def parse_date_str_series(format_str, tz, date_str_series, data_frequency,
trading_day):
"""
Efficient parsing for a 1d Pandas/numpy object containing string
representations of dates.
Note: pd.to_datetime is significantly faster when no format string is
passed, and in pandas 0.12.0 the %p strptime directive is not correctly
handled if a format string is explicitly passed, but AM/PM is handled
properly if format=None.
Moreover, we were previously ignoring this parameter unintentionally
because we were incorrectly passing it as a positional. For all these
reasons, we ignore the format_str parameter when parsing datetimes.
"""
# Explicitly ignoring this parameter. See note above.
if format_str is not None:
logger.warn(
"The 'format_str' parameter to fetch_csv is deprecated. "
"Ignoring and defaulting to pandas default date parsing."
)
format_str = None
tz_str = str(tz)
if tz_str == pytz.utc.zone:
parsed = pd.to_datetime(
date_str_series.values,
format=format_str,
utc=True,
errors='coerce',
)
else:
parsed = pd.to_datetime(
date_str_series.values,
format=format_str,
errors='coerce',
).tz_localize(tz_str).tz_convert('UTC')
if data_frequency == 'daily':
parsed = roll_dts_to_midnight(parsed, trading_day)
return parsed
|
[
"def",
"parse_date_str_series",
"(",
"format_str",
",",
"tz",
",",
"date_str_series",
",",
"data_frequency",
",",
"trading_day",
")",
":",
"# Explicitly ignoring this parameter. See note above.",
"if",
"format_str",
"is",
"not",
"None",
":",
"logger",
".",
"warn",
"(",
"\"The 'format_str' parameter to fetch_csv is deprecated. \"",
"\"Ignoring and defaulting to pandas default date parsing.\"",
")",
"format_str",
"=",
"None",
"tz_str",
"=",
"str",
"(",
"tz",
")",
"if",
"tz_str",
"==",
"pytz",
".",
"utc",
".",
"zone",
":",
"parsed",
"=",
"pd",
".",
"to_datetime",
"(",
"date_str_series",
".",
"values",
",",
"format",
"=",
"format_str",
",",
"utc",
"=",
"True",
",",
"errors",
"=",
"'coerce'",
",",
")",
"else",
":",
"parsed",
"=",
"pd",
".",
"to_datetime",
"(",
"date_str_series",
".",
"values",
",",
"format",
"=",
"format_str",
",",
"errors",
"=",
"'coerce'",
",",
")",
".",
"tz_localize",
"(",
"tz_str",
")",
".",
"tz_convert",
"(",
"'UTC'",
")",
"if",
"data_frequency",
"==",
"'daily'",
":",
"parsed",
"=",
"roll_dts_to_midnight",
"(",
"parsed",
",",
"trading_day",
")",
"return",
"parsed"
] |
Efficient parsing for a 1d Pandas/numpy object containing string
representations of dates.
Note: pd.to_datetime is significantly faster when no format string is
passed, and in pandas 0.12.0 the %p strptime directive is not correctly
handled if a format string is explicitly passed, but AM/PM is handled
properly if format=None.
Moreover, we were previously ignoring this parameter unintentionally
because we were incorrectly passing it as a positional. For all these
reasons, we ignore the format_str parameter when parsing datetimes.
|
[
"Efficient",
"parsing",
"for",
"a",
"1d",
"Pandas",
"/",
"numpy",
"object",
"containing",
"string",
"representations",
"of",
"dates",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/sources/requests_csv.py#L201-L242
|
train
|
quantopian/zipline
|
zipline/sources/requests_csv.py
|
PandasCSV._lookup_unconflicted_symbol
|
def _lookup_unconflicted_symbol(self, symbol):
"""
Attempt to find a unique asset whose symbol is the given string.
If multiple assets have held the given symbol, return a 0.
If no asset has held the given symbol, return a NaN.
"""
try:
uppered = symbol.upper()
except AttributeError:
# The mapping fails because symbol was a non-string
return numpy.nan
try:
return self.finder.lookup_symbol(
uppered,
as_of_date=None,
country_code=self.country_code,
)
except MultipleSymbolsFound:
# Fill conflicted entries with zeros to mark that they need to be
# resolved by date.
return 0
except SymbolNotFound:
# Fill not found entries with nans.
return numpy.nan
|
python
|
def _lookup_unconflicted_symbol(self, symbol):
"""
Attempt to find a unique asset whose symbol is the given string.
If multiple assets have held the given symbol, return a 0.
If no asset has held the given symbol, return a NaN.
"""
try:
uppered = symbol.upper()
except AttributeError:
# The mapping fails because symbol was a non-string
return numpy.nan
try:
return self.finder.lookup_symbol(
uppered,
as_of_date=None,
country_code=self.country_code,
)
except MultipleSymbolsFound:
# Fill conflicted entries with zeros to mark that they need to be
# resolved by date.
return 0
except SymbolNotFound:
# Fill not found entries with nans.
return numpy.nan
|
[
"def",
"_lookup_unconflicted_symbol",
"(",
"self",
",",
"symbol",
")",
":",
"try",
":",
"uppered",
"=",
"symbol",
".",
"upper",
"(",
")",
"except",
"AttributeError",
":",
"# The mapping fails because symbol was a non-string",
"return",
"numpy",
".",
"nan",
"try",
":",
"return",
"self",
".",
"finder",
".",
"lookup_symbol",
"(",
"uppered",
",",
"as_of_date",
"=",
"None",
",",
"country_code",
"=",
"self",
".",
"country_code",
",",
")",
"except",
"MultipleSymbolsFound",
":",
"# Fill conflicted entries with zeros to mark that they need to be",
"# resolved by date.",
"return",
"0",
"except",
"SymbolNotFound",
":",
"# Fill not found entries with nans.",
"return",
"numpy",
".",
"nan"
] |
Attempt to find a unique asset whose symbol is the given string.
If multiple assets have held the given symbol, return a 0.
If no asset has held the given symbol, return a NaN.
|
[
"Attempt",
"to",
"find",
"a",
"unique",
"asset",
"whose",
"symbol",
"is",
"the",
"given",
"string",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/sources/requests_csv.py#L262-L288
|
train
|
quantopian/zipline
|
zipline/gens/tradesimulation.py
|
AlgorithmSimulator.transform
|
def transform(self):
"""
Main generator work loop.
"""
algo = self.algo
metrics_tracker = algo.metrics_tracker
emission_rate = metrics_tracker.emission_rate
def every_bar(dt_to_use, current_data=self.current_data,
handle_data=algo.event_manager.handle_data):
for capital_change in calculate_minute_capital_changes(dt_to_use):
yield capital_change
self.simulation_dt = dt_to_use
# called every tick (minute or day).
algo.on_dt_changed(dt_to_use)
blotter = algo.blotter
# handle any transactions and commissions coming out new orders
# placed in the last bar
new_transactions, new_commissions, closed_orders = \
blotter.get_transactions(current_data)
blotter.prune_orders(closed_orders)
for transaction in new_transactions:
metrics_tracker.process_transaction(transaction)
# since this order was modified, record it
order = blotter.orders[transaction.order_id]
metrics_tracker.process_order(order)
for commission in new_commissions:
metrics_tracker.process_commission(commission)
handle_data(algo, current_data, dt_to_use)
# grab any new orders from the blotter, then clear the list.
# this includes cancelled orders.
new_orders = blotter.new_orders
blotter.new_orders = []
# if we have any new orders, record them so that we know
# in what perf period they were placed.
for new_order in new_orders:
metrics_tracker.process_order(new_order)
def once_a_day(midnight_dt, current_data=self.current_data,
data_portal=self.data_portal):
# process any capital changes that came overnight
for capital_change in algo.calculate_capital_changes(
midnight_dt, emission_rate=emission_rate,
is_interday=True):
yield capital_change
# set all the timestamps
self.simulation_dt = midnight_dt
algo.on_dt_changed(midnight_dt)
metrics_tracker.handle_market_open(
midnight_dt,
algo.data_portal,
)
# handle any splits that impact any positions or any open orders.
assets_we_care_about = (
viewkeys(metrics_tracker.positions) |
viewkeys(algo.blotter.open_orders)
)
if assets_we_care_about:
splits = data_portal.get_splits(assets_we_care_about,
midnight_dt)
if splits:
algo.blotter.process_splits(splits)
metrics_tracker.handle_splits(splits)
def on_exit():
# Remove references to algo, data portal, et al to break cycles
# and ensure deterministic cleanup of these objects when the
# simulation finishes.
self.algo = None
self.benchmark_source = self.current_data = self.data_portal = None
with ExitStack() as stack:
stack.callback(on_exit)
stack.enter_context(self.processor)
stack.enter_context(ZiplineAPI(self.algo))
if algo.data_frequency == 'minute':
def execute_order_cancellation_policy():
algo.blotter.execute_cancel_policy(SESSION_END)
def calculate_minute_capital_changes(dt):
# process any capital changes that came between the last
# and current minutes
return algo.calculate_capital_changes(
dt, emission_rate=emission_rate, is_interday=False)
else:
def execute_order_cancellation_policy():
pass
def calculate_minute_capital_changes(dt):
return []
for dt, action in self.clock:
if action == BAR:
for capital_change_packet in every_bar(dt):
yield capital_change_packet
elif action == SESSION_START:
for capital_change_packet in once_a_day(dt):
yield capital_change_packet
elif action == SESSION_END:
# End of the session.
positions = metrics_tracker.positions
position_assets = algo.asset_finder.retrieve_all(positions)
self._cleanup_expired_assets(dt, position_assets)
execute_order_cancellation_policy()
algo.validate_account_controls()
yield self._get_daily_message(dt, algo, metrics_tracker)
elif action == BEFORE_TRADING_START_BAR:
self.simulation_dt = dt
algo.on_dt_changed(dt)
algo.before_trading_start(self.current_data)
elif action == MINUTE_END:
minute_msg = self._get_minute_message(
dt,
algo,
metrics_tracker,
)
yield minute_msg
risk_message = metrics_tracker.handle_simulation_end(
self.data_portal,
)
yield risk_message
|
python
|
def transform(self):
"""
Main generator work loop.
"""
algo = self.algo
metrics_tracker = algo.metrics_tracker
emission_rate = metrics_tracker.emission_rate
def every_bar(dt_to_use, current_data=self.current_data,
handle_data=algo.event_manager.handle_data):
for capital_change in calculate_minute_capital_changes(dt_to_use):
yield capital_change
self.simulation_dt = dt_to_use
# called every tick (minute or day).
algo.on_dt_changed(dt_to_use)
blotter = algo.blotter
# handle any transactions and commissions coming out new orders
# placed in the last bar
new_transactions, new_commissions, closed_orders = \
blotter.get_transactions(current_data)
blotter.prune_orders(closed_orders)
for transaction in new_transactions:
metrics_tracker.process_transaction(transaction)
# since this order was modified, record it
order = blotter.orders[transaction.order_id]
metrics_tracker.process_order(order)
for commission in new_commissions:
metrics_tracker.process_commission(commission)
handle_data(algo, current_data, dt_to_use)
# grab any new orders from the blotter, then clear the list.
# this includes cancelled orders.
new_orders = blotter.new_orders
blotter.new_orders = []
# if we have any new orders, record them so that we know
# in what perf period they were placed.
for new_order in new_orders:
metrics_tracker.process_order(new_order)
def once_a_day(midnight_dt, current_data=self.current_data,
data_portal=self.data_portal):
# process any capital changes that came overnight
for capital_change in algo.calculate_capital_changes(
midnight_dt, emission_rate=emission_rate,
is_interday=True):
yield capital_change
# set all the timestamps
self.simulation_dt = midnight_dt
algo.on_dt_changed(midnight_dt)
metrics_tracker.handle_market_open(
midnight_dt,
algo.data_portal,
)
# handle any splits that impact any positions or any open orders.
assets_we_care_about = (
viewkeys(metrics_tracker.positions) |
viewkeys(algo.blotter.open_orders)
)
if assets_we_care_about:
splits = data_portal.get_splits(assets_we_care_about,
midnight_dt)
if splits:
algo.blotter.process_splits(splits)
metrics_tracker.handle_splits(splits)
def on_exit():
# Remove references to algo, data portal, et al to break cycles
# and ensure deterministic cleanup of these objects when the
# simulation finishes.
self.algo = None
self.benchmark_source = self.current_data = self.data_portal = None
with ExitStack() as stack:
stack.callback(on_exit)
stack.enter_context(self.processor)
stack.enter_context(ZiplineAPI(self.algo))
if algo.data_frequency == 'minute':
def execute_order_cancellation_policy():
algo.blotter.execute_cancel_policy(SESSION_END)
def calculate_minute_capital_changes(dt):
# process any capital changes that came between the last
# and current minutes
return algo.calculate_capital_changes(
dt, emission_rate=emission_rate, is_interday=False)
else:
def execute_order_cancellation_policy():
pass
def calculate_minute_capital_changes(dt):
return []
for dt, action in self.clock:
if action == BAR:
for capital_change_packet in every_bar(dt):
yield capital_change_packet
elif action == SESSION_START:
for capital_change_packet in once_a_day(dt):
yield capital_change_packet
elif action == SESSION_END:
# End of the session.
positions = metrics_tracker.positions
position_assets = algo.asset_finder.retrieve_all(positions)
self._cleanup_expired_assets(dt, position_assets)
execute_order_cancellation_policy()
algo.validate_account_controls()
yield self._get_daily_message(dt, algo, metrics_tracker)
elif action == BEFORE_TRADING_START_BAR:
self.simulation_dt = dt
algo.on_dt_changed(dt)
algo.before_trading_start(self.current_data)
elif action == MINUTE_END:
minute_msg = self._get_minute_message(
dt,
algo,
metrics_tracker,
)
yield minute_msg
risk_message = metrics_tracker.handle_simulation_end(
self.data_portal,
)
yield risk_message
|
[
"def",
"transform",
"(",
"self",
")",
":",
"algo",
"=",
"self",
".",
"algo",
"metrics_tracker",
"=",
"algo",
".",
"metrics_tracker",
"emission_rate",
"=",
"metrics_tracker",
".",
"emission_rate",
"def",
"every_bar",
"(",
"dt_to_use",
",",
"current_data",
"=",
"self",
".",
"current_data",
",",
"handle_data",
"=",
"algo",
".",
"event_manager",
".",
"handle_data",
")",
":",
"for",
"capital_change",
"in",
"calculate_minute_capital_changes",
"(",
"dt_to_use",
")",
":",
"yield",
"capital_change",
"self",
".",
"simulation_dt",
"=",
"dt_to_use",
"# called every tick (minute or day).",
"algo",
".",
"on_dt_changed",
"(",
"dt_to_use",
")",
"blotter",
"=",
"algo",
".",
"blotter",
"# handle any transactions and commissions coming out new orders",
"# placed in the last bar",
"new_transactions",
",",
"new_commissions",
",",
"closed_orders",
"=",
"blotter",
".",
"get_transactions",
"(",
"current_data",
")",
"blotter",
".",
"prune_orders",
"(",
"closed_orders",
")",
"for",
"transaction",
"in",
"new_transactions",
":",
"metrics_tracker",
".",
"process_transaction",
"(",
"transaction",
")",
"# since this order was modified, record it",
"order",
"=",
"blotter",
".",
"orders",
"[",
"transaction",
".",
"order_id",
"]",
"metrics_tracker",
".",
"process_order",
"(",
"order",
")",
"for",
"commission",
"in",
"new_commissions",
":",
"metrics_tracker",
".",
"process_commission",
"(",
"commission",
")",
"handle_data",
"(",
"algo",
",",
"current_data",
",",
"dt_to_use",
")",
"# grab any new orders from the blotter, then clear the list.",
"# this includes cancelled orders.",
"new_orders",
"=",
"blotter",
".",
"new_orders",
"blotter",
".",
"new_orders",
"=",
"[",
"]",
"# if we have any new orders, record them so that we know",
"# in what perf period they were placed.",
"for",
"new_order",
"in",
"new_orders",
":",
"metrics_tracker",
".",
"process_order",
"(",
"new_order",
")",
"def",
"once_a_day",
"(",
"midnight_dt",
",",
"current_data",
"=",
"self",
".",
"current_data",
",",
"data_portal",
"=",
"self",
".",
"data_portal",
")",
":",
"# process any capital changes that came overnight",
"for",
"capital_change",
"in",
"algo",
".",
"calculate_capital_changes",
"(",
"midnight_dt",
",",
"emission_rate",
"=",
"emission_rate",
",",
"is_interday",
"=",
"True",
")",
":",
"yield",
"capital_change",
"# set all the timestamps",
"self",
".",
"simulation_dt",
"=",
"midnight_dt",
"algo",
".",
"on_dt_changed",
"(",
"midnight_dt",
")",
"metrics_tracker",
".",
"handle_market_open",
"(",
"midnight_dt",
",",
"algo",
".",
"data_portal",
",",
")",
"# handle any splits that impact any positions or any open orders.",
"assets_we_care_about",
"=",
"(",
"viewkeys",
"(",
"metrics_tracker",
".",
"positions",
")",
"|",
"viewkeys",
"(",
"algo",
".",
"blotter",
".",
"open_orders",
")",
")",
"if",
"assets_we_care_about",
":",
"splits",
"=",
"data_portal",
".",
"get_splits",
"(",
"assets_we_care_about",
",",
"midnight_dt",
")",
"if",
"splits",
":",
"algo",
".",
"blotter",
".",
"process_splits",
"(",
"splits",
")",
"metrics_tracker",
".",
"handle_splits",
"(",
"splits",
")",
"def",
"on_exit",
"(",
")",
":",
"# Remove references to algo, data portal, et al to break cycles",
"# and ensure deterministic cleanup of these objects when the",
"# simulation finishes.",
"self",
".",
"algo",
"=",
"None",
"self",
".",
"benchmark_source",
"=",
"self",
".",
"current_data",
"=",
"self",
".",
"data_portal",
"=",
"None",
"with",
"ExitStack",
"(",
")",
"as",
"stack",
":",
"stack",
".",
"callback",
"(",
"on_exit",
")",
"stack",
".",
"enter_context",
"(",
"self",
".",
"processor",
")",
"stack",
".",
"enter_context",
"(",
"ZiplineAPI",
"(",
"self",
".",
"algo",
")",
")",
"if",
"algo",
".",
"data_frequency",
"==",
"'minute'",
":",
"def",
"execute_order_cancellation_policy",
"(",
")",
":",
"algo",
".",
"blotter",
".",
"execute_cancel_policy",
"(",
"SESSION_END",
")",
"def",
"calculate_minute_capital_changes",
"(",
"dt",
")",
":",
"# process any capital changes that came between the last",
"# and current minutes",
"return",
"algo",
".",
"calculate_capital_changes",
"(",
"dt",
",",
"emission_rate",
"=",
"emission_rate",
",",
"is_interday",
"=",
"False",
")",
"else",
":",
"def",
"execute_order_cancellation_policy",
"(",
")",
":",
"pass",
"def",
"calculate_minute_capital_changes",
"(",
"dt",
")",
":",
"return",
"[",
"]",
"for",
"dt",
",",
"action",
"in",
"self",
".",
"clock",
":",
"if",
"action",
"==",
"BAR",
":",
"for",
"capital_change_packet",
"in",
"every_bar",
"(",
"dt",
")",
":",
"yield",
"capital_change_packet",
"elif",
"action",
"==",
"SESSION_START",
":",
"for",
"capital_change_packet",
"in",
"once_a_day",
"(",
"dt",
")",
":",
"yield",
"capital_change_packet",
"elif",
"action",
"==",
"SESSION_END",
":",
"# End of the session.",
"positions",
"=",
"metrics_tracker",
".",
"positions",
"position_assets",
"=",
"algo",
".",
"asset_finder",
".",
"retrieve_all",
"(",
"positions",
")",
"self",
".",
"_cleanup_expired_assets",
"(",
"dt",
",",
"position_assets",
")",
"execute_order_cancellation_policy",
"(",
")",
"algo",
".",
"validate_account_controls",
"(",
")",
"yield",
"self",
".",
"_get_daily_message",
"(",
"dt",
",",
"algo",
",",
"metrics_tracker",
")",
"elif",
"action",
"==",
"BEFORE_TRADING_START_BAR",
":",
"self",
".",
"simulation_dt",
"=",
"dt",
"algo",
".",
"on_dt_changed",
"(",
"dt",
")",
"algo",
".",
"before_trading_start",
"(",
"self",
".",
"current_data",
")",
"elif",
"action",
"==",
"MINUTE_END",
":",
"minute_msg",
"=",
"self",
".",
"_get_minute_message",
"(",
"dt",
",",
"algo",
",",
"metrics_tracker",
",",
")",
"yield",
"minute_msg",
"risk_message",
"=",
"metrics_tracker",
".",
"handle_simulation_end",
"(",
"self",
".",
"data_portal",
",",
")",
"yield",
"risk_message"
] |
Main generator work loop.
|
[
"Main",
"generator",
"work",
"loop",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/gens/tradesimulation.py#L97-L236
|
train
|
quantopian/zipline
|
zipline/gens/tradesimulation.py
|
AlgorithmSimulator._cleanup_expired_assets
|
def _cleanup_expired_assets(self, dt, position_assets):
"""
Clear out any assets that have expired before starting a new sim day.
Performs two functions:
1. Finds all assets for which we have open orders and clears any
orders whose assets are on or after their auto_close_date.
2. Finds all assets for which we have positions and generates
close_position events for any assets that have reached their
auto_close_date.
"""
algo = self.algo
def past_auto_close_date(asset):
acd = asset.auto_close_date
return acd is not None and acd <= dt
# Remove positions in any sids that have reached their auto_close date.
assets_to_clear = \
[asset for asset in position_assets if past_auto_close_date(asset)]
metrics_tracker = algo.metrics_tracker
data_portal = self.data_portal
for asset in assets_to_clear:
metrics_tracker.process_close_position(asset, dt, data_portal)
# Remove open orders for any sids that have reached their auto close
# date. These orders get processed immediately because otherwise they
# would not be processed until the first bar of the next day.
blotter = algo.blotter
assets_to_cancel = [
asset for asset in blotter.open_orders
if past_auto_close_date(asset)
]
for asset in assets_to_cancel:
blotter.cancel_all_orders_for_asset(asset)
# Make a copy here so that we are not modifying the list that is being
# iterated over.
for order in copy(blotter.new_orders):
if order.status == ORDER_STATUS.CANCELLED:
metrics_tracker.process_order(order)
blotter.new_orders.remove(order)
|
python
|
def _cleanup_expired_assets(self, dt, position_assets):
"""
Clear out any assets that have expired before starting a new sim day.
Performs two functions:
1. Finds all assets for which we have open orders and clears any
orders whose assets are on or after their auto_close_date.
2. Finds all assets for which we have positions and generates
close_position events for any assets that have reached their
auto_close_date.
"""
algo = self.algo
def past_auto_close_date(asset):
acd = asset.auto_close_date
return acd is not None and acd <= dt
# Remove positions in any sids that have reached their auto_close date.
assets_to_clear = \
[asset for asset in position_assets if past_auto_close_date(asset)]
metrics_tracker = algo.metrics_tracker
data_portal = self.data_portal
for asset in assets_to_clear:
metrics_tracker.process_close_position(asset, dt, data_portal)
# Remove open orders for any sids that have reached their auto close
# date. These orders get processed immediately because otherwise they
# would not be processed until the first bar of the next day.
blotter = algo.blotter
assets_to_cancel = [
asset for asset in blotter.open_orders
if past_auto_close_date(asset)
]
for asset in assets_to_cancel:
blotter.cancel_all_orders_for_asset(asset)
# Make a copy here so that we are not modifying the list that is being
# iterated over.
for order in copy(blotter.new_orders):
if order.status == ORDER_STATUS.CANCELLED:
metrics_tracker.process_order(order)
blotter.new_orders.remove(order)
|
[
"def",
"_cleanup_expired_assets",
"(",
"self",
",",
"dt",
",",
"position_assets",
")",
":",
"algo",
"=",
"self",
".",
"algo",
"def",
"past_auto_close_date",
"(",
"asset",
")",
":",
"acd",
"=",
"asset",
".",
"auto_close_date",
"return",
"acd",
"is",
"not",
"None",
"and",
"acd",
"<=",
"dt",
"# Remove positions in any sids that have reached their auto_close date.",
"assets_to_clear",
"=",
"[",
"asset",
"for",
"asset",
"in",
"position_assets",
"if",
"past_auto_close_date",
"(",
"asset",
")",
"]",
"metrics_tracker",
"=",
"algo",
".",
"metrics_tracker",
"data_portal",
"=",
"self",
".",
"data_portal",
"for",
"asset",
"in",
"assets_to_clear",
":",
"metrics_tracker",
".",
"process_close_position",
"(",
"asset",
",",
"dt",
",",
"data_portal",
")",
"# Remove open orders for any sids that have reached their auto close",
"# date. These orders get processed immediately because otherwise they",
"# would not be processed until the first bar of the next day.",
"blotter",
"=",
"algo",
".",
"blotter",
"assets_to_cancel",
"=",
"[",
"asset",
"for",
"asset",
"in",
"blotter",
".",
"open_orders",
"if",
"past_auto_close_date",
"(",
"asset",
")",
"]",
"for",
"asset",
"in",
"assets_to_cancel",
":",
"blotter",
".",
"cancel_all_orders_for_asset",
"(",
"asset",
")",
"# Make a copy here so that we are not modifying the list that is being",
"# iterated over.",
"for",
"order",
"in",
"copy",
"(",
"blotter",
".",
"new_orders",
")",
":",
"if",
"order",
".",
"status",
"==",
"ORDER_STATUS",
".",
"CANCELLED",
":",
"metrics_tracker",
".",
"process_order",
"(",
"order",
")",
"blotter",
".",
"new_orders",
".",
"remove",
"(",
"order",
")"
] |
Clear out any assets that have expired before starting a new sim day.
Performs two functions:
1. Finds all assets for which we have open orders and clears any
orders whose assets are on or after their auto_close_date.
2. Finds all assets for which we have positions and generates
close_position events for any assets that have reached their
auto_close_date.
|
[
"Clear",
"out",
"any",
"assets",
"that",
"have",
"expired",
"before",
"starting",
"a",
"new",
"sim",
"day",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/gens/tradesimulation.py#L238-L281
|
train
|
quantopian/zipline
|
zipline/gens/tradesimulation.py
|
AlgorithmSimulator._get_daily_message
|
def _get_daily_message(self, dt, algo, metrics_tracker):
"""
Get a perf message for the given datetime.
"""
perf_message = metrics_tracker.handle_market_close(
dt,
self.data_portal,
)
perf_message['daily_perf']['recorded_vars'] = algo.recorded_vars
return perf_message
|
python
|
def _get_daily_message(self, dt, algo, metrics_tracker):
"""
Get a perf message for the given datetime.
"""
perf_message = metrics_tracker.handle_market_close(
dt,
self.data_portal,
)
perf_message['daily_perf']['recorded_vars'] = algo.recorded_vars
return perf_message
|
[
"def",
"_get_daily_message",
"(",
"self",
",",
"dt",
",",
"algo",
",",
"metrics_tracker",
")",
":",
"perf_message",
"=",
"metrics_tracker",
".",
"handle_market_close",
"(",
"dt",
",",
"self",
".",
"data_portal",
",",
")",
"perf_message",
"[",
"'daily_perf'",
"]",
"[",
"'recorded_vars'",
"]",
"=",
"algo",
".",
"recorded_vars",
"return",
"perf_message"
] |
Get a perf message for the given datetime.
|
[
"Get",
"a",
"perf",
"message",
"for",
"the",
"given",
"datetime",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/gens/tradesimulation.py#L283-L292
|
train
|
quantopian/zipline
|
zipline/gens/tradesimulation.py
|
AlgorithmSimulator._get_minute_message
|
def _get_minute_message(self, dt, algo, metrics_tracker):
"""
Get a perf message for the given datetime.
"""
rvars = algo.recorded_vars
minute_message = metrics_tracker.handle_minute_close(
dt,
self.data_portal,
)
minute_message['minute_perf']['recorded_vars'] = rvars
return minute_message
|
python
|
def _get_minute_message(self, dt, algo, metrics_tracker):
"""
Get a perf message for the given datetime.
"""
rvars = algo.recorded_vars
minute_message = metrics_tracker.handle_minute_close(
dt,
self.data_portal,
)
minute_message['minute_perf']['recorded_vars'] = rvars
return minute_message
|
[
"def",
"_get_minute_message",
"(",
"self",
",",
"dt",
",",
"algo",
",",
"metrics_tracker",
")",
":",
"rvars",
"=",
"algo",
".",
"recorded_vars",
"minute_message",
"=",
"metrics_tracker",
".",
"handle_minute_close",
"(",
"dt",
",",
"self",
".",
"data_portal",
",",
")",
"minute_message",
"[",
"'minute_perf'",
"]",
"[",
"'recorded_vars'",
"]",
"=",
"rvars",
"return",
"minute_message"
] |
Get a perf message for the given datetime.
|
[
"Get",
"a",
"perf",
"message",
"for",
"the",
"given",
"datetime",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/gens/tradesimulation.py#L294-L306
|
train
|
quantopian/zipline
|
zipline/data/adjustments.py
|
SQLiteAdjustmentReader.load_adjustments
|
def load_adjustments(self,
dates,
assets,
should_include_splits,
should_include_mergers,
should_include_dividends,
adjustment_type):
"""
Load collection of Adjustment objects from underlying adjustments db.
Parameters
----------
dates : pd.DatetimeIndex
Dates for which adjustments are needed.
assets : pd.Int64Index
Assets for which adjustments are needed.
should_include_splits : bool
Whether split adjustments should be included.
should_include_mergers : bool
Whether merger adjustments should be included.
should_include_dividends : bool
Whether dividend adjustments should be included.
adjustment_type : str
Whether price adjustments, volume adjustments, or both, should be
included in the output.
Returns
-------
adjustments : dict[str -> dict[int -> Adjustment]]
A dictionary containing price and/or volume adjustment mappings
from index to adjustment objects to apply at that index.
"""
return load_adjustments_from_sqlite(
self.conn,
dates,
assets,
should_include_splits,
should_include_mergers,
should_include_dividends,
adjustment_type,
)
|
python
|
def load_adjustments(self,
dates,
assets,
should_include_splits,
should_include_mergers,
should_include_dividends,
adjustment_type):
"""
Load collection of Adjustment objects from underlying adjustments db.
Parameters
----------
dates : pd.DatetimeIndex
Dates for which adjustments are needed.
assets : pd.Int64Index
Assets for which adjustments are needed.
should_include_splits : bool
Whether split adjustments should be included.
should_include_mergers : bool
Whether merger adjustments should be included.
should_include_dividends : bool
Whether dividend adjustments should be included.
adjustment_type : str
Whether price adjustments, volume adjustments, or both, should be
included in the output.
Returns
-------
adjustments : dict[str -> dict[int -> Adjustment]]
A dictionary containing price and/or volume adjustment mappings
from index to adjustment objects to apply at that index.
"""
return load_adjustments_from_sqlite(
self.conn,
dates,
assets,
should_include_splits,
should_include_mergers,
should_include_dividends,
adjustment_type,
)
|
[
"def",
"load_adjustments",
"(",
"self",
",",
"dates",
",",
"assets",
",",
"should_include_splits",
",",
"should_include_mergers",
",",
"should_include_dividends",
",",
"adjustment_type",
")",
":",
"return",
"load_adjustments_from_sqlite",
"(",
"self",
".",
"conn",
",",
"dates",
",",
"assets",
",",
"should_include_splits",
",",
"should_include_mergers",
",",
"should_include_dividends",
",",
"adjustment_type",
",",
")"
] |
Load collection of Adjustment objects from underlying adjustments db.
Parameters
----------
dates : pd.DatetimeIndex
Dates for which adjustments are needed.
assets : pd.Int64Index
Assets for which adjustments are needed.
should_include_splits : bool
Whether split adjustments should be included.
should_include_mergers : bool
Whether merger adjustments should be included.
should_include_dividends : bool
Whether dividend adjustments should be included.
adjustment_type : str
Whether price adjustments, volume adjustments, or both, should be
included in the output.
Returns
-------
adjustments : dict[str -> dict[int -> Adjustment]]
A dictionary containing price and/or volume adjustment mappings
from index to adjustment objects to apply at that index.
|
[
"Load",
"collection",
"of",
"Adjustment",
"objects",
"from",
"underlying",
"adjustments",
"db",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/adjustments.py#L142-L182
|
train
|
quantopian/zipline
|
zipline/data/adjustments.py
|
SQLiteAdjustmentReader.unpack_db_to_component_dfs
|
def unpack_db_to_component_dfs(self, convert_dates=False):
"""Returns the set of known tables in the adjustments file in DataFrame
form.
Parameters
----------
convert_dates : bool, optional
By default, dates are returned in seconds since EPOCH. If
convert_dates is True, all ints in date columns will be converted
to datetimes.
Returns
-------
dfs : dict{str->DataFrame}
Dictionary which maps table name to the corresponding DataFrame
version of the table, where all date columns have been coerced back
from int to datetime.
"""
return {
t_name: self.get_df_from_table(t_name, convert_dates)
for t_name in self._datetime_int_cols
}
|
python
|
def unpack_db_to_component_dfs(self, convert_dates=False):
"""Returns the set of known tables in the adjustments file in DataFrame
form.
Parameters
----------
convert_dates : bool, optional
By default, dates are returned in seconds since EPOCH. If
convert_dates is True, all ints in date columns will be converted
to datetimes.
Returns
-------
dfs : dict{str->DataFrame}
Dictionary which maps table name to the corresponding DataFrame
version of the table, where all date columns have been coerced back
from int to datetime.
"""
return {
t_name: self.get_df_from_table(t_name, convert_dates)
for t_name in self._datetime_int_cols
}
|
[
"def",
"unpack_db_to_component_dfs",
"(",
"self",
",",
"convert_dates",
"=",
"False",
")",
":",
"return",
"{",
"t_name",
":",
"self",
".",
"get_df_from_table",
"(",
"t_name",
",",
"convert_dates",
")",
"for",
"t_name",
"in",
"self",
".",
"_datetime_int_cols",
"}"
] |
Returns the set of known tables in the adjustments file in DataFrame
form.
Parameters
----------
convert_dates : bool, optional
By default, dates are returned in seconds since EPOCH. If
convert_dates is True, all ints in date columns will be converted
to datetimes.
Returns
-------
dfs : dict{str->DataFrame}
Dictionary which maps table name to the corresponding DataFrame
version of the table, where all date columns have been coerced back
from int to datetime.
|
[
"Returns",
"the",
"set",
"of",
"known",
"tables",
"in",
"the",
"adjustments",
"file",
"in",
"DataFrame",
"form",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/adjustments.py#L268-L289
|
train
|
quantopian/zipline
|
zipline/data/adjustments.py
|
SQLiteAdjustmentReader._df_dtypes
|
def _df_dtypes(self, table_name, convert_dates):
"""Get dtypes to use when unpacking sqlite tables as dataframes.
"""
out = self._raw_table_dtypes[table_name]
if convert_dates:
out = out.copy()
for date_column in self._datetime_int_cols[table_name]:
out[date_column] = datetime64ns_dtype
return out
|
python
|
def _df_dtypes(self, table_name, convert_dates):
"""Get dtypes to use when unpacking sqlite tables as dataframes.
"""
out = self._raw_table_dtypes[table_name]
if convert_dates:
out = out.copy()
for date_column in self._datetime_int_cols[table_name]:
out[date_column] = datetime64ns_dtype
return out
|
[
"def",
"_df_dtypes",
"(",
"self",
",",
"table_name",
",",
"convert_dates",
")",
":",
"out",
"=",
"self",
".",
"_raw_table_dtypes",
"[",
"table_name",
"]",
"if",
"convert_dates",
":",
"out",
"=",
"out",
".",
"copy",
"(",
")",
"for",
"date_column",
"in",
"self",
".",
"_datetime_int_cols",
"[",
"table_name",
"]",
":",
"out",
"[",
"date_column",
"]",
"=",
"datetime64ns_dtype",
"return",
"out"
] |
Get dtypes to use when unpacking sqlite tables as dataframes.
|
[
"Get",
"dtypes",
"to",
"use",
"when",
"unpacking",
"sqlite",
"tables",
"as",
"dataframes",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/adjustments.py#L326-L335
|
train
|
quantopian/zipline
|
zipline/data/adjustments.py
|
SQLiteAdjustmentWriter.calc_dividend_ratios
|
def calc_dividend_ratios(self, dividends):
"""
Calculate the ratios to apply to equities when looking back at pricing
history so that the price is smoothed over the ex_date, when the market
adjusts to the change in equity value due to upcoming dividend.
Returns
-------
DataFrame
A frame in the same format as splits and mergers, with keys
- sid, the id of the equity
- effective_date, the date in seconds on which to apply the ratio.
- ratio, the ratio to apply to backwards looking pricing data.
"""
if dividends is None or dividends.empty:
return pd.DataFrame(np.array(
[],
dtype=[
('sid', uint64_dtype),
('effective_date', uint32_dtype),
('ratio', float64_dtype),
],
))
pricing_reader = self._equity_daily_bar_reader
input_sids = dividends.sid.values
unique_sids, sids_ix = np.unique(input_sids, return_inverse=True)
dates = pricing_reader.sessions.values
close, = pricing_reader.load_raw_arrays(
['close'],
pd.Timestamp(dates[0], tz='UTC'),
pd.Timestamp(dates[-1], tz='UTC'),
unique_sids,
)
date_ix = np.searchsorted(dates, dividends.ex_date.values)
mask = date_ix > 0
date_ix = date_ix[mask]
sids_ix = sids_ix[mask]
input_dates = dividends.ex_date.values[mask]
# subtract one day to get the close on the day prior to the merger
previous_close = close[date_ix - 1, sids_ix]
input_sids = input_sids[mask]
amount = dividends.amount.values[mask]
ratio = 1.0 - amount / previous_close
non_nan_ratio_mask = ~np.isnan(ratio)
for ix in np.flatnonzero(~non_nan_ratio_mask):
log.warn(
"Couldn't compute ratio for dividend"
" sid={sid}, ex_date={ex_date:%Y-%m-%d}, amount={amount:.3f}",
sid=input_sids[ix],
ex_date=pd.Timestamp(input_dates[ix]),
amount=amount[ix],
)
positive_ratio_mask = ratio > 0
for ix in np.flatnonzero(~positive_ratio_mask & non_nan_ratio_mask):
log.warn(
"Dividend ratio <= 0 for dividend"
" sid={sid}, ex_date={ex_date:%Y-%m-%d}, amount={amount:.3f}",
sid=input_sids[ix],
ex_date=pd.Timestamp(input_dates[ix]),
amount=amount[ix],
)
valid_ratio_mask = non_nan_ratio_mask & positive_ratio_mask
return pd.DataFrame({
'sid': input_sids[valid_ratio_mask],
'effective_date': input_dates[valid_ratio_mask],
'ratio': ratio[valid_ratio_mask],
})
|
python
|
def calc_dividend_ratios(self, dividends):
"""
Calculate the ratios to apply to equities when looking back at pricing
history so that the price is smoothed over the ex_date, when the market
adjusts to the change in equity value due to upcoming dividend.
Returns
-------
DataFrame
A frame in the same format as splits and mergers, with keys
- sid, the id of the equity
- effective_date, the date in seconds on which to apply the ratio.
- ratio, the ratio to apply to backwards looking pricing data.
"""
if dividends is None or dividends.empty:
return pd.DataFrame(np.array(
[],
dtype=[
('sid', uint64_dtype),
('effective_date', uint32_dtype),
('ratio', float64_dtype),
],
))
pricing_reader = self._equity_daily_bar_reader
input_sids = dividends.sid.values
unique_sids, sids_ix = np.unique(input_sids, return_inverse=True)
dates = pricing_reader.sessions.values
close, = pricing_reader.load_raw_arrays(
['close'],
pd.Timestamp(dates[0], tz='UTC'),
pd.Timestamp(dates[-1], tz='UTC'),
unique_sids,
)
date_ix = np.searchsorted(dates, dividends.ex_date.values)
mask = date_ix > 0
date_ix = date_ix[mask]
sids_ix = sids_ix[mask]
input_dates = dividends.ex_date.values[mask]
# subtract one day to get the close on the day prior to the merger
previous_close = close[date_ix - 1, sids_ix]
input_sids = input_sids[mask]
amount = dividends.amount.values[mask]
ratio = 1.0 - amount / previous_close
non_nan_ratio_mask = ~np.isnan(ratio)
for ix in np.flatnonzero(~non_nan_ratio_mask):
log.warn(
"Couldn't compute ratio for dividend"
" sid={sid}, ex_date={ex_date:%Y-%m-%d}, amount={amount:.3f}",
sid=input_sids[ix],
ex_date=pd.Timestamp(input_dates[ix]),
amount=amount[ix],
)
positive_ratio_mask = ratio > 0
for ix in np.flatnonzero(~positive_ratio_mask & non_nan_ratio_mask):
log.warn(
"Dividend ratio <= 0 for dividend"
" sid={sid}, ex_date={ex_date:%Y-%m-%d}, amount={amount:.3f}",
sid=input_sids[ix],
ex_date=pd.Timestamp(input_dates[ix]),
amount=amount[ix],
)
valid_ratio_mask = non_nan_ratio_mask & positive_ratio_mask
return pd.DataFrame({
'sid': input_sids[valid_ratio_mask],
'effective_date': input_dates[valid_ratio_mask],
'ratio': ratio[valid_ratio_mask],
})
|
[
"def",
"calc_dividend_ratios",
"(",
"self",
",",
"dividends",
")",
":",
"if",
"dividends",
"is",
"None",
"or",
"dividends",
".",
"empty",
":",
"return",
"pd",
".",
"DataFrame",
"(",
"np",
".",
"array",
"(",
"[",
"]",
",",
"dtype",
"=",
"[",
"(",
"'sid'",
",",
"uint64_dtype",
")",
",",
"(",
"'effective_date'",
",",
"uint32_dtype",
")",
",",
"(",
"'ratio'",
",",
"float64_dtype",
")",
",",
"]",
",",
")",
")",
"pricing_reader",
"=",
"self",
".",
"_equity_daily_bar_reader",
"input_sids",
"=",
"dividends",
".",
"sid",
".",
"values",
"unique_sids",
",",
"sids_ix",
"=",
"np",
".",
"unique",
"(",
"input_sids",
",",
"return_inverse",
"=",
"True",
")",
"dates",
"=",
"pricing_reader",
".",
"sessions",
".",
"values",
"close",
",",
"=",
"pricing_reader",
".",
"load_raw_arrays",
"(",
"[",
"'close'",
"]",
",",
"pd",
".",
"Timestamp",
"(",
"dates",
"[",
"0",
"]",
",",
"tz",
"=",
"'UTC'",
")",
",",
"pd",
".",
"Timestamp",
"(",
"dates",
"[",
"-",
"1",
"]",
",",
"tz",
"=",
"'UTC'",
")",
",",
"unique_sids",
",",
")",
"date_ix",
"=",
"np",
".",
"searchsorted",
"(",
"dates",
",",
"dividends",
".",
"ex_date",
".",
"values",
")",
"mask",
"=",
"date_ix",
">",
"0",
"date_ix",
"=",
"date_ix",
"[",
"mask",
"]",
"sids_ix",
"=",
"sids_ix",
"[",
"mask",
"]",
"input_dates",
"=",
"dividends",
".",
"ex_date",
".",
"values",
"[",
"mask",
"]",
"# subtract one day to get the close on the day prior to the merger",
"previous_close",
"=",
"close",
"[",
"date_ix",
"-",
"1",
",",
"sids_ix",
"]",
"input_sids",
"=",
"input_sids",
"[",
"mask",
"]",
"amount",
"=",
"dividends",
".",
"amount",
".",
"values",
"[",
"mask",
"]",
"ratio",
"=",
"1.0",
"-",
"amount",
"/",
"previous_close",
"non_nan_ratio_mask",
"=",
"~",
"np",
".",
"isnan",
"(",
"ratio",
")",
"for",
"ix",
"in",
"np",
".",
"flatnonzero",
"(",
"~",
"non_nan_ratio_mask",
")",
":",
"log",
".",
"warn",
"(",
"\"Couldn't compute ratio for dividend\"",
"\" sid={sid}, ex_date={ex_date:%Y-%m-%d}, amount={amount:.3f}\"",
",",
"sid",
"=",
"input_sids",
"[",
"ix",
"]",
",",
"ex_date",
"=",
"pd",
".",
"Timestamp",
"(",
"input_dates",
"[",
"ix",
"]",
")",
",",
"amount",
"=",
"amount",
"[",
"ix",
"]",
",",
")",
"positive_ratio_mask",
"=",
"ratio",
">",
"0",
"for",
"ix",
"in",
"np",
".",
"flatnonzero",
"(",
"~",
"positive_ratio_mask",
"&",
"non_nan_ratio_mask",
")",
":",
"log",
".",
"warn",
"(",
"\"Dividend ratio <= 0 for dividend\"",
"\" sid={sid}, ex_date={ex_date:%Y-%m-%d}, amount={amount:.3f}\"",
",",
"sid",
"=",
"input_sids",
"[",
"ix",
"]",
",",
"ex_date",
"=",
"pd",
".",
"Timestamp",
"(",
"input_dates",
"[",
"ix",
"]",
")",
",",
"amount",
"=",
"amount",
"[",
"ix",
"]",
",",
")",
"valid_ratio_mask",
"=",
"non_nan_ratio_mask",
"&",
"positive_ratio_mask",
"return",
"pd",
".",
"DataFrame",
"(",
"{",
"'sid'",
":",
"input_sids",
"[",
"valid_ratio_mask",
"]",
",",
"'effective_date'",
":",
"input_dates",
"[",
"valid_ratio_mask",
"]",
",",
"'ratio'",
":",
"ratio",
"[",
"valid_ratio_mask",
"]",
",",
"}",
")"
] |
Calculate the ratios to apply to equities when looking back at pricing
history so that the price is smoothed over the ex_date, when the market
adjusts to the change in equity value due to upcoming dividend.
Returns
-------
DataFrame
A frame in the same format as splits and mergers, with keys
- sid, the id of the equity
- effective_date, the date in seconds on which to apply the ratio.
- ratio, the ratio to apply to backwards looking pricing data.
|
[
"Calculate",
"the",
"ratios",
"to",
"apply",
"to",
"equities",
"when",
"looking",
"back",
"at",
"pricing",
"history",
"so",
"that",
"the",
"price",
"is",
"smoothed",
"over",
"the",
"ex_date",
"when",
"the",
"market",
"adjusts",
"to",
"the",
"change",
"in",
"equity",
"value",
"due",
"to",
"upcoming",
"dividend",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/adjustments.py#L456-L530
|
train
|
quantopian/zipline
|
zipline/data/adjustments.py
|
SQLiteAdjustmentWriter.write_dividend_data
|
def write_dividend_data(self, dividends, stock_dividends=None):
"""
Write both dividend payouts and the derived price adjustment ratios.
"""
# First write the dividend payouts.
self._write_dividends(dividends)
self._write_stock_dividends(stock_dividends)
# Second from the dividend payouts, calculate ratios.
dividend_ratios = self.calc_dividend_ratios(dividends)
self.write_frame('dividends', dividend_ratios)
|
python
|
def write_dividend_data(self, dividends, stock_dividends=None):
"""
Write both dividend payouts and the derived price adjustment ratios.
"""
# First write the dividend payouts.
self._write_dividends(dividends)
self._write_stock_dividends(stock_dividends)
# Second from the dividend payouts, calculate ratios.
dividend_ratios = self.calc_dividend_ratios(dividends)
self.write_frame('dividends', dividend_ratios)
|
[
"def",
"write_dividend_data",
"(",
"self",
",",
"dividends",
",",
"stock_dividends",
"=",
"None",
")",
":",
"# First write the dividend payouts.",
"self",
".",
"_write_dividends",
"(",
"dividends",
")",
"self",
".",
"_write_stock_dividends",
"(",
"stock_dividends",
")",
"# Second from the dividend payouts, calculate ratios.",
"dividend_ratios",
"=",
"self",
".",
"calc_dividend_ratios",
"(",
"dividends",
")",
"self",
".",
"write_frame",
"(",
"'dividends'",
",",
"dividend_ratios",
")"
] |
Write both dividend payouts and the derived price adjustment ratios.
|
[
"Write",
"both",
"dividend",
"payouts",
"and",
"the",
"derived",
"price",
"adjustment",
"ratios",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/adjustments.py#L570-L581
|
train
|
quantopian/zipline
|
zipline/data/adjustments.py
|
SQLiteAdjustmentWriter.write
|
def write(self,
splits=None,
mergers=None,
dividends=None,
stock_dividends=None):
"""
Writes data to a SQLite file to be read by SQLiteAdjustmentReader.
Parameters
----------
splits : pandas.DataFrame, optional
Dataframe containing split data. The format of this dataframe is:
effective_date : int
The date, represented as seconds since Unix epoch, on which
the adjustment should be applied.
ratio : float
A value to apply to all data earlier than the effective date.
For open, high, low, and close those values are multiplied by
the ratio. Volume is divided by this value.
sid : int
The asset id associated with this adjustment.
mergers : pandas.DataFrame, optional
DataFrame containing merger data. The format of this dataframe is:
effective_date : int
The date, represented as seconds since Unix epoch, on which
the adjustment should be applied.
ratio : float
A value to apply to all data earlier than the effective date.
For open, high, low, and close those values are multiplied by
the ratio. Volume is unaffected.
sid : int
The asset id associated with this adjustment.
dividends : pandas.DataFrame, optional
DataFrame containing dividend data. The format of the dataframe is:
sid : int
The asset id associated with this adjustment.
ex_date : datetime64
The date on which an equity must be held to be eligible to
receive payment.
declared_date : datetime64
The date on which the dividend is announced to the public.
pay_date : datetime64
The date on which the dividend is distributed.
record_date : datetime64
The date on which the stock ownership is checked to determine
distribution of dividends.
amount : float
The cash amount paid for each share.
Dividend ratios are calculated as:
``1.0 - (dividend_value / "close on day prior to ex_date")``
stock_dividends : pandas.DataFrame, optional
DataFrame containing stock dividend data. The format of the
dataframe is:
sid : int
The asset id associated with this adjustment.
ex_date : datetime64
The date on which an equity must be held to be eligible to
receive payment.
declared_date : datetime64
The date on which the dividend is announced to the public.
pay_date : datetime64
The date on which the dividend is distributed.
record_date : datetime64
The date on which the stock ownership is checked to determine
distribution of dividends.
payment_sid : int
The asset id of the shares that should be paid instead of
cash.
ratio : float
The ratio of currently held shares in the held sid that
should be paid with new shares of the payment_sid.
See Also
--------
zipline.data.adjustments.SQLiteAdjustmentReader
"""
self.write_frame('splits', splits)
self.write_frame('mergers', mergers)
self.write_dividend_data(dividends, stock_dividends)
# Use IF NOT EXISTS here to allow multiple writes if desired.
self.conn.execute(
"CREATE INDEX IF NOT EXISTS splits_sids "
"ON splits(sid)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS splits_effective_date "
"ON splits(effective_date)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS mergers_sids "
"ON mergers(sid)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS mergers_effective_date "
"ON mergers(effective_date)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS dividends_sid "
"ON dividends(sid)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS dividends_effective_date "
"ON dividends(effective_date)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS dividend_payouts_sid "
"ON dividend_payouts(sid)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS dividends_payouts_ex_date "
"ON dividend_payouts(ex_date)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS stock_dividend_payouts_sid "
"ON stock_dividend_payouts(sid)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS stock_dividends_payouts_ex_date "
"ON stock_dividend_payouts(ex_date)"
)
|
python
|
def write(self,
splits=None,
mergers=None,
dividends=None,
stock_dividends=None):
"""
Writes data to a SQLite file to be read by SQLiteAdjustmentReader.
Parameters
----------
splits : pandas.DataFrame, optional
Dataframe containing split data. The format of this dataframe is:
effective_date : int
The date, represented as seconds since Unix epoch, on which
the adjustment should be applied.
ratio : float
A value to apply to all data earlier than the effective date.
For open, high, low, and close those values are multiplied by
the ratio. Volume is divided by this value.
sid : int
The asset id associated with this adjustment.
mergers : pandas.DataFrame, optional
DataFrame containing merger data. The format of this dataframe is:
effective_date : int
The date, represented as seconds since Unix epoch, on which
the adjustment should be applied.
ratio : float
A value to apply to all data earlier than the effective date.
For open, high, low, and close those values are multiplied by
the ratio. Volume is unaffected.
sid : int
The asset id associated with this adjustment.
dividends : pandas.DataFrame, optional
DataFrame containing dividend data. The format of the dataframe is:
sid : int
The asset id associated with this adjustment.
ex_date : datetime64
The date on which an equity must be held to be eligible to
receive payment.
declared_date : datetime64
The date on which the dividend is announced to the public.
pay_date : datetime64
The date on which the dividend is distributed.
record_date : datetime64
The date on which the stock ownership is checked to determine
distribution of dividends.
amount : float
The cash amount paid for each share.
Dividend ratios are calculated as:
``1.0 - (dividend_value / "close on day prior to ex_date")``
stock_dividends : pandas.DataFrame, optional
DataFrame containing stock dividend data. The format of the
dataframe is:
sid : int
The asset id associated with this adjustment.
ex_date : datetime64
The date on which an equity must be held to be eligible to
receive payment.
declared_date : datetime64
The date on which the dividend is announced to the public.
pay_date : datetime64
The date on which the dividend is distributed.
record_date : datetime64
The date on which the stock ownership is checked to determine
distribution of dividends.
payment_sid : int
The asset id of the shares that should be paid instead of
cash.
ratio : float
The ratio of currently held shares in the held sid that
should be paid with new shares of the payment_sid.
See Also
--------
zipline.data.adjustments.SQLiteAdjustmentReader
"""
self.write_frame('splits', splits)
self.write_frame('mergers', mergers)
self.write_dividend_data(dividends, stock_dividends)
# Use IF NOT EXISTS here to allow multiple writes if desired.
self.conn.execute(
"CREATE INDEX IF NOT EXISTS splits_sids "
"ON splits(sid)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS splits_effective_date "
"ON splits(effective_date)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS mergers_sids "
"ON mergers(sid)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS mergers_effective_date "
"ON mergers(effective_date)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS dividends_sid "
"ON dividends(sid)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS dividends_effective_date "
"ON dividends(effective_date)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS dividend_payouts_sid "
"ON dividend_payouts(sid)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS dividends_payouts_ex_date "
"ON dividend_payouts(ex_date)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS stock_dividend_payouts_sid "
"ON stock_dividend_payouts(sid)"
)
self.conn.execute(
"CREATE INDEX IF NOT EXISTS stock_dividends_payouts_ex_date "
"ON stock_dividend_payouts(ex_date)"
)
|
[
"def",
"write",
"(",
"self",
",",
"splits",
"=",
"None",
",",
"mergers",
"=",
"None",
",",
"dividends",
"=",
"None",
",",
"stock_dividends",
"=",
"None",
")",
":",
"self",
".",
"write_frame",
"(",
"'splits'",
",",
"splits",
")",
"self",
".",
"write_frame",
"(",
"'mergers'",
",",
"mergers",
")",
"self",
".",
"write_dividend_data",
"(",
"dividends",
",",
"stock_dividends",
")",
"# Use IF NOT EXISTS here to allow multiple writes if desired.",
"self",
".",
"conn",
".",
"execute",
"(",
"\"CREATE INDEX IF NOT EXISTS splits_sids \"",
"\"ON splits(sid)\"",
")",
"self",
".",
"conn",
".",
"execute",
"(",
"\"CREATE INDEX IF NOT EXISTS splits_effective_date \"",
"\"ON splits(effective_date)\"",
")",
"self",
".",
"conn",
".",
"execute",
"(",
"\"CREATE INDEX IF NOT EXISTS mergers_sids \"",
"\"ON mergers(sid)\"",
")",
"self",
".",
"conn",
".",
"execute",
"(",
"\"CREATE INDEX IF NOT EXISTS mergers_effective_date \"",
"\"ON mergers(effective_date)\"",
")",
"self",
".",
"conn",
".",
"execute",
"(",
"\"CREATE INDEX IF NOT EXISTS dividends_sid \"",
"\"ON dividends(sid)\"",
")",
"self",
".",
"conn",
".",
"execute",
"(",
"\"CREATE INDEX IF NOT EXISTS dividends_effective_date \"",
"\"ON dividends(effective_date)\"",
")",
"self",
".",
"conn",
".",
"execute",
"(",
"\"CREATE INDEX IF NOT EXISTS dividend_payouts_sid \"",
"\"ON dividend_payouts(sid)\"",
")",
"self",
".",
"conn",
".",
"execute",
"(",
"\"CREATE INDEX IF NOT EXISTS dividends_payouts_ex_date \"",
"\"ON dividend_payouts(ex_date)\"",
")",
"self",
".",
"conn",
".",
"execute",
"(",
"\"CREATE INDEX IF NOT EXISTS stock_dividend_payouts_sid \"",
"\"ON stock_dividend_payouts(sid)\"",
")",
"self",
".",
"conn",
".",
"execute",
"(",
"\"CREATE INDEX IF NOT EXISTS stock_dividends_payouts_ex_date \"",
"\"ON stock_dividend_payouts(ex_date)\"",
")"
] |
Writes data to a SQLite file to be read by SQLiteAdjustmentReader.
Parameters
----------
splits : pandas.DataFrame, optional
Dataframe containing split data. The format of this dataframe is:
effective_date : int
The date, represented as seconds since Unix epoch, on which
the adjustment should be applied.
ratio : float
A value to apply to all data earlier than the effective date.
For open, high, low, and close those values are multiplied by
the ratio. Volume is divided by this value.
sid : int
The asset id associated with this adjustment.
mergers : pandas.DataFrame, optional
DataFrame containing merger data. The format of this dataframe is:
effective_date : int
The date, represented as seconds since Unix epoch, on which
the adjustment should be applied.
ratio : float
A value to apply to all data earlier than the effective date.
For open, high, low, and close those values are multiplied by
the ratio. Volume is unaffected.
sid : int
The asset id associated with this adjustment.
dividends : pandas.DataFrame, optional
DataFrame containing dividend data. The format of the dataframe is:
sid : int
The asset id associated with this adjustment.
ex_date : datetime64
The date on which an equity must be held to be eligible to
receive payment.
declared_date : datetime64
The date on which the dividend is announced to the public.
pay_date : datetime64
The date on which the dividend is distributed.
record_date : datetime64
The date on which the stock ownership is checked to determine
distribution of dividends.
amount : float
The cash amount paid for each share.
Dividend ratios are calculated as:
``1.0 - (dividend_value / "close on day prior to ex_date")``
stock_dividends : pandas.DataFrame, optional
DataFrame containing stock dividend data. The format of the
dataframe is:
sid : int
The asset id associated with this adjustment.
ex_date : datetime64
The date on which an equity must be held to be eligible to
receive payment.
declared_date : datetime64
The date on which the dividend is announced to the public.
pay_date : datetime64
The date on which the dividend is distributed.
record_date : datetime64
The date on which the stock ownership is checked to determine
distribution of dividends.
payment_sid : int
The asset id of the shares that should be paid instead of
cash.
ratio : float
The ratio of currently held shares in the held sid that
should be paid with new shares of the payment_sid.
See Also
--------
zipline.data.adjustments.SQLiteAdjustmentReader
|
[
"Writes",
"data",
"to",
"a",
"SQLite",
"file",
"to",
"be",
"read",
"by",
"SQLiteAdjustmentReader",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/adjustments.py#L583-L703
|
train
|
quantopian/zipline
|
zipline/pipeline/mixins.py
|
CustomTermMixin.compute
|
def compute(self, today, assets, out, *arrays):
"""
Override this method with a function that writes a value into `out`.
"""
raise NotImplementedError(
"{name} must define a compute method".format(
name=type(self).__name__
)
)
|
python
|
def compute(self, today, assets, out, *arrays):
"""
Override this method with a function that writes a value into `out`.
"""
raise NotImplementedError(
"{name} must define a compute method".format(
name=type(self).__name__
)
)
|
[
"def",
"compute",
"(",
"self",
",",
"today",
",",
"assets",
",",
"out",
",",
"*",
"arrays",
")",
":",
"raise",
"NotImplementedError",
"(",
"\"{name} must define a compute method\"",
".",
"format",
"(",
"name",
"=",
"type",
"(",
"self",
")",
".",
"__name__",
")",
")"
] |
Override this method with a function that writes a value into `out`.
|
[
"Override",
"this",
"method",
"with",
"a",
"function",
"that",
"writes",
"a",
"value",
"into",
"out",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/mixins.py#L143-L151
|
train
|
quantopian/zipline
|
zipline/pipeline/mixins.py
|
CustomTermMixin._allocate_output
|
def _allocate_output(self, windows, shape):
"""
Allocate an output array whose rows should be passed to `self.compute`.
The resulting array must have a shape of ``shape``.
If we have standard outputs (i.e. self.outputs is NotSpecified), the
default is an empty ndarray whose dtype is ``self.dtype``.
If we have an outputs tuple, the default is an empty recarray with
``self.outputs`` as field names. Each field will have dtype
``self.dtype``.
This can be overridden to control the kind of array constructed
(e.g. to produce a LabelArray instead of an ndarray).
"""
missing_value = self.missing_value
outputs = self.outputs
if outputs is not NotSpecified:
out = recarray(
shape,
formats=[self.dtype.str] * len(outputs),
names=outputs,
)
out[:] = missing_value
else:
out = full(shape, missing_value, dtype=self.dtype)
return out
|
python
|
def _allocate_output(self, windows, shape):
"""
Allocate an output array whose rows should be passed to `self.compute`.
The resulting array must have a shape of ``shape``.
If we have standard outputs (i.e. self.outputs is NotSpecified), the
default is an empty ndarray whose dtype is ``self.dtype``.
If we have an outputs tuple, the default is an empty recarray with
``self.outputs`` as field names. Each field will have dtype
``self.dtype``.
This can be overridden to control the kind of array constructed
(e.g. to produce a LabelArray instead of an ndarray).
"""
missing_value = self.missing_value
outputs = self.outputs
if outputs is not NotSpecified:
out = recarray(
shape,
formats=[self.dtype.str] * len(outputs),
names=outputs,
)
out[:] = missing_value
else:
out = full(shape, missing_value, dtype=self.dtype)
return out
|
[
"def",
"_allocate_output",
"(",
"self",
",",
"windows",
",",
"shape",
")",
":",
"missing_value",
"=",
"self",
".",
"missing_value",
"outputs",
"=",
"self",
".",
"outputs",
"if",
"outputs",
"is",
"not",
"NotSpecified",
":",
"out",
"=",
"recarray",
"(",
"shape",
",",
"formats",
"=",
"[",
"self",
".",
"dtype",
".",
"str",
"]",
"*",
"len",
"(",
"outputs",
")",
",",
"names",
"=",
"outputs",
",",
")",
"out",
"[",
":",
"]",
"=",
"missing_value",
"else",
":",
"out",
"=",
"full",
"(",
"shape",
",",
"missing_value",
",",
"dtype",
"=",
"self",
".",
"dtype",
")",
"return",
"out"
] |
Allocate an output array whose rows should be passed to `self.compute`.
The resulting array must have a shape of ``shape``.
If we have standard outputs (i.e. self.outputs is NotSpecified), the
default is an empty ndarray whose dtype is ``self.dtype``.
If we have an outputs tuple, the default is an empty recarray with
``self.outputs`` as field names. Each field will have dtype
``self.dtype``.
This can be overridden to control the kind of array constructed
(e.g. to produce a LabelArray instead of an ndarray).
|
[
"Allocate",
"an",
"output",
"array",
"whose",
"rows",
"should",
"be",
"passed",
"to",
"self",
".",
"compute",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/mixins.py#L153-L180
|
train
|
quantopian/zipline
|
zipline/pipeline/mixins.py
|
CustomTermMixin._compute
|
def _compute(self, windows, dates, assets, mask):
"""
Call the user's `compute` function on each window with a pre-built
output array.
"""
format_inputs = self._format_inputs
compute = self.compute
params = self.params
ndim = self.ndim
shape = (len(mask), 1) if ndim == 1 else mask.shape
out = self._allocate_output(windows, shape)
with self.ctx:
for idx, date in enumerate(dates):
# Never apply a mask to 1D outputs.
out_mask = array([True]) if ndim == 1 else mask[idx]
# Mask our inputs as usual.
inputs_mask = mask[idx]
masked_assets = assets[inputs_mask]
out_row = out[idx][out_mask]
inputs = format_inputs(windows, inputs_mask)
compute(date, masked_assets, out_row, *inputs, **params)
out[idx][out_mask] = out_row
return out
|
python
|
def _compute(self, windows, dates, assets, mask):
"""
Call the user's `compute` function on each window with a pre-built
output array.
"""
format_inputs = self._format_inputs
compute = self.compute
params = self.params
ndim = self.ndim
shape = (len(mask), 1) if ndim == 1 else mask.shape
out = self._allocate_output(windows, shape)
with self.ctx:
for idx, date in enumerate(dates):
# Never apply a mask to 1D outputs.
out_mask = array([True]) if ndim == 1 else mask[idx]
# Mask our inputs as usual.
inputs_mask = mask[idx]
masked_assets = assets[inputs_mask]
out_row = out[idx][out_mask]
inputs = format_inputs(windows, inputs_mask)
compute(date, masked_assets, out_row, *inputs, **params)
out[idx][out_mask] = out_row
return out
|
[
"def",
"_compute",
"(",
"self",
",",
"windows",
",",
"dates",
",",
"assets",
",",
"mask",
")",
":",
"format_inputs",
"=",
"self",
".",
"_format_inputs",
"compute",
"=",
"self",
".",
"compute",
"params",
"=",
"self",
".",
"params",
"ndim",
"=",
"self",
".",
"ndim",
"shape",
"=",
"(",
"len",
"(",
"mask",
")",
",",
"1",
")",
"if",
"ndim",
"==",
"1",
"else",
"mask",
".",
"shape",
"out",
"=",
"self",
".",
"_allocate_output",
"(",
"windows",
",",
"shape",
")",
"with",
"self",
".",
"ctx",
":",
"for",
"idx",
",",
"date",
"in",
"enumerate",
"(",
"dates",
")",
":",
"# Never apply a mask to 1D outputs.",
"out_mask",
"=",
"array",
"(",
"[",
"True",
"]",
")",
"if",
"ndim",
"==",
"1",
"else",
"mask",
"[",
"idx",
"]",
"# Mask our inputs as usual.",
"inputs_mask",
"=",
"mask",
"[",
"idx",
"]",
"masked_assets",
"=",
"assets",
"[",
"inputs_mask",
"]",
"out_row",
"=",
"out",
"[",
"idx",
"]",
"[",
"out_mask",
"]",
"inputs",
"=",
"format_inputs",
"(",
"windows",
",",
"inputs_mask",
")",
"compute",
"(",
"date",
",",
"masked_assets",
",",
"out_row",
",",
"*",
"inputs",
",",
"*",
"*",
"params",
")",
"out",
"[",
"idx",
"]",
"[",
"out_mask",
"]",
"=",
"out_row",
"return",
"out"
] |
Call the user's `compute` function on each window with a pre-built
output array.
|
[
"Call",
"the",
"user",
"s",
"compute",
"function",
"on",
"each",
"window",
"with",
"a",
"pre",
"-",
"built",
"output",
"array",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/mixins.py#L193-L220
|
train
|
quantopian/zipline
|
zipline/pipeline/mixins.py
|
AliasedMixin.make_aliased_type
|
def make_aliased_type(cls, other_base):
"""
Factory for making Aliased{Filter,Factor,Classifier}.
"""
docstring = dedent(
"""
A {t} that names another {t}.
Parameters
----------
term : {t}
{{name}}
"""
).format(t=other_base.__name__)
doc = format_docstring(
owner_name=other_base.__name__,
docstring=docstring,
formatters={'name': PIPELINE_ALIAS_NAME_DOC},
)
return type(
'Aliased' + other_base.__name__,
(cls, other_base),
{'__doc__': doc,
'__module__': other_base.__module__},
)
|
python
|
def make_aliased_type(cls, other_base):
"""
Factory for making Aliased{Filter,Factor,Classifier}.
"""
docstring = dedent(
"""
A {t} that names another {t}.
Parameters
----------
term : {t}
{{name}}
"""
).format(t=other_base.__name__)
doc = format_docstring(
owner_name=other_base.__name__,
docstring=docstring,
formatters={'name': PIPELINE_ALIAS_NAME_DOC},
)
return type(
'Aliased' + other_base.__name__,
(cls, other_base),
{'__doc__': doc,
'__module__': other_base.__module__},
)
|
[
"def",
"make_aliased_type",
"(",
"cls",
",",
"other_base",
")",
":",
"docstring",
"=",
"dedent",
"(",
"\"\"\"\n A {t} that names another {t}.\n\n Parameters\n ----------\n term : {t}\n {{name}}\n \"\"\"",
")",
".",
"format",
"(",
"t",
"=",
"other_base",
".",
"__name__",
")",
"doc",
"=",
"format_docstring",
"(",
"owner_name",
"=",
"other_base",
".",
"__name__",
",",
"docstring",
"=",
"docstring",
",",
"formatters",
"=",
"{",
"'name'",
":",
"PIPELINE_ALIAS_NAME_DOC",
"}",
",",
")",
"return",
"type",
"(",
"'Aliased'",
"+",
"other_base",
".",
"__name__",
",",
"(",
"cls",
",",
"other_base",
")",
",",
"{",
"'__doc__'",
":",
"doc",
",",
"'__module__'",
":",
"other_base",
".",
"__module__",
"}",
",",
")"
] |
Factory for making Aliased{Filter,Factor,Classifier}.
|
[
"Factory",
"for",
"making",
"Aliased",
"{",
"Filter",
"Factor",
"Classifier",
"}",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/mixins.py#L297-L323
|
train
|
quantopian/zipline
|
zipline/pipeline/mixins.py
|
DownsampledMixin.compute_extra_rows
|
def compute_extra_rows(self,
all_dates,
start_date,
end_date,
min_extra_rows):
"""
Ensure that min_extra_rows pushes us back to a computation date.
Parameters
----------
all_dates : pd.DatetimeIndex
The trading sessions against which ``self`` will be computed.
start_date : pd.Timestamp
The first date for which final output is requested.
end_date : pd.Timestamp
The last date for which final output is requested.
min_extra_rows : int
The minimum number of extra rows required of ``self``, as
determined by other terms that depend on ``self``.
Returns
-------
extra_rows : int
The number of extra rows to compute. This will be the minimum
number of rows required to make our computed start_date fall on a
recomputation date.
"""
try:
current_start_pos = all_dates.get_loc(start_date) - min_extra_rows
if current_start_pos < 0:
raise NoFurtherDataError.from_lookback_window(
initial_message="Insufficient data to compute Pipeline:",
first_date=all_dates[0],
lookback_start=start_date,
lookback_length=min_extra_rows,
)
except KeyError:
before, after = nearest_unequal_elements(all_dates, start_date)
raise ValueError(
"Pipeline start_date {start_date} is not in calendar.\n"
"Latest date before start_date is {before}.\n"
"Earliest date after start_date is {after}.".format(
start_date=start_date,
before=before,
after=after,
)
)
# Our possible target dates are all the dates on or before the current
# starting position.
# TODO: Consider bounding this below by self.window_length
candidates = all_dates[:current_start_pos + 1]
# Choose the latest date in the candidates that is the start of a new
# period at our frequency.
choices = select_sampling_indices(candidates, self._frequency)
# If we have choices, the last choice is the first date if the
# period containing current_start_date. Choose it.
new_start_date = candidates[choices[-1]]
# Add the difference between the new and old start dates to get the
# number of rows for the new start_date.
new_start_pos = all_dates.get_loc(new_start_date)
assert new_start_pos <= current_start_pos, \
"Computed negative extra rows!"
return min_extra_rows + (current_start_pos - new_start_pos)
|
python
|
def compute_extra_rows(self,
all_dates,
start_date,
end_date,
min_extra_rows):
"""
Ensure that min_extra_rows pushes us back to a computation date.
Parameters
----------
all_dates : pd.DatetimeIndex
The trading sessions against which ``self`` will be computed.
start_date : pd.Timestamp
The first date for which final output is requested.
end_date : pd.Timestamp
The last date for which final output is requested.
min_extra_rows : int
The minimum number of extra rows required of ``self``, as
determined by other terms that depend on ``self``.
Returns
-------
extra_rows : int
The number of extra rows to compute. This will be the minimum
number of rows required to make our computed start_date fall on a
recomputation date.
"""
try:
current_start_pos = all_dates.get_loc(start_date) - min_extra_rows
if current_start_pos < 0:
raise NoFurtherDataError.from_lookback_window(
initial_message="Insufficient data to compute Pipeline:",
first_date=all_dates[0],
lookback_start=start_date,
lookback_length=min_extra_rows,
)
except KeyError:
before, after = nearest_unequal_elements(all_dates, start_date)
raise ValueError(
"Pipeline start_date {start_date} is not in calendar.\n"
"Latest date before start_date is {before}.\n"
"Earliest date after start_date is {after}.".format(
start_date=start_date,
before=before,
after=after,
)
)
# Our possible target dates are all the dates on or before the current
# starting position.
# TODO: Consider bounding this below by self.window_length
candidates = all_dates[:current_start_pos + 1]
# Choose the latest date in the candidates that is the start of a new
# period at our frequency.
choices = select_sampling_indices(candidates, self._frequency)
# If we have choices, the last choice is the first date if the
# period containing current_start_date. Choose it.
new_start_date = candidates[choices[-1]]
# Add the difference between the new and old start dates to get the
# number of rows for the new start_date.
new_start_pos = all_dates.get_loc(new_start_date)
assert new_start_pos <= current_start_pos, \
"Computed negative extra rows!"
return min_extra_rows + (current_start_pos - new_start_pos)
|
[
"def",
"compute_extra_rows",
"(",
"self",
",",
"all_dates",
",",
"start_date",
",",
"end_date",
",",
"min_extra_rows",
")",
":",
"try",
":",
"current_start_pos",
"=",
"all_dates",
".",
"get_loc",
"(",
"start_date",
")",
"-",
"min_extra_rows",
"if",
"current_start_pos",
"<",
"0",
":",
"raise",
"NoFurtherDataError",
".",
"from_lookback_window",
"(",
"initial_message",
"=",
"\"Insufficient data to compute Pipeline:\"",
",",
"first_date",
"=",
"all_dates",
"[",
"0",
"]",
",",
"lookback_start",
"=",
"start_date",
",",
"lookback_length",
"=",
"min_extra_rows",
",",
")",
"except",
"KeyError",
":",
"before",
",",
"after",
"=",
"nearest_unequal_elements",
"(",
"all_dates",
",",
"start_date",
")",
"raise",
"ValueError",
"(",
"\"Pipeline start_date {start_date} is not in calendar.\\n\"",
"\"Latest date before start_date is {before}.\\n\"",
"\"Earliest date after start_date is {after}.\"",
".",
"format",
"(",
"start_date",
"=",
"start_date",
",",
"before",
"=",
"before",
",",
"after",
"=",
"after",
",",
")",
")",
"# Our possible target dates are all the dates on or before the current",
"# starting position.",
"# TODO: Consider bounding this below by self.window_length",
"candidates",
"=",
"all_dates",
"[",
":",
"current_start_pos",
"+",
"1",
"]",
"# Choose the latest date in the candidates that is the start of a new",
"# period at our frequency.",
"choices",
"=",
"select_sampling_indices",
"(",
"candidates",
",",
"self",
".",
"_frequency",
")",
"# If we have choices, the last choice is the first date if the",
"# period containing current_start_date. Choose it.",
"new_start_date",
"=",
"candidates",
"[",
"choices",
"[",
"-",
"1",
"]",
"]",
"# Add the difference between the new and old start dates to get the",
"# number of rows for the new start_date.",
"new_start_pos",
"=",
"all_dates",
".",
"get_loc",
"(",
"new_start_date",
")",
"assert",
"new_start_pos",
"<=",
"current_start_pos",
",",
"\"Computed negative extra rows!\"",
"return",
"min_extra_rows",
"+",
"(",
"current_start_pos",
"-",
"new_start_pos",
")"
] |
Ensure that min_extra_rows pushes us back to a computation date.
Parameters
----------
all_dates : pd.DatetimeIndex
The trading sessions against which ``self`` will be computed.
start_date : pd.Timestamp
The first date for which final output is requested.
end_date : pd.Timestamp
The last date for which final output is requested.
min_extra_rows : int
The minimum number of extra rows required of ``self``, as
determined by other terms that depend on ``self``.
Returns
-------
extra_rows : int
The number of extra rows to compute. This will be the minimum
number of rows required to make our computed start_date fall on a
recomputation date.
|
[
"Ensure",
"that",
"min_extra_rows",
"pushes",
"us",
"back",
"to",
"a",
"computation",
"date",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/mixins.py#L370-L437
|
train
|
quantopian/zipline
|
zipline/pipeline/mixins.py
|
DownsampledMixin._compute
|
def _compute(self, inputs, dates, assets, mask):
"""
Compute by delegating to self._wrapped_term._compute on sample dates.
On non-sample dates, forward-fill from previously-computed samples.
"""
to_sample = dates[select_sampling_indices(dates, self._frequency)]
assert to_sample[0] == dates[0], \
"Misaligned sampling dates in %s." % type(self).__name__
real_compute = self._wrapped_term._compute
# Inputs will contain different kinds of values depending on whether or
# not we're a windowed computation.
# If we're windowed, then `inputs` is a list of iterators of ndarrays.
# If we're not windowed, then `inputs` is just a list of ndarrays.
# There are two things we care about doing with the input:
# 1. Preparing an input to be passed to our wrapped term.
# 2. Skipping an input if we're going to use an already-computed row.
# We perform these actions differently based on the expected kind of
# input, and we encapsulate these actions with closures so that we
# don't clutter the code below with lots of branching.
if self.windowed:
# If we're windowed, inputs are stateful AdjustedArrays. We don't
# need to do any preparation before forwarding to real_compute, but
# we need to call `next` on them if we want to skip an iteration.
def prepare_inputs():
return inputs
def skip_this_input():
for w in inputs:
next(w)
else:
# If we're not windowed, inputs are just ndarrays. We need to
# slice out a single row when forwarding to real_compute, but we
# don't need to do anything to skip an input.
def prepare_inputs():
# i is the loop iteration variable below.
return [a[[i]] for a in inputs]
def skip_this_input():
pass
results = []
samples = iter(to_sample)
next_sample = next(samples)
for i, compute_date in enumerate(dates):
if next_sample == compute_date:
results.append(
real_compute(
prepare_inputs(),
dates[i:i + 1],
assets,
mask[i:i + 1],
)
)
try:
next_sample = next(samples)
except StopIteration:
# No more samples to take. Set next_sample to Nat, which
# compares False with any other datetime.
next_sample = pd_NaT
else:
skip_this_input()
# Copy results from previous sample period.
results.append(results[-1])
# We should have exhausted our sample dates.
try:
next_sample = next(samples)
except StopIteration:
pass
else:
raise AssertionError("Unconsumed sample date: %s" % next_sample)
# Concatenate stored results.
return vstack(results)
|
python
|
def _compute(self, inputs, dates, assets, mask):
"""
Compute by delegating to self._wrapped_term._compute on sample dates.
On non-sample dates, forward-fill from previously-computed samples.
"""
to_sample = dates[select_sampling_indices(dates, self._frequency)]
assert to_sample[0] == dates[0], \
"Misaligned sampling dates in %s." % type(self).__name__
real_compute = self._wrapped_term._compute
# Inputs will contain different kinds of values depending on whether or
# not we're a windowed computation.
# If we're windowed, then `inputs` is a list of iterators of ndarrays.
# If we're not windowed, then `inputs` is just a list of ndarrays.
# There are two things we care about doing with the input:
# 1. Preparing an input to be passed to our wrapped term.
# 2. Skipping an input if we're going to use an already-computed row.
# We perform these actions differently based on the expected kind of
# input, and we encapsulate these actions with closures so that we
# don't clutter the code below with lots of branching.
if self.windowed:
# If we're windowed, inputs are stateful AdjustedArrays. We don't
# need to do any preparation before forwarding to real_compute, but
# we need to call `next` on them if we want to skip an iteration.
def prepare_inputs():
return inputs
def skip_this_input():
for w in inputs:
next(w)
else:
# If we're not windowed, inputs are just ndarrays. We need to
# slice out a single row when forwarding to real_compute, but we
# don't need to do anything to skip an input.
def prepare_inputs():
# i is the loop iteration variable below.
return [a[[i]] for a in inputs]
def skip_this_input():
pass
results = []
samples = iter(to_sample)
next_sample = next(samples)
for i, compute_date in enumerate(dates):
if next_sample == compute_date:
results.append(
real_compute(
prepare_inputs(),
dates[i:i + 1],
assets,
mask[i:i + 1],
)
)
try:
next_sample = next(samples)
except StopIteration:
# No more samples to take. Set next_sample to Nat, which
# compares False with any other datetime.
next_sample = pd_NaT
else:
skip_this_input()
# Copy results from previous sample period.
results.append(results[-1])
# We should have exhausted our sample dates.
try:
next_sample = next(samples)
except StopIteration:
pass
else:
raise AssertionError("Unconsumed sample date: %s" % next_sample)
# Concatenate stored results.
return vstack(results)
|
[
"def",
"_compute",
"(",
"self",
",",
"inputs",
",",
"dates",
",",
"assets",
",",
"mask",
")",
":",
"to_sample",
"=",
"dates",
"[",
"select_sampling_indices",
"(",
"dates",
",",
"self",
".",
"_frequency",
")",
"]",
"assert",
"to_sample",
"[",
"0",
"]",
"==",
"dates",
"[",
"0",
"]",
",",
"\"Misaligned sampling dates in %s.\"",
"%",
"type",
"(",
"self",
")",
".",
"__name__",
"real_compute",
"=",
"self",
".",
"_wrapped_term",
".",
"_compute",
"# Inputs will contain different kinds of values depending on whether or",
"# not we're a windowed computation.",
"# If we're windowed, then `inputs` is a list of iterators of ndarrays.",
"# If we're not windowed, then `inputs` is just a list of ndarrays.",
"# There are two things we care about doing with the input:",
"# 1. Preparing an input to be passed to our wrapped term.",
"# 2. Skipping an input if we're going to use an already-computed row.",
"# We perform these actions differently based on the expected kind of",
"# input, and we encapsulate these actions with closures so that we",
"# don't clutter the code below with lots of branching.",
"if",
"self",
".",
"windowed",
":",
"# If we're windowed, inputs are stateful AdjustedArrays. We don't",
"# need to do any preparation before forwarding to real_compute, but",
"# we need to call `next` on them if we want to skip an iteration.",
"def",
"prepare_inputs",
"(",
")",
":",
"return",
"inputs",
"def",
"skip_this_input",
"(",
")",
":",
"for",
"w",
"in",
"inputs",
":",
"next",
"(",
"w",
")",
"else",
":",
"# If we're not windowed, inputs are just ndarrays. We need to",
"# slice out a single row when forwarding to real_compute, but we",
"# don't need to do anything to skip an input.",
"def",
"prepare_inputs",
"(",
")",
":",
"# i is the loop iteration variable below.",
"return",
"[",
"a",
"[",
"[",
"i",
"]",
"]",
"for",
"a",
"in",
"inputs",
"]",
"def",
"skip_this_input",
"(",
")",
":",
"pass",
"results",
"=",
"[",
"]",
"samples",
"=",
"iter",
"(",
"to_sample",
")",
"next_sample",
"=",
"next",
"(",
"samples",
")",
"for",
"i",
",",
"compute_date",
"in",
"enumerate",
"(",
"dates",
")",
":",
"if",
"next_sample",
"==",
"compute_date",
":",
"results",
".",
"append",
"(",
"real_compute",
"(",
"prepare_inputs",
"(",
")",
",",
"dates",
"[",
"i",
":",
"i",
"+",
"1",
"]",
",",
"assets",
",",
"mask",
"[",
"i",
":",
"i",
"+",
"1",
"]",
",",
")",
")",
"try",
":",
"next_sample",
"=",
"next",
"(",
"samples",
")",
"except",
"StopIteration",
":",
"# No more samples to take. Set next_sample to Nat, which",
"# compares False with any other datetime.",
"next_sample",
"=",
"pd_NaT",
"else",
":",
"skip_this_input",
"(",
")",
"# Copy results from previous sample period.",
"results",
".",
"append",
"(",
"results",
"[",
"-",
"1",
"]",
")",
"# We should have exhausted our sample dates.",
"try",
":",
"next_sample",
"=",
"next",
"(",
"samples",
")",
"except",
"StopIteration",
":",
"pass",
"else",
":",
"raise",
"AssertionError",
"(",
"\"Unconsumed sample date: %s\"",
"%",
"next_sample",
")",
"# Concatenate stored results.",
"return",
"vstack",
"(",
"results",
")"
] |
Compute by delegating to self._wrapped_term._compute on sample dates.
On non-sample dates, forward-fill from previously-computed samples.
|
[
"Compute",
"by",
"delegating",
"to",
"self",
".",
"_wrapped_term",
".",
"_compute",
"on",
"sample",
"dates",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/mixins.py#L439-L516
|
train
|
quantopian/zipline
|
zipline/pipeline/mixins.py
|
DownsampledMixin.make_downsampled_type
|
def make_downsampled_type(cls, other_base):
"""
Factory for making Downsampled{Filter,Factor,Classifier}.
"""
docstring = dedent(
"""
A {t} that defers to another {t} at lower-than-daily frequency.
Parameters
----------
term : {t}
{{frequency}}
"""
).format(t=other_base.__name__)
doc = format_docstring(
owner_name=other_base.__name__,
docstring=docstring,
formatters={'frequency': PIPELINE_DOWNSAMPLING_FREQUENCY_DOC},
)
return type(
'Downsampled' + other_base.__name__,
(cls, other_base,),
{'__doc__': doc,
'__module__': other_base.__module__},
)
|
python
|
def make_downsampled_type(cls, other_base):
"""
Factory for making Downsampled{Filter,Factor,Classifier}.
"""
docstring = dedent(
"""
A {t} that defers to another {t} at lower-than-daily frequency.
Parameters
----------
term : {t}
{{frequency}}
"""
).format(t=other_base.__name__)
doc = format_docstring(
owner_name=other_base.__name__,
docstring=docstring,
formatters={'frequency': PIPELINE_DOWNSAMPLING_FREQUENCY_DOC},
)
return type(
'Downsampled' + other_base.__name__,
(cls, other_base,),
{'__doc__': doc,
'__module__': other_base.__module__},
)
|
[
"def",
"make_downsampled_type",
"(",
"cls",
",",
"other_base",
")",
":",
"docstring",
"=",
"dedent",
"(",
"\"\"\"\n A {t} that defers to another {t} at lower-than-daily frequency.\n\n Parameters\n ----------\n term : {t}\n {{frequency}}\n \"\"\"",
")",
".",
"format",
"(",
"t",
"=",
"other_base",
".",
"__name__",
")",
"doc",
"=",
"format_docstring",
"(",
"owner_name",
"=",
"other_base",
".",
"__name__",
",",
"docstring",
"=",
"docstring",
",",
"formatters",
"=",
"{",
"'frequency'",
":",
"PIPELINE_DOWNSAMPLING_FREQUENCY_DOC",
"}",
",",
")",
"return",
"type",
"(",
"'Downsampled'",
"+",
"other_base",
".",
"__name__",
",",
"(",
"cls",
",",
"other_base",
",",
")",
",",
"{",
"'__doc__'",
":",
"doc",
",",
"'__module__'",
":",
"other_base",
".",
"__module__",
"}",
",",
")"
] |
Factory for making Downsampled{Filter,Factor,Classifier}.
|
[
"Factory",
"for",
"making",
"Downsampled",
"{",
"Filter",
"Factor",
"Classifier",
"}",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/mixins.py#L519-L545
|
train
|
quantopian/zipline
|
zipline/utils/preprocess.py
|
preprocess
|
def preprocess(*_unused, **processors):
"""
Decorator that applies pre-processors to the arguments of a function before
calling the function.
Parameters
----------
**processors : dict
Map from argument name -> processor function.
A processor function takes three arguments: (func, argname, argvalue).
`func` is the the function for which we're processing args.
`argname` is the name of the argument we're processing.
`argvalue` is the value of the argument we're processing.
Examples
--------
>>> def _ensure_tuple(func, argname, arg):
... if isinstance(arg, tuple):
... return argvalue
... try:
... return tuple(arg)
... except TypeError:
... raise TypeError(
... "%s() expected argument '%s' to"
... " be iterable, but got %s instead." % (
... func.__name__, argname, arg,
... )
... )
...
>>> @preprocess(arg=_ensure_tuple)
... def foo(arg):
... return arg
...
>>> foo([1, 2, 3])
(1, 2, 3)
>>> foo("a")
('a',)
>>> foo(2)
Traceback (most recent call last):
...
TypeError: foo() expected argument 'arg' to be iterable, but got 2 instead.
"""
if _unused:
raise TypeError("preprocess() doesn't accept positional arguments")
def _decorator(f):
args, varargs, varkw, defaults = argspec = getargspec(f)
if defaults is None:
defaults = ()
no_defaults = (NO_DEFAULT,) * (len(args) - len(defaults))
args_defaults = list(zip(args, no_defaults + defaults))
if varargs:
args_defaults.append((varargs, NO_DEFAULT))
if varkw:
args_defaults.append((varkw, NO_DEFAULT))
argset = set(args) | {varargs, varkw} - {None}
# Arguments can be declared as tuples in Python 2.
if not all(isinstance(arg, str) for arg in args):
raise TypeError(
"Can't validate functions using tuple unpacking: %s" %
(argspec,)
)
# Ensure that all processors map to valid names.
bad_names = viewkeys(processors) - argset
if bad_names:
raise TypeError(
"Got processors for unknown arguments: %s." % bad_names
)
return _build_preprocessed_function(
f, processors, args_defaults, varargs, varkw,
)
return _decorator
|
python
|
def preprocess(*_unused, **processors):
"""
Decorator that applies pre-processors to the arguments of a function before
calling the function.
Parameters
----------
**processors : dict
Map from argument name -> processor function.
A processor function takes three arguments: (func, argname, argvalue).
`func` is the the function for which we're processing args.
`argname` is the name of the argument we're processing.
`argvalue` is the value of the argument we're processing.
Examples
--------
>>> def _ensure_tuple(func, argname, arg):
... if isinstance(arg, tuple):
... return argvalue
... try:
... return tuple(arg)
... except TypeError:
... raise TypeError(
... "%s() expected argument '%s' to"
... " be iterable, but got %s instead." % (
... func.__name__, argname, arg,
... )
... )
...
>>> @preprocess(arg=_ensure_tuple)
... def foo(arg):
... return arg
...
>>> foo([1, 2, 3])
(1, 2, 3)
>>> foo("a")
('a',)
>>> foo(2)
Traceback (most recent call last):
...
TypeError: foo() expected argument 'arg' to be iterable, but got 2 instead.
"""
if _unused:
raise TypeError("preprocess() doesn't accept positional arguments")
def _decorator(f):
args, varargs, varkw, defaults = argspec = getargspec(f)
if defaults is None:
defaults = ()
no_defaults = (NO_DEFAULT,) * (len(args) - len(defaults))
args_defaults = list(zip(args, no_defaults + defaults))
if varargs:
args_defaults.append((varargs, NO_DEFAULT))
if varkw:
args_defaults.append((varkw, NO_DEFAULT))
argset = set(args) | {varargs, varkw} - {None}
# Arguments can be declared as tuples in Python 2.
if not all(isinstance(arg, str) for arg in args):
raise TypeError(
"Can't validate functions using tuple unpacking: %s" %
(argspec,)
)
# Ensure that all processors map to valid names.
bad_names = viewkeys(processors) - argset
if bad_names:
raise TypeError(
"Got processors for unknown arguments: %s." % bad_names
)
return _build_preprocessed_function(
f, processors, args_defaults, varargs, varkw,
)
return _decorator
|
[
"def",
"preprocess",
"(",
"*",
"_unused",
",",
"*",
"*",
"processors",
")",
":",
"if",
"_unused",
":",
"raise",
"TypeError",
"(",
"\"preprocess() doesn't accept positional arguments\"",
")",
"def",
"_decorator",
"(",
"f",
")",
":",
"args",
",",
"varargs",
",",
"varkw",
",",
"defaults",
"=",
"argspec",
"=",
"getargspec",
"(",
"f",
")",
"if",
"defaults",
"is",
"None",
":",
"defaults",
"=",
"(",
")",
"no_defaults",
"=",
"(",
"NO_DEFAULT",
",",
")",
"*",
"(",
"len",
"(",
"args",
")",
"-",
"len",
"(",
"defaults",
")",
")",
"args_defaults",
"=",
"list",
"(",
"zip",
"(",
"args",
",",
"no_defaults",
"+",
"defaults",
")",
")",
"if",
"varargs",
":",
"args_defaults",
".",
"append",
"(",
"(",
"varargs",
",",
"NO_DEFAULT",
")",
")",
"if",
"varkw",
":",
"args_defaults",
".",
"append",
"(",
"(",
"varkw",
",",
"NO_DEFAULT",
")",
")",
"argset",
"=",
"set",
"(",
"args",
")",
"|",
"{",
"varargs",
",",
"varkw",
"}",
"-",
"{",
"None",
"}",
"# Arguments can be declared as tuples in Python 2.",
"if",
"not",
"all",
"(",
"isinstance",
"(",
"arg",
",",
"str",
")",
"for",
"arg",
"in",
"args",
")",
":",
"raise",
"TypeError",
"(",
"\"Can't validate functions using tuple unpacking: %s\"",
"%",
"(",
"argspec",
",",
")",
")",
"# Ensure that all processors map to valid names.",
"bad_names",
"=",
"viewkeys",
"(",
"processors",
")",
"-",
"argset",
"if",
"bad_names",
":",
"raise",
"TypeError",
"(",
"\"Got processors for unknown arguments: %s.\"",
"%",
"bad_names",
")",
"return",
"_build_preprocessed_function",
"(",
"f",
",",
"processors",
",",
"args_defaults",
",",
"varargs",
",",
"varkw",
",",
")",
"return",
"_decorator"
] |
Decorator that applies pre-processors to the arguments of a function before
calling the function.
Parameters
----------
**processors : dict
Map from argument name -> processor function.
A processor function takes three arguments: (func, argname, argvalue).
`func` is the the function for which we're processing args.
`argname` is the name of the argument we're processing.
`argvalue` is the value of the argument we're processing.
Examples
--------
>>> def _ensure_tuple(func, argname, arg):
... if isinstance(arg, tuple):
... return argvalue
... try:
... return tuple(arg)
... except TypeError:
... raise TypeError(
... "%s() expected argument '%s' to"
... " be iterable, but got %s instead." % (
... func.__name__, argname, arg,
... )
... )
...
>>> @preprocess(arg=_ensure_tuple)
... def foo(arg):
... return arg
...
>>> foo([1, 2, 3])
(1, 2, 3)
>>> foo("a")
('a',)
>>> foo(2)
Traceback (most recent call last):
...
TypeError: foo() expected argument 'arg' to be iterable, but got 2 instead.
|
[
"Decorator",
"that",
"applies",
"pre",
"-",
"processors",
"to",
"the",
"arguments",
"of",
"a",
"function",
"before",
"calling",
"the",
"function",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/preprocess.py#L35-L112
|
train
|
quantopian/zipline
|
zipline/utils/preprocess.py
|
call
|
def call(f):
"""
Wrap a function in a processor that calls `f` on the argument before
passing it along.
Useful for creating simple arguments to the `@preprocess` decorator.
Parameters
----------
f : function
Function accepting a single argument and returning a replacement.
Examples
--------
>>> @preprocess(x=call(lambda x: x + 1))
... def foo(x):
... return x
...
>>> foo(1)
2
"""
@wraps(f)
def processor(func, argname, arg):
return f(arg)
return processor
|
python
|
def call(f):
"""
Wrap a function in a processor that calls `f` on the argument before
passing it along.
Useful for creating simple arguments to the `@preprocess` decorator.
Parameters
----------
f : function
Function accepting a single argument and returning a replacement.
Examples
--------
>>> @preprocess(x=call(lambda x: x + 1))
... def foo(x):
... return x
...
>>> foo(1)
2
"""
@wraps(f)
def processor(func, argname, arg):
return f(arg)
return processor
|
[
"def",
"call",
"(",
"f",
")",
":",
"@",
"wraps",
"(",
"f",
")",
"def",
"processor",
"(",
"func",
",",
"argname",
",",
"arg",
")",
":",
"return",
"f",
"(",
"arg",
")",
"return",
"processor"
] |
Wrap a function in a processor that calls `f` on the argument before
passing it along.
Useful for creating simple arguments to the `@preprocess` decorator.
Parameters
----------
f : function
Function accepting a single argument and returning a replacement.
Examples
--------
>>> @preprocess(x=call(lambda x: x + 1))
... def foo(x):
... return x
...
>>> foo(1)
2
|
[
"Wrap",
"a",
"function",
"in",
"a",
"processor",
"that",
"calls",
"f",
"on",
"the",
"argument",
"before",
"passing",
"it",
"along",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/preprocess.py#L115-L139
|
train
|
quantopian/zipline
|
zipline/utils/preprocess.py
|
_build_preprocessed_function
|
def _build_preprocessed_function(func,
processors,
args_defaults,
varargs,
varkw):
"""
Build a preprocessed function with the same signature as `func`.
Uses `exec` internally to build a function that actually has the same
signature as `func.
"""
format_kwargs = {'func_name': func.__name__}
def mangle(name):
return 'a' + uuid4().hex + name
format_kwargs['mangled_func'] = mangled_funcname = mangle(func.__name__)
def make_processor_assignment(arg, processor_name):
template = "{arg} = {processor}({func}, '{arg}', {arg})"
return template.format(
arg=arg,
processor=processor_name,
func=mangled_funcname,
)
exec_globals = {mangled_funcname: func, 'wraps': wraps}
defaults_seen = 0
default_name_template = 'a' + uuid4().hex + '_%d'
signature = []
call_args = []
assignments = []
star_map = {
varargs: '*',
varkw: '**',
}
def name_as_arg(arg):
return star_map.get(arg, '') + arg
for arg, default in args_defaults:
if default is NO_DEFAULT:
signature.append(name_as_arg(arg))
else:
default_name = default_name_template % defaults_seen
exec_globals[default_name] = default
signature.append('='.join([name_as_arg(arg), default_name]))
defaults_seen += 1
if arg in processors:
procname = mangle('_processor_' + arg)
exec_globals[procname] = processors[arg]
assignments.append(make_processor_assignment(arg, procname))
call_args.append(name_as_arg(arg))
exec_str = dedent(
"""\
@wraps({wrapped_funcname})
def {func_name}({signature}):
{assignments}
return {wrapped_funcname}({call_args})
"""
).format(
func_name=func.__name__,
signature=', '.join(signature),
assignments='\n '.join(assignments),
wrapped_funcname=mangled_funcname,
call_args=', '.join(call_args),
)
compiled = compile(
exec_str,
func.__code__.co_filename,
mode='exec',
)
exec_locals = {}
exec_(compiled, exec_globals, exec_locals)
new_func = exec_locals[func.__name__]
code = new_func.__code__
args = {
attr: getattr(code, attr)
for attr in dir(code)
if attr.startswith('co_')
}
# Copy the firstlineno out of the underlying function so that exceptions
# get raised with the correct traceback.
# This also makes dynamic source inspection (like IPython `??` operator)
# work as intended.
try:
# Try to get the pycode object from the underlying function.
original_code = func.__code__
except AttributeError:
try:
# The underlying callable was not a function, try to grab the
# `__func__.__code__` which exists on method objects.
original_code = func.__func__.__code__
except AttributeError:
# The underlying callable does not have a `__code__`. There is
# nothing for us to correct.
return new_func
args['co_firstlineno'] = original_code.co_firstlineno
new_func.__code__ = CodeType(*map(getitem(args), _code_argorder))
return new_func
|
python
|
def _build_preprocessed_function(func,
processors,
args_defaults,
varargs,
varkw):
"""
Build a preprocessed function with the same signature as `func`.
Uses `exec` internally to build a function that actually has the same
signature as `func.
"""
format_kwargs = {'func_name': func.__name__}
def mangle(name):
return 'a' + uuid4().hex + name
format_kwargs['mangled_func'] = mangled_funcname = mangle(func.__name__)
def make_processor_assignment(arg, processor_name):
template = "{arg} = {processor}({func}, '{arg}', {arg})"
return template.format(
arg=arg,
processor=processor_name,
func=mangled_funcname,
)
exec_globals = {mangled_funcname: func, 'wraps': wraps}
defaults_seen = 0
default_name_template = 'a' + uuid4().hex + '_%d'
signature = []
call_args = []
assignments = []
star_map = {
varargs: '*',
varkw: '**',
}
def name_as_arg(arg):
return star_map.get(arg, '') + arg
for arg, default in args_defaults:
if default is NO_DEFAULT:
signature.append(name_as_arg(arg))
else:
default_name = default_name_template % defaults_seen
exec_globals[default_name] = default
signature.append('='.join([name_as_arg(arg), default_name]))
defaults_seen += 1
if arg in processors:
procname = mangle('_processor_' + arg)
exec_globals[procname] = processors[arg]
assignments.append(make_processor_assignment(arg, procname))
call_args.append(name_as_arg(arg))
exec_str = dedent(
"""\
@wraps({wrapped_funcname})
def {func_name}({signature}):
{assignments}
return {wrapped_funcname}({call_args})
"""
).format(
func_name=func.__name__,
signature=', '.join(signature),
assignments='\n '.join(assignments),
wrapped_funcname=mangled_funcname,
call_args=', '.join(call_args),
)
compiled = compile(
exec_str,
func.__code__.co_filename,
mode='exec',
)
exec_locals = {}
exec_(compiled, exec_globals, exec_locals)
new_func = exec_locals[func.__name__]
code = new_func.__code__
args = {
attr: getattr(code, attr)
for attr in dir(code)
if attr.startswith('co_')
}
# Copy the firstlineno out of the underlying function so that exceptions
# get raised with the correct traceback.
# This also makes dynamic source inspection (like IPython `??` operator)
# work as intended.
try:
# Try to get the pycode object from the underlying function.
original_code = func.__code__
except AttributeError:
try:
# The underlying callable was not a function, try to grab the
# `__func__.__code__` which exists on method objects.
original_code = func.__func__.__code__
except AttributeError:
# The underlying callable does not have a `__code__`. There is
# nothing for us to correct.
return new_func
args['co_firstlineno'] = original_code.co_firstlineno
new_func.__code__ = CodeType(*map(getitem(args), _code_argorder))
return new_func
|
[
"def",
"_build_preprocessed_function",
"(",
"func",
",",
"processors",
",",
"args_defaults",
",",
"varargs",
",",
"varkw",
")",
":",
"format_kwargs",
"=",
"{",
"'func_name'",
":",
"func",
".",
"__name__",
"}",
"def",
"mangle",
"(",
"name",
")",
":",
"return",
"'a'",
"+",
"uuid4",
"(",
")",
".",
"hex",
"+",
"name",
"format_kwargs",
"[",
"'mangled_func'",
"]",
"=",
"mangled_funcname",
"=",
"mangle",
"(",
"func",
".",
"__name__",
")",
"def",
"make_processor_assignment",
"(",
"arg",
",",
"processor_name",
")",
":",
"template",
"=",
"\"{arg} = {processor}({func}, '{arg}', {arg})\"",
"return",
"template",
".",
"format",
"(",
"arg",
"=",
"arg",
",",
"processor",
"=",
"processor_name",
",",
"func",
"=",
"mangled_funcname",
",",
")",
"exec_globals",
"=",
"{",
"mangled_funcname",
":",
"func",
",",
"'wraps'",
":",
"wraps",
"}",
"defaults_seen",
"=",
"0",
"default_name_template",
"=",
"'a'",
"+",
"uuid4",
"(",
")",
".",
"hex",
"+",
"'_%d'",
"signature",
"=",
"[",
"]",
"call_args",
"=",
"[",
"]",
"assignments",
"=",
"[",
"]",
"star_map",
"=",
"{",
"varargs",
":",
"'*'",
",",
"varkw",
":",
"'**'",
",",
"}",
"def",
"name_as_arg",
"(",
"arg",
")",
":",
"return",
"star_map",
".",
"get",
"(",
"arg",
",",
"''",
")",
"+",
"arg",
"for",
"arg",
",",
"default",
"in",
"args_defaults",
":",
"if",
"default",
"is",
"NO_DEFAULT",
":",
"signature",
".",
"append",
"(",
"name_as_arg",
"(",
"arg",
")",
")",
"else",
":",
"default_name",
"=",
"default_name_template",
"%",
"defaults_seen",
"exec_globals",
"[",
"default_name",
"]",
"=",
"default",
"signature",
".",
"append",
"(",
"'='",
".",
"join",
"(",
"[",
"name_as_arg",
"(",
"arg",
")",
",",
"default_name",
"]",
")",
")",
"defaults_seen",
"+=",
"1",
"if",
"arg",
"in",
"processors",
":",
"procname",
"=",
"mangle",
"(",
"'_processor_'",
"+",
"arg",
")",
"exec_globals",
"[",
"procname",
"]",
"=",
"processors",
"[",
"arg",
"]",
"assignments",
".",
"append",
"(",
"make_processor_assignment",
"(",
"arg",
",",
"procname",
")",
")",
"call_args",
".",
"append",
"(",
"name_as_arg",
"(",
"arg",
")",
")",
"exec_str",
"=",
"dedent",
"(",
"\"\"\"\\\n @wraps({wrapped_funcname})\n def {func_name}({signature}):\n {assignments}\n return {wrapped_funcname}({call_args})\n \"\"\"",
")",
".",
"format",
"(",
"func_name",
"=",
"func",
".",
"__name__",
",",
"signature",
"=",
"', '",
".",
"join",
"(",
"signature",
")",
",",
"assignments",
"=",
"'\\n '",
".",
"join",
"(",
"assignments",
")",
",",
"wrapped_funcname",
"=",
"mangled_funcname",
",",
"call_args",
"=",
"', '",
".",
"join",
"(",
"call_args",
")",
",",
")",
"compiled",
"=",
"compile",
"(",
"exec_str",
",",
"func",
".",
"__code__",
".",
"co_filename",
",",
"mode",
"=",
"'exec'",
",",
")",
"exec_locals",
"=",
"{",
"}",
"exec_",
"(",
"compiled",
",",
"exec_globals",
",",
"exec_locals",
")",
"new_func",
"=",
"exec_locals",
"[",
"func",
".",
"__name__",
"]",
"code",
"=",
"new_func",
".",
"__code__",
"args",
"=",
"{",
"attr",
":",
"getattr",
"(",
"code",
",",
"attr",
")",
"for",
"attr",
"in",
"dir",
"(",
"code",
")",
"if",
"attr",
".",
"startswith",
"(",
"'co_'",
")",
"}",
"# Copy the firstlineno out of the underlying function so that exceptions",
"# get raised with the correct traceback.",
"# This also makes dynamic source inspection (like IPython `??` operator)",
"# work as intended.",
"try",
":",
"# Try to get the pycode object from the underlying function.",
"original_code",
"=",
"func",
".",
"__code__",
"except",
"AttributeError",
":",
"try",
":",
"# The underlying callable was not a function, try to grab the",
"# `__func__.__code__` which exists on method objects.",
"original_code",
"=",
"func",
".",
"__func__",
".",
"__code__",
"except",
"AttributeError",
":",
"# The underlying callable does not have a `__code__`. There is",
"# nothing for us to correct.",
"return",
"new_func",
"args",
"[",
"'co_firstlineno'",
"]",
"=",
"original_code",
".",
"co_firstlineno",
"new_func",
".",
"__code__",
"=",
"CodeType",
"(",
"*",
"map",
"(",
"getitem",
"(",
"args",
")",
",",
"_code_argorder",
")",
")",
"return",
"new_func"
] |
Build a preprocessed function with the same signature as `func`.
Uses `exec` internally to build a function that actually has the same
signature as `func.
|
[
"Build",
"a",
"preprocessed",
"function",
"with",
"the",
"same",
"signature",
"as",
"func",
"."
] |
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
|
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/preprocess.py#L142-L247
|
train
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.