repository_name
stringlengths 7
55
| func_path_in_repository
stringlengths 4
223
| func_name
stringlengths 1
134
| whole_func_string
stringlengths 75
104k
| language
stringclasses 1
value | func_code_string
stringlengths 75
104k
| func_code_tokens
listlengths 19
28.4k
| func_documentation_string
stringlengths 1
46.9k
| func_documentation_tokens
listlengths 1
1.97k
| split_name
stringclasses 1
value | func_code_url
stringlengths 87
315
|
|---|---|---|---|---|---|---|---|---|---|---|
aouyar/PyMunin
|
pysysinfo/filesystem.py
|
FilesystemInfo.getSpaceUse
|
def getSpaceUse(self):
"""Get disk space usage.
@return: Dictionary of filesystem space utilization stats for filesystems.
"""
stats = {}
try:
out = subprocess.Popen([dfCmd, "-Pk"],
stdout=subprocess.PIPE).communicate()[0]
except:
raise Exception('Execution of command %s failed.' % dfCmd)
lines = out.splitlines()
if len(lines) > 1:
for line in lines[1:]:
fsstats = {}
cols = line.split()
fsstats['device'] = cols[0]
fsstats['type'] = self._fstypeDict[cols[5]]
fsstats['total'] = 1024 * int(cols[1])
fsstats['inuse'] = 1024 * int(cols[2])
fsstats['avail'] = 1024 * int(cols[3])
fsstats['inuse_pcent'] = int(cols[4][:-1])
stats[cols[5]] = fsstats
return stats
|
python
|
def getSpaceUse(self):
"""Get disk space usage.
@return: Dictionary of filesystem space utilization stats for filesystems.
"""
stats = {}
try:
out = subprocess.Popen([dfCmd, "-Pk"],
stdout=subprocess.PIPE).communicate()[0]
except:
raise Exception('Execution of command %s failed.' % dfCmd)
lines = out.splitlines()
if len(lines) > 1:
for line in lines[1:]:
fsstats = {}
cols = line.split()
fsstats['device'] = cols[0]
fsstats['type'] = self._fstypeDict[cols[5]]
fsstats['total'] = 1024 * int(cols[1])
fsstats['inuse'] = 1024 * int(cols[2])
fsstats['avail'] = 1024 * int(cols[3])
fsstats['inuse_pcent'] = int(cols[4][:-1])
stats[cols[5]] = fsstats
return stats
|
[
"def",
"getSpaceUse",
"(",
"self",
")",
":",
"stats",
"=",
"{",
"}",
"try",
":",
"out",
"=",
"subprocess",
".",
"Popen",
"(",
"[",
"dfCmd",
",",
"\"-Pk\"",
"]",
",",
"stdout",
"=",
"subprocess",
".",
"PIPE",
")",
".",
"communicate",
"(",
")",
"[",
"0",
"]",
"except",
":",
"raise",
"Exception",
"(",
"'Execution of command %s failed.'",
"%",
"dfCmd",
")",
"lines",
"=",
"out",
".",
"splitlines",
"(",
")",
"if",
"len",
"(",
"lines",
")",
">",
"1",
":",
"for",
"line",
"in",
"lines",
"[",
"1",
":",
"]",
":",
"fsstats",
"=",
"{",
"}",
"cols",
"=",
"line",
".",
"split",
"(",
")",
"fsstats",
"[",
"'device'",
"]",
"=",
"cols",
"[",
"0",
"]",
"fsstats",
"[",
"'type'",
"]",
"=",
"self",
".",
"_fstypeDict",
"[",
"cols",
"[",
"5",
"]",
"]",
"fsstats",
"[",
"'total'",
"]",
"=",
"1024",
"*",
"int",
"(",
"cols",
"[",
"1",
"]",
")",
"fsstats",
"[",
"'inuse'",
"]",
"=",
"1024",
"*",
"int",
"(",
"cols",
"[",
"2",
"]",
")",
"fsstats",
"[",
"'avail'",
"]",
"=",
"1024",
"*",
"int",
"(",
"cols",
"[",
"3",
"]",
")",
"fsstats",
"[",
"'inuse_pcent'",
"]",
"=",
"int",
"(",
"cols",
"[",
"4",
"]",
"[",
":",
"-",
"1",
"]",
")",
"stats",
"[",
"cols",
"[",
"5",
"]",
"]",
"=",
"fsstats",
"return",
"stats"
] |
Get disk space usage.
@return: Dictionary of filesystem space utilization stats for filesystems.
|
[
"Get",
"disk",
"space",
"usage",
"."
] |
train
|
https://github.com/aouyar/PyMunin/blob/4f58a64b6b37c85a84cc7e1e07aafaa0321b249d/pysysinfo/filesystem.py#L67-L91
|
aouyar/PyMunin
|
pymunin/plugins/pgstats.py
|
MuninPgPlugin.retrieveVals
|
def retrieveVals(self):
"""Retrieve values for graphs."""
stats = self._dbconn.getDatabaseStats()
databases = stats.get('databases')
totals = stats.get('totals')
if self.hasGraph('pg_connections'):
limit = self._dbconn.getParam('max_connections')
self.setGraphVal('pg_connections', 'max_conn', limit)
for (db, dbstats) in databases.iteritems():
if self.dbIncluded(db):
self.setGraphVal('pg_connections', db,
dbstats['numbackends'])
self.setGraphVal('pg_connections', 'total', totals['numbackends'])
if self.hasGraph('pg_diskspace'):
for (db, dbstats) in databases.iteritems():
if self.dbIncluded(db):
self.setGraphVal('pg_diskspace', db, dbstats['disk_size'])
self.setGraphVal('pg_diskspace', 'total', totals['disk_size'])
if self.hasGraph('pg_blockreads'):
self.setGraphVal('pg_blockreads', 'blk_hit', totals['blks_hit'])
self.setGraphVal('pg_blockreads', 'blk_read', totals['blks_read'])
if self.hasGraph('pg_xact'):
self.setGraphVal('pg_xact', 'commits', totals['xact_commit'])
self.setGraphVal('pg_xact', 'rollbacks', totals['xact_rollback'])
if self.hasGraph('pg_tup_read'):
self.setGraphVal('pg_tup_read', 'fetch', totals['tup_fetched'])
self.setGraphVal('pg_tup_read', 'return', totals['tup_returned'])
if self.hasGraph('pg_tup_write'):
self.setGraphVal('pg_tup_write', 'delete', totals['tup_deleted'])
self.setGraphVal('pg_tup_write', 'update', totals['tup_updated'])
self.setGraphVal('pg_tup_write', 'insert', totals['tup_inserted'])
lock_stats = None
for lock_state in ('all', 'wait',):
graph_name = "pg_lock_%s" % lock_state
if self.hasGraph(graph_name):
if lock_stats is None:
lock_stats = self._dbconn.getLockStatsMode()
mode_iter = iter(PgInfo.lockModes)
for mode in ('AccessExcl', 'Excl', 'ShrRwExcl', 'Shr',
'ShrUpdExcl', 'RwExcl', 'RwShr', 'AccessShr',):
self.setGraphVal(graph_name, mode,
lock_stats[lock_state].get(mode_iter.next()))
stats = None
if self.hasGraph('pg_checkpoints'):
if stats is None:
stats = self._dbconn.getBgWriterStats()
self.setGraphVal('pg_checkpoints', 'req',
stats.get('checkpoints_req'))
self.setGraphVal('pg_checkpoints', 'timed',
stats.get('checkpoints_timed'))
if self.hasGraph('pg_bgwriter'):
if stats is None:
stats = self._dbconn.getBgWriterStats()
self.setGraphVal('pg_bgwriter', 'backend',
stats.get('buffers_backend'))
self.setGraphVal('pg_bgwriter', 'clean',
stats.get('buffers_clean'))
self.setGraphVal('pg_bgwriter', 'chkpoint',
stats.get('buffers_checkpoint'))
if self._detailGraphs:
for (db, dbstats) in databases.iteritems():
if self.dbIncluded(db):
if self.hasGraph('pg_blockread_detail'):
self.setGraphVal('pg_blockread_detail', db,
dbstats['blks_hit'] + dbstats['blks_read'])
for (graph_name, attr_name) in (
('pg_xact_commit_detail', 'xact_commit'),
('pg_xact_rollback_detail', 'xact_rollback'),
('pg_tup_return_detail', 'tup_returned'),
('pg_tup_fetch_detail', 'tup_fetched'),
('pg_tup_delete_detail', 'tup_deleted'),
('pg_tup_update_detail', 'tup_updated'),
('pg_tup_insert_detail', 'tup_inserted'),
):
if self.hasGraph(graph_name):
self.setGraphVal(graph_name, db, dbstats[attr_name])
lock_stats_db = None
for lock_state in ('all', 'wait',):
graph_name = "pg_lock_%s_detail" % lock_state
if self.hasGraph(graph_name):
if lock_stats_db is None:
lock_stats_db = self._dbconn.getLockStatsDB()
self.setGraphVal(graph_name, db,
lock_stats_db[lock_state].get(db, 0))
if self._replGraphs:
repl_stats = self._dbconn.getSlaveConflictStats()
if self.hasGraph('pg_repl_conflicts'):
for field in self.getGraphFieldList('pg_repl_conflicts'):
self.setGraphVal('pg_repl_conflicts', field,
repl_stats['totals'].get("confl_%s" % field))
if self._detailGraphs and self.hasGraph('pg_repl_conflicts_detail'):
for (db, dbstats) in repl_stats['databases'].iteritems():
if self.dbIncluded(db):
self.setGraphVal('pg_repl_conflicts_detail', db,
sum(dbstats.values()))
|
python
|
def retrieveVals(self):
"""Retrieve values for graphs."""
stats = self._dbconn.getDatabaseStats()
databases = stats.get('databases')
totals = stats.get('totals')
if self.hasGraph('pg_connections'):
limit = self._dbconn.getParam('max_connections')
self.setGraphVal('pg_connections', 'max_conn', limit)
for (db, dbstats) in databases.iteritems():
if self.dbIncluded(db):
self.setGraphVal('pg_connections', db,
dbstats['numbackends'])
self.setGraphVal('pg_connections', 'total', totals['numbackends'])
if self.hasGraph('pg_diskspace'):
for (db, dbstats) in databases.iteritems():
if self.dbIncluded(db):
self.setGraphVal('pg_diskspace', db, dbstats['disk_size'])
self.setGraphVal('pg_diskspace', 'total', totals['disk_size'])
if self.hasGraph('pg_blockreads'):
self.setGraphVal('pg_blockreads', 'blk_hit', totals['blks_hit'])
self.setGraphVal('pg_blockreads', 'blk_read', totals['blks_read'])
if self.hasGraph('pg_xact'):
self.setGraphVal('pg_xact', 'commits', totals['xact_commit'])
self.setGraphVal('pg_xact', 'rollbacks', totals['xact_rollback'])
if self.hasGraph('pg_tup_read'):
self.setGraphVal('pg_tup_read', 'fetch', totals['tup_fetched'])
self.setGraphVal('pg_tup_read', 'return', totals['tup_returned'])
if self.hasGraph('pg_tup_write'):
self.setGraphVal('pg_tup_write', 'delete', totals['tup_deleted'])
self.setGraphVal('pg_tup_write', 'update', totals['tup_updated'])
self.setGraphVal('pg_tup_write', 'insert', totals['tup_inserted'])
lock_stats = None
for lock_state in ('all', 'wait',):
graph_name = "pg_lock_%s" % lock_state
if self.hasGraph(graph_name):
if lock_stats is None:
lock_stats = self._dbconn.getLockStatsMode()
mode_iter = iter(PgInfo.lockModes)
for mode in ('AccessExcl', 'Excl', 'ShrRwExcl', 'Shr',
'ShrUpdExcl', 'RwExcl', 'RwShr', 'AccessShr',):
self.setGraphVal(graph_name, mode,
lock_stats[lock_state].get(mode_iter.next()))
stats = None
if self.hasGraph('pg_checkpoints'):
if stats is None:
stats = self._dbconn.getBgWriterStats()
self.setGraphVal('pg_checkpoints', 'req',
stats.get('checkpoints_req'))
self.setGraphVal('pg_checkpoints', 'timed',
stats.get('checkpoints_timed'))
if self.hasGraph('pg_bgwriter'):
if stats is None:
stats = self._dbconn.getBgWriterStats()
self.setGraphVal('pg_bgwriter', 'backend',
stats.get('buffers_backend'))
self.setGraphVal('pg_bgwriter', 'clean',
stats.get('buffers_clean'))
self.setGraphVal('pg_bgwriter', 'chkpoint',
stats.get('buffers_checkpoint'))
if self._detailGraphs:
for (db, dbstats) in databases.iteritems():
if self.dbIncluded(db):
if self.hasGraph('pg_blockread_detail'):
self.setGraphVal('pg_blockread_detail', db,
dbstats['blks_hit'] + dbstats['blks_read'])
for (graph_name, attr_name) in (
('pg_xact_commit_detail', 'xact_commit'),
('pg_xact_rollback_detail', 'xact_rollback'),
('pg_tup_return_detail', 'tup_returned'),
('pg_tup_fetch_detail', 'tup_fetched'),
('pg_tup_delete_detail', 'tup_deleted'),
('pg_tup_update_detail', 'tup_updated'),
('pg_tup_insert_detail', 'tup_inserted'),
):
if self.hasGraph(graph_name):
self.setGraphVal(graph_name, db, dbstats[attr_name])
lock_stats_db = None
for lock_state in ('all', 'wait',):
graph_name = "pg_lock_%s_detail" % lock_state
if self.hasGraph(graph_name):
if lock_stats_db is None:
lock_stats_db = self._dbconn.getLockStatsDB()
self.setGraphVal(graph_name, db,
lock_stats_db[lock_state].get(db, 0))
if self._replGraphs:
repl_stats = self._dbconn.getSlaveConflictStats()
if self.hasGraph('pg_repl_conflicts'):
for field in self.getGraphFieldList('pg_repl_conflicts'):
self.setGraphVal('pg_repl_conflicts', field,
repl_stats['totals'].get("confl_%s" % field))
if self._detailGraphs and self.hasGraph('pg_repl_conflicts_detail'):
for (db, dbstats) in repl_stats['databases'].iteritems():
if self.dbIncluded(db):
self.setGraphVal('pg_repl_conflicts_detail', db,
sum(dbstats.values()))
|
[
"def",
"retrieveVals",
"(",
"self",
")",
":",
"stats",
"=",
"self",
".",
"_dbconn",
".",
"getDatabaseStats",
"(",
")",
"databases",
"=",
"stats",
".",
"get",
"(",
"'databases'",
")",
"totals",
"=",
"stats",
".",
"get",
"(",
"'totals'",
")",
"if",
"self",
".",
"hasGraph",
"(",
"'pg_connections'",
")",
":",
"limit",
"=",
"self",
".",
"_dbconn",
".",
"getParam",
"(",
"'max_connections'",
")",
"self",
".",
"setGraphVal",
"(",
"'pg_connections'",
",",
"'max_conn'",
",",
"limit",
")",
"for",
"(",
"db",
",",
"dbstats",
")",
"in",
"databases",
".",
"iteritems",
"(",
")",
":",
"if",
"self",
".",
"dbIncluded",
"(",
"db",
")",
":",
"self",
".",
"setGraphVal",
"(",
"'pg_connections'",
",",
"db",
",",
"dbstats",
"[",
"'numbackends'",
"]",
")",
"self",
".",
"setGraphVal",
"(",
"'pg_connections'",
",",
"'total'",
",",
"totals",
"[",
"'numbackends'",
"]",
")",
"if",
"self",
".",
"hasGraph",
"(",
"'pg_diskspace'",
")",
":",
"for",
"(",
"db",
",",
"dbstats",
")",
"in",
"databases",
".",
"iteritems",
"(",
")",
":",
"if",
"self",
".",
"dbIncluded",
"(",
"db",
")",
":",
"self",
".",
"setGraphVal",
"(",
"'pg_diskspace'",
",",
"db",
",",
"dbstats",
"[",
"'disk_size'",
"]",
")",
"self",
".",
"setGraphVal",
"(",
"'pg_diskspace'",
",",
"'total'",
",",
"totals",
"[",
"'disk_size'",
"]",
")",
"if",
"self",
".",
"hasGraph",
"(",
"'pg_blockreads'",
")",
":",
"self",
".",
"setGraphVal",
"(",
"'pg_blockreads'",
",",
"'blk_hit'",
",",
"totals",
"[",
"'blks_hit'",
"]",
")",
"self",
".",
"setGraphVal",
"(",
"'pg_blockreads'",
",",
"'blk_read'",
",",
"totals",
"[",
"'blks_read'",
"]",
")",
"if",
"self",
".",
"hasGraph",
"(",
"'pg_xact'",
")",
":",
"self",
".",
"setGraphVal",
"(",
"'pg_xact'",
",",
"'commits'",
",",
"totals",
"[",
"'xact_commit'",
"]",
")",
"self",
".",
"setGraphVal",
"(",
"'pg_xact'",
",",
"'rollbacks'",
",",
"totals",
"[",
"'xact_rollback'",
"]",
")",
"if",
"self",
".",
"hasGraph",
"(",
"'pg_tup_read'",
")",
":",
"self",
".",
"setGraphVal",
"(",
"'pg_tup_read'",
",",
"'fetch'",
",",
"totals",
"[",
"'tup_fetched'",
"]",
")",
"self",
".",
"setGraphVal",
"(",
"'pg_tup_read'",
",",
"'return'",
",",
"totals",
"[",
"'tup_returned'",
"]",
")",
"if",
"self",
".",
"hasGraph",
"(",
"'pg_tup_write'",
")",
":",
"self",
".",
"setGraphVal",
"(",
"'pg_tup_write'",
",",
"'delete'",
",",
"totals",
"[",
"'tup_deleted'",
"]",
")",
"self",
".",
"setGraphVal",
"(",
"'pg_tup_write'",
",",
"'update'",
",",
"totals",
"[",
"'tup_updated'",
"]",
")",
"self",
".",
"setGraphVal",
"(",
"'pg_tup_write'",
",",
"'insert'",
",",
"totals",
"[",
"'tup_inserted'",
"]",
")",
"lock_stats",
"=",
"None",
"for",
"lock_state",
"in",
"(",
"'all'",
",",
"'wait'",
",",
")",
":",
"graph_name",
"=",
"\"pg_lock_%s\"",
"%",
"lock_state",
"if",
"self",
".",
"hasGraph",
"(",
"graph_name",
")",
":",
"if",
"lock_stats",
"is",
"None",
":",
"lock_stats",
"=",
"self",
".",
"_dbconn",
".",
"getLockStatsMode",
"(",
")",
"mode_iter",
"=",
"iter",
"(",
"PgInfo",
".",
"lockModes",
")",
"for",
"mode",
"in",
"(",
"'AccessExcl'",
",",
"'Excl'",
",",
"'ShrRwExcl'",
",",
"'Shr'",
",",
"'ShrUpdExcl'",
",",
"'RwExcl'",
",",
"'RwShr'",
",",
"'AccessShr'",
",",
")",
":",
"self",
".",
"setGraphVal",
"(",
"graph_name",
",",
"mode",
",",
"lock_stats",
"[",
"lock_state",
"]",
".",
"get",
"(",
"mode_iter",
".",
"next",
"(",
")",
")",
")",
"stats",
"=",
"None",
"if",
"self",
".",
"hasGraph",
"(",
"'pg_checkpoints'",
")",
":",
"if",
"stats",
"is",
"None",
":",
"stats",
"=",
"self",
".",
"_dbconn",
".",
"getBgWriterStats",
"(",
")",
"self",
".",
"setGraphVal",
"(",
"'pg_checkpoints'",
",",
"'req'",
",",
"stats",
".",
"get",
"(",
"'checkpoints_req'",
")",
")",
"self",
".",
"setGraphVal",
"(",
"'pg_checkpoints'",
",",
"'timed'",
",",
"stats",
".",
"get",
"(",
"'checkpoints_timed'",
")",
")",
"if",
"self",
".",
"hasGraph",
"(",
"'pg_bgwriter'",
")",
":",
"if",
"stats",
"is",
"None",
":",
"stats",
"=",
"self",
".",
"_dbconn",
".",
"getBgWriterStats",
"(",
")",
"self",
".",
"setGraphVal",
"(",
"'pg_bgwriter'",
",",
"'backend'",
",",
"stats",
".",
"get",
"(",
"'buffers_backend'",
")",
")",
"self",
".",
"setGraphVal",
"(",
"'pg_bgwriter'",
",",
"'clean'",
",",
"stats",
".",
"get",
"(",
"'buffers_clean'",
")",
")",
"self",
".",
"setGraphVal",
"(",
"'pg_bgwriter'",
",",
"'chkpoint'",
",",
"stats",
".",
"get",
"(",
"'buffers_checkpoint'",
")",
")",
"if",
"self",
".",
"_detailGraphs",
":",
"for",
"(",
"db",
",",
"dbstats",
")",
"in",
"databases",
".",
"iteritems",
"(",
")",
":",
"if",
"self",
".",
"dbIncluded",
"(",
"db",
")",
":",
"if",
"self",
".",
"hasGraph",
"(",
"'pg_blockread_detail'",
")",
":",
"self",
".",
"setGraphVal",
"(",
"'pg_blockread_detail'",
",",
"db",
",",
"dbstats",
"[",
"'blks_hit'",
"]",
"+",
"dbstats",
"[",
"'blks_read'",
"]",
")",
"for",
"(",
"graph_name",
",",
"attr_name",
")",
"in",
"(",
"(",
"'pg_xact_commit_detail'",
",",
"'xact_commit'",
")",
",",
"(",
"'pg_xact_rollback_detail'",
",",
"'xact_rollback'",
")",
",",
"(",
"'pg_tup_return_detail'",
",",
"'tup_returned'",
")",
",",
"(",
"'pg_tup_fetch_detail'",
",",
"'tup_fetched'",
")",
",",
"(",
"'pg_tup_delete_detail'",
",",
"'tup_deleted'",
")",
",",
"(",
"'pg_tup_update_detail'",
",",
"'tup_updated'",
")",
",",
"(",
"'pg_tup_insert_detail'",
",",
"'tup_inserted'",
")",
",",
")",
":",
"if",
"self",
".",
"hasGraph",
"(",
"graph_name",
")",
":",
"self",
".",
"setGraphVal",
"(",
"graph_name",
",",
"db",
",",
"dbstats",
"[",
"attr_name",
"]",
")",
"lock_stats_db",
"=",
"None",
"for",
"lock_state",
"in",
"(",
"'all'",
",",
"'wait'",
",",
")",
":",
"graph_name",
"=",
"\"pg_lock_%s_detail\"",
"%",
"lock_state",
"if",
"self",
".",
"hasGraph",
"(",
"graph_name",
")",
":",
"if",
"lock_stats_db",
"is",
"None",
":",
"lock_stats_db",
"=",
"self",
".",
"_dbconn",
".",
"getLockStatsDB",
"(",
")",
"self",
".",
"setGraphVal",
"(",
"graph_name",
",",
"db",
",",
"lock_stats_db",
"[",
"lock_state",
"]",
".",
"get",
"(",
"db",
",",
"0",
")",
")",
"if",
"self",
".",
"_replGraphs",
":",
"repl_stats",
"=",
"self",
".",
"_dbconn",
".",
"getSlaveConflictStats",
"(",
")",
"if",
"self",
".",
"hasGraph",
"(",
"'pg_repl_conflicts'",
")",
":",
"for",
"field",
"in",
"self",
".",
"getGraphFieldList",
"(",
"'pg_repl_conflicts'",
")",
":",
"self",
".",
"setGraphVal",
"(",
"'pg_repl_conflicts'",
",",
"field",
",",
"repl_stats",
"[",
"'totals'",
"]",
".",
"get",
"(",
"\"confl_%s\"",
"%",
"field",
")",
")",
"if",
"self",
".",
"_detailGraphs",
"and",
"self",
".",
"hasGraph",
"(",
"'pg_repl_conflicts_detail'",
")",
":",
"for",
"(",
"db",
",",
"dbstats",
")",
"in",
"repl_stats",
"[",
"'databases'",
"]",
".",
"iteritems",
"(",
")",
":",
"if",
"self",
".",
"dbIncluded",
"(",
"db",
")",
":",
"self",
".",
"setGraphVal",
"(",
"'pg_repl_conflicts_detail'",
",",
"db",
",",
"sum",
"(",
"dbstats",
".",
"values",
"(",
")",
")",
")"
] |
Retrieve values for graphs.
|
[
"Retrieve",
"values",
"for",
"graphs",
"."
] |
train
|
https://github.com/aouyar/PyMunin/blob/4f58a64b6b37c85a84cc7e1e07aafaa0321b249d/pymunin/plugins/pgstats.py#L389-L486
|
swilson/aqualogic
|
aqualogic/core.py
|
AquaLogic.connect
|
def connect(self, host, port):
"""Connects via a RS-485 to Ethernet adapter."""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
self._reader = sock.makefile(mode='rb')
self._writer = sock.makefile(mode='wb')
|
python
|
def connect(self, host, port):
"""Connects via a RS-485 to Ethernet adapter."""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
self._reader = sock.makefile(mode='rb')
self._writer = sock.makefile(mode='wb')
|
[
"def",
"connect",
"(",
"self",
",",
"host",
",",
"port",
")",
":",
"sock",
"=",
"socket",
".",
"socket",
"(",
"socket",
".",
"AF_INET",
",",
"socket",
".",
"SOCK_STREAM",
")",
"sock",
".",
"connect",
"(",
"(",
"host",
",",
"port",
")",
")",
"self",
".",
"_reader",
"=",
"sock",
".",
"makefile",
"(",
"mode",
"=",
"'rb'",
")",
"self",
".",
"_writer",
"=",
"sock",
".",
"makefile",
"(",
"mode",
"=",
"'wb'",
")"
] |
Connects via a RS-485 to Ethernet adapter.
|
[
"Connects",
"via",
"a",
"RS",
"-",
"485",
"to",
"Ethernet",
"adapter",
"."
] |
train
|
https://github.com/swilson/aqualogic/blob/b6e904363efc4f64c70aae127d040079587ecbc6/aqualogic/core.py#L104-L109
|
swilson/aqualogic
|
aqualogic/core.py
|
AquaLogic.process
|
def process(self, data_changed_callback):
"""Process data; returns when the reader signals EOF.
Callback is notified when any data changes."""
# pylint: disable=too-many-locals,too-many-branches,too-many-statements
while True:
byte = self._reader.read(1)
while True:
# Search for FRAME_DLE + FRAME_STX
if not byte:
return
if byte[0] == self.FRAME_DLE:
next_byte = self._reader.read(1)
if not next_byte:
return
if next_byte[0] == self.FRAME_STX:
break
else:
continue
byte = self._reader.read(1)
frame = bytearray()
byte = self._reader.read(1)
while True:
if not byte:
return
if byte[0] == self.FRAME_DLE:
# Should be FRAME_ETX or 0 according to
# the AQ-CO-SERIAL manual
next_byte = self._reader.read(1)
if not next_byte:
return
if next_byte[0] == self.FRAME_ETX:
break
elif next_byte[0] != 0:
# Error?
pass
frame.append(byte[0])
byte = self._reader.read(1)
# Verify CRC
frame_crc = int.from_bytes(frame[-2:], byteorder='big')
frame = frame[:-2]
calculated_crc = self.FRAME_DLE + self.FRAME_STX
for byte in frame:
calculated_crc += byte
if frame_crc != calculated_crc:
_LOGGER.warning('Bad CRC')
continue
frame_type = frame[0:2]
frame = frame[2:]
if frame_type == self.FRAME_TYPE_KEEP_ALIVE:
# Keep alive
# If a frame has been queued for transmit, send it.
if not self._send_queue.empty():
data = self._send_queue.get(block=False)
self._writer.write(data['frame'])
self._writer.flush()
_LOGGER.info('Sent: %s', binascii.hexlify(data['frame']))
try:
if data['desired_states'] is not None:
# Set a timer to verify the state changes
# Wait 2 seconds as it can take a while for
# the state to change.
Timer(2.0, self._check_state, [data]).start()
except KeyError:
pass
continue
elif frame_type == self.FRAME_TYPE_KEY_EVENT:
_LOGGER.info('Key: %s', binascii.hexlify(frame))
elif frame_type == self.FRAME_TYPE_LEDS:
_LOGGER.debug('LEDs: %s', binascii.hexlify(frame))
# First 4 bytes are the LEDs that are on;
# second 4 bytes_ are the LEDs that are flashing
states = int.from_bytes(frame[0:4], byteorder='little')
flashing_states = int.from_bytes(frame[4:8],
byteorder='little')
states |= flashing_states
if (states != self._states
or flashing_states != self._flashing_states):
self._states = states
self._flashing_states = flashing_states
data_changed_callback(self)
elif frame_type == self.FRAME_TYPE_PUMP_SPEED_REQUEST:
value = int.from_bytes(frame[0:2], byteorder='big')
_LOGGER.debug('Pump speed request: %d%%', value)
if self._pump_speed != value:
self._pump_speed = value
data_changed_callback(self)
elif frame_type == self.FRAME_TYPE_PUMP_STATUS:
# Pump status messages sent out by Hayward VSP pumps
self._multi_speed_pump = True
speed = frame[2]
# Power is in BCD
power = ((((frame[3] & 0xf0) >> 4) * 1000)
+ (((frame[3] & 0x0f)) * 100)
+ (((frame[4] & 0xf0) >> 4) * 10)
+ (((frame[4] & 0x0f))))
_LOGGER.debug('Pump speed: %d%%, power: %d watts',
speed, power)
if self._pump_power != power:
self._pump_power = power
data_changed_callback(self)
elif frame_type == self.FRAME_TYPE_DISPLAY_UPDATE:
parts = frame.decode('latin-1').split()
_LOGGER.debug('Display update: %s', parts)
try:
if parts[0] == 'Pool' and parts[1] == 'Temp':
# Pool Temp <temp>°[C|F]
value = int(parts[2][:-2])
if self._pool_temp != value:
self._pool_temp = value
self._is_metric = parts[2][-1:] == 'C'
data_changed_callback(self)
elif parts[0] == 'Spa' and parts[1] == 'Temp':
# Spa Temp <temp>°[C|F]
value = int(parts[2][:-2])
if self._spa_temp != value:
self._spa_temp = value
self._is_metric = parts[2][-1:] == 'C'
data_changed_callback(self)
elif parts[0] == 'Air' and parts[1] == 'Temp':
# Air Temp <temp>°[C|F]
value = int(parts[2][:-2])
if self._air_temp != value:
self._air_temp = value
self._is_metric = parts[2][-1:] == 'C'
data_changed_callback(self)
elif parts[0] == 'Pool' and parts[1] == 'Chlorinator':
# Pool Chlorinator <value>%
value = int(parts[2][:-1])
if self._pool_chlorinator != value:
self._pool_chlorinator = value
data_changed_callback(self)
elif parts[0] == 'Spa' and parts[1] == 'Chlorinator':
# Spa Chlorinator <value>%
value = int(parts[2][:-1])
if self._spa_chlorinator != value:
self._spa_chlorinator = value
data_changed_callback(self)
elif parts[0] == 'Salt' and parts[1] == 'Level':
# Salt Level <value> [g/L|PPM|
value = float(parts[2])
if self._salt_level != value:
self._salt_level = value
self._is_metric = parts[3] == 'g/L'
data_changed_callback(self)
elif parts[0] == 'Check' and parts[1] == 'System':
# Check System <msg>
value = ' '.join(parts[2:])
if self._check_system_msg != value:
self._check_system_msg = value
data_changed_callback(self)
except ValueError:
pass
else:
_LOGGER.info('Unknown frame: %s %s',
binascii.hexlify(frame_type),
binascii.hexlify(frame))
|
python
|
def process(self, data_changed_callback):
"""Process data; returns when the reader signals EOF.
Callback is notified when any data changes."""
# pylint: disable=too-many-locals,too-many-branches,too-many-statements
while True:
byte = self._reader.read(1)
while True:
# Search for FRAME_DLE + FRAME_STX
if not byte:
return
if byte[0] == self.FRAME_DLE:
next_byte = self._reader.read(1)
if not next_byte:
return
if next_byte[0] == self.FRAME_STX:
break
else:
continue
byte = self._reader.read(1)
frame = bytearray()
byte = self._reader.read(1)
while True:
if not byte:
return
if byte[0] == self.FRAME_DLE:
# Should be FRAME_ETX or 0 according to
# the AQ-CO-SERIAL manual
next_byte = self._reader.read(1)
if not next_byte:
return
if next_byte[0] == self.FRAME_ETX:
break
elif next_byte[0] != 0:
# Error?
pass
frame.append(byte[0])
byte = self._reader.read(1)
# Verify CRC
frame_crc = int.from_bytes(frame[-2:], byteorder='big')
frame = frame[:-2]
calculated_crc = self.FRAME_DLE + self.FRAME_STX
for byte in frame:
calculated_crc += byte
if frame_crc != calculated_crc:
_LOGGER.warning('Bad CRC')
continue
frame_type = frame[0:2]
frame = frame[2:]
if frame_type == self.FRAME_TYPE_KEEP_ALIVE:
# Keep alive
# If a frame has been queued for transmit, send it.
if not self._send_queue.empty():
data = self._send_queue.get(block=False)
self._writer.write(data['frame'])
self._writer.flush()
_LOGGER.info('Sent: %s', binascii.hexlify(data['frame']))
try:
if data['desired_states'] is not None:
# Set a timer to verify the state changes
# Wait 2 seconds as it can take a while for
# the state to change.
Timer(2.0, self._check_state, [data]).start()
except KeyError:
pass
continue
elif frame_type == self.FRAME_TYPE_KEY_EVENT:
_LOGGER.info('Key: %s', binascii.hexlify(frame))
elif frame_type == self.FRAME_TYPE_LEDS:
_LOGGER.debug('LEDs: %s', binascii.hexlify(frame))
# First 4 bytes are the LEDs that are on;
# second 4 bytes_ are the LEDs that are flashing
states = int.from_bytes(frame[0:4], byteorder='little')
flashing_states = int.from_bytes(frame[4:8],
byteorder='little')
states |= flashing_states
if (states != self._states
or flashing_states != self._flashing_states):
self._states = states
self._flashing_states = flashing_states
data_changed_callback(self)
elif frame_type == self.FRAME_TYPE_PUMP_SPEED_REQUEST:
value = int.from_bytes(frame[0:2], byteorder='big')
_LOGGER.debug('Pump speed request: %d%%', value)
if self._pump_speed != value:
self._pump_speed = value
data_changed_callback(self)
elif frame_type == self.FRAME_TYPE_PUMP_STATUS:
# Pump status messages sent out by Hayward VSP pumps
self._multi_speed_pump = True
speed = frame[2]
# Power is in BCD
power = ((((frame[3] & 0xf0) >> 4) * 1000)
+ (((frame[3] & 0x0f)) * 100)
+ (((frame[4] & 0xf0) >> 4) * 10)
+ (((frame[4] & 0x0f))))
_LOGGER.debug('Pump speed: %d%%, power: %d watts',
speed, power)
if self._pump_power != power:
self._pump_power = power
data_changed_callback(self)
elif frame_type == self.FRAME_TYPE_DISPLAY_UPDATE:
parts = frame.decode('latin-1').split()
_LOGGER.debug('Display update: %s', parts)
try:
if parts[0] == 'Pool' and parts[1] == 'Temp':
# Pool Temp <temp>°[C|F]
value = int(parts[2][:-2])
if self._pool_temp != value:
self._pool_temp = value
self._is_metric = parts[2][-1:] == 'C'
data_changed_callback(self)
elif parts[0] == 'Spa' and parts[1] == 'Temp':
# Spa Temp <temp>°[C|F]
value = int(parts[2][:-2])
if self._spa_temp != value:
self._spa_temp = value
self._is_metric = parts[2][-1:] == 'C'
data_changed_callback(self)
elif parts[0] == 'Air' and parts[1] == 'Temp':
# Air Temp <temp>°[C|F]
value = int(parts[2][:-2])
if self._air_temp != value:
self._air_temp = value
self._is_metric = parts[2][-1:] == 'C'
data_changed_callback(self)
elif parts[0] == 'Pool' and parts[1] == 'Chlorinator':
# Pool Chlorinator <value>%
value = int(parts[2][:-1])
if self._pool_chlorinator != value:
self._pool_chlorinator = value
data_changed_callback(self)
elif parts[0] == 'Spa' and parts[1] == 'Chlorinator':
# Spa Chlorinator <value>%
value = int(parts[2][:-1])
if self._spa_chlorinator != value:
self._spa_chlorinator = value
data_changed_callback(self)
elif parts[0] == 'Salt' and parts[1] == 'Level':
# Salt Level <value> [g/L|PPM|
value = float(parts[2])
if self._salt_level != value:
self._salt_level = value
self._is_metric = parts[3] == 'g/L'
data_changed_callback(self)
elif parts[0] == 'Check' and parts[1] == 'System':
# Check System <msg>
value = ' '.join(parts[2:])
if self._check_system_msg != value:
self._check_system_msg = value
data_changed_callback(self)
except ValueError:
pass
else:
_LOGGER.info('Unknown frame: %s %s',
binascii.hexlify(frame_type),
binascii.hexlify(frame))
|
[
"def",
"process",
"(",
"self",
",",
"data_changed_callback",
")",
":",
"# pylint: disable=too-many-locals,too-many-branches,too-many-statements",
"while",
"True",
":",
"byte",
"=",
"self",
".",
"_reader",
".",
"read",
"(",
"1",
")",
"while",
"True",
":",
"# Search for FRAME_DLE + FRAME_STX",
"if",
"not",
"byte",
":",
"return",
"if",
"byte",
"[",
"0",
"]",
"==",
"self",
".",
"FRAME_DLE",
":",
"next_byte",
"=",
"self",
".",
"_reader",
".",
"read",
"(",
"1",
")",
"if",
"not",
"next_byte",
":",
"return",
"if",
"next_byte",
"[",
"0",
"]",
"==",
"self",
".",
"FRAME_STX",
":",
"break",
"else",
":",
"continue",
"byte",
"=",
"self",
".",
"_reader",
".",
"read",
"(",
"1",
")",
"frame",
"=",
"bytearray",
"(",
")",
"byte",
"=",
"self",
".",
"_reader",
".",
"read",
"(",
"1",
")",
"while",
"True",
":",
"if",
"not",
"byte",
":",
"return",
"if",
"byte",
"[",
"0",
"]",
"==",
"self",
".",
"FRAME_DLE",
":",
"# Should be FRAME_ETX or 0 according to",
"# the AQ-CO-SERIAL manual",
"next_byte",
"=",
"self",
".",
"_reader",
".",
"read",
"(",
"1",
")",
"if",
"not",
"next_byte",
":",
"return",
"if",
"next_byte",
"[",
"0",
"]",
"==",
"self",
".",
"FRAME_ETX",
":",
"break",
"elif",
"next_byte",
"[",
"0",
"]",
"!=",
"0",
":",
"# Error?",
"pass",
"frame",
".",
"append",
"(",
"byte",
"[",
"0",
"]",
")",
"byte",
"=",
"self",
".",
"_reader",
".",
"read",
"(",
"1",
")",
"# Verify CRC",
"frame_crc",
"=",
"int",
".",
"from_bytes",
"(",
"frame",
"[",
"-",
"2",
":",
"]",
",",
"byteorder",
"=",
"'big'",
")",
"frame",
"=",
"frame",
"[",
":",
"-",
"2",
"]",
"calculated_crc",
"=",
"self",
".",
"FRAME_DLE",
"+",
"self",
".",
"FRAME_STX",
"for",
"byte",
"in",
"frame",
":",
"calculated_crc",
"+=",
"byte",
"if",
"frame_crc",
"!=",
"calculated_crc",
":",
"_LOGGER",
".",
"warning",
"(",
"'Bad CRC'",
")",
"continue",
"frame_type",
"=",
"frame",
"[",
"0",
":",
"2",
"]",
"frame",
"=",
"frame",
"[",
"2",
":",
"]",
"if",
"frame_type",
"==",
"self",
".",
"FRAME_TYPE_KEEP_ALIVE",
":",
"# Keep alive",
"# If a frame has been queued for transmit, send it.",
"if",
"not",
"self",
".",
"_send_queue",
".",
"empty",
"(",
")",
":",
"data",
"=",
"self",
".",
"_send_queue",
".",
"get",
"(",
"block",
"=",
"False",
")",
"self",
".",
"_writer",
".",
"write",
"(",
"data",
"[",
"'frame'",
"]",
")",
"self",
".",
"_writer",
".",
"flush",
"(",
")",
"_LOGGER",
".",
"info",
"(",
"'Sent: %s'",
",",
"binascii",
".",
"hexlify",
"(",
"data",
"[",
"'frame'",
"]",
")",
")",
"try",
":",
"if",
"data",
"[",
"'desired_states'",
"]",
"is",
"not",
"None",
":",
"# Set a timer to verify the state changes",
"# Wait 2 seconds as it can take a while for",
"# the state to change.",
"Timer",
"(",
"2.0",
",",
"self",
".",
"_check_state",
",",
"[",
"data",
"]",
")",
".",
"start",
"(",
")",
"except",
"KeyError",
":",
"pass",
"continue",
"elif",
"frame_type",
"==",
"self",
".",
"FRAME_TYPE_KEY_EVENT",
":",
"_LOGGER",
".",
"info",
"(",
"'Key: %s'",
",",
"binascii",
".",
"hexlify",
"(",
"frame",
")",
")",
"elif",
"frame_type",
"==",
"self",
".",
"FRAME_TYPE_LEDS",
":",
"_LOGGER",
".",
"debug",
"(",
"'LEDs: %s'",
",",
"binascii",
".",
"hexlify",
"(",
"frame",
")",
")",
"# First 4 bytes are the LEDs that are on;",
"# second 4 bytes_ are the LEDs that are flashing",
"states",
"=",
"int",
".",
"from_bytes",
"(",
"frame",
"[",
"0",
":",
"4",
"]",
",",
"byteorder",
"=",
"'little'",
")",
"flashing_states",
"=",
"int",
".",
"from_bytes",
"(",
"frame",
"[",
"4",
":",
"8",
"]",
",",
"byteorder",
"=",
"'little'",
")",
"states",
"|=",
"flashing_states",
"if",
"(",
"states",
"!=",
"self",
".",
"_states",
"or",
"flashing_states",
"!=",
"self",
".",
"_flashing_states",
")",
":",
"self",
".",
"_states",
"=",
"states",
"self",
".",
"_flashing_states",
"=",
"flashing_states",
"data_changed_callback",
"(",
"self",
")",
"elif",
"frame_type",
"==",
"self",
".",
"FRAME_TYPE_PUMP_SPEED_REQUEST",
":",
"value",
"=",
"int",
".",
"from_bytes",
"(",
"frame",
"[",
"0",
":",
"2",
"]",
",",
"byteorder",
"=",
"'big'",
")",
"_LOGGER",
".",
"debug",
"(",
"'Pump speed request: %d%%'",
",",
"value",
")",
"if",
"self",
".",
"_pump_speed",
"!=",
"value",
":",
"self",
".",
"_pump_speed",
"=",
"value",
"data_changed_callback",
"(",
"self",
")",
"elif",
"frame_type",
"==",
"self",
".",
"FRAME_TYPE_PUMP_STATUS",
":",
"# Pump status messages sent out by Hayward VSP pumps",
"self",
".",
"_multi_speed_pump",
"=",
"True",
"speed",
"=",
"frame",
"[",
"2",
"]",
"# Power is in BCD",
"power",
"=",
"(",
"(",
"(",
"(",
"frame",
"[",
"3",
"]",
"&",
"0xf0",
")",
">>",
"4",
")",
"*",
"1000",
")",
"+",
"(",
"(",
"(",
"frame",
"[",
"3",
"]",
"&",
"0x0f",
")",
")",
"*",
"100",
")",
"+",
"(",
"(",
"(",
"frame",
"[",
"4",
"]",
"&",
"0xf0",
")",
">>",
"4",
")",
"*",
"10",
")",
"+",
"(",
"(",
"(",
"frame",
"[",
"4",
"]",
"&",
"0x0f",
")",
")",
")",
")",
"_LOGGER",
".",
"debug",
"(",
"'Pump speed: %d%%, power: %d watts'",
",",
"speed",
",",
"power",
")",
"if",
"self",
".",
"_pump_power",
"!=",
"power",
":",
"self",
".",
"_pump_power",
"=",
"power",
"data_changed_callback",
"(",
"self",
")",
"elif",
"frame_type",
"==",
"self",
".",
"FRAME_TYPE_DISPLAY_UPDATE",
":",
"parts",
"=",
"frame",
".",
"decode",
"(",
"'latin-1'",
")",
".",
"split",
"(",
")",
"_LOGGER",
".",
"debug",
"(",
"'Display update: %s'",
",",
"parts",
")",
"try",
":",
"if",
"parts",
"[",
"0",
"]",
"==",
"'Pool'",
"and",
"parts",
"[",
"1",
"]",
"==",
"'Temp'",
":",
"# Pool Temp <temp>°[C|F]",
"value",
"=",
"int",
"(",
"parts",
"[",
"2",
"]",
"[",
":",
"-",
"2",
"]",
")",
"if",
"self",
".",
"_pool_temp",
"!=",
"value",
":",
"self",
".",
"_pool_temp",
"=",
"value",
"self",
".",
"_is_metric",
"=",
"parts",
"[",
"2",
"]",
"[",
"-",
"1",
":",
"]",
"==",
"'C'",
"data_changed_callback",
"(",
"self",
")",
"elif",
"parts",
"[",
"0",
"]",
"==",
"'Spa'",
"and",
"parts",
"[",
"1",
"]",
"==",
"'Temp'",
":",
"# Spa Temp <temp>°[C|F]",
"value",
"=",
"int",
"(",
"parts",
"[",
"2",
"]",
"[",
":",
"-",
"2",
"]",
")",
"if",
"self",
".",
"_spa_temp",
"!=",
"value",
":",
"self",
".",
"_spa_temp",
"=",
"value",
"self",
".",
"_is_metric",
"=",
"parts",
"[",
"2",
"]",
"[",
"-",
"1",
":",
"]",
"==",
"'C'",
"data_changed_callback",
"(",
"self",
")",
"elif",
"parts",
"[",
"0",
"]",
"==",
"'Air'",
"and",
"parts",
"[",
"1",
"]",
"==",
"'Temp'",
":",
"# Air Temp <temp>°[C|F]",
"value",
"=",
"int",
"(",
"parts",
"[",
"2",
"]",
"[",
":",
"-",
"2",
"]",
")",
"if",
"self",
".",
"_air_temp",
"!=",
"value",
":",
"self",
".",
"_air_temp",
"=",
"value",
"self",
".",
"_is_metric",
"=",
"parts",
"[",
"2",
"]",
"[",
"-",
"1",
":",
"]",
"==",
"'C'",
"data_changed_callback",
"(",
"self",
")",
"elif",
"parts",
"[",
"0",
"]",
"==",
"'Pool'",
"and",
"parts",
"[",
"1",
"]",
"==",
"'Chlorinator'",
":",
"# Pool Chlorinator <value>%",
"value",
"=",
"int",
"(",
"parts",
"[",
"2",
"]",
"[",
":",
"-",
"1",
"]",
")",
"if",
"self",
".",
"_pool_chlorinator",
"!=",
"value",
":",
"self",
".",
"_pool_chlorinator",
"=",
"value",
"data_changed_callback",
"(",
"self",
")",
"elif",
"parts",
"[",
"0",
"]",
"==",
"'Spa'",
"and",
"parts",
"[",
"1",
"]",
"==",
"'Chlorinator'",
":",
"# Spa Chlorinator <value>%",
"value",
"=",
"int",
"(",
"parts",
"[",
"2",
"]",
"[",
":",
"-",
"1",
"]",
")",
"if",
"self",
".",
"_spa_chlorinator",
"!=",
"value",
":",
"self",
".",
"_spa_chlorinator",
"=",
"value",
"data_changed_callback",
"(",
"self",
")",
"elif",
"parts",
"[",
"0",
"]",
"==",
"'Salt'",
"and",
"parts",
"[",
"1",
"]",
"==",
"'Level'",
":",
"# Salt Level <value> [g/L|PPM|",
"value",
"=",
"float",
"(",
"parts",
"[",
"2",
"]",
")",
"if",
"self",
".",
"_salt_level",
"!=",
"value",
":",
"self",
".",
"_salt_level",
"=",
"value",
"self",
".",
"_is_metric",
"=",
"parts",
"[",
"3",
"]",
"==",
"'g/L'",
"data_changed_callback",
"(",
"self",
")",
"elif",
"parts",
"[",
"0",
"]",
"==",
"'Check'",
"and",
"parts",
"[",
"1",
"]",
"==",
"'System'",
":",
"# Check System <msg>",
"value",
"=",
"' '",
".",
"join",
"(",
"parts",
"[",
"2",
":",
"]",
")",
"if",
"self",
".",
"_check_system_msg",
"!=",
"value",
":",
"self",
".",
"_check_system_msg",
"=",
"value",
"data_changed_callback",
"(",
"self",
")",
"except",
"ValueError",
":",
"pass",
"else",
":",
"_LOGGER",
".",
"info",
"(",
"'Unknown frame: %s %s'",
",",
"binascii",
".",
"hexlify",
"(",
"frame_type",
")",
",",
"binascii",
".",
"hexlify",
"(",
"frame",
")",
")"
] |
Process data; returns when the reader signals EOF.
Callback is notified when any data changes.
|
[
"Process",
"data",
";",
"returns",
"when",
"the",
"reader",
"signals",
"EOF",
".",
"Callback",
"is",
"notified",
"when",
"any",
"data",
"changes",
"."
] |
train
|
https://github.com/swilson/aqualogic/blob/b6e904363efc4f64c70aae127d040079587ecbc6/aqualogic/core.py#L125-L292
|
swilson/aqualogic
|
aqualogic/core.py
|
AquaLogic.send_key
|
def send_key(self, key):
"""Sends a key."""
_LOGGER.info('Queueing key %s', key)
frame = self._get_key_event_frame(key)
# Queue it to send immediately following the reception
# of a keep-alive packet in an attempt to avoid bus collisions.
self._send_queue.put({'frame': frame})
|
python
|
def send_key(self, key):
"""Sends a key."""
_LOGGER.info('Queueing key %s', key)
frame = self._get_key_event_frame(key)
# Queue it to send immediately following the reception
# of a keep-alive packet in an attempt to avoid bus collisions.
self._send_queue.put({'frame': frame})
|
[
"def",
"send_key",
"(",
"self",
",",
"key",
")",
":",
"_LOGGER",
".",
"info",
"(",
"'Queueing key %s'",
",",
"key",
")",
"frame",
"=",
"self",
".",
"_get_key_event_frame",
"(",
"key",
")",
"# Queue it to send immediately following the reception",
"# of a keep-alive packet in an attempt to avoid bus collisions.",
"self",
".",
"_send_queue",
".",
"put",
"(",
"{",
"'frame'",
":",
"frame",
"}",
")"
] |
Sends a key.
|
[
"Sends",
"a",
"key",
"."
] |
train
|
https://github.com/swilson/aqualogic/blob/b6e904363efc4f64c70aae127d040079587ecbc6/aqualogic/core.py#L319-L326
|
swilson/aqualogic
|
aqualogic/core.py
|
AquaLogic.states
|
def states(self):
"""Returns a set containing the enabled states."""
state_list = []
for state in States:
if state.value & self._states != 0:
state_list.append(state)
if (self._flashing_states & States.FILTER) != 0:
state_list.append(States.FILTER_LOW_SPEED)
return state_list
|
python
|
def states(self):
"""Returns a set containing the enabled states."""
state_list = []
for state in States:
if state.value & self._states != 0:
state_list.append(state)
if (self._flashing_states & States.FILTER) != 0:
state_list.append(States.FILTER_LOW_SPEED)
return state_list
|
[
"def",
"states",
"(",
"self",
")",
":",
"state_list",
"=",
"[",
"]",
"for",
"state",
"in",
"States",
":",
"if",
"state",
".",
"value",
"&",
"self",
".",
"_states",
"!=",
"0",
":",
"state_list",
".",
"append",
"(",
"state",
")",
"if",
"(",
"self",
".",
"_flashing_states",
"&",
"States",
".",
"FILTER",
")",
"!=",
"0",
":",
"state_list",
".",
"append",
"(",
"States",
".",
"FILTER_LOW_SPEED",
")",
"return",
"state_list"
] |
Returns a set containing the enabled states.
|
[
"Returns",
"a",
"set",
"containing",
"the",
"enabled",
"states",
"."
] |
train
|
https://github.com/swilson/aqualogic/blob/b6e904363efc4f64c70aae127d040079587ecbc6/aqualogic/core.py#L392-L402
|
swilson/aqualogic
|
aqualogic/core.py
|
AquaLogic.get_state
|
def get_state(self, state):
"""Returns True if the specified state is enabled."""
# Check to see if we have a change request pending; if we do
# return the value we expect it to change to.
for data in list(self._send_queue.queue):
desired_states = data['desired_states']
for desired_state in desired_states:
if desired_state['state'] == state:
return desired_state['enabled']
if state == States.FILTER_LOW_SPEED:
return (States.FILTER.value & self._flashing_states) != 0
return (state.value & self._states) != 0
|
python
|
def get_state(self, state):
"""Returns True if the specified state is enabled."""
# Check to see if we have a change request pending; if we do
# return the value we expect it to change to.
for data in list(self._send_queue.queue):
desired_states = data['desired_states']
for desired_state in desired_states:
if desired_state['state'] == state:
return desired_state['enabled']
if state == States.FILTER_LOW_SPEED:
return (States.FILTER.value & self._flashing_states) != 0
return (state.value & self._states) != 0
|
[
"def",
"get_state",
"(",
"self",
",",
"state",
")",
":",
"# Check to see if we have a change request pending; if we do",
"# return the value we expect it to change to.",
"for",
"data",
"in",
"list",
"(",
"self",
".",
"_send_queue",
".",
"queue",
")",
":",
"desired_states",
"=",
"data",
"[",
"'desired_states'",
"]",
"for",
"desired_state",
"in",
"desired_states",
":",
"if",
"desired_state",
"[",
"'state'",
"]",
"==",
"state",
":",
"return",
"desired_state",
"[",
"'enabled'",
"]",
"if",
"state",
"==",
"States",
".",
"FILTER_LOW_SPEED",
":",
"return",
"(",
"States",
".",
"FILTER",
".",
"value",
"&",
"self",
".",
"_flashing_states",
")",
"!=",
"0",
"return",
"(",
"state",
".",
"value",
"&",
"self",
".",
"_states",
")",
"!=",
"0"
] |
Returns True if the specified state is enabled.
|
[
"Returns",
"True",
"if",
"the",
"specified",
"state",
"is",
"enabled",
"."
] |
train
|
https://github.com/swilson/aqualogic/blob/b6e904363efc4f64c70aae127d040079587ecbc6/aqualogic/core.py#L404-L415
|
swilson/aqualogic
|
aqualogic/core.py
|
AquaLogic.set_state
|
def set_state(self, state, enable):
"""Set the state."""
is_enabled = self.get_state(state)
if is_enabled == enable:
return True
key = None
desired_states = [{'state': state, 'enabled': not is_enabled}]
if state == States.FILTER_LOW_SPEED:
if not self._multi_speed_pump:
return False
# Send the FILTER key once.
# If the pump is in high speed, it wil switch to low speed.
# If the pump is off the retry mechanism will send an additional
# FILTER key to switch into low speed.
# If the pump is in low speed then we pretend the pump is off;
# the retry mechanism will send an additional FILTER key
# to switch into high speed.
key = Keys.FILTER
desired_states.append({'state': States.FILTER, 'enabled': True})
else:
# See if this state has a corresponding Key
try:
key = Keys[state.name]
except KeyError:
# TODO: send the appropriate combination of keys
# to enable the state
return False
frame = self._get_key_event_frame(key)
# Queue it to send immediately following the reception
# of a keep-alive packet in an attempt to avoid bus collisions.
self._send_queue.put({'frame': frame, 'desired_states': desired_states,
'retries': 10})
return True
|
python
|
def set_state(self, state, enable):
"""Set the state."""
is_enabled = self.get_state(state)
if is_enabled == enable:
return True
key = None
desired_states = [{'state': state, 'enabled': not is_enabled}]
if state == States.FILTER_LOW_SPEED:
if not self._multi_speed_pump:
return False
# Send the FILTER key once.
# If the pump is in high speed, it wil switch to low speed.
# If the pump is off the retry mechanism will send an additional
# FILTER key to switch into low speed.
# If the pump is in low speed then we pretend the pump is off;
# the retry mechanism will send an additional FILTER key
# to switch into high speed.
key = Keys.FILTER
desired_states.append({'state': States.FILTER, 'enabled': True})
else:
# See if this state has a corresponding Key
try:
key = Keys[state.name]
except KeyError:
# TODO: send the appropriate combination of keys
# to enable the state
return False
frame = self._get_key_event_frame(key)
# Queue it to send immediately following the reception
# of a keep-alive packet in an attempt to avoid bus collisions.
self._send_queue.put({'frame': frame, 'desired_states': desired_states,
'retries': 10})
return True
|
[
"def",
"set_state",
"(",
"self",
",",
"state",
",",
"enable",
")",
":",
"is_enabled",
"=",
"self",
".",
"get_state",
"(",
"state",
")",
"if",
"is_enabled",
"==",
"enable",
":",
"return",
"True",
"key",
"=",
"None",
"desired_states",
"=",
"[",
"{",
"'state'",
":",
"state",
",",
"'enabled'",
":",
"not",
"is_enabled",
"}",
"]",
"if",
"state",
"==",
"States",
".",
"FILTER_LOW_SPEED",
":",
"if",
"not",
"self",
".",
"_multi_speed_pump",
":",
"return",
"False",
"# Send the FILTER key once.",
"# If the pump is in high speed, it wil switch to low speed.",
"# If the pump is off the retry mechanism will send an additional",
"# FILTER key to switch into low speed.",
"# If the pump is in low speed then we pretend the pump is off;",
"# the retry mechanism will send an additional FILTER key",
"# to switch into high speed.",
"key",
"=",
"Keys",
".",
"FILTER",
"desired_states",
".",
"append",
"(",
"{",
"'state'",
":",
"States",
".",
"FILTER",
",",
"'enabled'",
":",
"True",
"}",
")",
"else",
":",
"# See if this state has a corresponding Key",
"try",
":",
"key",
"=",
"Keys",
"[",
"state",
".",
"name",
"]",
"except",
"KeyError",
":",
"# TODO: send the appropriate combination of keys",
"# to enable the state",
"return",
"False",
"frame",
"=",
"self",
".",
"_get_key_event_frame",
"(",
"key",
")",
"# Queue it to send immediately following the reception",
"# of a keep-alive packet in an attempt to avoid bus collisions.",
"self",
".",
"_send_queue",
".",
"put",
"(",
"{",
"'frame'",
":",
"frame",
",",
"'desired_states'",
":",
"desired_states",
",",
"'retries'",
":",
"10",
"}",
")",
"return",
"True"
] |
Set the state.
|
[
"Set",
"the",
"state",
"."
] |
train
|
https://github.com/swilson/aqualogic/blob/b6e904363efc4f64c70aae127d040079587ecbc6/aqualogic/core.py#L417-L455
|
vokimon/python-qgmap
|
qgmap/__init__.py
|
trace
|
def trace(function, *args, **k) :
"""Decorates a function by tracing the begining and
end of the function execution, if doTrace global is True"""
if doTrace : print ("> "+function.__name__, args, k)
result = function(*args, **k)
if doTrace : print ("< "+function.__name__, args, k, "->", result)
return result
|
python
|
def trace(function, *args, **k) :
"""Decorates a function by tracing the begining and
end of the function execution, if doTrace global is True"""
if doTrace : print ("> "+function.__name__, args, k)
result = function(*args, **k)
if doTrace : print ("< "+function.__name__, args, k, "->", result)
return result
|
[
"def",
"trace",
"(",
"function",
",",
"*",
"args",
",",
"*",
"*",
"k",
")",
":",
"if",
"doTrace",
":",
"print",
"(",
"\"> \"",
"+",
"function",
".",
"__name__",
",",
"args",
",",
"k",
")",
"result",
"=",
"function",
"(",
"*",
"args",
",",
"*",
"*",
"k",
")",
"if",
"doTrace",
":",
"print",
"(",
"\"< \"",
"+",
"function",
".",
"__name__",
",",
"args",
",",
"k",
",",
"\"->\"",
",",
"result",
")",
"return",
"result"
] |
Decorates a function by tracing the begining and
end of the function execution, if doTrace global is True
|
[
"Decorates",
"a",
"function",
"by",
"tracing",
"the",
"begining",
"and",
"end",
"of",
"the",
"function",
"execution",
"if",
"doTrace",
"global",
"is",
"True"
] |
train
|
https://github.com/vokimon/python-qgmap/blob/9b01b48c5a8f4726938d38326f89644e7fb95f51/qgmap/__init__.py#L18-L25
|
vokimon/python-qgmap
|
qgmap/__init__.py
|
GeoCoder.geocode
|
def geocode(self, location) :
url = QtCore.QUrl("http://maps.googleapis.com/maps/api/geocode/xml")
url.addQueryItem("address", location)
url.addQueryItem("sensor", "false")
"""
url = QtCore.QUrl("http://maps.google.com/maps/geo/")
url.addQueryItem("q", location)
url.addQueryItem("output", "csv")
url.addQueryItem("sensor", "false")
"""
request = QtNetwork.QNetworkRequest(url)
reply = self.get(request)
while reply.isRunning() :
QtGui.QApplication.processEvents()
reply.deleteLater()
self.deleteLater()
return self._parseResult(reply)
|
python
|
def geocode(self, location) :
url = QtCore.QUrl("http://maps.googleapis.com/maps/api/geocode/xml")
url.addQueryItem("address", location)
url.addQueryItem("sensor", "false")
"""
url = QtCore.QUrl("http://maps.google.com/maps/geo/")
url.addQueryItem("q", location)
url.addQueryItem("output", "csv")
url.addQueryItem("sensor", "false")
"""
request = QtNetwork.QNetworkRequest(url)
reply = self.get(request)
while reply.isRunning() :
QtGui.QApplication.processEvents()
reply.deleteLater()
self.deleteLater()
return self._parseResult(reply)
|
[
"def",
"geocode",
"(",
"self",
",",
"location",
")",
":",
"url",
"=",
"QtCore",
".",
"QUrl",
"(",
"\"http://maps.googleapis.com/maps/api/geocode/xml\"",
")",
"url",
".",
"addQueryItem",
"(",
"\"address\"",
",",
"location",
")",
"url",
".",
"addQueryItem",
"(",
"\"sensor\"",
",",
"\"false\"",
")",
"request",
"=",
"QtNetwork",
".",
"QNetworkRequest",
"(",
"url",
")",
"reply",
"=",
"self",
".",
"get",
"(",
"request",
")",
"while",
"reply",
".",
"isRunning",
"(",
")",
":",
"QtGui",
".",
"QApplication",
".",
"processEvents",
"(",
")",
"reply",
".",
"deleteLater",
"(",
")",
"self",
".",
"deleteLater",
"(",
")",
"return",
"self",
".",
"_parseResult",
"(",
"reply",
")"
] |
url = QtCore.QUrl("http://maps.google.com/maps/geo/")
url.addQueryItem("q", location)
url.addQueryItem("output", "csv")
url.addQueryItem("sensor", "false")
|
[
"url",
"=",
"QtCore",
".",
"QUrl",
"(",
"http",
":",
"//",
"maps",
".",
"google",
".",
"com",
"/",
"maps",
"/",
"geo",
"/",
")",
"url",
".",
"addQueryItem",
"(",
"q",
"location",
")",
"url",
".",
"addQueryItem",
"(",
"output",
"csv",
")",
"url",
".",
"addQueryItem",
"(",
"sensor",
"false",
")"
] |
train
|
https://github.com/vokimon/python-qgmap/blob/9b01b48c5a8f4726938d38326f89644e7fb95f51/qgmap/__init__.py#L40-L57
|
tisimst/mcerp
|
mcerp/correlate.py
|
correlate
|
def correlate(params, corrmat):
"""
Force a correlation matrix on a set of statistically distributed objects.
This function works on objects in-place.
Parameters
----------
params : array
An array of of uv objects.
corrmat : 2d-array
The correlation matrix to be imposed
"""
# Make sure all inputs are compatible
assert all(
[isinstance(param, UncertainFunction) for param in params]
), 'All inputs to "correlate" must be of type "UncertainFunction"'
# Put each ufunc's samples in a column-wise matrix
data = np.vstack([param._mcpts for param in params]).T
# Apply the correlation matrix to the sampled data
new_data = induce_correlations(data, corrmat)
# Re-set the samples to the respective variables
for i in range(len(params)):
params[i]._mcpts = new_data[:, i]
|
python
|
def correlate(params, corrmat):
"""
Force a correlation matrix on a set of statistically distributed objects.
This function works on objects in-place.
Parameters
----------
params : array
An array of of uv objects.
corrmat : 2d-array
The correlation matrix to be imposed
"""
# Make sure all inputs are compatible
assert all(
[isinstance(param, UncertainFunction) for param in params]
), 'All inputs to "correlate" must be of type "UncertainFunction"'
# Put each ufunc's samples in a column-wise matrix
data = np.vstack([param._mcpts for param in params]).T
# Apply the correlation matrix to the sampled data
new_data = induce_correlations(data, corrmat)
# Re-set the samples to the respective variables
for i in range(len(params)):
params[i]._mcpts = new_data[:, i]
|
[
"def",
"correlate",
"(",
"params",
",",
"corrmat",
")",
":",
"# Make sure all inputs are compatible\r",
"assert",
"all",
"(",
"[",
"isinstance",
"(",
"param",
",",
"UncertainFunction",
")",
"for",
"param",
"in",
"params",
"]",
")",
",",
"'All inputs to \"correlate\" must be of type \"UncertainFunction\"'",
"# Put each ufunc's samples in a column-wise matrix\r",
"data",
"=",
"np",
".",
"vstack",
"(",
"[",
"param",
".",
"_mcpts",
"for",
"param",
"in",
"params",
"]",
")",
".",
"T",
"# Apply the correlation matrix to the sampled data\r",
"new_data",
"=",
"induce_correlations",
"(",
"data",
",",
"corrmat",
")",
"# Re-set the samples to the respective variables\r",
"for",
"i",
"in",
"range",
"(",
"len",
"(",
"params",
")",
")",
":",
"params",
"[",
"i",
"]",
".",
"_mcpts",
"=",
"new_data",
"[",
":",
",",
"i",
"]"
] |
Force a correlation matrix on a set of statistically distributed objects.
This function works on objects in-place.
Parameters
----------
params : array
An array of of uv objects.
corrmat : 2d-array
The correlation matrix to be imposed
|
[
"Force",
"a",
"correlation",
"matrix",
"on",
"a",
"set",
"of",
"statistically",
"distributed",
"objects",
".",
"This",
"function",
"works",
"on",
"objects",
"in",
"-",
"place",
".",
"Parameters",
"----------",
"params",
":",
"array",
"An",
"array",
"of",
"of",
"uv",
"objects",
".",
"corrmat",
":",
"2d",
"-",
"array",
"The",
"correlation",
"matrix",
"to",
"be",
"imposed"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/correlate.py#L8-L34
|
tisimst/mcerp
|
mcerp/correlate.py
|
induce_correlations
|
def induce_correlations(data, corrmat):
"""
Induce a set of correlations on a column-wise dataset
Parameters
----------
data : 2d-array
An m-by-n array where m is the number of samples and n is the
number of independent variables, each column of the array corresponding
to each variable
corrmat : 2d-array
An n-by-n array that defines the desired correlation coefficients
(between -1 and 1). Note: the matrix must be symmetric and
positive-definite in order to induce.
Returns
-------
new_data : 2d-array
An m-by-n array that has the desired correlations.
"""
# Create an rank-matrix
data_rank = np.vstack([rankdata(datai) for datai in data.T]).T
# Generate van der Waerden scores
data_rank_score = data_rank / (data_rank.shape[0] + 1.0)
data_rank_score = norm(0, 1).ppf(data_rank_score)
# Calculate the lower triangular matrix of the Cholesky decomposition
# of the desired correlation matrix
p = chol(corrmat)
# Calculate the current correlations
t = np.corrcoef(data_rank_score, rowvar=0)
# Calculate the lower triangular matrix of the Cholesky decomposition
# of the current correlation matrix
q = chol(t)
# Calculate the re-correlation matrix
s = np.dot(p, np.linalg.inv(q))
# Calculate the re-sampled matrix
new_data = np.dot(data_rank_score, s.T)
# Create the new rank matrix
new_data_rank = np.vstack([rankdata(datai) for datai in new_data.T]).T
# Sort the original data according to new_data_rank
for i in range(data.shape[1]):
vals, order = np.unique(
np.hstack((data_rank[:, i], new_data_rank[:, i])), return_inverse=True
)
old_order = order[: new_data_rank.shape[0]]
new_order = order[-new_data_rank.shape[0] :]
tmp = data[np.argsort(old_order), i][new_order]
data[:, i] = tmp[:]
return data
|
python
|
def induce_correlations(data, corrmat):
"""
Induce a set of correlations on a column-wise dataset
Parameters
----------
data : 2d-array
An m-by-n array where m is the number of samples and n is the
number of independent variables, each column of the array corresponding
to each variable
corrmat : 2d-array
An n-by-n array that defines the desired correlation coefficients
(between -1 and 1). Note: the matrix must be symmetric and
positive-definite in order to induce.
Returns
-------
new_data : 2d-array
An m-by-n array that has the desired correlations.
"""
# Create an rank-matrix
data_rank = np.vstack([rankdata(datai) for datai in data.T]).T
# Generate van der Waerden scores
data_rank_score = data_rank / (data_rank.shape[0] + 1.0)
data_rank_score = norm(0, 1).ppf(data_rank_score)
# Calculate the lower triangular matrix of the Cholesky decomposition
# of the desired correlation matrix
p = chol(corrmat)
# Calculate the current correlations
t = np.corrcoef(data_rank_score, rowvar=0)
# Calculate the lower triangular matrix of the Cholesky decomposition
# of the current correlation matrix
q = chol(t)
# Calculate the re-correlation matrix
s = np.dot(p, np.linalg.inv(q))
# Calculate the re-sampled matrix
new_data = np.dot(data_rank_score, s.T)
# Create the new rank matrix
new_data_rank = np.vstack([rankdata(datai) for datai in new_data.T]).T
# Sort the original data according to new_data_rank
for i in range(data.shape[1]):
vals, order = np.unique(
np.hstack((data_rank[:, i], new_data_rank[:, i])), return_inverse=True
)
old_order = order[: new_data_rank.shape[0]]
new_order = order[-new_data_rank.shape[0] :]
tmp = data[np.argsort(old_order), i][new_order]
data[:, i] = tmp[:]
return data
|
[
"def",
"induce_correlations",
"(",
"data",
",",
"corrmat",
")",
":",
"# Create an rank-matrix\r",
"data_rank",
"=",
"np",
".",
"vstack",
"(",
"[",
"rankdata",
"(",
"datai",
")",
"for",
"datai",
"in",
"data",
".",
"T",
"]",
")",
".",
"T",
"# Generate van der Waerden scores\r",
"data_rank_score",
"=",
"data_rank",
"/",
"(",
"data_rank",
".",
"shape",
"[",
"0",
"]",
"+",
"1.0",
")",
"data_rank_score",
"=",
"norm",
"(",
"0",
",",
"1",
")",
".",
"ppf",
"(",
"data_rank_score",
")",
"# Calculate the lower triangular matrix of the Cholesky decomposition\r",
"# of the desired correlation matrix\r",
"p",
"=",
"chol",
"(",
"corrmat",
")",
"# Calculate the current correlations\r",
"t",
"=",
"np",
".",
"corrcoef",
"(",
"data_rank_score",
",",
"rowvar",
"=",
"0",
")",
"# Calculate the lower triangular matrix of the Cholesky decomposition\r",
"# of the current correlation matrix\r",
"q",
"=",
"chol",
"(",
"t",
")",
"# Calculate the re-correlation matrix\r",
"s",
"=",
"np",
".",
"dot",
"(",
"p",
",",
"np",
".",
"linalg",
".",
"inv",
"(",
"q",
")",
")",
"# Calculate the re-sampled matrix\r",
"new_data",
"=",
"np",
".",
"dot",
"(",
"data_rank_score",
",",
"s",
".",
"T",
")",
"# Create the new rank matrix\r",
"new_data_rank",
"=",
"np",
".",
"vstack",
"(",
"[",
"rankdata",
"(",
"datai",
")",
"for",
"datai",
"in",
"new_data",
".",
"T",
"]",
")",
".",
"T",
"# Sort the original data according to new_data_rank\r",
"for",
"i",
"in",
"range",
"(",
"data",
".",
"shape",
"[",
"1",
"]",
")",
":",
"vals",
",",
"order",
"=",
"np",
".",
"unique",
"(",
"np",
".",
"hstack",
"(",
"(",
"data_rank",
"[",
":",
",",
"i",
"]",
",",
"new_data_rank",
"[",
":",
",",
"i",
"]",
")",
")",
",",
"return_inverse",
"=",
"True",
")",
"old_order",
"=",
"order",
"[",
":",
"new_data_rank",
".",
"shape",
"[",
"0",
"]",
"]",
"new_order",
"=",
"order",
"[",
"-",
"new_data_rank",
".",
"shape",
"[",
"0",
"]",
":",
"]",
"tmp",
"=",
"data",
"[",
"np",
".",
"argsort",
"(",
"old_order",
")",
",",
"i",
"]",
"[",
"new_order",
"]",
"data",
"[",
":",
",",
"i",
"]",
"=",
"tmp",
"[",
":",
"]",
"return",
"data"
] |
Induce a set of correlations on a column-wise dataset
Parameters
----------
data : 2d-array
An m-by-n array where m is the number of samples and n is the
number of independent variables, each column of the array corresponding
to each variable
corrmat : 2d-array
An n-by-n array that defines the desired correlation coefficients
(between -1 and 1). Note: the matrix must be symmetric and
positive-definite in order to induce.
Returns
-------
new_data : 2d-array
An m-by-n array that has the desired correlations.
|
[
"Induce",
"a",
"set",
"of",
"correlations",
"on",
"a",
"column",
"-",
"wise",
"dataset",
"Parameters",
"----------",
"data",
":",
"2d",
"-",
"array",
"An",
"m",
"-",
"by",
"-",
"n",
"array",
"where",
"m",
"is",
"the",
"number",
"of",
"samples",
"and",
"n",
"is",
"the",
"number",
"of",
"independent",
"variables",
"each",
"column",
"of",
"the",
"array",
"corresponding",
"to",
"each",
"variable",
"corrmat",
":",
"2d",
"-",
"array",
"An",
"n",
"-",
"by",
"-",
"n",
"array",
"that",
"defines",
"the",
"desired",
"correlation",
"coefficients",
"(",
"between",
"-",
"1",
"and",
"1",
")",
".",
"Note",
":",
"the",
"matrix",
"must",
"be",
"symmetric",
"and",
"positive",
"-",
"definite",
"in",
"order",
"to",
"induce",
".",
"Returns",
"-------",
"new_data",
":",
"2d",
"-",
"array",
"An",
"m",
"-",
"by",
"-",
"n",
"array",
"that",
"has",
"the",
"desired",
"correlations",
"."
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/correlate.py#L37-L95
|
tisimst/mcerp
|
mcerp/correlate.py
|
plotcorr
|
def plotcorr(X, plotargs=None, full=True, labels=None):
"""
Plots a scatterplot matrix of subplots.
Usage:
plotcorr(X)
plotcorr(..., plotargs=...) # e.g., 'r*', 'bo', etc.
plotcorr(..., full=...) # e.g., True or False
plotcorr(..., labels=...) # e.g., ['label1', 'label2', ...]
Each column of "X" is plotted against other columns, resulting in
a ncols by ncols grid of subplots with the diagonal subplots labeled
with "labels". "X" is an array of arrays (i.e., a 2d matrix), a 1d array
of MCERP.UncertainFunction/Variable objects, or a mixture of the two.
Additional keyword arguments are passed on to matplotlib's "plot" command.
Returns the matplotlib figure object containing the subplot grid.
"""
import matplotlib.pyplot as plt
X = [Xi._mcpts if isinstance(Xi, UncertainFunction) else Xi for Xi in X]
X = np.atleast_2d(X)
numvars, numdata = X.shape
fig, axes = plt.subplots(nrows=numvars, ncols=numvars, figsize=(8, 8))
fig.subplots_adjust(hspace=0.0, wspace=0.0)
for ax in axes.flat:
# Hide all ticks and labels
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
# Set up ticks only on one side for the "edge" subplots...
if full:
if ax.is_first_col():
ax.yaxis.set_ticks_position("left")
if ax.is_last_col():
ax.yaxis.set_ticks_position("right")
if ax.is_first_row():
ax.xaxis.set_ticks_position("top")
if ax.is_last_row():
ax.xaxis.set_ticks_position("bottom")
else:
if ax.is_first_row():
ax.xaxis.set_ticks_position("top")
if ax.is_last_col():
ax.yaxis.set_ticks_position("right")
# Label the diagonal subplots...
if not labels:
labels = ["x" + str(i) for i in range(numvars)]
for i, label in enumerate(labels):
axes[i, i].annotate(
label, (0.5, 0.5), xycoords="axes fraction", ha="center", va="center"
)
# Plot the data
for i, j in zip(*np.triu_indices_from(axes, k=1)):
if full:
idx = [(i, j), (j, i)]
else:
idx = [(i, j)]
for x, y in idx:
# FIX #1: this needed to be changed from ...(data[x], data[y],...)
if plotargs is None:
if len(X[x]) > 100:
plotargs = ",b" # pixel marker
else:
plotargs = ".b" # point marker
axes[x, y].plot(X[y], X[x], plotargs)
ylim = min(X[y]), max(X[y])
xlim = min(X[x]), max(X[x])
axes[x, y].set_ylim(
xlim[0] - (xlim[1] - xlim[0]) * 0.1, xlim[1] + (xlim[1] - xlim[0]) * 0.1
)
axes[x, y].set_xlim(
ylim[0] - (ylim[1] - ylim[0]) * 0.1, ylim[1] + (ylim[1] - ylim[0]) * 0.1
)
# Turn on the proper x or y axes ticks.
if full:
for i, j in zip(list(range(numvars)), itertools.cycle((-1, 0))):
axes[j, i].xaxis.set_visible(True)
axes[i, j].yaxis.set_visible(True)
else:
for i in range(numvars - 1):
axes[0, i + 1].xaxis.set_visible(True)
axes[i, -1].yaxis.set_visible(True)
for i in range(1, numvars):
for j in range(0, i):
fig.delaxes(axes[i, j])
# FIX #2: if numvars is odd, the bottom right corner plot doesn't have the
# correct axes limits, so we pull them from other axes
if numvars % 2:
xlimits = axes[0, -1].get_xlim()
ylimits = axes[-1, 0].get_ylim()
axes[-1, -1].set_xlim(xlimits)
axes[-1, -1].set_ylim(ylimits)
return fig
|
python
|
def plotcorr(X, plotargs=None, full=True, labels=None):
"""
Plots a scatterplot matrix of subplots.
Usage:
plotcorr(X)
plotcorr(..., plotargs=...) # e.g., 'r*', 'bo', etc.
plotcorr(..., full=...) # e.g., True or False
plotcorr(..., labels=...) # e.g., ['label1', 'label2', ...]
Each column of "X" is plotted against other columns, resulting in
a ncols by ncols grid of subplots with the diagonal subplots labeled
with "labels". "X" is an array of arrays (i.e., a 2d matrix), a 1d array
of MCERP.UncertainFunction/Variable objects, or a mixture of the two.
Additional keyword arguments are passed on to matplotlib's "plot" command.
Returns the matplotlib figure object containing the subplot grid.
"""
import matplotlib.pyplot as plt
X = [Xi._mcpts if isinstance(Xi, UncertainFunction) else Xi for Xi in X]
X = np.atleast_2d(X)
numvars, numdata = X.shape
fig, axes = plt.subplots(nrows=numvars, ncols=numvars, figsize=(8, 8))
fig.subplots_adjust(hspace=0.0, wspace=0.0)
for ax in axes.flat:
# Hide all ticks and labels
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
# Set up ticks only on one side for the "edge" subplots...
if full:
if ax.is_first_col():
ax.yaxis.set_ticks_position("left")
if ax.is_last_col():
ax.yaxis.set_ticks_position("right")
if ax.is_first_row():
ax.xaxis.set_ticks_position("top")
if ax.is_last_row():
ax.xaxis.set_ticks_position("bottom")
else:
if ax.is_first_row():
ax.xaxis.set_ticks_position("top")
if ax.is_last_col():
ax.yaxis.set_ticks_position("right")
# Label the diagonal subplots...
if not labels:
labels = ["x" + str(i) for i in range(numvars)]
for i, label in enumerate(labels):
axes[i, i].annotate(
label, (0.5, 0.5), xycoords="axes fraction", ha="center", va="center"
)
# Plot the data
for i, j in zip(*np.triu_indices_from(axes, k=1)):
if full:
idx = [(i, j), (j, i)]
else:
idx = [(i, j)]
for x, y in idx:
# FIX #1: this needed to be changed from ...(data[x], data[y],...)
if plotargs is None:
if len(X[x]) > 100:
plotargs = ",b" # pixel marker
else:
plotargs = ".b" # point marker
axes[x, y].plot(X[y], X[x], plotargs)
ylim = min(X[y]), max(X[y])
xlim = min(X[x]), max(X[x])
axes[x, y].set_ylim(
xlim[0] - (xlim[1] - xlim[0]) * 0.1, xlim[1] + (xlim[1] - xlim[0]) * 0.1
)
axes[x, y].set_xlim(
ylim[0] - (ylim[1] - ylim[0]) * 0.1, ylim[1] + (ylim[1] - ylim[0]) * 0.1
)
# Turn on the proper x or y axes ticks.
if full:
for i, j in zip(list(range(numvars)), itertools.cycle((-1, 0))):
axes[j, i].xaxis.set_visible(True)
axes[i, j].yaxis.set_visible(True)
else:
for i in range(numvars - 1):
axes[0, i + 1].xaxis.set_visible(True)
axes[i, -1].yaxis.set_visible(True)
for i in range(1, numvars):
for j in range(0, i):
fig.delaxes(axes[i, j])
# FIX #2: if numvars is odd, the bottom right corner plot doesn't have the
# correct axes limits, so we pull them from other axes
if numvars % 2:
xlimits = axes[0, -1].get_xlim()
ylimits = axes[-1, 0].get_ylim()
axes[-1, -1].set_xlim(xlimits)
axes[-1, -1].set_ylim(ylimits)
return fig
|
[
"def",
"plotcorr",
"(",
"X",
",",
"plotargs",
"=",
"None",
",",
"full",
"=",
"True",
",",
"labels",
"=",
"None",
")",
":",
"import",
"matplotlib",
".",
"pyplot",
"as",
"plt",
"X",
"=",
"[",
"Xi",
".",
"_mcpts",
"if",
"isinstance",
"(",
"Xi",
",",
"UncertainFunction",
")",
"else",
"Xi",
"for",
"Xi",
"in",
"X",
"]",
"X",
"=",
"np",
".",
"atleast_2d",
"(",
"X",
")",
"numvars",
",",
"numdata",
"=",
"X",
".",
"shape",
"fig",
",",
"axes",
"=",
"plt",
".",
"subplots",
"(",
"nrows",
"=",
"numvars",
",",
"ncols",
"=",
"numvars",
",",
"figsize",
"=",
"(",
"8",
",",
"8",
")",
")",
"fig",
".",
"subplots_adjust",
"(",
"hspace",
"=",
"0.0",
",",
"wspace",
"=",
"0.0",
")",
"for",
"ax",
"in",
"axes",
".",
"flat",
":",
"# Hide all ticks and labels\r",
"ax",
".",
"xaxis",
".",
"set_visible",
"(",
"False",
")",
"ax",
".",
"yaxis",
".",
"set_visible",
"(",
"False",
")",
"# Set up ticks only on one side for the \"edge\" subplots...\r",
"if",
"full",
":",
"if",
"ax",
".",
"is_first_col",
"(",
")",
":",
"ax",
".",
"yaxis",
".",
"set_ticks_position",
"(",
"\"left\"",
")",
"if",
"ax",
".",
"is_last_col",
"(",
")",
":",
"ax",
".",
"yaxis",
".",
"set_ticks_position",
"(",
"\"right\"",
")",
"if",
"ax",
".",
"is_first_row",
"(",
")",
":",
"ax",
".",
"xaxis",
".",
"set_ticks_position",
"(",
"\"top\"",
")",
"if",
"ax",
".",
"is_last_row",
"(",
")",
":",
"ax",
".",
"xaxis",
".",
"set_ticks_position",
"(",
"\"bottom\"",
")",
"else",
":",
"if",
"ax",
".",
"is_first_row",
"(",
")",
":",
"ax",
".",
"xaxis",
".",
"set_ticks_position",
"(",
"\"top\"",
")",
"if",
"ax",
".",
"is_last_col",
"(",
")",
":",
"ax",
".",
"yaxis",
".",
"set_ticks_position",
"(",
"\"right\"",
")",
"# Label the diagonal subplots...\r",
"if",
"not",
"labels",
":",
"labels",
"=",
"[",
"\"x\"",
"+",
"str",
"(",
"i",
")",
"for",
"i",
"in",
"range",
"(",
"numvars",
")",
"]",
"for",
"i",
",",
"label",
"in",
"enumerate",
"(",
"labels",
")",
":",
"axes",
"[",
"i",
",",
"i",
"]",
".",
"annotate",
"(",
"label",
",",
"(",
"0.5",
",",
"0.5",
")",
",",
"xycoords",
"=",
"\"axes fraction\"",
",",
"ha",
"=",
"\"center\"",
",",
"va",
"=",
"\"center\"",
")",
"# Plot the data\r",
"for",
"i",
",",
"j",
"in",
"zip",
"(",
"*",
"np",
".",
"triu_indices_from",
"(",
"axes",
",",
"k",
"=",
"1",
")",
")",
":",
"if",
"full",
":",
"idx",
"=",
"[",
"(",
"i",
",",
"j",
")",
",",
"(",
"j",
",",
"i",
")",
"]",
"else",
":",
"idx",
"=",
"[",
"(",
"i",
",",
"j",
")",
"]",
"for",
"x",
",",
"y",
"in",
"idx",
":",
"# FIX #1: this needed to be changed from ...(data[x], data[y],...)\r",
"if",
"plotargs",
"is",
"None",
":",
"if",
"len",
"(",
"X",
"[",
"x",
"]",
")",
">",
"100",
":",
"plotargs",
"=",
"\",b\"",
"# pixel marker\r",
"else",
":",
"plotargs",
"=",
"\".b\"",
"# point marker\r",
"axes",
"[",
"x",
",",
"y",
"]",
".",
"plot",
"(",
"X",
"[",
"y",
"]",
",",
"X",
"[",
"x",
"]",
",",
"plotargs",
")",
"ylim",
"=",
"min",
"(",
"X",
"[",
"y",
"]",
")",
",",
"max",
"(",
"X",
"[",
"y",
"]",
")",
"xlim",
"=",
"min",
"(",
"X",
"[",
"x",
"]",
")",
",",
"max",
"(",
"X",
"[",
"x",
"]",
")",
"axes",
"[",
"x",
",",
"y",
"]",
".",
"set_ylim",
"(",
"xlim",
"[",
"0",
"]",
"-",
"(",
"xlim",
"[",
"1",
"]",
"-",
"xlim",
"[",
"0",
"]",
")",
"*",
"0.1",
",",
"xlim",
"[",
"1",
"]",
"+",
"(",
"xlim",
"[",
"1",
"]",
"-",
"xlim",
"[",
"0",
"]",
")",
"*",
"0.1",
")",
"axes",
"[",
"x",
",",
"y",
"]",
".",
"set_xlim",
"(",
"ylim",
"[",
"0",
"]",
"-",
"(",
"ylim",
"[",
"1",
"]",
"-",
"ylim",
"[",
"0",
"]",
")",
"*",
"0.1",
",",
"ylim",
"[",
"1",
"]",
"+",
"(",
"ylim",
"[",
"1",
"]",
"-",
"ylim",
"[",
"0",
"]",
")",
"*",
"0.1",
")",
"# Turn on the proper x or y axes ticks.\r",
"if",
"full",
":",
"for",
"i",
",",
"j",
"in",
"zip",
"(",
"list",
"(",
"range",
"(",
"numvars",
")",
")",
",",
"itertools",
".",
"cycle",
"(",
"(",
"-",
"1",
",",
"0",
")",
")",
")",
":",
"axes",
"[",
"j",
",",
"i",
"]",
".",
"xaxis",
".",
"set_visible",
"(",
"True",
")",
"axes",
"[",
"i",
",",
"j",
"]",
".",
"yaxis",
".",
"set_visible",
"(",
"True",
")",
"else",
":",
"for",
"i",
"in",
"range",
"(",
"numvars",
"-",
"1",
")",
":",
"axes",
"[",
"0",
",",
"i",
"+",
"1",
"]",
".",
"xaxis",
".",
"set_visible",
"(",
"True",
")",
"axes",
"[",
"i",
",",
"-",
"1",
"]",
".",
"yaxis",
".",
"set_visible",
"(",
"True",
")",
"for",
"i",
"in",
"range",
"(",
"1",
",",
"numvars",
")",
":",
"for",
"j",
"in",
"range",
"(",
"0",
",",
"i",
")",
":",
"fig",
".",
"delaxes",
"(",
"axes",
"[",
"i",
",",
"j",
"]",
")",
"# FIX #2: if numvars is odd, the bottom right corner plot doesn't have the\r",
"# correct axes limits, so we pull them from other axes\r",
"if",
"numvars",
"%",
"2",
":",
"xlimits",
"=",
"axes",
"[",
"0",
",",
"-",
"1",
"]",
".",
"get_xlim",
"(",
")",
"ylimits",
"=",
"axes",
"[",
"-",
"1",
",",
"0",
"]",
".",
"get_ylim",
"(",
")",
"axes",
"[",
"-",
"1",
",",
"-",
"1",
"]",
".",
"set_xlim",
"(",
"xlimits",
")",
"axes",
"[",
"-",
"1",
",",
"-",
"1",
"]",
".",
"set_ylim",
"(",
"ylimits",
")",
"return",
"fig"
] |
Plots a scatterplot matrix of subplots.
Usage:
plotcorr(X)
plotcorr(..., plotargs=...) # e.g., 'r*', 'bo', etc.
plotcorr(..., full=...) # e.g., True or False
plotcorr(..., labels=...) # e.g., ['label1', 'label2', ...]
Each column of "X" is plotted against other columns, resulting in
a ncols by ncols grid of subplots with the diagonal subplots labeled
with "labels". "X" is an array of arrays (i.e., a 2d matrix), a 1d array
of MCERP.UncertainFunction/Variable objects, or a mixture of the two.
Additional keyword arguments are passed on to matplotlib's "plot" command.
Returns the matplotlib figure object containing the subplot grid.
|
[
"Plots",
"a",
"scatterplot",
"matrix",
"of",
"subplots",
".",
"Usage",
":",
"plotcorr",
"(",
"X",
")",
"plotcorr",
"(",
"...",
"plotargs",
"=",
"...",
")",
"#",
"e",
".",
"g",
".",
"r",
"*",
"bo",
"etc",
".",
"plotcorr",
"(",
"...",
"full",
"=",
"...",
")",
"#",
"e",
".",
"g",
".",
"True",
"or",
"False",
"plotcorr",
"(",
"...",
"labels",
"=",
"...",
")",
"#",
"e",
".",
"g",
".",
"[",
"label1",
"label2",
"...",
"]",
"Each",
"column",
"of",
"X",
"is",
"plotted",
"against",
"other",
"columns",
"resulting",
"in",
"a",
"ncols",
"by",
"ncols",
"grid",
"of",
"subplots",
"with",
"the",
"diagonal",
"subplots",
"labeled",
"with",
"labels",
".",
"X",
"is",
"an",
"array",
"of",
"arrays",
"(",
"i",
".",
"e",
".",
"a",
"2d",
"matrix",
")",
"a",
"1d",
"array",
"of",
"MCERP",
".",
"UncertainFunction",
"/",
"Variable",
"objects",
"or",
"a",
"mixture",
"of",
"the",
"two",
".",
"Additional",
"keyword",
"arguments",
"are",
"passed",
"on",
"to",
"matplotlib",
"s",
"plot",
"command",
".",
"Returns",
"the",
"matplotlib",
"figure",
"object",
"containing",
"the",
"subplot",
"grid",
"."
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/correlate.py#L98-L201
|
tisimst/mcerp
|
mcerp/correlate.py
|
chol
|
def chol(A):
"""
Calculate the lower triangular matrix of the Cholesky decomposition of
a symmetric, positive-definite matrix.
"""
A = np.array(A)
assert A.shape[0] == A.shape[1], "Input matrix must be square"
L = [[0.0] * len(A) for _ in range(len(A))]
for i in range(len(A)):
for j in range(i + 1):
s = sum(L[i][k] * L[j][k] for k in range(j))
L[i][j] = (
(A[i][i] - s) ** 0.5 if (i == j) else (1.0 / L[j][j] * (A[i][j] - s))
)
return np.array(L)
|
python
|
def chol(A):
"""
Calculate the lower triangular matrix of the Cholesky decomposition of
a symmetric, positive-definite matrix.
"""
A = np.array(A)
assert A.shape[0] == A.shape[1], "Input matrix must be square"
L = [[0.0] * len(A) for _ in range(len(A))]
for i in range(len(A)):
for j in range(i + 1):
s = sum(L[i][k] * L[j][k] for k in range(j))
L[i][j] = (
(A[i][i] - s) ** 0.5 if (i == j) else (1.0 / L[j][j] * (A[i][j] - s))
)
return np.array(L)
|
[
"def",
"chol",
"(",
"A",
")",
":",
"A",
"=",
"np",
".",
"array",
"(",
"A",
")",
"assert",
"A",
".",
"shape",
"[",
"0",
"]",
"==",
"A",
".",
"shape",
"[",
"1",
"]",
",",
"\"Input matrix must be square\"",
"L",
"=",
"[",
"[",
"0.0",
"]",
"*",
"len",
"(",
"A",
")",
"for",
"_",
"in",
"range",
"(",
"len",
"(",
"A",
")",
")",
"]",
"for",
"i",
"in",
"range",
"(",
"len",
"(",
"A",
")",
")",
":",
"for",
"j",
"in",
"range",
"(",
"i",
"+",
"1",
")",
":",
"s",
"=",
"sum",
"(",
"L",
"[",
"i",
"]",
"[",
"k",
"]",
"*",
"L",
"[",
"j",
"]",
"[",
"k",
"]",
"for",
"k",
"in",
"range",
"(",
"j",
")",
")",
"L",
"[",
"i",
"]",
"[",
"j",
"]",
"=",
"(",
"(",
"A",
"[",
"i",
"]",
"[",
"i",
"]",
"-",
"s",
")",
"**",
"0.5",
"if",
"(",
"i",
"==",
"j",
")",
"else",
"(",
"1.0",
"/",
"L",
"[",
"j",
"]",
"[",
"j",
"]",
"*",
"(",
"A",
"[",
"i",
"]",
"[",
"j",
"]",
"-",
"s",
")",
")",
")",
"return",
"np",
".",
"array",
"(",
"L",
")"
] |
Calculate the lower triangular matrix of the Cholesky decomposition of
a symmetric, positive-definite matrix.
|
[
"Calculate",
"the",
"lower",
"triangular",
"matrix",
"of",
"the",
"Cholesky",
"decomposition",
"of",
"a",
"symmetric",
"positive",
"-",
"definite",
"matrix",
"."
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/correlate.py#L204-L220
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.get
|
def get(self, uri, params={}):
'''A generic method to make GET requests to the OpenDNS Investigate API
on the given URI.
'''
return self._session.get(urljoin(Investigate.BASE_URL, uri),
params=params, headers=self._auth_header, proxies=self.proxies
)
|
python
|
def get(self, uri, params={}):
'''A generic method to make GET requests to the OpenDNS Investigate API
on the given URI.
'''
return self._session.get(urljoin(Investigate.BASE_URL, uri),
params=params, headers=self._auth_header, proxies=self.proxies
)
|
[
"def",
"get",
"(",
"self",
",",
"uri",
",",
"params",
"=",
"{",
"}",
")",
":",
"return",
"self",
".",
"_session",
".",
"get",
"(",
"urljoin",
"(",
"Investigate",
".",
"BASE_URL",
",",
"uri",
")",
",",
"params",
"=",
"params",
",",
"headers",
"=",
"self",
".",
"_auth_header",
",",
"proxies",
"=",
"self",
".",
"proxies",
")"
] |
A generic method to make GET requests to the OpenDNS Investigate API
on the given URI.
|
[
"A",
"generic",
"method",
"to",
"make",
"GET",
"requests",
"to",
"the",
"OpenDNS",
"Investigate",
"API",
"on",
"the",
"given",
"URI",
"."
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L62-L68
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.post
|
def post(self, uri, params={}, data={}):
'''A generic method to make POST requests to the OpenDNS Investigate API
on the given URI.
'''
return self._session.post(
urljoin(Investigate.BASE_URL, uri),
params=params, data=data, headers=self._auth_header,
proxies=self.proxies
)
|
python
|
def post(self, uri, params={}, data={}):
'''A generic method to make POST requests to the OpenDNS Investigate API
on the given URI.
'''
return self._session.post(
urljoin(Investigate.BASE_URL, uri),
params=params, data=data, headers=self._auth_header,
proxies=self.proxies
)
|
[
"def",
"post",
"(",
"self",
",",
"uri",
",",
"params",
"=",
"{",
"}",
",",
"data",
"=",
"{",
"}",
")",
":",
"return",
"self",
".",
"_session",
".",
"post",
"(",
"urljoin",
"(",
"Investigate",
".",
"BASE_URL",
",",
"uri",
")",
",",
"params",
"=",
"params",
",",
"data",
"=",
"data",
",",
"headers",
"=",
"self",
".",
"_auth_header",
",",
"proxies",
"=",
"self",
".",
"proxies",
")"
] |
A generic method to make POST requests to the OpenDNS Investigate API
on the given URI.
|
[
"A",
"generic",
"method",
"to",
"make",
"POST",
"requests",
"to",
"the",
"OpenDNS",
"Investigate",
"API",
"on",
"the",
"given",
"URI",
"."
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L70-L78
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.get_parse
|
def get_parse(self, uri, params={}):
'''Convenience method to call get() on an arbitrary URI and parse the response
into a JSON object. Raises an error on non-200 response status.
'''
return self._request_parse(self.get, uri, params)
|
python
|
def get_parse(self, uri, params={}):
'''Convenience method to call get() on an arbitrary URI and parse the response
into a JSON object. Raises an error on non-200 response status.
'''
return self._request_parse(self.get, uri, params)
|
[
"def",
"get_parse",
"(",
"self",
",",
"uri",
",",
"params",
"=",
"{",
"}",
")",
":",
"return",
"self",
".",
"_request_parse",
"(",
"self",
".",
"get",
",",
"uri",
",",
"params",
")"
] |
Convenience method to call get() on an arbitrary URI and parse the response
into a JSON object. Raises an error on non-200 response status.
|
[
"Convenience",
"method",
"to",
"call",
"get",
"()",
"on",
"an",
"arbitrary",
"URI",
"and",
"parse",
"the",
"response",
"into",
"a",
"JSON",
"object",
".",
"Raises",
"an",
"error",
"on",
"non",
"-",
"200",
"response",
"status",
"."
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L85-L89
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.post_parse
|
def post_parse(self, uri, params={}, data={}):
'''Convenience method to call post() on an arbitrary URI and parse the response
into a JSON object. Raises an error on non-200 response status.
'''
return self._request_parse(self.post, uri, params, data)
|
python
|
def post_parse(self, uri, params={}, data={}):
'''Convenience method to call post() on an arbitrary URI and parse the response
into a JSON object. Raises an error on non-200 response status.
'''
return self._request_parse(self.post, uri, params, data)
|
[
"def",
"post_parse",
"(",
"self",
",",
"uri",
",",
"params",
"=",
"{",
"}",
",",
"data",
"=",
"{",
"}",
")",
":",
"return",
"self",
".",
"_request_parse",
"(",
"self",
".",
"post",
",",
"uri",
",",
"params",
",",
"data",
")"
] |
Convenience method to call post() on an arbitrary URI and parse the response
into a JSON object. Raises an error on non-200 response status.
|
[
"Convenience",
"method",
"to",
"call",
"post",
"()",
"on",
"an",
"arbitrary",
"URI",
"and",
"parse",
"the",
"response",
"into",
"a",
"JSON",
"object",
".",
"Raises",
"an",
"error",
"on",
"non",
"-",
"200",
"response",
"status",
"."
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L91-L95
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.categorization
|
def categorization(self, domains, labels=False):
'''Get the domain status and categorization of a domain or list of domains.
'domains' can be either a single domain, or a list of domains.
Setting 'labels' to True will give back categorizations in human-readable
form.
For more detail, see https://investigate.umbrella.com/docs/api#categorization
'''
if type(domains) is str:
return self._get_categorization(domains, labels)
elif type(domains) is list:
return self._post_categorization(domains, labels)
else:
raise Investigate.DOMAIN_ERR
|
python
|
def categorization(self, domains, labels=False):
'''Get the domain status and categorization of a domain or list of domains.
'domains' can be either a single domain, or a list of domains.
Setting 'labels' to True will give back categorizations in human-readable
form.
For more detail, see https://investigate.umbrella.com/docs/api#categorization
'''
if type(domains) is str:
return self._get_categorization(domains, labels)
elif type(domains) is list:
return self._post_categorization(domains, labels)
else:
raise Investigate.DOMAIN_ERR
|
[
"def",
"categorization",
"(",
"self",
",",
"domains",
",",
"labels",
"=",
"False",
")",
":",
"if",
"type",
"(",
"domains",
")",
"is",
"str",
":",
"return",
"self",
".",
"_get_categorization",
"(",
"domains",
",",
"labels",
")",
"elif",
"type",
"(",
"domains",
")",
"is",
"list",
":",
"return",
"self",
".",
"_post_categorization",
"(",
"domains",
",",
"labels",
")",
"else",
":",
"raise",
"Investigate",
".",
"DOMAIN_ERR"
] |
Get the domain status and categorization of a domain or list of domains.
'domains' can be either a single domain, or a list of domains.
Setting 'labels' to True will give back categorizations in human-readable
form.
For more detail, see https://investigate.umbrella.com/docs/api#categorization
|
[
"Get",
"the",
"domain",
"status",
"and",
"categorization",
"of",
"a",
"domain",
"or",
"list",
"of",
"domains",
".",
"domains",
"can",
"be",
"either",
"a",
"single",
"domain",
"or",
"a",
"list",
"of",
"domains",
".",
"Setting",
"labels",
"to",
"True",
"will",
"give",
"back",
"categorizations",
"in",
"human",
"-",
"readable",
"form",
"."
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L108-L121
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.cooccurrences
|
def cooccurrences(self, domain):
'''Get the cooccurrences of the given domain.
For details, see https://investigate.umbrella.com/docs/api#co-occurrences
'''
uri = self._uris["cooccurrences"].format(domain)
return self.get_parse(uri)
|
python
|
def cooccurrences(self, domain):
'''Get the cooccurrences of the given domain.
For details, see https://investigate.umbrella.com/docs/api#co-occurrences
'''
uri = self._uris["cooccurrences"].format(domain)
return self.get_parse(uri)
|
[
"def",
"cooccurrences",
"(",
"self",
",",
"domain",
")",
":",
"uri",
"=",
"self",
".",
"_uris",
"[",
"\"cooccurrences\"",
"]",
".",
"format",
"(",
"domain",
")",
"return",
"self",
".",
"get_parse",
"(",
"uri",
")"
] |
Get the cooccurrences of the given domain.
For details, see https://investigate.umbrella.com/docs/api#co-occurrences
|
[
"Get",
"the",
"cooccurrences",
"of",
"the",
"given",
"domain",
"."
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L123-L129
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.related
|
def related(self, domain):
'''Get the related domains of the given domain.
For details, see https://investigate.umbrella.com/docs/api#relatedDomains
'''
uri = self._uris["related"].format(domain)
return self.get_parse(uri)
|
python
|
def related(self, domain):
'''Get the related domains of the given domain.
For details, see https://investigate.umbrella.com/docs/api#relatedDomains
'''
uri = self._uris["related"].format(domain)
return self.get_parse(uri)
|
[
"def",
"related",
"(",
"self",
",",
"domain",
")",
":",
"uri",
"=",
"self",
".",
"_uris",
"[",
"\"related\"",
"]",
".",
"format",
"(",
"domain",
")",
"return",
"self",
".",
"get_parse",
"(",
"uri",
")"
] |
Get the related domains of the given domain.
For details, see https://investigate.umbrella.com/docs/api#relatedDomains
|
[
"Get",
"the",
"related",
"domains",
"of",
"the",
"given",
"domain",
"."
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L131-L137
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.security
|
def security(self, domain):
'''Get the Security Information for the given domain.
For details, see https://investigate.umbrella.com/docs/api#securityInfo
'''
uri = self._uris["security"].format(domain)
return self.get_parse(uri)
|
python
|
def security(self, domain):
'''Get the Security Information for the given domain.
For details, see https://investigate.umbrella.com/docs/api#securityInfo
'''
uri = self._uris["security"].format(domain)
return self.get_parse(uri)
|
[
"def",
"security",
"(",
"self",
",",
"domain",
")",
":",
"uri",
"=",
"self",
".",
"_uris",
"[",
"\"security\"",
"]",
".",
"format",
"(",
"domain",
")",
"return",
"self",
".",
"get_parse",
"(",
"uri",
")"
] |
Get the Security Information for the given domain.
For details, see https://investigate.umbrella.com/docs/api#securityInfo
|
[
"Get",
"the",
"Security",
"Information",
"for",
"the",
"given",
"domain",
"."
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L139-L145
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.rr_history
|
def rr_history(self, query, query_type="A"):
'''Get the RR (Resource Record) History of the given domain or IP.
The default query type is for 'A' records, but the following query types
are supported:
A, NS, MX, TXT, CNAME
For details, see https://investigate.umbrella.com/docs/api#dnsrr_domain
'''
if query_type not in Investigate.SUPPORTED_DNS_TYPES:
raise Investigate.UNSUPPORTED_DNS_QUERY
# if this is an IP address, query the IP
if Investigate.IP_PATTERN.match(query):
return self._ip_rr_history(query, query_type)
# otherwise, query the domain
return self._domain_rr_history(query, query_type)
|
python
|
def rr_history(self, query, query_type="A"):
'''Get the RR (Resource Record) History of the given domain or IP.
The default query type is for 'A' records, but the following query types
are supported:
A, NS, MX, TXT, CNAME
For details, see https://investigate.umbrella.com/docs/api#dnsrr_domain
'''
if query_type not in Investigate.SUPPORTED_DNS_TYPES:
raise Investigate.UNSUPPORTED_DNS_QUERY
# if this is an IP address, query the IP
if Investigate.IP_PATTERN.match(query):
return self._ip_rr_history(query, query_type)
# otherwise, query the domain
return self._domain_rr_history(query, query_type)
|
[
"def",
"rr_history",
"(",
"self",
",",
"query",
",",
"query_type",
"=",
"\"A\"",
")",
":",
"if",
"query_type",
"not",
"in",
"Investigate",
".",
"SUPPORTED_DNS_TYPES",
":",
"raise",
"Investigate",
".",
"UNSUPPORTED_DNS_QUERY",
"# if this is an IP address, query the IP",
"if",
"Investigate",
".",
"IP_PATTERN",
".",
"match",
"(",
"query",
")",
":",
"return",
"self",
".",
"_ip_rr_history",
"(",
"query",
",",
"query_type",
")",
"# otherwise, query the domain",
"return",
"self",
".",
"_domain_rr_history",
"(",
"query",
",",
"query_type",
")"
] |
Get the RR (Resource Record) History of the given domain or IP.
The default query type is for 'A' records, but the following query types
are supported:
A, NS, MX, TXT, CNAME
For details, see https://investigate.umbrella.com/docs/api#dnsrr_domain
|
[
"Get",
"the",
"RR",
"(",
"Resource",
"Record",
")",
"History",
"of",
"the",
"given",
"domain",
"or",
"IP",
".",
"The",
"default",
"query",
"type",
"is",
"for",
"A",
"records",
"but",
"the",
"following",
"query",
"types",
"are",
"supported",
":"
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L155-L172
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.domain_whois
|
def domain_whois(self, domain):
'''Gets whois information for a domain'''
uri = self._uris["whois_domain"].format(domain)
resp_json = self.get_parse(uri)
return resp_json
|
python
|
def domain_whois(self, domain):
'''Gets whois information for a domain'''
uri = self._uris["whois_domain"].format(domain)
resp_json = self.get_parse(uri)
return resp_json
|
[
"def",
"domain_whois",
"(",
"self",
",",
"domain",
")",
":",
"uri",
"=",
"self",
".",
"_uris",
"[",
"\"whois_domain\"",
"]",
".",
"format",
"(",
"domain",
")",
"resp_json",
"=",
"self",
".",
"get_parse",
"(",
"uri",
")",
"return",
"resp_json"
] |
Gets whois information for a domain
|
[
"Gets",
"whois",
"information",
"for",
"a",
"domain"
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L187-L191
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.domain_whois_history
|
def domain_whois_history(self, domain, limit=None):
'''Gets whois history for a domain'''
params = dict()
if limit is not None:
params['limit'] = limit
uri = self._uris["whois_domain_history"].format(domain)
resp_json = self.get_parse(uri, params)
return resp_json
|
python
|
def domain_whois_history(self, domain, limit=None):
'''Gets whois history for a domain'''
params = dict()
if limit is not None:
params['limit'] = limit
uri = self._uris["whois_domain_history"].format(domain)
resp_json = self.get_parse(uri, params)
return resp_json
|
[
"def",
"domain_whois_history",
"(",
"self",
",",
"domain",
",",
"limit",
"=",
"None",
")",
":",
"params",
"=",
"dict",
"(",
")",
"if",
"limit",
"is",
"not",
"None",
":",
"params",
"[",
"'limit'",
"]",
"=",
"limit",
"uri",
"=",
"self",
".",
"_uris",
"[",
"\"whois_domain_history\"",
"]",
".",
"format",
"(",
"domain",
")",
"resp_json",
"=",
"self",
".",
"get_parse",
"(",
"uri",
",",
"params",
")",
"return",
"resp_json"
] |
Gets whois history for a domain
|
[
"Gets",
"whois",
"history",
"for",
"a",
"domain"
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L193-L202
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.ns_whois
|
def ns_whois(self, nameservers, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, sort_field=DEFAULT_SORT):
'''Gets the domains that have been registered with a nameserver or
nameservers'''
if not isinstance(nameservers, list):
uri = self._uris["whois_ns"].format(nameservers)
params = {'limit': limit, 'offset': offset, 'sortField': sort_field}
else:
uri = self._uris["whois_ns"].format('')
params = {'emailList' : ','.join(nameservers), 'limit': limit, 'offset': offset, 'sortField': sort_field}
resp_json = self.get_parse(uri, params=params)
return resp_json
|
python
|
def ns_whois(self, nameservers, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, sort_field=DEFAULT_SORT):
'''Gets the domains that have been registered with a nameserver or
nameservers'''
if not isinstance(nameservers, list):
uri = self._uris["whois_ns"].format(nameservers)
params = {'limit': limit, 'offset': offset, 'sortField': sort_field}
else:
uri = self._uris["whois_ns"].format('')
params = {'emailList' : ','.join(nameservers), 'limit': limit, 'offset': offset, 'sortField': sort_field}
resp_json = self.get_parse(uri, params=params)
return resp_json
|
[
"def",
"ns_whois",
"(",
"self",
",",
"nameservers",
",",
"limit",
"=",
"DEFAULT_LIMIT",
",",
"offset",
"=",
"DEFAULT_OFFSET",
",",
"sort_field",
"=",
"DEFAULT_SORT",
")",
":",
"if",
"not",
"isinstance",
"(",
"nameservers",
",",
"list",
")",
":",
"uri",
"=",
"self",
".",
"_uris",
"[",
"\"whois_ns\"",
"]",
".",
"format",
"(",
"nameservers",
")",
"params",
"=",
"{",
"'limit'",
":",
"limit",
",",
"'offset'",
":",
"offset",
",",
"'sortField'",
":",
"sort_field",
"}",
"else",
":",
"uri",
"=",
"self",
".",
"_uris",
"[",
"\"whois_ns\"",
"]",
".",
"format",
"(",
"''",
")",
"params",
"=",
"{",
"'emailList'",
":",
"','",
".",
"join",
"(",
"nameservers",
")",
",",
"'limit'",
":",
"limit",
",",
"'offset'",
":",
"offset",
",",
"'sortField'",
":",
"sort_field",
"}",
"resp_json",
"=",
"self",
".",
"get_parse",
"(",
"uri",
",",
"params",
"=",
"params",
")",
"return",
"resp_json"
] |
Gets the domains that have been registered with a nameserver or
nameservers
|
[
"Gets",
"the",
"domains",
"that",
"have",
"been",
"registered",
"with",
"a",
"nameserver",
"or",
"nameservers"
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L204-L215
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.search
|
def search(self, pattern, start=None, limit=None, include_category=None):
'''Searches for domains that match a given pattern'''
params = dict()
if start is None:
start = datetime.timedelta(days=30)
if isinstance(start, datetime.timedelta):
params['start'] = int(time.mktime((datetime.datetime.utcnow() - start).timetuple()) * 1000)
elif isinstance(start, datetime.datetime):
params['start'] = int(time.mktime(start.timetuple()) * 1000)
else:
raise Investigate.SEARCH_ERR
if limit is not None and isinstance(limit, int):
params['limit'] = limit
if include_category is not None and isinstance(include_category, bool):
params['includeCategory'] = str(include_category).lower()
uri = self._uris['search'].format(quote_plus(pattern))
return self.get_parse(uri, params)
|
python
|
def search(self, pattern, start=None, limit=None, include_category=None):
'''Searches for domains that match a given pattern'''
params = dict()
if start is None:
start = datetime.timedelta(days=30)
if isinstance(start, datetime.timedelta):
params['start'] = int(time.mktime((datetime.datetime.utcnow() - start).timetuple()) * 1000)
elif isinstance(start, datetime.datetime):
params['start'] = int(time.mktime(start.timetuple()) * 1000)
else:
raise Investigate.SEARCH_ERR
if limit is not None and isinstance(limit, int):
params['limit'] = limit
if include_category is not None and isinstance(include_category, bool):
params['includeCategory'] = str(include_category).lower()
uri = self._uris['search'].format(quote_plus(pattern))
return self.get_parse(uri, params)
|
[
"def",
"search",
"(",
"self",
",",
"pattern",
",",
"start",
"=",
"None",
",",
"limit",
"=",
"None",
",",
"include_category",
"=",
"None",
")",
":",
"params",
"=",
"dict",
"(",
")",
"if",
"start",
"is",
"None",
":",
"start",
"=",
"datetime",
".",
"timedelta",
"(",
"days",
"=",
"30",
")",
"if",
"isinstance",
"(",
"start",
",",
"datetime",
".",
"timedelta",
")",
":",
"params",
"[",
"'start'",
"]",
"=",
"int",
"(",
"time",
".",
"mktime",
"(",
"(",
"datetime",
".",
"datetime",
".",
"utcnow",
"(",
")",
"-",
"start",
")",
".",
"timetuple",
"(",
")",
")",
"*",
"1000",
")",
"elif",
"isinstance",
"(",
"start",
",",
"datetime",
".",
"datetime",
")",
":",
"params",
"[",
"'start'",
"]",
"=",
"int",
"(",
"time",
".",
"mktime",
"(",
"start",
".",
"timetuple",
"(",
")",
")",
"*",
"1000",
")",
"else",
":",
"raise",
"Investigate",
".",
"SEARCH_ERR",
"if",
"limit",
"is",
"not",
"None",
"and",
"isinstance",
"(",
"limit",
",",
"int",
")",
":",
"params",
"[",
"'limit'",
"]",
"=",
"limit",
"if",
"include_category",
"is",
"not",
"None",
"and",
"isinstance",
"(",
"include_category",
",",
"bool",
")",
":",
"params",
"[",
"'includeCategory'",
"]",
"=",
"str",
"(",
"include_category",
")",
".",
"lower",
"(",
")",
"uri",
"=",
"self",
".",
"_uris",
"[",
"'search'",
"]",
".",
"format",
"(",
"quote_plus",
"(",
"pattern",
")",
")",
"return",
"self",
".",
"get_parse",
"(",
"uri",
",",
"params",
")"
] |
Searches for domains that match a given pattern
|
[
"Searches",
"for",
"domains",
"that",
"match",
"a",
"given",
"pattern"
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L231-L253
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.samples
|
def samples(self, anystring, limit=None, offset=None, sortby=None):
'''Return an object representing the samples identified by the input domain, IP, or URL'''
uri = self._uris['samples'].format(anystring)
params = {'limit': limit, 'offset': offset, 'sortby': sortby}
return self.get_parse(uri, params)
|
python
|
def samples(self, anystring, limit=None, offset=None, sortby=None):
'''Return an object representing the samples identified by the input domain, IP, or URL'''
uri = self._uris['samples'].format(anystring)
params = {'limit': limit, 'offset': offset, 'sortby': sortby}
return self.get_parse(uri, params)
|
[
"def",
"samples",
"(",
"self",
",",
"anystring",
",",
"limit",
"=",
"None",
",",
"offset",
"=",
"None",
",",
"sortby",
"=",
"None",
")",
":",
"uri",
"=",
"self",
".",
"_uris",
"[",
"'samples'",
"]",
".",
"format",
"(",
"anystring",
")",
"params",
"=",
"{",
"'limit'",
":",
"limit",
",",
"'offset'",
":",
"offset",
",",
"'sortby'",
":",
"sortby",
"}",
"return",
"self",
".",
"get_parse",
"(",
"uri",
",",
"params",
")"
] |
Return an object representing the samples identified by the input domain, IP, or URL
|
[
"Return",
"an",
"object",
"representing",
"the",
"samples",
"identified",
"by",
"the",
"input",
"domain",
"IP",
"or",
"URL"
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L255-L261
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.sample
|
def sample(self, hash, limit=None, offset=None):
'''Return an object representing the sample identified by the input hash, or an empty object if that sample is not found'''
uri = self._uris['sample'].format(hash)
params = {'limit': limit, 'offset': offset}
return self.get_parse(uri, params)
|
python
|
def sample(self, hash, limit=None, offset=None):
'''Return an object representing the sample identified by the input hash, or an empty object if that sample is not found'''
uri = self._uris['sample'].format(hash)
params = {'limit': limit, 'offset': offset}
return self.get_parse(uri, params)
|
[
"def",
"sample",
"(",
"self",
",",
"hash",
",",
"limit",
"=",
"None",
",",
"offset",
"=",
"None",
")",
":",
"uri",
"=",
"self",
".",
"_uris",
"[",
"'sample'",
"]",
".",
"format",
"(",
"hash",
")",
"params",
"=",
"{",
"'limit'",
":",
"limit",
",",
"'offset'",
":",
"offset",
"}",
"return",
"self",
".",
"get_parse",
"(",
"uri",
",",
"params",
")"
] |
Return an object representing the sample identified by the input hash, or an empty object if that sample is not found
|
[
"Return",
"an",
"object",
"representing",
"the",
"sample",
"identified",
"by",
"the",
"input",
"hash",
"or",
"an",
"empty",
"object",
"if",
"that",
"sample",
"is",
"not",
"found"
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L263-L269
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.as_for_ip
|
def as_for_ip(self, ip):
'''Gets the AS information for a given IP address.'''
if not Investigate.IP_PATTERN.match(ip):
raise Investigate.IP_ERR
uri = self._uris["as_for_ip"].format(ip)
resp_json = self.get_parse(uri)
return resp_json
|
python
|
def as_for_ip(self, ip):
'''Gets the AS information for a given IP address.'''
if not Investigate.IP_PATTERN.match(ip):
raise Investigate.IP_ERR
uri = self._uris["as_for_ip"].format(ip)
resp_json = self.get_parse(uri)
return resp_json
|
[
"def",
"as_for_ip",
"(",
"self",
",",
"ip",
")",
":",
"if",
"not",
"Investigate",
".",
"IP_PATTERN",
".",
"match",
"(",
"ip",
")",
":",
"raise",
"Investigate",
".",
"IP_ERR",
"uri",
"=",
"self",
".",
"_uris",
"[",
"\"as_for_ip\"",
"]",
".",
"format",
"(",
"ip",
")",
"resp_json",
"=",
"self",
".",
"get_parse",
"(",
"uri",
")",
"return",
"resp_json"
] |
Gets the AS information for a given IP address.
|
[
"Gets",
"the",
"AS",
"information",
"for",
"a",
"given",
"IP",
"address",
"."
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L298-L306
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.prefixes_for_asn
|
def prefixes_for_asn(self, asn):
'''Gets the AS information for a given ASN. Return the CIDR and geolocation associated with the AS.'''
uri = self._uris["prefixes_for_asn"].format(asn)
resp_json = self.get_parse(uri)
return resp_json
|
python
|
def prefixes_for_asn(self, asn):
'''Gets the AS information for a given ASN. Return the CIDR and geolocation associated with the AS.'''
uri = self._uris["prefixes_for_asn"].format(asn)
resp_json = self.get_parse(uri)
return resp_json
|
[
"def",
"prefixes_for_asn",
"(",
"self",
",",
"asn",
")",
":",
"uri",
"=",
"self",
".",
"_uris",
"[",
"\"prefixes_for_asn\"",
"]",
".",
"format",
"(",
"asn",
")",
"resp_json",
"=",
"self",
".",
"get_parse",
"(",
"uri",
")",
"return",
"resp_json"
] |
Gets the AS information for a given ASN. Return the CIDR and geolocation associated with the AS.
|
[
"Gets",
"the",
"AS",
"information",
"for",
"a",
"given",
"ASN",
".",
"Return",
"the",
"CIDR",
"and",
"geolocation",
"associated",
"with",
"the",
"AS",
"."
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L308-L314
|
opendns/pyinvestigate
|
investigate/investigate.py
|
Investigate.timeline
|
def timeline(self, uri):
'''Get the domain tagging timeline for a given uri.
Could be a domain, ip, or url.
For details, see https://docs.umbrella.com/investigate-api/docs/timeline
'''
uri = self._uris["timeline"].format(uri)
resp_json = self.get_parse(uri)
return resp_json
|
python
|
def timeline(self, uri):
'''Get the domain tagging timeline for a given uri.
Could be a domain, ip, or url.
For details, see https://docs.umbrella.com/investigate-api/docs/timeline
'''
uri = self._uris["timeline"].format(uri)
resp_json = self.get_parse(uri)
return resp_json
|
[
"def",
"timeline",
"(",
"self",
",",
"uri",
")",
":",
"uri",
"=",
"self",
".",
"_uris",
"[",
"\"timeline\"",
"]",
".",
"format",
"(",
"uri",
")",
"resp_json",
"=",
"self",
".",
"get_parse",
"(",
"uri",
")",
"return",
"resp_json"
] |
Get the domain tagging timeline for a given uri.
Could be a domain, ip, or url.
For details, see https://docs.umbrella.com/investigate-api/docs/timeline
|
[
"Get",
"the",
"domain",
"tagging",
"timeline",
"for",
"a",
"given",
"uri",
".",
"Could",
"be",
"a",
"domain",
"ip",
"or",
"url",
".",
"For",
"details",
"see",
"https",
":",
"//",
"docs",
".",
"umbrella",
".",
"com",
"/",
"investigate",
"-",
"api",
"/",
"docs",
"/",
"timeline"
] |
train
|
https://github.com/opendns/pyinvestigate/blob/a182e73a750f03e906d9b25842d556db8d2fd54f/investigate/investigate.py#L316-L324
|
tisimst/mcerp
|
mcerp/umath.py
|
abs
|
def abs(x):
"""
Absolute value
"""
if isinstance(x, UncertainFunction):
mcpts = np.abs(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.abs(x)
|
python
|
def abs(x):
"""
Absolute value
"""
if isinstance(x, UncertainFunction):
mcpts = np.abs(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.abs(x)
|
[
"def",
"abs",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"abs",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"abs",
"(",
"x",
")"
] |
Absolute value
|
[
"Absolute",
"value"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L18-L26
|
tisimst/mcerp
|
mcerp/umath.py
|
acos
|
def acos(x):
"""
Inverse cosine
"""
if isinstance(x, UncertainFunction):
mcpts = np.arccos(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.arccos(x)
|
python
|
def acos(x):
"""
Inverse cosine
"""
if isinstance(x, UncertainFunction):
mcpts = np.arccos(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.arccos(x)
|
[
"def",
"acos",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"arccos",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"arccos",
"(",
"x",
")"
] |
Inverse cosine
|
[
"Inverse",
"cosine"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L29-L37
|
tisimst/mcerp
|
mcerp/umath.py
|
acosh
|
def acosh(x):
"""
Inverse hyperbolic cosine
"""
if isinstance(x, UncertainFunction):
mcpts = np.arccosh(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.arccosh(x)
|
python
|
def acosh(x):
"""
Inverse hyperbolic cosine
"""
if isinstance(x, UncertainFunction):
mcpts = np.arccosh(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.arccosh(x)
|
[
"def",
"acosh",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"arccosh",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"arccosh",
"(",
"x",
")"
] |
Inverse hyperbolic cosine
|
[
"Inverse",
"hyperbolic",
"cosine"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L40-L48
|
tisimst/mcerp
|
mcerp/umath.py
|
asin
|
def asin(x):
"""
Inverse sine
"""
if isinstance(x, UncertainFunction):
mcpts = np.arcsin(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.arcsin(x)
|
python
|
def asin(x):
"""
Inverse sine
"""
if isinstance(x, UncertainFunction):
mcpts = np.arcsin(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.arcsin(x)
|
[
"def",
"asin",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"arcsin",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"arcsin",
"(",
"x",
")"
] |
Inverse sine
|
[
"Inverse",
"sine"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L51-L59
|
tisimst/mcerp
|
mcerp/umath.py
|
asinh
|
def asinh(x):
"""
Inverse hyperbolic sine
"""
if isinstance(x, UncertainFunction):
mcpts = np.arcsinh(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.arcsinh(x)
|
python
|
def asinh(x):
"""
Inverse hyperbolic sine
"""
if isinstance(x, UncertainFunction):
mcpts = np.arcsinh(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.arcsinh(x)
|
[
"def",
"asinh",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"arcsinh",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"arcsinh",
"(",
"x",
")"
] |
Inverse hyperbolic sine
|
[
"Inverse",
"hyperbolic",
"sine"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L62-L70
|
tisimst/mcerp
|
mcerp/umath.py
|
atan
|
def atan(x):
"""
Inverse tangent
"""
if isinstance(x, UncertainFunction):
mcpts = np.arctan(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.arctan(x)
|
python
|
def atan(x):
"""
Inverse tangent
"""
if isinstance(x, UncertainFunction):
mcpts = np.arctan(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.arctan(x)
|
[
"def",
"atan",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"arctan",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"arctan",
"(",
"x",
")"
] |
Inverse tangent
|
[
"Inverse",
"tangent"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L73-L81
|
tisimst/mcerp
|
mcerp/umath.py
|
atanh
|
def atanh(x):
"""
Inverse hyperbolic tangent
"""
if isinstance(x, UncertainFunction):
mcpts = np.arctanh(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.arctanh(x)
|
python
|
def atanh(x):
"""
Inverse hyperbolic tangent
"""
if isinstance(x, UncertainFunction):
mcpts = np.arctanh(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.arctanh(x)
|
[
"def",
"atanh",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"arctanh",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"arctanh",
"(",
"x",
")"
] |
Inverse hyperbolic tangent
|
[
"Inverse",
"hyperbolic",
"tangent"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L84-L92
|
tisimst/mcerp
|
mcerp/umath.py
|
ceil
|
def ceil(x):
"""
Ceiling function (round towards positive infinity)
"""
if isinstance(x, UncertainFunction):
mcpts = np.ceil(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.ceil(x)
|
python
|
def ceil(x):
"""
Ceiling function (round towards positive infinity)
"""
if isinstance(x, UncertainFunction):
mcpts = np.ceil(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.ceil(x)
|
[
"def",
"ceil",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"ceil",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"ceil",
"(",
"x",
")"
] |
Ceiling function (round towards positive infinity)
|
[
"Ceiling",
"function",
"(",
"round",
"towards",
"positive",
"infinity",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L95-L103
|
tisimst/mcerp
|
mcerp/umath.py
|
cos
|
def cos(x):
"""
Cosine
"""
if isinstance(x, UncertainFunction):
mcpts = np.cos(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.cos(x)
|
python
|
def cos(x):
"""
Cosine
"""
if isinstance(x, UncertainFunction):
mcpts = np.cos(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.cos(x)
|
[
"def",
"cos",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"cos",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"cos",
"(",
"x",
")"
] |
Cosine
|
[
"Cosine"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L106-L114
|
tisimst/mcerp
|
mcerp/umath.py
|
cosh
|
def cosh(x):
"""
Hyperbolic cosine
"""
if isinstance(x, UncertainFunction):
mcpts = np.cosh(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.cosh(x)
|
python
|
def cosh(x):
"""
Hyperbolic cosine
"""
if isinstance(x, UncertainFunction):
mcpts = np.cosh(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.cosh(x)
|
[
"def",
"cosh",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"cosh",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"cosh",
"(",
"x",
")"
] |
Hyperbolic cosine
|
[
"Hyperbolic",
"cosine"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L117-L125
|
tisimst/mcerp
|
mcerp/umath.py
|
degrees
|
def degrees(x):
"""
Convert radians to degrees
"""
if isinstance(x, UncertainFunction):
mcpts = np.degrees(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.degrees(x)
|
python
|
def degrees(x):
"""
Convert radians to degrees
"""
if isinstance(x, UncertainFunction):
mcpts = np.degrees(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.degrees(x)
|
[
"def",
"degrees",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"degrees",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"degrees",
"(",
"x",
")"
] |
Convert radians to degrees
|
[
"Convert",
"radians",
"to",
"degrees"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L128-L136
|
tisimst/mcerp
|
mcerp/umath.py
|
exp
|
def exp(x):
"""
Exponential function
"""
if isinstance(x, UncertainFunction):
mcpts = np.exp(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.exp(x)
|
python
|
def exp(x):
"""
Exponential function
"""
if isinstance(x, UncertainFunction):
mcpts = np.exp(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.exp(x)
|
[
"def",
"exp",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"exp",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"exp",
"(",
"x",
")"
] |
Exponential function
|
[
"Exponential",
"function"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L139-L147
|
tisimst/mcerp
|
mcerp/umath.py
|
expm1
|
def expm1(x):
"""
Calculate exp(x) - 1
"""
if isinstance(x, UncertainFunction):
mcpts = np.expm1(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.expm1(x)
|
python
|
def expm1(x):
"""
Calculate exp(x) - 1
"""
if isinstance(x, UncertainFunction):
mcpts = np.expm1(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.expm1(x)
|
[
"def",
"expm1",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"expm1",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"expm1",
"(",
"x",
")"
] |
Calculate exp(x) - 1
|
[
"Calculate",
"exp",
"(",
"x",
")",
"-",
"1"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L150-L158
|
tisimst/mcerp
|
mcerp/umath.py
|
fabs
|
def fabs(x):
"""
Absolute value function
"""
if isinstance(x, UncertainFunction):
mcpts = np.fabs(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.fabs(x)
|
python
|
def fabs(x):
"""
Absolute value function
"""
if isinstance(x, UncertainFunction):
mcpts = np.fabs(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.fabs(x)
|
[
"def",
"fabs",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"fabs",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"fabs",
"(",
"x",
")"
] |
Absolute value function
|
[
"Absolute",
"value",
"function"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L161-L169
|
tisimst/mcerp
|
mcerp/umath.py
|
floor
|
def floor(x):
"""
Floor function (round towards negative infinity)
"""
if isinstance(x, UncertainFunction):
mcpts = np.floor(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.floor(x)
|
python
|
def floor(x):
"""
Floor function (round towards negative infinity)
"""
if isinstance(x, UncertainFunction):
mcpts = np.floor(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.floor(x)
|
[
"def",
"floor",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"floor",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"floor",
"(",
"x",
")"
] |
Floor function (round towards negative infinity)
|
[
"Floor",
"function",
"(",
"round",
"towards",
"negative",
"infinity",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L172-L180
|
tisimst/mcerp
|
mcerp/umath.py
|
hypot
|
def hypot(x, y):
"""
Calculate the hypotenuse given two "legs" of a right triangle
"""
if isinstance(x, UncertainFunction) or isinstance(x, UncertainFunction):
ufx = to_uncertain_func(x)
ufy = to_uncertain_func(y)
mcpts = np.hypot(ufx._mcpts, ufy._mcpts)
return UncertainFunction(mcpts)
else:
return np.hypot(x, y)
|
python
|
def hypot(x, y):
"""
Calculate the hypotenuse given two "legs" of a right triangle
"""
if isinstance(x, UncertainFunction) or isinstance(x, UncertainFunction):
ufx = to_uncertain_func(x)
ufy = to_uncertain_func(y)
mcpts = np.hypot(ufx._mcpts, ufy._mcpts)
return UncertainFunction(mcpts)
else:
return np.hypot(x, y)
|
[
"def",
"hypot",
"(",
"x",
",",
"y",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
"or",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"ufx",
"=",
"to_uncertain_func",
"(",
"x",
")",
"ufy",
"=",
"to_uncertain_func",
"(",
"y",
")",
"mcpts",
"=",
"np",
".",
"hypot",
"(",
"ufx",
".",
"_mcpts",
",",
"ufy",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"hypot",
"(",
"x",
",",
"y",
")"
] |
Calculate the hypotenuse given two "legs" of a right triangle
|
[
"Calculate",
"the",
"hypotenuse",
"given",
"two",
"legs",
"of",
"a",
"right",
"triangle"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L183-L193
|
tisimst/mcerp
|
mcerp/umath.py
|
log
|
def log(x):
"""
Natural logarithm
"""
if isinstance(x, UncertainFunction):
mcpts = np.log(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.log(x)
|
python
|
def log(x):
"""
Natural logarithm
"""
if isinstance(x, UncertainFunction):
mcpts = np.log(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.log(x)
|
[
"def",
"log",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"log",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"log",
"(",
"x",
")"
] |
Natural logarithm
|
[
"Natural",
"logarithm"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L203-L211
|
tisimst/mcerp
|
mcerp/umath.py
|
log10
|
def log10(x):
"""
Base-10 logarithm
"""
if isinstance(x, UncertainFunction):
mcpts = np.log10(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.log10(x)
|
python
|
def log10(x):
"""
Base-10 logarithm
"""
if isinstance(x, UncertainFunction):
mcpts = np.log10(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.log10(x)
|
[
"def",
"log10",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"log10",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"log10",
"(",
"x",
")"
] |
Base-10 logarithm
|
[
"Base",
"-",
"10",
"logarithm"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L214-L222
|
tisimst/mcerp
|
mcerp/umath.py
|
log1p
|
def log1p(x):
"""
Natural logarithm of (1 + x)
"""
if isinstance(x, UncertainFunction):
mcpts = np.log1p(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.log1p(x)
|
python
|
def log1p(x):
"""
Natural logarithm of (1 + x)
"""
if isinstance(x, UncertainFunction):
mcpts = np.log1p(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.log1p(x)
|
[
"def",
"log1p",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"log1p",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"log1p",
"(",
"x",
")"
] |
Natural logarithm of (1 + x)
|
[
"Natural",
"logarithm",
"of",
"(",
"1",
"+",
"x",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L225-L233
|
tisimst/mcerp
|
mcerp/umath.py
|
radians
|
def radians(x):
"""
Convert degrees to radians
"""
if isinstance(x, UncertainFunction):
mcpts = np.radians(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.radians(x)
|
python
|
def radians(x):
"""
Convert degrees to radians
"""
if isinstance(x, UncertainFunction):
mcpts = np.radians(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.radians(x)
|
[
"def",
"radians",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"radians",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"radians",
"(",
"x",
")"
] |
Convert degrees to radians
|
[
"Convert",
"degrees",
"to",
"radians"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L236-L244
|
tisimst/mcerp
|
mcerp/umath.py
|
sin
|
def sin(x):
"""
Sine
"""
if isinstance(x, UncertainFunction):
mcpts = np.sin(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.sin(x)
|
python
|
def sin(x):
"""
Sine
"""
if isinstance(x, UncertainFunction):
mcpts = np.sin(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.sin(x)
|
[
"def",
"sin",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"sin",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"sin",
"(",
"x",
")"
] |
Sine
|
[
"Sine"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L247-L255
|
tisimst/mcerp
|
mcerp/umath.py
|
sinh
|
def sinh(x):
"""
Hyperbolic sine
"""
if isinstance(x, UncertainFunction):
mcpts = np.sinh(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.sinh(x)
|
python
|
def sinh(x):
"""
Hyperbolic sine
"""
if isinstance(x, UncertainFunction):
mcpts = np.sinh(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.sinh(x)
|
[
"def",
"sinh",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"sinh",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"sinh",
"(",
"x",
")"
] |
Hyperbolic sine
|
[
"Hyperbolic",
"sine"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L258-L266
|
tisimst/mcerp
|
mcerp/umath.py
|
sqrt
|
def sqrt(x):
"""
Square-root function
"""
if isinstance(x, UncertainFunction):
mcpts = np.sqrt(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.sqrt(x)
|
python
|
def sqrt(x):
"""
Square-root function
"""
if isinstance(x, UncertainFunction):
mcpts = np.sqrt(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.sqrt(x)
|
[
"def",
"sqrt",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"sqrt",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"sqrt",
"(",
"x",
")"
] |
Square-root function
|
[
"Square",
"-",
"root",
"function"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L269-L277
|
tisimst/mcerp
|
mcerp/umath.py
|
tan
|
def tan(x):
"""
Tangent
"""
if isinstance(x, UncertainFunction):
mcpts = np.tan(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.tan(x)
|
python
|
def tan(x):
"""
Tangent
"""
if isinstance(x, UncertainFunction):
mcpts = np.tan(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.tan(x)
|
[
"def",
"tan",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"tan",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"tan",
"(",
"x",
")"
] |
Tangent
|
[
"Tangent"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L280-L288
|
tisimst/mcerp
|
mcerp/umath.py
|
tanh
|
def tanh(x):
"""
Hyperbolic tangent
"""
if isinstance(x, UncertainFunction):
mcpts = np.tanh(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.tanh(x)
|
python
|
def tanh(x):
"""
Hyperbolic tangent
"""
if isinstance(x, UncertainFunction):
mcpts = np.tanh(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.tanh(x)
|
[
"def",
"tanh",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"tanh",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"tanh",
"(",
"x",
")"
] |
Hyperbolic tangent
|
[
"Hyperbolic",
"tangent"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L291-L299
|
tisimst/mcerp
|
mcerp/umath.py
|
trunc
|
def trunc(x):
"""
Truncate the values to the integer value without rounding
"""
if isinstance(x, UncertainFunction):
mcpts = np.trunc(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.trunc(x)
|
python
|
def trunc(x):
"""
Truncate the values to the integer value without rounding
"""
if isinstance(x, UncertainFunction):
mcpts = np.trunc(x._mcpts)
return UncertainFunction(mcpts)
else:
return np.trunc(x)
|
[
"def",
"trunc",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"mcpts",
"=",
"np",
".",
"trunc",
"(",
"x",
".",
"_mcpts",
")",
"return",
"UncertainFunction",
"(",
"mcpts",
")",
"else",
":",
"return",
"np",
".",
"trunc",
"(",
"x",
")"
] |
Truncate the values to the integer value without rounding
|
[
"Truncate",
"the",
"values",
"to",
"the",
"integer",
"value",
"without",
"rounding"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/umath.py#L302-L310
|
tisimst/mcerp
|
mcerp/lhd.py
|
lhd
|
def lhd(
dist=None,
size=None,
dims=1,
form="randomized",
iterations=100,
showcorrelations=False,
):
"""
Create a Latin-Hypercube sample design based on distributions defined in the
`scipy.stats` module
Parameters
----------
dist: array_like
frozen scipy.stats.rv_continuous or rv_discrete distribution objects
that are defined previous to calling LHD
size: int
integer value for the number of samples to generate for each
distribution object
dims: int, optional
if dist is a single distribution object, and dims > 1, the one
distribution will be used to generate a size-by-dims sampled design
form: str, optional (non-functional at the moment)
determines how the sampling is to occur, with the following optional
values:
- 'randomized' - completely randomized sampling
- 'spacefilling' - space-filling sampling (generally gives a more
accurate sampling of the design when the number of sample points
is small)
- 'orthogonal' - balanced space-filling sampling (experimental)
The 'spacefilling' and 'orthogonal' forms require some iterations to
determine the optimal sampling pattern.
iterations: int, optional (non-functional at the moment)
used to control the number of allowable search iterations for generating
'spacefilling' and 'orthogonal' designs
Returns
-------
out: 2d-array,
A 2d-array where each column corresponds to each input distribution and
each row is a sample in the design
Examples
--------
Single distribution:
- uniform distribution, low = -1, width = 2
>>> import scipy.stats as ss
>>> d0 = ss.uniform(loc=-1,scale=2)
>>> print lhd(dist=d0,size=5)
[[ 0.51031081]
[-0.28961427]
[-0.68342107]
[ 0.69784371]
[ 0.12248842]]
Single distribution for multiple variables:
- normal distribution, mean = 0, stdev = 1
>>> d1 = ss.norm(loc=0,scale=1)
>>> print lhd(dist=d1,size=7,dims=5)
[[-0.8612785 0.23034412 0.21808001]
[ 0.0455778 0.07001606 0.31586419]
[-0.978553 0.30394663 0.78483995]
[-0.26415983 0.15235896 0.51462024]
[ 0.80805686 0.38891031 0.02076505]
[ 1.63028931 0.52104917 1.48016008]]
Multiple distributions:
- beta distribution, alpha = 2, beta = 5
- exponential distribution, lambda = 1.5
>>> d2 = ss.beta(2,5)
>>> d3 = ss.expon(scale=1/1.5)
>>> print lhd(dist=(d1,d2,d3),size=6)
[[-0.8612785 0.23034412 0.21808001]
[ 0.0455778 0.07001606 0.31586419]
[-0.978553 0.30394663 0.78483995]
[-0.26415983 0.15235896 0.51462024]
[ 0.80805686 0.38891031 0.02076505]
[ 1.63028931 0.52104917 1.48016008]]
"""
assert dims > 0, 'kwarg "dims" must be at least 1'
if not size or not dist:
return None
def _lhs(x, samples=20):
"""
_lhs(x) returns a latin-hypercube matrix (each row is a different
set of sample inputs) using a default sample size of 20 for each column
of X. X must be a 2xN matrix that contains the lower and upper bounds of
each column. The lower bound(s) should be in the first row and the upper
bound(s) should be in the second row.
_lhs(x,samples=N) uses the sample size of N instead of the default (20).
Example:
>>> x = np.array([[0,-1,3],[1,2,6]])
>>> print 'x:'; print x
x:
[[ 0 -1 3]
[ 1 2 6]]
>>> print 'lhs(x):'; print _lhs(x)
lhs(x):
[[ 0.02989122 -0.93918734 3.14432618]
[ 0.08869833 -0.82140706 3.19875152]
[ 0.10627442 -0.66999234 3.33814979]
[ 0.15202861 -0.44157763 3.57036894]
[ 0.2067089 -0.34845384 3.66930908]
[ 0.26542056 -0.23706445 3.76361414]
[ 0.34201421 -0.00779306 3.90818257]
[ 0.37891646 0.15458423 4.15031708]
[ 0.43501575 0.23561118 4.20320064]
[ 0.4865449 0.36350601 4.45792314]
[ 0.54804367 0.56069855 4.60911539]
[ 0.59400712 0.7468415 4.69923486]
[ 0.63708876 0.9159176 4.83611204]
[ 0.68819855 0.98596354 4.97659182]
[ 0.7368695 1.18923511 5.11135111]
[ 0.78885724 1.28369441 5.2900157 ]
[ 0.80966513 1.47415703 5.4081971 ]
[ 0.86196731 1.57844205 5.61067689]
[ 0.94784517 1.71823504 5.78021164]
[ 0.96739728 1.94169017 5.88604772]]
>>> print 'lhs(x,samples=5):'; print _lhs(x,samples=5)
lhs(x,samples=5):
[[ 0.1949127 -0.54124725 3.49238369]
[ 0.21128576 -0.13439798 3.65652016]
[ 0.47516308 0.39957406 4.5797308 ]
[ 0.64400392 0.90890999 4.92379431]
[ 0.96279472 1.79415307 5.52028238]]
"""
# determine the segment size
segmentSize = 1.0 / samples
# get the number of dimensions to sample (number of columns)
numVars = x.shape[1]
# populate each dimension
out = np.zeros((samples, numVars))
pointValue = np.zeros(samples)
for n in range(numVars):
for i in range(samples):
segmentMin = i * segmentSize
point = segmentMin + (np.random.random() * segmentSize)
pointValue[i] = (point * (x[1, n] - x[0, n])) + x[0, n]
out[:, n] = pointValue
# now randomly arrange the different segments
return _mix(out)
def _mix(data, dim="cols"):
"""
Takes a data matrix and mixes up the values along dim (either "rows" or
"cols"). In other words, if dim='rows', then each row's data is mixed
ONLY WITHIN ITSELF. Likewise, if dim='cols', then each column's data is
mixed ONLY WITHIN ITSELF.
"""
data = np.atleast_2d(data)
n = data.shape[0]
if dim == "rows":
data = data.T
data_rank = list(range(n))
for i in range(data.shape[1]):
new_data_rank = np.random.permutation(data_rank)
vals, order = np.unique(
np.hstack((data_rank, new_data_rank)), return_inverse=True
)
old_order = order[:n]
new_order = order[-n:]
tmp = data[np.argsort(old_order), i][new_order]
data[:, i] = tmp[:]
if dim == "rows":
data = data.T
return data
if form is "randomized":
if hasattr(dist, "__getitem__"): # if multiple distributions were input
nvars = len(dist)
x = np.vstack((np.zeros(nvars), np.ones(nvars)))
unif_data = _lhs(x, samples=size)
dist_data = np.empty_like(unif_data)
for i, d in enumerate(dist):
dist_data[:, i] = d.ppf(unif_data[:, i])
else: # if a single distribution was input
nvars = dims
x = np.vstack((np.zeros(nvars), np.ones(nvars)))
unif_data = _lhs(x, samples=size)
dist_data = np.empty_like(unif_data)
for i in range(nvars):
dist_data[:, i] = dist.ppf(unif_data[:, i])
elif form is "spacefilling":
def euclid_distance(arr):
n = arr.shape[0]
ans = 0.0
for i in range(n - 1):
for j in range(i + 1, n):
d = np.sqrt(
np.sum(
[(arr[i, k] - arr[j, k]) ** 2 for k in range(arr.shape[1])]
)
)
ans += 1.0 / d ** 2
return ans
def fill_space(data):
best = 1e8
for it in range(iterations):
d = euclid_distance(data)
if d < best:
d_opt = d
data_opt = data.copy()
data = _mix(data)
print("Optimized Distance:", d_opt)
return data_opt
if hasattr(dist, "__getitem__"): # if multiple distributions were input
nvars = len(dist)
x = np.vstack((np.zeros(nvars), np.ones(nvars)))
unif_data = fill_space(_lhs(x, samples=size))
dist_data = np.empty_like(unif_data)
for i, d in enumerate(dist):
dist_data[:, i] = d.ppf(unif_data[:, i])
else: # if a single distribution was input
nvars = dims
x = np.vstack((np.zeros(nvars), np.ones(nvars)))
unif_data = fill_space(_lhs(x, samples=size))
dist_data = np.empty_like(unif_data)
for i in range(nvars):
dist_data[:, i] = dist.ppf(unif_data[:, i])
elif form is "orthogonal":
raise NotImplementedError(
"Sorry. The orthogonal space-filling algorithm hasn't been implemented yet."
)
else:
raise ValueError('Invalid "form" value: %s' % (form))
if dist_data.shape[1] > 1:
cor_matrix = np.zeros((nvars, nvars))
for i in range(nvars):
for j in range(nvars):
x_data = dist_data[:, i].copy()
y_data = dist_data[:, j].copy()
x_mean = x_data.mean()
y_mean = y_data.mean()
num = np.sum((x_data - x_mean) * (y_data - y_mean))
den = np.sqrt(
np.sum((x_data - x_mean) ** 2) * np.sum((y_data - y_mean) ** 2)
)
cor_matrix[i, j] = num / den
cor_matrix[j, i] = num / den
inv_cor_matrix = np.linalg.pinv(cor_matrix)
VIF = np.max(np.diag(inv_cor_matrix))
if showcorrelations:
print("Correlation Matrix:\n", cor_matrix)
print("Inverted Correlation Matrix:\n", inv_cor_matrix)
print("Variance Inflation Factor (VIF):", VIF)
return dist_data
|
python
|
def lhd(
dist=None,
size=None,
dims=1,
form="randomized",
iterations=100,
showcorrelations=False,
):
"""
Create a Latin-Hypercube sample design based on distributions defined in the
`scipy.stats` module
Parameters
----------
dist: array_like
frozen scipy.stats.rv_continuous or rv_discrete distribution objects
that are defined previous to calling LHD
size: int
integer value for the number of samples to generate for each
distribution object
dims: int, optional
if dist is a single distribution object, and dims > 1, the one
distribution will be used to generate a size-by-dims sampled design
form: str, optional (non-functional at the moment)
determines how the sampling is to occur, with the following optional
values:
- 'randomized' - completely randomized sampling
- 'spacefilling' - space-filling sampling (generally gives a more
accurate sampling of the design when the number of sample points
is small)
- 'orthogonal' - balanced space-filling sampling (experimental)
The 'spacefilling' and 'orthogonal' forms require some iterations to
determine the optimal sampling pattern.
iterations: int, optional (non-functional at the moment)
used to control the number of allowable search iterations for generating
'spacefilling' and 'orthogonal' designs
Returns
-------
out: 2d-array,
A 2d-array where each column corresponds to each input distribution and
each row is a sample in the design
Examples
--------
Single distribution:
- uniform distribution, low = -1, width = 2
>>> import scipy.stats as ss
>>> d0 = ss.uniform(loc=-1,scale=2)
>>> print lhd(dist=d0,size=5)
[[ 0.51031081]
[-0.28961427]
[-0.68342107]
[ 0.69784371]
[ 0.12248842]]
Single distribution for multiple variables:
- normal distribution, mean = 0, stdev = 1
>>> d1 = ss.norm(loc=0,scale=1)
>>> print lhd(dist=d1,size=7,dims=5)
[[-0.8612785 0.23034412 0.21808001]
[ 0.0455778 0.07001606 0.31586419]
[-0.978553 0.30394663 0.78483995]
[-0.26415983 0.15235896 0.51462024]
[ 0.80805686 0.38891031 0.02076505]
[ 1.63028931 0.52104917 1.48016008]]
Multiple distributions:
- beta distribution, alpha = 2, beta = 5
- exponential distribution, lambda = 1.5
>>> d2 = ss.beta(2,5)
>>> d3 = ss.expon(scale=1/1.5)
>>> print lhd(dist=(d1,d2,d3),size=6)
[[-0.8612785 0.23034412 0.21808001]
[ 0.0455778 0.07001606 0.31586419]
[-0.978553 0.30394663 0.78483995]
[-0.26415983 0.15235896 0.51462024]
[ 0.80805686 0.38891031 0.02076505]
[ 1.63028931 0.52104917 1.48016008]]
"""
assert dims > 0, 'kwarg "dims" must be at least 1'
if not size or not dist:
return None
def _lhs(x, samples=20):
"""
_lhs(x) returns a latin-hypercube matrix (each row is a different
set of sample inputs) using a default sample size of 20 for each column
of X. X must be a 2xN matrix that contains the lower and upper bounds of
each column. The lower bound(s) should be in the first row and the upper
bound(s) should be in the second row.
_lhs(x,samples=N) uses the sample size of N instead of the default (20).
Example:
>>> x = np.array([[0,-1,3],[1,2,6]])
>>> print 'x:'; print x
x:
[[ 0 -1 3]
[ 1 2 6]]
>>> print 'lhs(x):'; print _lhs(x)
lhs(x):
[[ 0.02989122 -0.93918734 3.14432618]
[ 0.08869833 -0.82140706 3.19875152]
[ 0.10627442 -0.66999234 3.33814979]
[ 0.15202861 -0.44157763 3.57036894]
[ 0.2067089 -0.34845384 3.66930908]
[ 0.26542056 -0.23706445 3.76361414]
[ 0.34201421 -0.00779306 3.90818257]
[ 0.37891646 0.15458423 4.15031708]
[ 0.43501575 0.23561118 4.20320064]
[ 0.4865449 0.36350601 4.45792314]
[ 0.54804367 0.56069855 4.60911539]
[ 0.59400712 0.7468415 4.69923486]
[ 0.63708876 0.9159176 4.83611204]
[ 0.68819855 0.98596354 4.97659182]
[ 0.7368695 1.18923511 5.11135111]
[ 0.78885724 1.28369441 5.2900157 ]
[ 0.80966513 1.47415703 5.4081971 ]
[ 0.86196731 1.57844205 5.61067689]
[ 0.94784517 1.71823504 5.78021164]
[ 0.96739728 1.94169017 5.88604772]]
>>> print 'lhs(x,samples=5):'; print _lhs(x,samples=5)
lhs(x,samples=5):
[[ 0.1949127 -0.54124725 3.49238369]
[ 0.21128576 -0.13439798 3.65652016]
[ 0.47516308 0.39957406 4.5797308 ]
[ 0.64400392 0.90890999 4.92379431]
[ 0.96279472 1.79415307 5.52028238]]
"""
# determine the segment size
segmentSize = 1.0 / samples
# get the number of dimensions to sample (number of columns)
numVars = x.shape[1]
# populate each dimension
out = np.zeros((samples, numVars))
pointValue = np.zeros(samples)
for n in range(numVars):
for i in range(samples):
segmentMin = i * segmentSize
point = segmentMin + (np.random.random() * segmentSize)
pointValue[i] = (point * (x[1, n] - x[0, n])) + x[0, n]
out[:, n] = pointValue
# now randomly arrange the different segments
return _mix(out)
def _mix(data, dim="cols"):
"""
Takes a data matrix and mixes up the values along dim (either "rows" or
"cols"). In other words, if dim='rows', then each row's data is mixed
ONLY WITHIN ITSELF. Likewise, if dim='cols', then each column's data is
mixed ONLY WITHIN ITSELF.
"""
data = np.atleast_2d(data)
n = data.shape[0]
if dim == "rows":
data = data.T
data_rank = list(range(n))
for i in range(data.shape[1]):
new_data_rank = np.random.permutation(data_rank)
vals, order = np.unique(
np.hstack((data_rank, new_data_rank)), return_inverse=True
)
old_order = order[:n]
new_order = order[-n:]
tmp = data[np.argsort(old_order), i][new_order]
data[:, i] = tmp[:]
if dim == "rows":
data = data.T
return data
if form is "randomized":
if hasattr(dist, "__getitem__"): # if multiple distributions were input
nvars = len(dist)
x = np.vstack((np.zeros(nvars), np.ones(nvars)))
unif_data = _lhs(x, samples=size)
dist_data = np.empty_like(unif_data)
for i, d in enumerate(dist):
dist_data[:, i] = d.ppf(unif_data[:, i])
else: # if a single distribution was input
nvars = dims
x = np.vstack((np.zeros(nvars), np.ones(nvars)))
unif_data = _lhs(x, samples=size)
dist_data = np.empty_like(unif_data)
for i in range(nvars):
dist_data[:, i] = dist.ppf(unif_data[:, i])
elif form is "spacefilling":
def euclid_distance(arr):
n = arr.shape[0]
ans = 0.0
for i in range(n - 1):
for j in range(i + 1, n):
d = np.sqrt(
np.sum(
[(arr[i, k] - arr[j, k]) ** 2 for k in range(arr.shape[1])]
)
)
ans += 1.0 / d ** 2
return ans
def fill_space(data):
best = 1e8
for it in range(iterations):
d = euclid_distance(data)
if d < best:
d_opt = d
data_opt = data.copy()
data = _mix(data)
print("Optimized Distance:", d_opt)
return data_opt
if hasattr(dist, "__getitem__"): # if multiple distributions were input
nvars = len(dist)
x = np.vstack((np.zeros(nvars), np.ones(nvars)))
unif_data = fill_space(_lhs(x, samples=size))
dist_data = np.empty_like(unif_data)
for i, d in enumerate(dist):
dist_data[:, i] = d.ppf(unif_data[:, i])
else: # if a single distribution was input
nvars = dims
x = np.vstack((np.zeros(nvars), np.ones(nvars)))
unif_data = fill_space(_lhs(x, samples=size))
dist_data = np.empty_like(unif_data)
for i in range(nvars):
dist_data[:, i] = dist.ppf(unif_data[:, i])
elif form is "orthogonal":
raise NotImplementedError(
"Sorry. The orthogonal space-filling algorithm hasn't been implemented yet."
)
else:
raise ValueError('Invalid "form" value: %s' % (form))
if dist_data.shape[1] > 1:
cor_matrix = np.zeros((nvars, nvars))
for i in range(nvars):
for j in range(nvars):
x_data = dist_data[:, i].copy()
y_data = dist_data[:, j].copy()
x_mean = x_data.mean()
y_mean = y_data.mean()
num = np.sum((x_data - x_mean) * (y_data - y_mean))
den = np.sqrt(
np.sum((x_data - x_mean) ** 2) * np.sum((y_data - y_mean) ** 2)
)
cor_matrix[i, j] = num / den
cor_matrix[j, i] = num / den
inv_cor_matrix = np.linalg.pinv(cor_matrix)
VIF = np.max(np.diag(inv_cor_matrix))
if showcorrelations:
print("Correlation Matrix:\n", cor_matrix)
print("Inverted Correlation Matrix:\n", inv_cor_matrix)
print("Variance Inflation Factor (VIF):", VIF)
return dist_data
|
[
"def",
"lhd",
"(",
"dist",
"=",
"None",
",",
"size",
"=",
"None",
",",
"dims",
"=",
"1",
",",
"form",
"=",
"\"randomized\"",
",",
"iterations",
"=",
"100",
",",
"showcorrelations",
"=",
"False",
",",
")",
":",
"assert",
"dims",
">",
"0",
",",
"'kwarg \"dims\" must be at least 1'",
"if",
"not",
"size",
"or",
"not",
"dist",
":",
"return",
"None",
"def",
"_lhs",
"(",
"x",
",",
"samples",
"=",
"20",
")",
":",
"\"\"\"\n _lhs(x) returns a latin-hypercube matrix (each row is a different\n set of sample inputs) using a default sample size of 20 for each column\n of X. X must be a 2xN matrix that contains the lower and upper bounds of\n each column. The lower bound(s) should be in the first row and the upper\n bound(s) should be in the second row.\n \n _lhs(x,samples=N) uses the sample size of N instead of the default (20).\n \n Example:\n >>> x = np.array([[0,-1,3],[1,2,6]])\n >>> print 'x:'; print x\n x:\n [[ 0 -1 3]\n [ 1 2 6]]\n\n >>> print 'lhs(x):'; print _lhs(x)\n lhs(x):\n [[ 0.02989122 -0.93918734 3.14432618]\n [ 0.08869833 -0.82140706 3.19875152]\n [ 0.10627442 -0.66999234 3.33814979]\n [ 0.15202861 -0.44157763 3.57036894]\n [ 0.2067089 -0.34845384 3.66930908]\n [ 0.26542056 -0.23706445 3.76361414]\n [ 0.34201421 -0.00779306 3.90818257]\n [ 0.37891646 0.15458423 4.15031708]\n [ 0.43501575 0.23561118 4.20320064]\n [ 0.4865449 0.36350601 4.45792314]\n [ 0.54804367 0.56069855 4.60911539]\n [ 0.59400712 0.7468415 4.69923486]\n [ 0.63708876 0.9159176 4.83611204]\n [ 0.68819855 0.98596354 4.97659182]\n [ 0.7368695 1.18923511 5.11135111]\n [ 0.78885724 1.28369441 5.2900157 ]\n [ 0.80966513 1.47415703 5.4081971 ]\n [ 0.86196731 1.57844205 5.61067689]\n [ 0.94784517 1.71823504 5.78021164]\n [ 0.96739728 1.94169017 5.88604772]]\n\n >>> print 'lhs(x,samples=5):'; print _lhs(x,samples=5)\n lhs(x,samples=5):\n [[ 0.1949127 -0.54124725 3.49238369]\n [ 0.21128576 -0.13439798 3.65652016]\n [ 0.47516308 0.39957406 4.5797308 ]\n [ 0.64400392 0.90890999 4.92379431]\n [ 0.96279472 1.79415307 5.52028238]] \n \"\"\"",
"# determine the segment size",
"segmentSize",
"=",
"1.0",
"/",
"samples",
"# get the number of dimensions to sample (number of columns)",
"numVars",
"=",
"x",
".",
"shape",
"[",
"1",
"]",
"# populate each dimension",
"out",
"=",
"np",
".",
"zeros",
"(",
"(",
"samples",
",",
"numVars",
")",
")",
"pointValue",
"=",
"np",
".",
"zeros",
"(",
"samples",
")",
"for",
"n",
"in",
"range",
"(",
"numVars",
")",
":",
"for",
"i",
"in",
"range",
"(",
"samples",
")",
":",
"segmentMin",
"=",
"i",
"*",
"segmentSize",
"point",
"=",
"segmentMin",
"+",
"(",
"np",
".",
"random",
".",
"random",
"(",
")",
"*",
"segmentSize",
")",
"pointValue",
"[",
"i",
"]",
"=",
"(",
"point",
"*",
"(",
"x",
"[",
"1",
",",
"n",
"]",
"-",
"x",
"[",
"0",
",",
"n",
"]",
")",
")",
"+",
"x",
"[",
"0",
",",
"n",
"]",
"out",
"[",
":",
",",
"n",
"]",
"=",
"pointValue",
"# now randomly arrange the different segments",
"return",
"_mix",
"(",
"out",
")",
"def",
"_mix",
"(",
"data",
",",
"dim",
"=",
"\"cols\"",
")",
":",
"\"\"\"\n Takes a data matrix and mixes up the values along dim (either \"rows\" or \n \"cols\"). In other words, if dim='rows', then each row's data is mixed\n ONLY WITHIN ITSELF. Likewise, if dim='cols', then each column's data is\n mixed ONLY WITHIN ITSELF.\n \"\"\"",
"data",
"=",
"np",
".",
"atleast_2d",
"(",
"data",
")",
"n",
"=",
"data",
".",
"shape",
"[",
"0",
"]",
"if",
"dim",
"==",
"\"rows\"",
":",
"data",
"=",
"data",
".",
"T",
"data_rank",
"=",
"list",
"(",
"range",
"(",
"n",
")",
")",
"for",
"i",
"in",
"range",
"(",
"data",
".",
"shape",
"[",
"1",
"]",
")",
":",
"new_data_rank",
"=",
"np",
".",
"random",
".",
"permutation",
"(",
"data_rank",
")",
"vals",
",",
"order",
"=",
"np",
".",
"unique",
"(",
"np",
".",
"hstack",
"(",
"(",
"data_rank",
",",
"new_data_rank",
")",
")",
",",
"return_inverse",
"=",
"True",
")",
"old_order",
"=",
"order",
"[",
":",
"n",
"]",
"new_order",
"=",
"order",
"[",
"-",
"n",
":",
"]",
"tmp",
"=",
"data",
"[",
"np",
".",
"argsort",
"(",
"old_order",
")",
",",
"i",
"]",
"[",
"new_order",
"]",
"data",
"[",
":",
",",
"i",
"]",
"=",
"tmp",
"[",
":",
"]",
"if",
"dim",
"==",
"\"rows\"",
":",
"data",
"=",
"data",
".",
"T",
"return",
"data",
"if",
"form",
"is",
"\"randomized\"",
":",
"if",
"hasattr",
"(",
"dist",
",",
"\"__getitem__\"",
")",
":",
"# if multiple distributions were input",
"nvars",
"=",
"len",
"(",
"dist",
")",
"x",
"=",
"np",
".",
"vstack",
"(",
"(",
"np",
".",
"zeros",
"(",
"nvars",
")",
",",
"np",
".",
"ones",
"(",
"nvars",
")",
")",
")",
"unif_data",
"=",
"_lhs",
"(",
"x",
",",
"samples",
"=",
"size",
")",
"dist_data",
"=",
"np",
".",
"empty_like",
"(",
"unif_data",
")",
"for",
"i",
",",
"d",
"in",
"enumerate",
"(",
"dist",
")",
":",
"dist_data",
"[",
":",
",",
"i",
"]",
"=",
"d",
".",
"ppf",
"(",
"unif_data",
"[",
":",
",",
"i",
"]",
")",
"else",
":",
"# if a single distribution was input",
"nvars",
"=",
"dims",
"x",
"=",
"np",
".",
"vstack",
"(",
"(",
"np",
".",
"zeros",
"(",
"nvars",
")",
",",
"np",
".",
"ones",
"(",
"nvars",
")",
")",
")",
"unif_data",
"=",
"_lhs",
"(",
"x",
",",
"samples",
"=",
"size",
")",
"dist_data",
"=",
"np",
".",
"empty_like",
"(",
"unif_data",
")",
"for",
"i",
"in",
"range",
"(",
"nvars",
")",
":",
"dist_data",
"[",
":",
",",
"i",
"]",
"=",
"dist",
".",
"ppf",
"(",
"unif_data",
"[",
":",
",",
"i",
"]",
")",
"elif",
"form",
"is",
"\"spacefilling\"",
":",
"def",
"euclid_distance",
"(",
"arr",
")",
":",
"n",
"=",
"arr",
".",
"shape",
"[",
"0",
"]",
"ans",
"=",
"0.0",
"for",
"i",
"in",
"range",
"(",
"n",
"-",
"1",
")",
":",
"for",
"j",
"in",
"range",
"(",
"i",
"+",
"1",
",",
"n",
")",
":",
"d",
"=",
"np",
".",
"sqrt",
"(",
"np",
".",
"sum",
"(",
"[",
"(",
"arr",
"[",
"i",
",",
"k",
"]",
"-",
"arr",
"[",
"j",
",",
"k",
"]",
")",
"**",
"2",
"for",
"k",
"in",
"range",
"(",
"arr",
".",
"shape",
"[",
"1",
"]",
")",
"]",
")",
")",
"ans",
"+=",
"1.0",
"/",
"d",
"**",
"2",
"return",
"ans",
"def",
"fill_space",
"(",
"data",
")",
":",
"best",
"=",
"1e8",
"for",
"it",
"in",
"range",
"(",
"iterations",
")",
":",
"d",
"=",
"euclid_distance",
"(",
"data",
")",
"if",
"d",
"<",
"best",
":",
"d_opt",
"=",
"d",
"data_opt",
"=",
"data",
".",
"copy",
"(",
")",
"data",
"=",
"_mix",
"(",
"data",
")",
"print",
"(",
"\"Optimized Distance:\"",
",",
"d_opt",
")",
"return",
"data_opt",
"if",
"hasattr",
"(",
"dist",
",",
"\"__getitem__\"",
")",
":",
"# if multiple distributions were input",
"nvars",
"=",
"len",
"(",
"dist",
")",
"x",
"=",
"np",
".",
"vstack",
"(",
"(",
"np",
".",
"zeros",
"(",
"nvars",
")",
",",
"np",
".",
"ones",
"(",
"nvars",
")",
")",
")",
"unif_data",
"=",
"fill_space",
"(",
"_lhs",
"(",
"x",
",",
"samples",
"=",
"size",
")",
")",
"dist_data",
"=",
"np",
".",
"empty_like",
"(",
"unif_data",
")",
"for",
"i",
",",
"d",
"in",
"enumerate",
"(",
"dist",
")",
":",
"dist_data",
"[",
":",
",",
"i",
"]",
"=",
"d",
".",
"ppf",
"(",
"unif_data",
"[",
":",
",",
"i",
"]",
")",
"else",
":",
"# if a single distribution was input",
"nvars",
"=",
"dims",
"x",
"=",
"np",
".",
"vstack",
"(",
"(",
"np",
".",
"zeros",
"(",
"nvars",
")",
",",
"np",
".",
"ones",
"(",
"nvars",
")",
")",
")",
"unif_data",
"=",
"fill_space",
"(",
"_lhs",
"(",
"x",
",",
"samples",
"=",
"size",
")",
")",
"dist_data",
"=",
"np",
".",
"empty_like",
"(",
"unif_data",
")",
"for",
"i",
"in",
"range",
"(",
"nvars",
")",
":",
"dist_data",
"[",
":",
",",
"i",
"]",
"=",
"dist",
".",
"ppf",
"(",
"unif_data",
"[",
":",
",",
"i",
"]",
")",
"elif",
"form",
"is",
"\"orthogonal\"",
":",
"raise",
"NotImplementedError",
"(",
"\"Sorry. The orthogonal space-filling algorithm hasn't been implemented yet.\"",
")",
"else",
":",
"raise",
"ValueError",
"(",
"'Invalid \"form\" value: %s'",
"%",
"(",
"form",
")",
")",
"if",
"dist_data",
".",
"shape",
"[",
"1",
"]",
">",
"1",
":",
"cor_matrix",
"=",
"np",
".",
"zeros",
"(",
"(",
"nvars",
",",
"nvars",
")",
")",
"for",
"i",
"in",
"range",
"(",
"nvars",
")",
":",
"for",
"j",
"in",
"range",
"(",
"nvars",
")",
":",
"x_data",
"=",
"dist_data",
"[",
":",
",",
"i",
"]",
".",
"copy",
"(",
")",
"y_data",
"=",
"dist_data",
"[",
":",
",",
"j",
"]",
".",
"copy",
"(",
")",
"x_mean",
"=",
"x_data",
".",
"mean",
"(",
")",
"y_mean",
"=",
"y_data",
".",
"mean",
"(",
")",
"num",
"=",
"np",
".",
"sum",
"(",
"(",
"x_data",
"-",
"x_mean",
")",
"*",
"(",
"y_data",
"-",
"y_mean",
")",
")",
"den",
"=",
"np",
".",
"sqrt",
"(",
"np",
".",
"sum",
"(",
"(",
"x_data",
"-",
"x_mean",
")",
"**",
"2",
")",
"*",
"np",
".",
"sum",
"(",
"(",
"y_data",
"-",
"y_mean",
")",
"**",
"2",
")",
")",
"cor_matrix",
"[",
"i",
",",
"j",
"]",
"=",
"num",
"/",
"den",
"cor_matrix",
"[",
"j",
",",
"i",
"]",
"=",
"num",
"/",
"den",
"inv_cor_matrix",
"=",
"np",
".",
"linalg",
".",
"pinv",
"(",
"cor_matrix",
")",
"VIF",
"=",
"np",
".",
"max",
"(",
"np",
".",
"diag",
"(",
"inv_cor_matrix",
")",
")",
"if",
"showcorrelations",
":",
"print",
"(",
"\"Correlation Matrix:\\n\"",
",",
"cor_matrix",
")",
"print",
"(",
"\"Inverted Correlation Matrix:\\n\"",
",",
"inv_cor_matrix",
")",
"print",
"(",
"\"Variance Inflation Factor (VIF):\"",
",",
"VIF",
")",
"return",
"dist_data"
] |
Create a Latin-Hypercube sample design based on distributions defined in the
`scipy.stats` module
Parameters
----------
dist: array_like
frozen scipy.stats.rv_continuous or rv_discrete distribution objects
that are defined previous to calling LHD
size: int
integer value for the number of samples to generate for each
distribution object
dims: int, optional
if dist is a single distribution object, and dims > 1, the one
distribution will be used to generate a size-by-dims sampled design
form: str, optional (non-functional at the moment)
determines how the sampling is to occur, with the following optional
values:
- 'randomized' - completely randomized sampling
- 'spacefilling' - space-filling sampling (generally gives a more
accurate sampling of the design when the number of sample points
is small)
- 'orthogonal' - balanced space-filling sampling (experimental)
The 'spacefilling' and 'orthogonal' forms require some iterations to
determine the optimal sampling pattern.
iterations: int, optional (non-functional at the moment)
used to control the number of allowable search iterations for generating
'spacefilling' and 'orthogonal' designs
Returns
-------
out: 2d-array,
A 2d-array where each column corresponds to each input distribution and
each row is a sample in the design
Examples
--------
Single distribution:
- uniform distribution, low = -1, width = 2
>>> import scipy.stats as ss
>>> d0 = ss.uniform(loc=-1,scale=2)
>>> print lhd(dist=d0,size=5)
[[ 0.51031081]
[-0.28961427]
[-0.68342107]
[ 0.69784371]
[ 0.12248842]]
Single distribution for multiple variables:
- normal distribution, mean = 0, stdev = 1
>>> d1 = ss.norm(loc=0,scale=1)
>>> print lhd(dist=d1,size=7,dims=5)
[[-0.8612785 0.23034412 0.21808001]
[ 0.0455778 0.07001606 0.31586419]
[-0.978553 0.30394663 0.78483995]
[-0.26415983 0.15235896 0.51462024]
[ 0.80805686 0.38891031 0.02076505]
[ 1.63028931 0.52104917 1.48016008]]
Multiple distributions:
- beta distribution, alpha = 2, beta = 5
- exponential distribution, lambda = 1.5
>>> d2 = ss.beta(2,5)
>>> d3 = ss.expon(scale=1/1.5)
>>> print lhd(dist=(d1,d2,d3),size=6)
[[-0.8612785 0.23034412 0.21808001]
[ 0.0455778 0.07001606 0.31586419]
[-0.978553 0.30394663 0.78483995]
[-0.26415983 0.15235896 0.51462024]
[ 0.80805686 0.38891031 0.02076505]
[ 1.63028931 0.52104917 1.48016008]]
|
[
"Create",
"a",
"Latin",
"-",
"Hypercube",
"sample",
"design",
"based",
"on",
"distributions",
"defined",
"in",
"the",
"scipy",
".",
"stats",
"module",
"Parameters",
"----------",
"dist",
":",
"array_like",
"frozen",
"scipy",
".",
"stats",
".",
"rv_continuous",
"or",
"rv_discrete",
"distribution",
"objects",
"that",
"are",
"defined",
"previous",
"to",
"calling",
"LHD"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/lhd.py#L5-L286
|
tisimst/mcerp
|
mcerp/__init__.py
|
to_uncertain_func
|
def to_uncertain_func(x):
"""
Transforms x into an UncertainFunction-compatible object,
unless it is already an UncertainFunction (in which case x is returned
unchanged).
Raises an exception unless 'x' belongs to some specific classes of
objects that are known not to depend on UncertainFunction objects
(which then cannot be considered as constants).
"""
if isinstance(x, UncertainFunction):
return x
# ! In Python 2.6+, numbers.Number could be used instead, here:
elif isinstance(x, CONSTANT_TYPES):
# No variable => no derivative to define:
return UncertainFunction([x] * npts)
raise NotUpcast("%s cannot be converted to a number with" " uncertainty" % type(x))
|
python
|
def to_uncertain_func(x):
"""
Transforms x into an UncertainFunction-compatible object,
unless it is already an UncertainFunction (in which case x is returned
unchanged).
Raises an exception unless 'x' belongs to some specific classes of
objects that are known not to depend on UncertainFunction objects
(which then cannot be considered as constants).
"""
if isinstance(x, UncertainFunction):
return x
# ! In Python 2.6+, numbers.Number could be used instead, here:
elif isinstance(x, CONSTANT_TYPES):
# No variable => no derivative to define:
return UncertainFunction([x] * npts)
raise NotUpcast("%s cannot be converted to a number with" " uncertainty" % type(x))
|
[
"def",
"to_uncertain_func",
"(",
"x",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"UncertainFunction",
")",
":",
"return",
"x",
"# ! In Python 2.6+, numbers.Number could be used instead, here:",
"elif",
"isinstance",
"(",
"x",
",",
"CONSTANT_TYPES",
")",
":",
"# No variable => no derivative to define:",
"return",
"UncertainFunction",
"(",
"[",
"x",
"]",
"*",
"npts",
")",
"raise",
"NotUpcast",
"(",
"\"%s cannot be converted to a number with\"",
"\" uncertainty\"",
"%",
"type",
"(",
"x",
")",
")"
] |
Transforms x into an UncertainFunction-compatible object,
unless it is already an UncertainFunction (in which case x is returned
unchanged).
Raises an exception unless 'x' belongs to some specific classes of
objects that are known not to depend on UncertainFunction objects
(which then cannot be considered as constants).
|
[
"Transforms",
"x",
"into",
"an",
"UncertainFunction",
"-",
"compatible",
"object",
"unless",
"it",
"is",
"already",
"an",
"UncertainFunction",
"(",
"in",
"which",
"case",
"x",
"is",
"returned",
"unchanged",
")",
"."
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L31-L49
|
tisimst/mcerp
|
mcerp/__init__.py
|
Beta
|
def Beta(alpha, beta, low=0, high=1, tag=None):
"""
A Beta random variate
Parameters
----------
alpha : scalar
The first shape parameter
beta : scalar
The second shape parameter
Optional
--------
low : scalar
Lower bound of the distribution support (default=0)
high : scalar
Upper bound of the distribution support (default=1)
"""
assert (
alpha > 0 and beta > 0
), 'Beta "alpha" and "beta" parameters must be greater than zero'
assert low < high, 'Beta "low" must be less than "high"'
return uv(ss.beta(alpha, beta, loc=low, scale=high - low), tag=tag)
|
python
|
def Beta(alpha, beta, low=0, high=1, tag=None):
"""
A Beta random variate
Parameters
----------
alpha : scalar
The first shape parameter
beta : scalar
The second shape parameter
Optional
--------
low : scalar
Lower bound of the distribution support (default=0)
high : scalar
Upper bound of the distribution support (default=1)
"""
assert (
alpha > 0 and beta > 0
), 'Beta "alpha" and "beta" parameters must be greater than zero'
assert low < high, 'Beta "low" must be less than "high"'
return uv(ss.beta(alpha, beta, loc=low, scale=high - low), tag=tag)
|
[
"def",
"Beta",
"(",
"alpha",
",",
"beta",
",",
"low",
"=",
"0",
",",
"high",
"=",
"1",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"(",
"alpha",
">",
"0",
"and",
"beta",
">",
"0",
")",
",",
"'Beta \"alpha\" and \"beta\" parameters must be greater than zero'",
"assert",
"low",
"<",
"high",
",",
"'Beta \"low\" must be less than \"high\"'",
"return",
"uv",
"(",
"ss",
".",
"beta",
"(",
"alpha",
",",
"beta",
",",
"loc",
"=",
"low",
",",
"scale",
"=",
"high",
"-",
"low",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Beta random variate
Parameters
----------
alpha : scalar
The first shape parameter
beta : scalar
The second shape parameter
Optional
--------
low : scalar
Lower bound of the distribution support (default=0)
high : scalar
Upper bound of the distribution support (default=1)
|
[
"A",
"Beta",
"random",
"variate",
"Parameters",
"----------",
"alpha",
":",
"scalar",
"The",
"first",
"shape",
"parameter",
"beta",
":",
"scalar",
"The",
"second",
"shape",
"parameter",
"Optional",
"--------",
"low",
":",
"scalar",
"Lower",
"bound",
"of",
"the",
"distribution",
"support",
"(",
"default",
"=",
"0",
")",
"high",
":",
"scalar",
"Upper",
"bound",
"of",
"the",
"distribution",
"support",
"(",
"default",
"=",
"1",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L721-L743
|
tisimst/mcerp
|
mcerp/__init__.py
|
BetaPrime
|
def BetaPrime(alpha, beta, tag=None):
"""
A BetaPrime random variate
Parameters
----------
alpha : scalar
The first shape parameter
beta : scalar
The second shape parameter
"""
assert (
alpha > 0 and beta > 0
), 'BetaPrime "alpha" and "beta" parameters must be greater than zero'
x = Beta(alpha, beta, tag)
return x / (1 - x)
|
python
|
def BetaPrime(alpha, beta, tag=None):
"""
A BetaPrime random variate
Parameters
----------
alpha : scalar
The first shape parameter
beta : scalar
The second shape parameter
"""
assert (
alpha > 0 and beta > 0
), 'BetaPrime "alpha" and "beta" parameters must be greater than zero'
x = Beta(alpha, beta, tag)
return x / (1 - x)
|
[
"def",
"BetaPrime",
"(",
"alpha",
",",
"beta",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"(",
"alpha",
">",
"0",
"and",
"beta",
">",
"0",
")",
",",
"'BetaPrime \"alpha\" and \"beta\" parameters must be greater than zero'",
"x",
"=",
"Beta",
"(",
"alpha",
",",
"beta",
",",
"tag",
")",
"return",
"x",
"/",
"(",
"1",
"-",
"x",
")"
] |
A BetaPrime random variate
Parameters
----------
alpha : scalar
The first shape parameter
beta : scalar
The second shape parameter
|
[
"A",
"BetaPrime",
"random",
"variate",
"Parameters",
"----------",
"alpha",
":",
"scalar",
"The",
"first",
"shape",
"parameter",
"beta",
":",
"scalar",
"The",
"second",
"shape",
"parameter"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L746-L762
|
tisimst/mcerp
|
mcerp/__init__.py
|
Bradford
|
def Bradford(q, low=0, high=1, tag=None):
"""
A Bradford random variate
Parameters
----------
q : scalar
The shape parameter
low : scalar
The lower bound of the distribution (default=0)
high : scalar
The upper bound of the distribution (default=1)
"""
assert q > 0, 'Bradford "q" parameter must be greater than zero'
assert low < high, 'Bradford "low" parameter must be less than "high"'
return uv(ss.bradford(q, loc=low, scale=high - low), tag=tag)
|
python
|
def Bradford(q, low=0, high=1, tag=None):
"""
A Bradford random variate
Parameters
----------
q : scalar
The shape parameter
low : scalar
The lower bound of the distribution (default=0)
high : scalar
The upper bound of the distribution (default=1)
"""
assert q > 0, 'Bradford "q" parameter must be greater than zero'
assert low < high, 'Bradford "low" parameter must be less than "high"'
return uv(ss.bradford(q, loc=low, scale=high - low), tag=tag)
|
[
"def",
"Bradford",
"(",
"q",
",",
"low",
"=",
"0",
",",
"high",
"=",
"1",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"q",
">",
"0",
",",
"'Bradford \"q\" parameter must be greater than zero'",
"assert",
"low",
"<",
"high",
",",
"'Bradford \"low\" parameter must be less than \"high\"'",
"return",
"uv",
"(",
"ss",
".",
"bradford",
"(",
"q",
",",
"loc",
"=",
"low",
",",
"scale",
"=",
"high",
"-",
"low",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Bradford random variate
Parameters
----------
q : scalar
The shape parameter
low : scalar
The lower bound of the distribution (default=0)
high : scalar
The upper bound of the distribution (default=1)
|
[
"A",
"Bradford",
"random",
"variate",
"Parameters",
"----------",
"q",
":",
"scalar",
"The",
"shape",
"parameter",
"low",
":",
"scalar",
"The",
"lower",
"bound",
"of",
"the",
"distribution",
"(",
"default",
"=",
"0",
")",
"high",
":",
"scalar",
"The",
"upper",
"bound",
"of",
"the",
"distribution",
"(",
"default",
"=",
"1",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L765-L780
|
tisimst/mcerp
|
mcerp/__init__.py
|
Burr
|
def Burr(c, k, tag=None):
"""
A Burr random variate
Parameters
----------
c : scalar
The first shape parameter
k : scalar
The second shape parameter
"""
assert c > 0 and k > 0, 'Burr "c" and "k" parameters must be greater than zero'
return uv(ss.burr(c, k), tag=tag)
|
python
|
def Burr(c, k, tag=None):
"""
A Burr random variate
Parameters
----------
c : scalar
The first shape parameter
k : scalar
The second shape parameter
"""
assert c > 0 and k > 0, 'Burr "c" and "k" parameters must be greater than zero'
return uv(ss.burr(c, k), tag=tag)
|
[
"def",
"Burr",
"(",
"c",
",",
"k",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"c",
">",
"0",
"and",
"k",
">",
"0",
",",
"'Burr \"c\" and \"k\" parameters must be greater than zero'",
"return",
"uv",
"(",
"ss",
".",
"burr",
"(",
"c",
",",
"k",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Burr random variate
Parameters
----------
c : scalar
The first shape parameter
k : scalar
The second shape parameter
|
[
"A",
"Burr",
"random",
"variate",
"Parameters",
"----------",
"c",
":",
"scalar",
"The",
"first",
"shape",
"parameter",
"k",
":",
"scalar",
"The",
"second",
"shape",
"parameter"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L783-L796
|
tisimst/mcerp
|
mcerp/__init__.py
|
ChiSquared
|
def ChiSquared(k, tag=None):
"""
A Chi-Squared random variate
Parameters
----------
k : int
The degrees of freedom of the distribution (must be greater than one)
"""
assert int(k) == k and k >= 1, 'Chi-Squared "k" must be an integer greater than 0'
return uv(ss.chi2(k), tag=tag)
|
python
|
def ChiSquared(k, tag=None):
"""
A Chi-Squared random variate
Parameters
----------
k : int
The degrees of freedom of the distribution (must be greater than one)
"""
assert int(k) == k and k >= 1, 'Chi-Squared "k" must be an integer greater than 0'
return uv(ss.chi2(k), tag=tag)
|
[
"def",
"ChiSquared",
"(",
"k",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"int",
"(",
"k",
")",
"==",
"k",
"and",
"k",
">=",
"1",
",",
"'Chi-Squared \"k\" must be an integer greater than 0'",
"return",
"uv",
"(",
"ss",
".",
"chi2",
"(",
"k",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Chi-Squared random variate
Parameters
----------
k : int
The degrees of freedom of the distribution (must be greater than one)
|
[
"A",
"Chi",
"-",
"Squared",
"random",
"variate",
"Parameters",
"----------",
"k",
":",
"int",
"The",
"degrees",
"of",
"freedom",
"of",
"the",
"distribution",
"(",
"must",
"be",
"greater",
"than",
"one",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L799-L809
|
tisimst/mcerp
|
mcerp/__init__.py
|
Erlang
|
def Erlang(k, lamda, tag=None):
"""
An Erlang random variate.
This distribution is the same as a Gamma(k, theta) distribution, but
with the restriction that k must be a positive integer. This
is provided for greater compatibility with other simulation tools, but
provides no advantage over the Gamma distribution in its applications.
Parameters
----------
k : int
The shape parameter (must be a positive integer)
lamda : scalar
The scale parameter (must be greater than zero)
"""
assert int(k) == k and k > 0, 'Erlang "k" must be a positive integer'
assert lamda > 0, 'Erlang "lamda" must be greater than zero'
return Gamma(k, lamda, tag)
|
python
|
def Erlang(k, lamda, tag=None):
"""
An Erlang random variate.
This distribution is the same as a Gamma(k, theta) distribution, but
with the restriction that k must be a positive integer. This
is provided for greater compatibility with other simulation tools, but
provides no advantage over the Gamma distribution in its applications.
Parameters
----------
k : int
The shape parameter (must be a positive integer)
lamda : scalar
The scale parameter (must be greater than zero)
"""
assert int(k) == k and k > 0, 'Erlang "k" must be a positive integer'
assert lamda > 0, 'Erlang "lamda" must be greater than zero'
return Gamma(k, lamda, tag)
|
[
"def",
"Erlang",
"(",
"k",
",",
"lamda",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"int",
"(",
"k",
")",
"==",
"k",
"and",
"k",
">",
"0",
",",
"'Erlang \"k\" must be a positive integer'",
"assert",
"lamda",
">",
"0",
",",
"'Erlang \"lamda\" must be greater than zero'",
"return",
"Gamma",
"(",
"k",
",",
"lamda",
",",
"tag",
")"
] |
An Erlang random variate.
This distribution is the same as a Gamma(k, theta) distribution, but
with the restriction that k must be a positive integer. This
is provided for greater compatibility with other simulation tools, but
provides no advantage over the Gamma distribution in its applications.
Parameters
----------
k : int
The shape parameter (must be a positive integer)
lamda : scalar
The scale parameter (must be greater than zero)
|
[
"An",
"Erlang",
"random",
"variate",
".",
"This",
"distribution",
"is",
"the",
"same",
"as",
"a",
"Gamma",
"(",
"k",
"theta",
")",
"distribution",
"but",
"with",
"the",
"restriction",
"that",
"k",
"must",
"be",
"a",
"positive",
"integer",
".",
"This",
"is",
"provided",
"for",
"greater",
"compatibility",
"with",
"other",
"simulation",
"tools",
"but",
"provides",
"no",
"advantage",
"over",
"the",
"Gamma",
"distribution",
"in",
"its",
"applications",
".",
"Parameters",
"----------",
"k",
":",
"int",
"The",
"shape",
"parameter",
"(",
"must",
"be",
"a",
"positive",
"integer",
")",
"lamda",
":",
"scalar",
"The",
"scale",
"parameter",
"(",
"must",
"be",
"greater",
"than",
"zero",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L832-L850
|
tisimst/mcerp
|
mcerp/__init__.py
|
Exponential
|
def Exponential(lamda, tag=None):
"""
An Exponential random variate
Parameters
----------
lamda : scalar
The inverse scale (as shown on Wikipedia). (FYI: mu = 1/lamda.)
"""
assert lamda > 0, 'Exponential "lamda" must be greater than zero'
return uv(ss.expon(scale=1.0 / lamda), tag=tag)
|
python
|
def Exponential(lamda, tag=None):
"""
An Exponential random variate
Parameters
----------
lamda : scalar
The inverse scale (as shown on Wikipedia). (FYI: mu = 1/lamda.)
"""
assert lamda > 0, 'Exponential "lamda" must be greater than zero'
return uv(ss.expon(scale=1.0 / lamda), tag=tag)
|
[
"def",
"Exponential",
"(",
"lamda",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"lamda",
">",
"0",
",",
"'Exponential \"lamda\" must be greater than zero'",
"return",
"uv",
"(",
"ss",
".",
"expon",
"(",
"scale",
"=",
"1.0",
"/",
"lamda",
")",
",",
"tag",
"=",
"tag",
")"
] |
An Exponential random variate
Parameters
----------
lamda : scalar
The inverse scale (as shown on Wikipedia). (FYI: mu = 1/lamda.)
|
[
"An",
"Exponential",
"random",
"variate",
"Parameters",
"----------",
"lamda",
":",
"scalar",
"The",
"inverse",
"scale",
"(",
"as",
"shown",
"on",
"Wikipedia",
")",
".",
"(",
"FYI",
":",
"mu",
"=",
"1",
"/",
"lamda",
".",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L853-L863
|
tisimst/mcerp
|
mcerp/__init__.py
|
ExtValueMax
|
def ExtValueMax(mu, sigma, tag=None):
"""
An Extreme Value Maximum random variate.
Parameters
----------
mu : scalar
The location parameter
sigma : scalar
The scale parameter (must be greater than zero)
"""
assert sigma > 0, 'ExtremeValueMax "sigma" must be greater than zero'
p = U(0, 1)._mcpts[:]
return UncertainFunction(mu - sigma * np.log(-np.log(p)), tag=tag)
|
python
|
def ExtValueMax(mu, sigma, tag=None):
"""
An Extreme Value Maximum random variate.
Parameters
----------
mu : scalar
The location parameter
sigma : scalar
The scale parameter (must be greater than zero)
"""
assert sigma > 0, 'ExtremeValueMax "sigma" must be greater than zero'
p = U(0, 1)._mcpts[:]
return UncertainFunction(mu - sigma * np.log(-np.log(p)), tag=tag)
|
[
"def",
"ExtValueMax",
"(",
"mu",
",",
"sigma",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"sigma",
">",
"0",
",",
"'ExtremeValueMax \"sigma\" must be greater than zero'",
"p",
"=",
"U",
"(",
"0",
",",
"1",
")",
".",
"_mcpts",
"[",
":",
"]",
"return",
"UncertainFunction",
"(",
"mu",
"-",
"sigma",
"*",
"np",
".",
"log",
"(",
"-",
"np",
".",
"log",
"(",
"p",
")",
")",
",",
"tag",
"=",
"tag",
")"
] |
An Extreme Value Maximum random variate.
Parameters
----------
mu : scalar
The location parameter
sigma : scalar
The scale parameter (must be greater than zero)
|
[
"An",
"Extreme",
"Value",
"Maximum",
"random",
"variate",
".",
"Parameters",
"----------",
"mu",
":",
"scalar",
"The",
"location",
"parameter",
"sigma",
":",
"scalar",
"The",
"scale",
"parameter",
"(",
"must",
"be",
"greater",
"than",
"zero",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L869-L882
|
tisimst/mcerp
|
mcerp/__init__.py
|
Fisher
|
def Fisher(d1, d2, tag=None):
"""
An F (fisher) random variate
Parameters
----------
d1 : int
Numerator degrees of freedom
d2 : int
Denominator degrees of freedom
"""
assert (
int(d1) == d1 and d1 >= 1
), 'Fisher (F) "d1" must be an integer greater than 0'
assert (
int(d2) == d2 and d2 >= 1
), 'Fisher (F) "d2" must be an integer greater than 0'
return uv(ss.f(d1, d2), tag=tag)
|
python
|
def Fisher(d1, d2, tag=None):
"""
An F (fisher) random variate
Parameters
----------
d1 : int
Numerator degrees of freedom
d2 : int
Denominator degrees of freedom
"""
assert (
int(d1) == d1 and d1 >= 1
), 'Fisher (F) "d1" must be an integer greater than 0'
assert (
int(d2) == d2 and d2 >= 1
), 'Fisher (F) "d2" must be an integer greater than 0'
return uv(ss.f(d1, d2), tag=tag)
|
[
"def",
"Fisher",
"(",
"d1",
",",
"d2",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"(",
"int",
"(",
"d1",
")",
"==",
"d1",
"and",
"d1",
">=",
"1",
")",
",",
"'Fisher (F) \"d1\" must be an integer greater than 0'",
"assert",
"(",
"int",
"(",
"d2",
")",
"==",
"d2",
"and",
"d2",
">=",
"1",
")",
",",
"'Fisher (F) \"d2\" must be an integer greater than 0'",
"return",
"uv",
"(",
"ss",
".",
"f",
"(",
"d1",
",",
"d2",
")",
",",
"tag",
"=",
"tag",
")"
] |
An F (fisher) random variate
Parameters
----------
d1 : int
Numerator degrees of freedom
d2 : int
Denominator degrees of freedom
|
[
"An",
"F",
"(",
"fisher",
")",
"random",
"variate",
"Parameters",
"----------",
"d1",
":",
"int",
"Numerator",
"degrees",
"of",
"freedom",
"d2",
":",
"int",
"Denominator",
"degrees",
"of",
"freedom"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L907-L924
|
tisimst/mcerp
|
mcerp/__init__.py
|
Gamma
|
def Gamma(k, theta, tag=None):
"""
A Gamma random variate
Parameters
----------
k : scalar
The shape parameter (must be positive and non-zero)
theta : scalar
The scale parameter (must be positive and non-zero)
"""
assert (
k > 0 and theta > 0
), 'Gamma "k" and "theta" parameters must be greater than zero'
return uv(ss.gamma(k, scale=theta), tag=tag)
|
python
|
def Gamma(k, theta, tag=None):
"""
A Gamma random variate
Parameters
----------
k : scalar
The shape parameter (must be positive and non-zero)
theta : scalar
The scale parameter (must be positive and non-zero)
"""
assert (
k > 0 and theta > 0
), 'Gamma "k" and "theta" parameters must be greater than zero'
return uv(ss.gamma(k, scale=theta), tag=tag)
|
[
"def",
"Gamma",
"(",
"k",
",",
"theta",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"(",
"k",
">",
"0",
"and",
"theta",
">",
"0",
")",
",",
"'Gamma \"k\" and \"theta\" parameters must be greater than zero'",
"return",
"uv",
"(",
"ss",
".",
"gamma",
"(",
"k",
",",
"scale",
"=",
"theta",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Gamma random variate
Parameters
----------
k : scalar
The shape parameter (must be positive and non-zero)
theta : scalar
The scale parameter (must be positive and non-zero)
|
[
"A",
"Gamma",
"random",
"variate",
"Parameters",
"----------",
"k",
":",
"scalar",
"The",
"shape",
"parameter",
"(",
"must",
"be",
"positive",
"and",
"non",
"-",
"zero",
")",
"theta",
":",
"scalar",
"The",
"scale",
"parameter",
"(",
"must",
"be",
"positive",
"and",
"non",
"-",
"zero",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L930-L944
|
tisimst/mcerp
|
mcerp/__init__.py
|
LogNormal
|
def LogNormal(mu, sigma, tag=None):
"""
A Log-Normal random variate
Parameters
----------
mu : scalar
The location parameter
sigma : scalar
The scale parameter (must be positive and non-zero)
"""
assert sigma > 0, 'Log-Normal "sigma" must be positive'
return uv(ss.lognorm(sigma, loc=mu), tag=tag)
|
python
|
def LogNormal(mu, sigma, tag=None):
"""
A Log-Normal random variate
Parameters
----------
mu : scalar
The location parameter
sigma : scalar
The scale parameter (must be positive and non-zero)
"""
assert sigma > 0, 'Log-Normal "sigma" must be positive'
return uv(ss.lognorm(sigma, loc=mu), tag=tag)
|
[
"def",
"LogNormal",
"(",
"mu",
",",
"sigma",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"sigma",
">",
"0",
",",
"'Log-Normal \"sigma\" must be positive'",
"return",
"uv",
"(",
"ss",
".",
"lognorm",
"(",
"sigma",
",",
"loc",
"=",
"mu",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Log-Normal random variate
Parameters
----------
mu : scalar
The location parameter
sigma : scalar
The scale parameter (must be positive and non-zero)
|
[
"A",
"Log",
"-",
"Normal",
"random",
"variate",
"Parameters",
"----------",
"mu",
":",
"scalar",
"The",
"location",
"parameter",
"sigma",
":",
"scalar",
"The",
"scale",
"parameter",
"(",
"must",
"be",
"positive",
"and",
"non",
"-",
"zero",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L947-L959
|
tisimst/mcerp
|
mcerp/__init__.py
|
Normal
|
def Normal(mu, sigma, tag=None):
"""
A Normal (or Gaussian) random variate
Parameters
----------
mu : scalar
The mean value of the distribution
sigma : scalar
The standard deviation (must be positive and non-zero)
"""
assert sigma > 0, 'Normal "sigma" must be greater than zero'
return uv(ss.norm(loc=mu, scale=sigma), tag=tag)
|
python
|
def Normal(mu, sigma, tag=None):
"""
A Normal (or Gaussian) random variate
Parameters
----------
mu : scalar
The mean value of the distribution
sigma : scalar
The standard deviation (must be positive and non-zero)
"""
assert sigma > 0, 'Normal "sigma" must be greater than zero'
return uv(ss.norm(loc=mu, scale=sigma), tag=tag)
|
[
"def",
"Normal",
"(",
"mu",
",",
"sigma",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"sigma",
">",
"0",
",",
"'Normal \"sigma\" must be greater than zero'",
"return",
"uv",
"(",
"ss",
".",
"norm",
"(",
"loc",
"=",
"mu",
",",
"scale",
"=",
"sigma",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Normal (or Gaussian) random variate
Parameters
----------
mu : scalar
The mean value of the distribution
sigma : scalar
The standard deviation (must be positive and non-zero)
|
[
"A",
"Normal",
"(",
"or",
"Gaussian",
")",
"random",
"variate",
"Parameters",
"----------",
"mu",
":",
"scalar",
"The",
"mean",
"value",
"of",
"the",
"distribution",
"sigma",
":",
"scalar",
"The",
"standard",
"deviation",
"(",
"must",
"be",
"positive",
"and",
"non",
"-",
"zero",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L965-L977
|
tisimst/mcerp
|
mcerp/__init__.py
|
Pareto
|
def Pareto(q, a, tag=None):
"""
A Pareto random variate (first kind)
Parameters
----------
q : scalar
The scale parameter
a : scalar
The shape parameter (the minimum possible value)
"""
assert q > 0 and a > 0, 'Pareto "q" and "a" must be positive scalars'
p = Uniform(0, 1, tag)
return a * (1 - p) ** (-1.0 / q)
|
python
|
def Pareto(q, a, tag=None):
"""
A Pareto random variate (first kind)
Parameters
----------
q : scalar
The scale parameter
a : scalar
The shape parameter (the minimum possible value)
"""
assert q > 0 and a > 0, 'Pareto "q" and "a" must be positive scalars'
p = Uniform(0, 1, tag)
return a * (1 - p) ** (-1.0 / q)
|
[
"def",
"Pareto",
"(",
"q",
",",
"a",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"q",
">",
"0",
"and",
"a",
">",
"0",
",",
"'Pareto \"q\" and \"a\" must be positive scalars'",
"p",
"=",
"Uniform",
"(",
"0",
",",
"1",
",",
"tag",
")",
"return",
"a",
"*",
"(",
"1",
"-",
"p",
")",
"**",
"(",
"-",
"1.0",
"/",
"q",
")"
] |
A Pareto random variate (first kind)
Parameters
----------
q : scalar
The scale parameter
a : scalar
The shape parameter (the minimum possible value)
|
[
"A",
"Pareto",
"random",
"variate",
"(",
"first",
"kind",
")",
"Parameters",
"----------",
"q",
":",
"scalar",
"The",
"scale",
"parameter",
"a",
":",
"scalar",
"The",
"shape",
"parameter",
"(",
"the",
"minimum",
"possible",
"value",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L983-L996
|
tisimst/mcerp
|
mcerp/__init__.py
|
Pareto2
|
def Pareto2(q, b, tag=None):
"""
A Pareto random variate (second kind). This form always starts at the
origin.
Parameters
----------
q : scalar
The scale parameter
b : scalar
The shape parameter
"""
assert q > 0 and b > 0, 'Pareto2 "q" and "b" must be positive scalars'
return Pareto(q, b, tag) - b
|
python
|
def Pareto2(q, b, tag=None):
"""
A Pareto random variate (second kind). This form always starts at the
origin.
Parameters
----------
q : scalar
The scale parameter
b : scalar
The shape parameter
"""
assert q > 0 and b > 0, 'Pareto2 "q" and "b" must be positive scalars'
return Pareto(q, b, tag) - b
|
[
"def",
"Pareto2",
"(",
"q",
",",
"b",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"q",
">",
"0",
"and",
"b",
">",
"0",
",",
"'Pareto2 \"q\" and \"b\" must be positive scalars'",
"return",
"Pareto",
"(",
"q",
",",
"b",
",",
"tag",
")",
"-",
"b"
] |
A Pareto random variate (second kind). This form always starts at the
origin.
Parameters
----------
q : scalar
The scale parameter
b : scalar
The shape parameter
|
[
"A",
"Pareto",
"random",
"variate",
"(",
"second",
"kind",
")",
".",
"This",
"form",
"always",
"starts",
"at",
"the",
"origin",
".",
"Parameters",
"----------",
"q",
":",
"scalar",
"The",
"scale",
"parameter",
"b",
":",
"scalar",
"The",
"shape",
"parameter"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L999-L1012
|
tisimst/mcerp
|
mcerp/__init__.py
|
PERT
|
def PERT(low, peak, high, g=4.0, tag=None):
"""
A PERT random variate
Parameters
----------
low : scalar
Lower bound of the distribution support
peak : scalar
The location of the distribution's peak (low <= peak <= high)
high : scalar
Upper bound of the distribution support
Optional
--------
g : scalar
Controls the uncertainty of the distribution around the peak. Smaller
values make the distribution flatter and more uncertain around the
peak while larger values make it focused and less uncertain around
the peak. (Default: 4)
"""
a, b, c = [float(x) for x in [low, peak, high]]
assert a <= b <= c, 'PERT "peak" must be greater than "low" and less than "high"'
assert g >= 0, 'PERT "g" must be non-negative'
mu = (a + g * b + c) / (g + 2)
if mu == b:
a1 = a2 = 3.0
else:
a1 = ((mu - a) * (2 * b - a - c)) / ((b - mu) * (c - a))
a2 = a1 * (c - mu) / (mu - a)
return Beta(a1, a2, a, c, tag)
|
python
|
def PERT(low, peak, high, g=4.0, tag=None):
"""
A PERT random variate
Parameters
----------
low : scalar
Lower bound of the distribution support
peak : scalar
The location of the distribution's peak (low <= peak <= high)
high : scalar
Upper bound of the distribution support
Optional
--------
g : scalar
Controls the uncertainty of the distribution around the peak. Smaller
values make the distribution flatter and more uncertain around the
peak while larger values make it focused and less uncertain around
the peak. (Default: 4)
"""
a, b, c = [float(x) for x in [low, peak, high]]
assert a <= b <= c, 'PERT "peak" must be greater than "low" and less than "high"'
assert g >= 0, 'PERT "g" must be non-negative'
mu = (a + g * b + c) / (g + 2)
if mu == b:
a1 = a2 = 3.0
else:
a1 = ((mu - a) * (2 * b - a - c)) / ((b - mu) * (c - a))
a2 = a1 * (c - mu) / (mu - a)
return Beta(a1, a2, a, c, tag)
|
[
"def",
"PERT",
"(",
"low",
",",
"peak",
",",
"high",
",",
"g",
"=",
"4.0",
",",
"tag",
"=",
"None",
")",
":",
"a",
",",
"b",
",",
"c",
"=",
"[",
"float",
"(",
"x",
")",
"for",
"x",
"in",
"[",
"low",
",",
"peak",
",",
"high",
"]",
"]",
"assert",
"a",
"<=",
"b",
"<=",
"c",
",",
"'PERT \"peak\" must be greater than \"low\" and less than \"high\"'",
"assert",
"g",
">=",
"0",
",",
"'PERT \"g\" must be non-negative'",
"mu",
"=",
"(",
"a",
"+",
"g",
"*",
"b",
"+",
"c",
")",
"/",
"(",
"g",
"+",
"2",
")",
"if",
"mu",
"==",
"b",
":",
"a1",
"=",
"a2",
"=",
"3.0",
"else",
":",
"a1",
"=",
"(",
"(",
"mu",
"-",
"a",
")",
"*",
"(",
"2",
"*",
"b",
"-",
"a",
"-",
"c",
")",
")",
"/",
"(",
"(",
"b",
"-",
"mu",
")",
"*",
"(",
"c",
"-",
"a",
")",
")",
"a2",
"=",
"a1",
"*",
"(",
"c",
"-",
"mu",
")",
"/",
"(",
"mu",
"-",
"a",
")",
"return",
"Beta",
"(",
"a1",
",",
"a2",
",",
"a",
",",
"c",
",",
"tag",
")"
] |
A PERT random variate
Parameters
----------
low : scalar
Lower bound of the distribution support
peak : scalar
The location of the distribution's peak (low <= peak <= high)
high : scalar
Upper bound of the distribution support
Optional
--------
g : scalar
Controls the uncertainty of the distribution around the peak. Smaller
values make the distribution flatter and more uncertain around the
peak while larger values make it focused and less uncertain around
the peak. (Default: 4)
|
[
"A",
"PERT",
"random",
"variate",
"Parameters",
"----------",
"low",
":",
"scalar",
"Lower",
"bound",
"of",
"the",
"distribution",
"support",
"peak",
":",
"scalar",
"The",
"location",
"of",
"the",
"distribution",
"s",
"peak",
"(",
"low",
"<",
"=",
"peak",
"<",
"=",
"high",
")",
"high",
":",
"scalar",
"Upper",
"bound",
"of",
"the",
"distribution",
"support",
"Optional",
"--------",
"g",
":",
"scalar",
"Controls",
"the",
"uncertainty",
"of",
"the",
"distribution",
"around",
"the",
"peak",
".",
"Smaller",
"values",
"make",
"the",
"distribution",
"flatter",
"and",
"more",
"uncertain",
"around",
"the",
"peak",
"while",
"larger",
"values",
"make",
"it",
"focused",
"and",
"less",
"uncertain",
"around",
"the",
"peak",
".",
"(",
"Default",
":",
"4",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L1015-L1046
|
tisimst/mcerp
|
mcerp/__init__.py
|
StudentT
|
def StudentT(v, tag=None):
"""
A Student-T random variate
Parameters
----------
v : int
The degrees of freedom of the distribution (must be greater than one)
"""
assert int(v) == v and v >= 1, 'Student-T "v" must be an integer greater than 0'
return uv(ss.t(v), tag=tag)
|
python
|
def StudentT(v, tag=None):
"""
A Student-T random variate
Parameters
----------
v : int
The degrees of freedom of the distribution (must be greater than one)
"""
assert int(v) == v and v >= 1, 'Student-T "v" must be an integer greater than 0'
return uv(ss.t(v), tag=tag)
|
[
"def",
"StudentT",
"(",
"v",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"int",
"(",
"v",
")",
"==",
"v",
"and",
"v",
">=",
"1",
",",
"'Student-T \"v\" must be an integer greater than 0'",
"return",
"uv",
"(",
"ss",
".",
"t",
"(",
"v",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Student-T random variate
Parameters
----------
v : int
The degrees of freedom of the distribution (must be greater than one)
|
[
"A",
"Student",
"-",
"T",
"random",
"variate",
"Parameters",
"----------",
"v",
":",
"int",
"The",
"degrees",
"of",
"freedom",
"of",
"the",
"distribution",
"(",
"must",
"be",
"greater",
"than",
"one",
")"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L1049-L1059
|
tisimst/mcerp
|
mcerp/__init__.py
|
Triangular
|
def Triangular(low, peak, high, tag=None):
"""
A triangular random variate
Parameters
----------
low : scalar
Lower bound of the distribution support
peak : scalar
The location of the triangle's peak (low <= peak <= high)
high : scalar
Upper bound of the distribution support
"""
assert low <= peak <= high, 'Triangular "peak" must lie between "low" and "high"'
low, peak, high = [float(x) for x in [low, peak, high]]
return uv(
ss.triang((1.0 * peak - low) / (high - low), loc=low, scale=(high - low)),
tag=tag,
)
|
python
|
def Triangular(low, peak, high, tag=None):
"""
A triangular random variate
Parameters
----------
low : scalar
Lower bound of the distribution support
peak : scalar
The location of the triangle's peak (low <= peak <= high)
high : scalar
Upper bound of the distribution support
"""
assert low <= peak <= high, 'Triangular "peak" must lie between "low" and "high"'
low, peak, high = [float(x) for x in [low, peak, high]]
return uv(
ss.triang((1.0 * peak - low) / (high - low), loc=low, scale=(high - low)),
tag=tag,
)
|
[
"def",
"Triangular",
"(",
"low",
",",
"peak",
",",
"high",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"low",
"<=",
"peak",
"<=",
"high",
",",
"'Triangular \"peak\" must lie between \"low\" and \"high\"'",
"low",
",",
"peak",
",",
"high",
"=",
"[",
"float",
"(",
"x",
")",
"for",
"x",
"in",
"[",
"low",
",",
"peak",
",",
"high",
"]",
"]",
"return",
"uv",
"(",
"ss",
".",
"triang",
"(",
"(",
"1.0",
"*",
"peak",
"-",
"low",
")",
"/",
"(",
"high",
"-",
"low",
")",
",",
"loc",
"=",
"low",
",",
"scale",
"=",
"(",
"high",
"-",
"low",
")",
")",
",",
"tag",
"=",
"tag",
",",
")"
] |
A triangular random variate
Parameters
----------
low : scalar
Lower bound of the distribution support
peak : scalar
The location of the triangle's peak (low <= peak <= high)
high : scalar
Upper bound of the distribution support
|
[
"A",
"triangular",
"random",
"variate",
"Parameters",
"----------",
"low",
":",
"scalar",
"Lower",
"bound",
"of",
"the",
"distribution",
"support",
"peak",
":",
"scalar",
"The",
"location",
"of",
"the",
"triangle",
"s",
"peak",
"(",
"low",
"<",
"=",
"peak",
"<",
"=",
"high",
")",
"high",
":",
"scalar",
"Upper",
"bound",
"of",
"the",
"distribution",
"support"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L1065-L1083
|
tisimst/mcerp
|
mcerp/__init__.py
|
Uniform
|
def Uniform(low, high, tag=None):
"""
A Uniform random variate
Parameters
----------
low : scalar
Lower bound of the distribution support.
high : scalar
Upper bound of the distribution support.
"""
assert low < high, 'Uniform "low" must be less than "high"'
return uv(ss.uniform(loc=low, scale=high - low), tag=tag)
|
python
|
def Uniform(low, high, tag=None):
"""
A Uniform random variate
Parameters
----------
low : scalar
Lower bound of the distribution support.
high : scalar
Upper bound of the distribution support.
"""
assert low < high, 'Uniform "low" must be less than "high"'
return uv(ss.uniform(loc=low, scale=high - low), tag=tag)
|
[
"def",
"Uniform",
"(",
"low",
",",
"high",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"low",
"<",
"high",
",",
"'Uniform \"low\" must be less than \"high\"'",
"return",
"uv",
"(",
"ss",
".",
"uniform",
"(",
"loc",
"=",
"low",
",",
"scale",
"=",
"high",
"-",
"low",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Uniform random variate
Parameters
----------
low : scalar
Lower bound of the distribution support.
high : scalar
Upper bound of the distribution support.
|
[
"A",
"Uniform",
"random",
"variate",
"Parameters",
"----------",
"low",
":",
"scalar",
"Lower",
"bound",
"of",
"the",
"distribution",
"support",
".",
"high",
":",
"scalar",
"Upper",
"bound",
"of",
"the",
"distribution",
"support",
"."
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L1089-L1101
|
tisimst/mcerp
|
mcerp/__init__.py
|
Weibull
|
def Weibull(lamda, k, tag=None):
"""
A Weibull random variate
Parameters
----------
lamda : scalar
The scale parameter
k : scalar
The shape parameter
"""
assert (
lamda > 0 and k > 0
), 'Weibull "lamda" and "k" parameters must be greater than zero'
return uv(ss.exponweib(lamda, k), tag=tag)
|
python
|
def Weibull(lamda, k, tag=None):
"""
A Weibull random variate
Parameters
----------
lamda : scalar
The scale parameter
k : scalar
The shape parameter
"""
assert (
lamda > 0 and k > 0
), 'Weibull "lamda" and "k" parameters must be greater than zero'
return uv(ss.exponweib(lamda, k), tag=tag)
|
[
"def",
"Weibull",
"(",
"lamda",
",",
"k",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"(",
"lamda",
">",
"0",
"and",
"k",
">",
"0",
")",
",",
"'Weibull \"lamda\" and \"k\" parameters must be greater than zero'",
"return",
"uv",
"(",
"ss",
".",
"exponweib",
"(",
"lamda",
",",
"k",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Weibull random variate
Parameters
----------
lamda : scalar
The scale parameter
k : scalar
The shape parameter
|
[
"A",
"Weibull",
"random",
"variate",
"Parameters",
"----------",
"lamda",
":",
"scalar",
"The",
"scale",
"parameter",
"k",
":",
"scalar",
"The",
"shape",
"parameter"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L1107-L1121
|
tisimst/mcerp
|
mcerp/__init__.py
|
Bernoulli
|
def Bernoulli(p, tag=None):
"""
A Bernoulli random variate
Parameters
----------
p : scalar
The probability of success
"""
assert (
0 < p < 1
), 'Bernoulli probability "p" must be between zero and one, non-inclusive'
return uv(ss.bernoulli(p), tag=tag)
|
python
|
def Bernoulli(p, tag=None):
"""
A Bernoulli random variate
Parameters
----------
p : scalar
The probability of success
"""
assert (
0 < p < 1
), 'Bernoulli probability "p" must be between zero and one, non-inclusive'
return uv(ss.bernoulli(p), tag=tag)
|
[
"def",
"Bernoulli",
"(",
"p",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"(",
"0",
"<",
"p",
"<",
"1",
")",
",",
"'Bernoulli probability \"p\" must be between zero and one, non-inclusive'",
"return",
"uv",
"(",
"ss",
".",
"bernoulli",
"(",
"p",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Bernoulli random variate
Parameters
----------
p : scalar
The probability of success
|
[
"A",
"Bernoulli",
"random",
"variate",
"Parameters",
"----------",
"p",
":",
"scalar",
"The",
"probability",
"of",
"success"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L1132-L1144
|
tisimst/mcerp
|
mcerp/__init__.py
|
Binomial
|
def Binomial(n, p, tag=None):
"""
A Binomial random variate
Parameters
----------
n : int
The number of trials
p : scalar
The probability of success
"""
assert (
int(n) == n and n > 0
), 'Binomial number of trials "n" must be an integer greater than zero'
assert (
0 < p < 1
), 'Binomial probability "p" must be between zero and one, non-inclusive'
return uv(ss.binom(n, p), tag=tag)
|
python
|
def Binomial(n, p, tag=None):
"""
A Binomial random variate
Parameters
----------
n : int
The number of trials
p : scalar
The probability of success
"""
assert (
int(n) == n and n > 0
), 'Binomial number of trials "n" must be an integer greater than zero'
assert (
0 < p < 1
), 'Binomial probability "p" must be between zero and one, non-inclusive'
return uv(ss.binom(n, p), tag=tag)
|
[
"def",
"Binomial",
"(",
"n",
",",
"p",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"(",
"int",
"(",
"n",
")",
"==",
"n",
"and",
"n",
">",
"0",
")",
",",
"'Binomial number of trials \"n\" must be an integer greater than zero'",
"assert",
"(",
"0",
"<",
"p",
"<",
"1",
")",
",",
"'Binomial probability \"p\" must be between zero and one, non-inclusive'",
"return",
"uv",
"(",
"ss",
".",
"binom",
"(",
"n",
",",
"p",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Binomial random variate
Parameters
----------
n : int
The number of trials
p : scalar
The probability of success
|
[
"A",
"Binomial",
"random",
"variate",
"Parameters",
"----------",
"n",
":",
"int",
"The",
"number",
"of",
"trials",
"p",
":",
"scalar",
"The",
"probability",
"of",
"success"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L1150-L1167
|
tisimst/mcerp
|
mcerp/__init__.py
|
Geometric
|
def Geometric(p, tag=None):
"""
A Geometric random variate
Parameters
----------
p : scalar
The probability of success
"""
assert (
0 < p < 1
), 'Geometric probability "p" must be between zero and one, non-inclusive'
return uv(ss.geom(p), tag=tag)
|
python
|
def Geometric(p, tag=None):
"""
A Geometric random variate
Parameters
----------
p : scalar
The probability of success
"""
assert (
0 < p < 1
), 'Geometric probability "p" must be between zero and one, non-inclusive'
return uv(ss.geom(p), tag=tag)
|
[
"def",
"Geometric",
"(",
"p",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"(",
"0",
"<",
"p",
"<",
"1",
")",
",",
"'Geometric probability \"p\" must be between zero and one, non-inclusive'",
"return",
"uv",
"(",
"ss",
".",
"geom",
"(",
"p",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Geometric random variate
Parameters
----------
p : scalar
The probability of success
|
[
"A",
"Geometric",
"random",
"variate",
"Parameters",
"----------",
"p",
":",
"scalar",
"The",
"probability",
"of",
"success"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L1173-L1185
|
tisimst/mcerp
|
mcerp/__init__.py
|
Hypergeometric
|
def Hypergeometric(N, n, K, tag=None):
"""
A Hypergeometric random variate
Parameters
----------
N : int
The total population size
n : int
The number of individuals of interest in the population
K : int
The number of individuals that will be chosen from the population
Example
-------
(Taken from the wikipedia page) Assume we have an urn with two types of
marbles, 45 black ones and 5 white ones. Standing next to the urn, you
close your eyes and draw 10 marbles without replacement. What is the
probability that exactly 4 of the 10 are white?
::
>>> black = 45
>>> white = 5
>>> draw = 10
# Now we create the distribution
>>> h = H(black + white, white, draw)
# To check the probability, in this case, we can use the underlying
# scipy.stats object
>>> h.rv.pmf(4) # What is the probability that white count = 4?
0.0039645830580151975
"""
assert (
int(N) == N and N > 0
), 'Hypergeometric total population size "N" must be an integer greater than zero.'
assert (
int(n) == n and 0 < n <= N
), 'Hypergeometric interest population size "n" must be an integer greater than zero and no more than the total population size.'
assert (
int(K) == K and 0 < K <= N
), 'Hypergeometric chosen population size "K" must be an integer greater than zero and no more than the total population size.'
return uv(ss.hypergeom(N, n, K), tag=tag)
|
python
|
def Hypergeometric(N, n, K, tag=None):
"""
A Hypergeometric random variate
Parameters
----------
N : int
The total population size
n : int
The number of individuals of interest in the population
K : int
The number of individuals that will be chosen from the population
Example
-------
(Taken from the wikipedia page) Assume we have an urn with two types of
marbles, 45 black ones and 5 white ones. Standing next to the urn, you
close your eyes and draw 10 marbles without replacement. What is the
probability that exactly 4 of the 10 are white?
::
>>> black = 45
>>> white = 5
>>> draw = 10
# Now we create the distribution
>>> h = H(black + white, white, draw)
# To check the probability, in this case, we can use the underlying
# scipy.stats object
>>> h.rv.pmf(4) # What is the probability that white count = 4?
0.0039645830580151975
"""
assert (
int(N) == N and N > 0
), 'Hypergeometric total population size "N" must be an integer greater than zero.'
assert (
int(n) == n and 0 < n <= N
), 'Hypergeometric interest population size "n" must be an integer greater than zero and no more than the total population size.'
assert (
int(K) == K and 0 < K <= N
), 'Hypergeometric chosen population size "K" must be an integer greater than zero and no more than the total population size.'
return uv(ss.hypergeom(N, n, K), tag=tag)
|
[
"def",
"Hypergeometric",
"(",
"N",
",",
"n",
",",
"K",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"(",
"int",
"(",
"N",
")",
"==",
"N",
"and",
"N",
">",
"0",
")",
",",
"'Hypergeometric total population size \"N\" must be an integer greater than zero.'",
"assert",
"(",
"int",
"(",
"n",
")",
"==",
"n",
"and",
"0",
"<",
"n",
"<=",
"N",
")",
",",
"'Hypergeometric interest population size \"n\" must be an integer greater than zero and no more than the total population size.'",
"assert",
"(",
"int",
"(",
"K",
")",
"==",
"K",
"and",
"0",
"<",
"K",
"<=",
"N",
")",
",",
"'Hypergeometric chosen population size \"K\" must be an integer greater than zero and no more than the total population size.'",
"return",
"uv",
"(",
"ss",
".",
"hypergeom",
"(",
"N",
",",
"n",
",",
"K",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Hypergeometric random variate
Parameters
----------
N : int
The total population size
n : int
The number of individuals of interest in the population
K : int
The number of individuals that will be chosen from the population
Example
-------
(Taken from the wikipedia page) Assume we have an urn with two types of
marbles, 45 black ones and 5 white ones. Standing next to the urn, you
close your eyes and draw 10 marbles without replacement. What is the
probability that exactly 4 of the 10 are white?
::
>>> black = 45
>>> white = 5
>>> draw = 10
# Now we create the distribution
>>> h = H(black + white, white, draw)
# To check the probability, in this case, we can use the underlying
# scipy.stats object
>>> h.rv.pmf(4) # What is the probability that white count = 4?
0.0039645830580151975
|
[
"A",
"Hypergeometric",
"random",
"variate",
"Parameters",
"----------",
"N",
":",
"int",
"The",
"total",
"population",
"size",
"n",
":",
"int",
"The",
"number",
"of",
"individuals",
"of",
"interest",
"in",
"the",
"population",
"K",
":",
"int",
"The",
"number",
"of",
"individuals",
"that",
"will",
"be",
"chosen",
"from",
"the",
"population",
"Example",
"-------",
"(",
"Taken",
"from",
"the",
"wikipedia",
"page",
")",
"Assume",
"we",
"have",
"an",
"urn",
"with",
"two",
"types",
"of",
"marbles",
"45",
"black",
"ones",
"and",
"5",
"white",
"ones",
".",
"Standing",
"next",
"to",
"the",
"urn",
"you",
"close",
"your",
"eyes",
"and",
"draw",
"10",
"marbles",
"without",
"replacement",
".",
"What",
"is",
"the",
"probability",
"that",
"exactly",
"4",
"of",
"the",
"10",
"are",
"white?",
"::",
">>>",
"black",
"=",
"45",
">>>",
"white",
"=",
"5",
">>>",
"draw",
"=",
"10",
"#",
"Now",
"we",
"create",
"the",
"distribution",
">>>",
"h",
"=",
"H",
"(",
"black",
"+",
"white",
"white",
"draw",
")",
"#",
"To",
"check",
"the",
"probability",
"in",
"this",
"case",
"we",
"can",
"use",
"the",
"underlying",
"#",
"scipy",
".",
"stats",
"object",
">>>",
"h",
".",
"rv",
".",
"pmf",
"(",
"4",
")",
"#",
"What",
"is",
"the",
"probability",
"that",
"white",
"count",
"=",
"4?",
"0",
".",
"0039645830580151975"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L1191-L1234
|
tisimst/mcerp
|
mcerp/__init__.py
|
Poisson
|
def Poisson(lamda, tag=None):
"""
A Poisson random variate
Parameters
----------
lamda : scalar
The rate of an occurance within a specified interval of time or space.
"""
assert lamda > 0, 'Poisson "lamda" must be greater than zero.'
return uv(ss.poisson(lamda), tag=tag)
|
python
|
def Poisson(lamda, tag=None):
"""
A Poisson random variate
Parameters
----------
lamda : scalar
The rate of an occurance within a specified interval of time or space.
"""
assert lamda > 0, 'Poisson "lamda" must be greater than zero.'
return uv(ss.poisson(lamda), tag=tag)
|
[
"def",
"Poisson",
"(",
"lamda",
",",
"tag",
"=",
"None",
")",
":",
"assert",
"lamda",
">",
"0",
",",
"'Poisson \"lamda\" must be greater than zero.'",
"return",
"uv",
"(",
"ss",
".",
"poisson",
"(",
"lamda",
")",
",",
"tag",
"=",
"tag",
")"
] |
A Poisson random variate
Parameters
----------
lamda : scalar
The rate of an occurance within a specified interval of time or space.
|
[
"A",
"Poisson",
"random",
"variate",
"Parameters",
"----------",
"lamda",
":",
"scalar",
"The",
"rate",
"of",
"an",
"occurance",
"within",
"a",
"specified",
"interval",
"of",
"time",
"or",
"space",
"."
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L1240-L1250
|
tisimst/mcerp
|
mcerp/__init__.py
|
covariance_matrix
|
def covariance_matrix(nums_with_uncert):
"""
Calculate the covariance matrix of uncertain variables, oriented by the
order of the inputs
Parameters
----------
nums_with_uncert : array-like
A list of variables that have an associated uncertainty
Returns
-------
cov_matrix : 2d-array-like
A nested list containing covariance values
Example
-------
>>> x = N(1, 0.1)
>>> y = N(10, 0.1)
>>> z = x + 2*y
>>> covariance_matrix([x,y,z])
[[ 9.99694861e-03 2.54000840e-05 1.00477488e-02]
[ 2.54000840e-05 9.99823207e-03 2.00218642e-02]
[ 1.00477488e-02 2.00218642e-02 5.00914772e-02]]
"""
ufuncs = list(map(to_uncertain_func, nums_with_uncert))
cov_matrix = []
for (i1, expr1) in enumerate(ufuncs):
coefs_expr1 = []
mean1 = expr1.mean
for (i2, expr2) in enumerate(ufuncs[: i1 + 1]):
mean2 = expr2.mean
coef = np.mean((expr1._mcpts - mean1) * (expr2._mcpts - mean2))
coefs_expr1.append(coef)
cov_matrix.append(coefs_expr1)
# We symmetrize the matrix:
for (i, covariance_coefs) in enumerate(cov_matrix):
covariance_coefs.extend(cov_matrix[j][i] for j in range(i + 1, len(cov_matrix)))
return cov_matrix
|
python
|
def covariance_matrix(nums_with_uncert):
"""
Calculate the covariance matrix of uncertain variables, oriented by the
order of the inputs
Parameters
----------
nums_with_uncert : array-like
A list of variables that have an associated uncertainty
Returns
-------
cov_matrix : 2d-array-like
A nested list containing covariance values
Example
-------
>>> x = N(1, 0.1)
>>> y = N(10, 0.1)
>>> z = x + 2*y
>>> covariance_matrix([x,y,z])
[[ 9.99694861e-03 2.54000840e-05 1.00477488e-02]
[ 2.54000840e-05 9.99823207e-03 2.00218642e-02]
[ 1.00477488e-02 2.00218642e-02 5.00914772e-02]]
"""
ufuncs = list(map(to_uncertain_func, nums_with_uncert))
cov_matrix = []
for (i1, expr1) in enumerate(ufuncs):
coefs_expr1 = []
mean1 = expr1.mean
for (i2, expr2) in enumerate(ufuncs[: i1 + 1]):
mean2 = expr2.mean
coef = np.mean((expr1._mcpts - mean1) * (expr2._mcpts - mean2))
coefs_expr1.append(coef)
cov_matrix.append(coefs_expr1)
# We symmetrize the matrix:
for (i, covariance_coefs) in enumerate(cov_matrix):
covariance_coefs.extend(cov_matrix[j][i] for j in range(i + 1, len(cov_matrix)))
return cov_matrix
|
[
"def",
"covariance_matrix",
"(",
"nums_with_uncert",
")",
":",
"ufuncs",
"=",
"list",
"(",
"map",
"(",
"to_uncertain_func",
",",
"nums_with_uncert",
")",
")",
"cov_matrix",
"=",
"[",
"]",
"for",
"(",
"i1",
",",
"expr1",
")",
"in",
"enumerate",
"(",
"ufuncs",
")",
":",
"coefs_expr1",
"=",
"[",
"]",
"mean1",
"=",
"expr1",
".",
"mean",
"for",
"(",
"i2",
",",
"expr2",
")",
"in",
"enumerate",
"(",
"ufuncs",
"[",
":",
"i1",
"+",
"1",
"]",
")",
":",
"mean2",
"=",
"expr2",
".",
"mean",
"coef",
"=",
"np",
".",
"mean",
"(",
"(",
"expr1",
".",
"_mcpts",
"-",
"mean1",
")",
"*",
"(",
"expr2",
".",
"_mcpts",
"-",
"mean2",
")",
")",
"coefs_expr1",
".",
"append",
"(",
"coef",
")",
"cov_matrix",
".",
"append",
"(",
"coefs_expr1",
")",
"# We symmetrize the matrix:",
"for",
"(",
"i",
",",
"covariance_coefs",
")",
"in",
"enumerate",
"(",
"cov_matrix",
")",
":",
"covariance_coefs",
".",
"extend",
"(",
"cov_matrix",
"[",
"j",
"]",
"[",
"i",
"]",
"for",
"j",
"in",
"range",
"(",
"i",
"+",
"1",
",",
"len",
"(",
"cov_matrix",
")",
")",
")",
"return",
"cov_matrix"
] |
Calculate the covariance matrix of uncertain variables, oriented by the
order of the inputs
Parameters
----------
nums_with_uncert : array-like
A list of variables that have an associated uncertainty
Returns
-------
cov_matrix : 2d-array-like
A nested list containing covariance values
Example
-------
>>> x = N(1, 0.1)
>>> y = N(10, 0.1)
>>> z = x + 2*y
>>> covariance_matrix([x,y,z])
[[ 9.99694861e-03 2.54000840e-05 1.00477488e-02]
[ 2.54000840e-05 9.99823207e-03 2.00218642e-02]
[ 1.00477488e-02 2.00218642e-02 5.00914772e-02]]
|
[
"Calculate",
"the",
"covariance",
"matrix",
"of",
"uncertain",
"variables",
"oriented",
"by",
"the",
"order",
"of",
"the",
"inputs",
"Parameters",
"----------",
"nums_with_uncert",
":",
"array",
"-",
"like",
"A",
"list",
"of",
"variables",
"that",
"have",
"an",
"associated",
"uncertainty",
"Returns",
"-------",
"cov_matrix",
":",
"2d",
"-",
"array",
"-",
"like",
"A",
"nested",
"list",
"containing",
"covariance",
"values",
"Example",
"-------",
">>>",
"x",
"=",
"N",
"(",
"1",
"0",
".",
"1",
")",
">>>",
"y",
"=",
"N",
"(",
"10",
"0",
".",
"1",
")",
">>>",
"z",
"=",
"x",
"+",
"2",
"*",
"y",
">>>",
"covariance_matrix",
"(",
"[",
"x",
"y",
"z",
"]",
")",
"[[",
"9",
".",
"99694861e",
"-",
"03",
"2",
".",
"54000840e",
"-",
"05",
"1",
".",
"00477488e",
"-",
"02",
"]",
"[",
"2",
".",
"54000840e",
"-",
"05",
"9",
".",
"99823207e",
"-",
"03",
"2",
".",
"00218642e",
"-",
"02",
"]",
"[",
"1",
".",
"00477488e",
"-",
"02",
"2",
".",
"00218642e",
"-",
"02",
"5",
".",
"00914772e",
"-",
"02",
"]]"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L1261-L1303
|
tisimst/mcerp
|
mcerp/__init__.py
|
correlation_matrix
|
def correlation_matrix(nums_with_uncert):
"""
Calculate the correlation matrix of uncertain variables, oriented by the
order of the inputs
Parameters
----------
nums_with_uncert : array-like
A list of variables that have an associated uncertainty
Returns
-------
corr_matrix : 2d-array-like
A nested list containing covariance values
Example
-------
>>> x = N(1, 0.1)
>>> y = N(10, 0.1)
>>> z = x + 2*y
>>> correlation_matrix([x,y,z])
[[ 0.99969486 0.00254001 0.4489385 ]
[ 0.00254001 0.99982321 0.89458702]
[ 0.4489385 0.89458702 1. ]]
"""
ufuncs = list(map(to_uncertain_func, nums_with_uncert))
data = np.vstack([ufunc._mcpts for ufunc in ufuncs])
return np.corrcoef(data.T, rowvar=0)
|
python
|
def correlation_matrix(nums_with_uncert):
"""
Calculate the correlation matrix of uncertain variables, oriented by the
order of the inputs
Parameters
----------
nums_with_uncert : array-like
A list of variables that have an associated uncertainty
Returns
-------
corr_matrix : 2d-array-like
A nested list containing covariance values
Example
-------
>>> x = N(1, 0.1)
>>> y = N(10, 0.1)
>>> z = x + 2*y
>>> correlation_matrix([x,y,z])
[[ 0.99969486 0.00254001 0.4489385 ]
[ 0.00254001 0.99982321 0.89458702]
[ 0.4489385 0.89458702 1. ]]
"""
ufuncs = list(map(to_uncertain_func, nums_with_uncert))
data = np.vstack([ufunc._mcpts for ufunc in ufuncs])
return np.corrcoef(data.T, rowvar=0)
|
[
"def",
"correlation_matrix",
"(",
"nums_with_uncert",
")",
":",
"ufuncs",
"=",
"list",
"(",
"map",
"(",
"to_uncertain_func",
",",
"nums_with_uncert",
")",
")",
"data",
"=",
"np",
".",
"vstack",
"(",
"[",
"ufunc",
".",
"_mcpts",
"for",
"ufunc",
"in",
"ufuncs",
"]",
")",
"return",
"np",
".",
"corrcoef",
"(",
"data",
".",
"T",
",",
"rowvar",
"=",
"0",
")"
] |
Calculate the correlation matrix of uncertain variables, oriented by the
order of the inputs
Parameters
----------
nums_with_uncert : array-like
A list of variables that have an associated uncertainty
Returns
-------
corr_matrix : 2d-array-like
A nested list containing covariance values
Example
-------
>>> x = N(1, 0.1)
>>> y = N(10, 0.1)
>>> z = x + 2*y
>>> correlation_matrix([x,y,z])
[[ 0.99969486 0.00254001 0.4489385 ]
[ 0.00254001 0.99982321 0.89458702]
[ 0.4489385 0.89458702 1. ]]
|
[
"Calculate",
"the",
"correlation",
"matrix",
"of",
"uncertain",
"variables",
"oriented",
"by",
"the",
"order",
"of",
"the",
"inputs",
"Parameters",
"----------",
"nums_with_uncert",
":",
"array",
"-",
"like",
"A",
"list",
"of",
"variables",
"that",
"have",
"an",
"associated",
"uncertainty",
"Returns",
"-------",
"corr_matrix",
":",
"2d",
"-",
"array",
"-",
"like",
"A",
"nested",
"list",
"containing",
"covariance",
"values",
"Example",
"-------",
">>>",
"x",
"=",
"N",
"(",
"1",
"0",
".",
"1",
")",
">>>",
"y",
"=",
"N",
"(",
"10",
"0",
".",
"1",
")",
">>>",
"z",
"=",
"x",
"+",
"2",
"*",
"y",
">>>",
"correlation_matrix",
"(",
"[",
"x",
"y",
"z",
"]",
")",
"[[",
"0",
".",
"99969486",
"0",
".",
"00254001",
"0",
".",
"4489385",
"]",
"[",
"0",
".",
"00254001",
"0",
".",
"99982321",
"0",
".",
"89458702",
"]",
"[",
"0",
".",
"4489385",
"0",
".",
"89458702",
"1",
".",
"]]"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L1306-L1335
|
tisimst/mcerp
|
mcerp/__init__.py
|
UncertainFunction.var
|
def var(self):
"""
Variance value as a result of an uncertainty calculation
"""
mn = self.mean
vr = np.mean((self._mcpts - mn) ** 2)
return vr
|
python
|
def var(self):
"""
Variance value as a result of an uncertainty calculation
"""
mn = self.mean
vr = np.mean((self._mcpts - mn) ** 2)
return vr
|
[
"def",
"var",
"(",
"self",
")",
":",
"mn",
"=",
"self",
".",
"mean",
"vr",
"=",
"np",
".",
"mean",
"(",
"(",
"self",
".",
"_mcpts",
"-",
"mn",
")",
"**",
"2",
")",
"return",
"vr"
] |
Variance value as a result of an uncertainty calculation
|
[
"Variance",
"value",
"as",
"a",
"result",
"of",
"an",
"uncertainty",
"calculation"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L74-L80
|
tisimst/mcerp
|
mcerp/__init__.py
|
UncertainFunction.skew
|
def skew(self):
r"""
Skewness coefficient value as a result of an uncertainty calculation,
defined as::
_____ m3
\/beta1 = ------
std**3
where m3 is the third central moment and std is the standard deviation
"""
mn = self.mean
sd = self.std
sk = 0.0 if abs(sd) <= 1e-8 else np.mean((self._mcpts - mn) ** 3) / sd ** 3
return sk
|
python
|
def skew(self):
r"""
Skewness coefficient value as a result of an uncertainty calculation,
defined as::
_____ m3
\/beta1 = ------
std**3
where m3 is the third central moment and std is the standard deviation
"""
mn = self.mean
sd = self.std
sk = 0.0 if abs(sd) <= 1e-8 else np.mean((self._mcpts - mn) ** 3) / sd ** 3
return sk
|
[
"def",
"skew",
"(",
"self",
")",
":",
"mn",
"=",
"self",
".",
"mean",
"sd",
"=",
"self",
".",
"std",
"sk",
"=",
"0.0",
"if",
"abs",
"(",
"sd",
")",
"<=",
"1e-8",
"else",
"np",
".",
"mean",
"(",
"(",
"self",
".",
"_mcpts",
"-",
"mn",
")",
"**",
"3",
")",
"/",
"sd",
"**",
"3",
"return",
"sk"
] |
r"""
Skewness coefficient value as a result of an uncertainty calculation,
defined as::
_____ m3
\/beta1 = ------
std**3
where m3 is the third central moment and std is the standard deviation
|
[
"r",
"Skewness",
"coefficient",
"value",
"as",
"a",
"result",
"of",
"an",
"uncertainty",
"calculation",
"defined",
"as",
"::",
"_____",
"m3",
"\\",
"/",
"beta1",
"=",
"------",
"std",
"**",
"3",
"where",
"m3",
"is",
"the",
"third",
"central",
"moment",
"and",
"std",
"is",
"the",
"standard",
"deviation"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L95-L109
|
tisimst/mcerp
|
mcerp/__init__.py
|
UncertainFunction.kurt
|
def kurt(self):
"""
Kurtosis coefficient value as a result of an uncertainty calculation,
defined as::
m4
beta2 = ------
std**4
where m4 is the fourth central moment and std is the standard deviation
"""
mn = self.mean
sd = self.std
kt = 0.0 if abs(sd) <= 1e-8 else np.mean((self._mcpts - mn) ** 4) / sd ** 4
return kt
|
python
|
def kurt(self):
"""
Kurtosis coefficient value as a result of an uncertainty calculation,
defined as::
m4
beta2 = ------
std**4
where m4 is the fourth central moment and std is the standard deviation
"""
mn = self.mean
sd = self.std
kt = 0.0 if abs(sd) <= 1e-8 else np.mean((self._mcpts - mn) ** 4) / sd ** 4
return kt
|
[
"def",
"kurt",
"(",
"self",
")",
":",
"mn",
"=",
"self",
".",
"mean",
"sd",
"=",
"self",
".",
"std",
"kt",
"=",
"0.0",
"if",
"abs",
"(",
"sd",
")",
"<=",
"1e-8",
"else",
"np",
".",
"mean",
"(",
"(",
"self",
".",
"_mcpts",
"-",
"mn",
")",
"**",
"4",
")",
"/",
"sd",
"**",
"4",
"return",
"kt"
] |
Kurtosis coefficient value as a result of an uncertainty calculation,
defined as::
m4
beta2 = ------
std**4
where m4 is the fourth central moment and std is the standard deviation
|
[
"Kurtosis",
"coefficient",
"value",
"as",
"a",
"result",
"of",
"an",
"uncertainty",
"calculation",
"defined",
"as",
"::",
"m4",
"beta2",
"=",
"------",
"std",
"**",
"4"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L112-L126
|
tisimst/mcerp
|
mcerp/__init__.py
|
UncertainFunction.stats
|
def stats(self):
"""
The first four standard moments of a distribution: mean, variance, and
standardized skewness and kurtosis coefficients.
"""
mn = self.mean
vr = self.var
sk = self.skew
kt = self.kurt
return [mn, vr, sk, kt]
|
python
|
def stats(self):
"""
The first four standard moments of a distribution: mean, variance, and
standardized skewness and kurtosis coefficients.
"""
mn = self.mean
vr = self.var
sk = self.skew
kt = self.kurt
return [mn, vr, sk, kt]
|
[
"def",
"stats",
"(",
"self",
")",
":",
"mn",
"=",
"self",
".",
"mean",
"vr",
"=",
"self",
".",
"var",
"sk",
"=",
"self",
".",
"skew",
"kt",
"=",
"self",
".",
"kurt",
"return",
"[",
"mn",
",",
"vr",
",",
"sk",
",",
"kt",
"]"
] |
The first four standard moments of a distribution: mean, variance, and
standardized skewness and kurtosis coefficients.
|
[
"The",
"first",
"four",
"standard",
"moments",
"of",
"a",
"distribution",
":",
"mean",
"variance",
"and",
"standardized",
"skewness",
"and",
"kurtosis",
"coefficients",
"."
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L129-L138
|
tisimst/mcerp
|
mcerp/__init__.py
|
UncertainFunction.percentile
|
def percentile(self, val):
"""
Get the distribution value at a given percentile or set of percentiles.
This follows the NIST method for calculating percentiles.
Parameters
----------
val : scalar or array
Either a single value or an array of values between 0 and 1.
Returns
-------
out : scalar or array
The actual distribution value that appears at the requested
percentile value or values
"""
try:
# test to see if an input is given as an array
out = [self.percentile(vi) for vi in val]
except (ValueError, TypeError):
if val <= 0:
out = float(min(self._mcpts))
elif val >= 1:
out = float(max(self._mcpts))
else:
tmp = np.sort(self._mcpts)
n = val * (len(tmp) + 1)
k, d = int(n), n - int(n)
out = float(tmp[k] + d * (tmp[k + 1] - tmp[k]))
if isinstance(val, np.ndarray):
out = np.array(out)
return out
|
python
|
def percentile(self, val):
"""
Get the distribution value at a given percentile or set of percentiles.
This follows the NIST method for calculating percentiles.
Parameters
----------
val : scalar or array
Either a single value or an array of values between 0 and 1.
Returns
-------
out : scalar or array
The actual distribution value that appears at the requested
percentile value or values
"""
try:
# test to see if an input is given as an array
out = [self.percentile(vi) for vi in val]
except (ValueError, TypeError):
if val <= 0:
out = float(min(self._mcpts))
elif val >= 1:
out = float(max(self._mcpts))
else:
tmp = np.sort(self._mcpts)
n = val * (len(tmp) + 1)
k, d = int(n), n - int(n)
out = float(tmp[k] + d * (tmp[k + 1] - tmp[k]))
if isinstance(val, np.ndarray):
out = np.array(out)
return out
|
[
"def",
"percentile",
"(",
"self",
",",
"val",
")",
":",
"try",
":",
"# test to see if an input is given as an array",
"out",
"=",
"[",
"self",
".",
"percentile",
"(",
"vi",
")",
"for",
"vi",
"in",
"val",
"]",
"except",
"(",
"ValueError",
",",
"TypeError",
")",
":",
"if",
"val",
"<=",
"0",
":",
"out",
"=",
"float",
"(",
"min",
"(",
"self",
".",
"_mcpts",
")",
")",
"elif",
"val",
">=",
"1",
":",
"out",
"=",
"float",
"(",
"max",
"(",
"self",
".",
"_mcpts",
")",
")",
"else",
":",
"tmp",
"=",
"np",
".",
"sort",
"(",
"self",
".",
"_mcpts",
")",
"n",
"=",
"val",
"*",
"(",
"len",
"(",
"tmp",
")",
"+",
"1",
")",
"k",
",",
"d",
"=",
"int",
"(",
"n",
")",
",",
"n",
"-",
"int",
"(",
"n",
")",
"out",
"=",
"float",
"(",
"tmp",
"[",
"k",
"]",
"+",
"d",
"*",
"(",
"tmp",
"[",
"k",
"+",
"1",
"]",
"-",
"tmp",
"[",
"k",
"]",
")",
")",
"if",
"isinstance",
"(",
"val",
",",
"np",
".",
"ndarray",
")",
":",
"out",
"=",
"np",
".",
"array",
"(",
"out",
")",
"return",
"out"
] |
Get the distribution value at a given percentile or set of percentiles.
This follows the NIST method for calculating percentiles.
Parameters
----------
val : scalar or array
Either a single value or an array of values between 0 and 1.
Returns
-------
out : scalar or array
The actual distribution value that appears at the requested
percentile value or values
|
[
"Get",
"the",
"distribution",
"value",
"at",
"a",
"given",
"percentile",
"or",
"set",
"of",
"percentiles",
".",
"This",
"follows",
"the",
"NIST",
"method",
"for",
"calculating",
"percentiles",
".",
"Parameters",
"----------",
"val",
":",
"scalar",
"or",
"array",
"Either",
"a",
"single",
"value",
"or",
"an",
"array",
"of",
"values",
"between",
"0",
"and",
"1",
".",
"Returns",
"-------",
"out",
":",
"scalar",
"or",
"array",
"The",
"actual",
"distribution",
"value",
"that",
"appears",
"at",
"the",
"requested",
"percentile",
"value",
"or",
"values"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L140-L172
|
tisimst/mcerp
|
mcerp/__init__.py
|
UncertainFunction.describe
|
def describe(self, name=None):
"""
Cleanly show what the four displayed distribution moments are:
- Mean
- Variance
- Standardized Skewness Coefficient
- Standardized Kurtosis Coefficient
For a standard Normal distribution, these are [0, 1, 0, 3].
If the object has an associated tag, this is presented. If the optional
``name`` kwarg is utilized, this is presented as with the moments.
Otherwise, no unique name is presented.
Example
=======
::
>>> x = N(0, 1, 'x')
>>> x.describe() # print tag since assigned
MCERP Uncertain Value (x):
...
>>> x.describe('foobar') # 'name' kwarg takes precedence
MCERP Uncertain Value (foobar):
...
>>> y = x**2
>>> y.describe('y') # print name since assigned
MCERP Uncertain Value (y):
...
>>> y.describe() # print nothing since no tag
MCERP Uncertain Value:
...
"""
mn, vr, sk, kt = self.stats
if name is not None:
s = "MCERP Uncertain Value (" + name + "):\n"
elif self.tag is not None:
s = "MCERP Uncertain Value (" + self.tag + "):\n"
else:
s = "MCERP Uncertain Value:\n"
s += " > Mean................... {: }\n".format(mn)
s += " > Variance............... {: }\n".format(vr)
s += " > Skewness Coefficient... {: }\n".format(sk)
s += " > Kurtosis Coefficient... {: }\n".format(kt)
print(s)
|
python
|
def describe(self, name=None):
"""
Cleanly show what the four displayed distribution moments are:
- Mean
- Variance
- Standardized Skewness Coefficient
- Standardized Kurtosis Coefficient
For a standard Normal distribution, these are [0, 1, 0, 3].
If the object has an associated tag, this is presented. If the optional
``name`` kwarg is utilized, this is presented as with the moments.
Otherwise, no unique name is presented.
Example
=======
::
>>> x = N(0, 1, 'x')
>>> x.describe() # print tag since assigned
MCERP Uncertain Value (x):
...
>>> x.describe('foobar') # 'name' kwarg takes precedence
MCERP Uncertain Value (foobar):
...
>>> y = x**2
>>> y.describe('y') # print name since assigned
MCERP Uncertain Value (y):
...
>>> y.describe() # print nothing since no tag
MCERP Uncertain Value:
...
"""
mn, vr, sk, kt = self.stats
if name is not None:
s = "MCERP Uncertain Value (" + name + "):\n"
elif self.tag is not None:
s = "MCERP Uncertain Value (" + self.tag + "):\n"
else:
s = "MCERP Uncertain Value:\n"
s += " > Mean................... {: }\n".format(mn)
s += " > Variance............... {: }\n".format(vr)
s += " > Skewness Coefficient... {: }\n".format(sk)
s += " > Kurtosis Coefficient... {: }\n".format(kt)
print(s)
|
[
"def",
"describe",
"(",
"self",
",",
"name",
"=",
"None",
")",
":",
"mn",
",",
"vr",
",",
"sk",
",",
"kt",
"=",
"self",
".",
"stats",
"if",
"name",
"is",
"not",
"None",
":",
"s",
"=",
"\"MCERP Uncertain Value (\"",
"+",
"name",
"+",
"\"):\\n\"",
"elif",
"self",
".",
"tag",
"is",
"not",
"None",
":",
"s",
"=",
"\"MCERP Uncertain Value (\"",
"+",
"self",
".",
"tag",
"+",
"\"):\\n\"",
"else",
":",
"s",
"=",
"\"MCERP Uncertain Value:\\n\"",
"s",
"+=",
"\" > Mean................... {: }\\n\"",
".",
"format",
"(",
"mn",
")",
"s",
"+=",
"\" > Variance............... {: }\\n\"",
".",
"format",
"(",
"vr",
")",
"s",
"+=",
"\" > Skewness Coefficient... {: }\\n\"",
".",
"format",
"(",
"sk",
")",
"s",
"+=",
"\" > Kurtosis Coefficient... {: }\\n\"",
".",
"format",
"(",
"kt",
")",
"print",
"(",
"s",
")"
] |
Cleanly show what the four displayed distribution moments are:
- Mean
- Variance
- Standardized Skewness Coefficient
- Standardized Kurtosis Coefficient
For a standard Normal distribution, these are [0, 1, 0, 3].
If the object has an associated tag, this is presented. If the optional
``name`` kwarg is utilized, this is presented as with the moments.
Otherwise, no unique name is presented.
Example
=======
::
>>> x = N(0, 1, 'x')
>>> x.describe() # print tag since assigned
MCERP Uncertain Value (x):
...
>>> x.describe('foobar') # 'name' kwarg takes precedence
MCERP Uncertain Value (foobar):
...
>>> y = x**2
>>> y.describe('y') # print name since assigned
MCERP Uncertain Value (y):
...
>>> y.describe() # print nothing since no tag
MCERP Uncertain Value:
...
|
[
"Cleanly",
"show",
"what",
"the",
"four",
"displayed",
"distribution",
"moments",
"are",
":",
"-",
"Mean",
"-",
"Variance",
"-",
"Standardized",
"Skewness",
"Coefficient",
"-",
"Standardized",
"Kurtosis",
"Coefficient",
"For",
"a",
"standard",
"Normal",
"distribution",
"these",
"are",
"[",
"0",
"1",
"0",
"3",
"]",
".",
"If",
"the",
"object",
"has",
"an",
"associated",
"tag",
"this",
"is",
"presented",
".",
"If",
"the",
"optional",
"name",
"kwarg",
"is",
"utilized",
"this",
"is",
"presented",
"as",
"with",
"the",
"moments",
".",
"Otherwise",
"no",
"unique",
"name",
"is",
"presented",
".",
"Example",
"=======",
"::",
">>>",
"x",
"=",
"N",
"(",
"0",
"1",
"x",
")",
">>>",
"x",
".",
"describe",
"()",
"#",
"print",
"tag",
"since",
"assigned",
"MCERP",
"Uncertain",
"Value",
"(",
"x",
")",
":",
"..."
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L191-L239
|
tisimst/mcerp
|
mcerp/__init__.py
|
UncertainFunction.plot
|
def plot(self, hist=False, show=False, **kwargs):
"""
Plot the distribution of the UncertainFunction. By default, the
distribution is shown with a kernel density estimate (kde).
Optional
--------
hist : bool
If true, a density histogram is displayed (histtype='stepfilled')
show : bool
If ``True``, the figure will be displayed after plotting the
distribution. If ``False``, an explicit call to ``plt.show()`` is
required to display the figure.
kwargs : any valid matplotlib.pyplot.plot or .hist kwarg
"""
import matplotlib.pyplot as plt
vals = self._mcpts
low = min(vals)
high = max(vals)
p = ss.kde.gaussian_kde(vals)
xp = np.linspace(low, high, 100)
if hist:
h = plt.hist(
vals,
bins=int(np.sqrt(len(vals)) + 0.5),
histtype="stepfilled",
normed=True,
**kwargs
)
plt.ylim(0, 1.1 * h[0].max())
else:
plt.plot(xp, p.evaluate(xp), **kwargs)
plt.xlim(low - (high - low) * 0.1, high + (high - low) * 0.1)
if show:
self.show()
|
python
|
def plot(self, hist=False, show=False, **kwargs):
"""
Plot the distribution of the UncertainFunction. By default, the
distribution is shown with a kernel density estimate (kde).
Optional
--------
hist : bool
If true, a density histogram is displayed (histtype='stepfilled')
show : bool
If ``True``, the figure will be displayed after plotting the
distribution. If ``False``, an explicit call to ``plt.show()`` is
required to display the figure.
kwargs : any valid matplotlib.pyplot.plot or .hist kwarg
"""
import matplotlib.pyplot as plt
vals = self._mcpts
low = min(vals)
high = max(vals)
p = ss.kde.gaussian_kde(vals)
xp = np.linspace(low, high, 100)
if hist:
h = plt.hist(
vals,
bins=int(np.sqrt(len(vals)) + 0.5),
histtype="stepfilled",
normed=True,
**kwargs
)
plt.ylim(0, 1.1 * h[0].max())
else:
plt.plot(xp, p.evaluate(xp), **kwargs)
plt.xlim(low - (high - low) * 0.1, high + (high - low) * 0.1)
if show:
self.show()
|
[
"def",
"plot",
"(",
"self",
",",
"hist",
"=",
"False",
",",
"show",
"=",
"False",
",",
"*",
"*",
"kwargs",
")",
":",
"import",
"matplotlib",
".",
"pyplot",
"as",
"plt",
"vals",
"=",
"self",
".",
"_mcpts",
"low",
"=",
"min",
"(",
"vals",
")",
"high",
"=",
"max",
"(",
"vals",
")",
"p",
"=",
"ss",
".",
"kde",
".",
"gaussian_kde",
"(",
"vals",
")",
"xp",
"=",
"np",
".",
"linspace",
"(",
"low",
",",
"high",
",",
"100",
")",
"if",
"hist",
":",
"h",
"=",
"plt",
".",
"hist",
"(",
"vals",
",",
"bins",
"=",
"int",
"(",
"np",
".",
"sqrt",
"(",
"len",
"(",
"vals",
")",
")",
"+",
"0.5",
")",
",",
"histtype",
"=",
"\"stepfilled\"",
",",
"normed",
"=",
"True",
",",
"*",
"*",
"kwargs",
")",
"plt",
".",
"ylim",
"(",
"0",
",",
"1.1",
"*",
"h",
"[",
"0",
"]",
".",
"max",
"(",
")",
")",
"else",
":",
"plt",
".",
"plot",
"(",
"xp",
",",
"p",
".",
"evaluate",
"(",
"xp",
")",
",",
"*",
"*",
"kwargs",
")",
"plt",
".",
"xlim",
"(",
"low",
"-",
"(",
"high",
"-",
"low",
")",
"*",
"0.1",
",",
"high",
"+",
"(",
"high",
"-",
"low",
")",
"*",
"0.1",
")",
"if",
"show",
":",
"self",
".",
"show",
"(",
")"
] |
Plot the distribution of the UncertainFunction. By default, the
distribution is shown with a kernel density estimate (kde).
Optional
--------
hist : bool
If true, a density histogram is displayed (histtype='stepfilled')
show : bool
If ``True``, the figure will be displayed after plotting the
distribution. If ``False``, an explicit call to ``plt.show()`` is
required to display the figure.
kwargs : any valid matplotlib.pyplot.plot or .hist kwarg
|
[
"Plot",
"the",
"distribution",
"of",
"the",
"UncertainFunction",
".",
"By",
"default",
"the",
"distribution",
"is",
"shown",
"with",
"a",
"kernel",
"density",
"estimate",
"(",
"kde",
")",
".",
"Optional",
"--------",
"hist",
":",
"bool",
"If",
"true",
"a",
"density",
"histogram",
"is",
"displayed",
"(",
"histtype",
"=",
"stepfilled",
")",
"show",
":",
"bool",
"If",
"True",
"the",
"figure",
"will",
"be",
"displayed",
"after",
"plotting",
"the",
"distribution",
".",
"If",
"False",
"an",
"explicit",
"call",
"to",
"plt",
".",
"show",
"()",
"is",
"required",
"to",
"display",
"the",
"figure",
".",
"kwargs",
":",
"any",
"valid",
"matplotlib",
".",
"pyplot",
".",
"plot",
"or",
".",
"hist",
"kwarg"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L241-L281
|
tisimst/mcerp
|
mcerp/__init__.py
|
UncertainVariable.plot
|
def plot(self, hist=False, show=False, **kwargs):
"""
Plot the distribution of the UncertainVariable. Continuous
distributions are plotted with a line plot and discrete distributions
are plotted with discrete circles.
Optional
--------
hist : bool
If true, a histogram is displayed
show : bool
If ``True``, the figure will be displayed after plotting the
distribution. If ``False``, an explicit call to ``plt.show()`` is
required to display the figure.
kwargs : any valid matplotlib.pyplot.plot kwarg
"""
import matplotlib.pyplot as plt
if hist:
vals = self._mcpts
low = vals.min()
high = vals.max()
h = plt.hist(
vals,
bins=int(np.sqrt(len(vals)) + 0.5),
histtype="stepfilled",
normed=True,
**kwargs
)
plt.ylim(0, 1.1 * h[0].max())
else:
bound = 0.0001
low = self.rv.ppf(bound)
high = self.rv.ppf(1 - bound)
if hasattr(self.rv.dist, "pmf"):
low = int(low)
high = int(high)
vals = list(range(low, high + 1))
plt.plot(vals, self.rv.pmf(vals), "o", **kwargs)
else:
vals = np.linspace(low, high, 500)
plt.plot(vals, self.rv.pdf(vals), **kwargs)
plt.xlim(low - (high - low) * 0.1, high + (high - low) * 0.1)
if show:
self.show()
|
python
|
def plot(self, hist=False, show=False, **kwargs):
"""
Plot the distribution of the UncertainVariable. Continuous
distributions are plotted with a line plot and discrete distributions
are plotted with discrete circles.
Optional
--------
hist : bool
If true, a histogram is displayed
show : bool
If ``True``, the figure will be displayed after plotting the
distribution. If ``False``, an explicit call to ``plt.show()`` is
required to display the figure.
kwargs : any valid matplotlib.pyplot.plot kwarg
"""
import matplotlib.pyplot as plt
if hist:
vals = self._mcpts
low = vals.min()
high = vals.max()
h = plt.hist(
vals,
bins=int(np.sqrt(len(vals)) + 0.5),
histtype="stepfilled",
normed=True,
**kwargs
)
plt.ylim(0, 1.1 * h[0].max())
else:
bound = 0.0001
low = self.rv.ppf(bound)
high = self.rv.ppf(1 - bound)
if hasattr(self.rv.dist, "pmf"):
low = int(low)
high = int(high)
vals = list(range(low, high + 1))
plt.plot(vals, self.rv.pmf(vals), "o", **kwargs)
else:
vals = np.linspace(low, high, 500)
plt.plot(vals, self.rv.pdf(vals), **kwargs)
plt.xlim(low - (high - low) * 0.1, high + (high - low) * 0.1)
if show:
self.show()
|
[
"def",
"plot",
"(",
"self",
",",
"hist",
"=",
"False",
",",
"show",
"=",
"False",
",",
"*",
"*",
"kwargs",
")",
":",
"import",
"matplotlib",
".",
"pyplot",
"as",
"plt",
"if",
"hist",
":",
"vals",
"=",
"self",
".",
"_mcpts",
"low",
"=",
"vals",
".",
"min",
"(",
")",
"high",
"=",
"vals",
".",
"max",
"(",
")",
"h",
"=",
"plt",
".",
"hist",
"(",
"vals",
",",
"bins",
"=",
"int",
"(",
"np",
".",
"sqrt",
"(",
"len",
"(",
"vals",
")",
")",
"+",
"0.5",
")",
",",
"histtype",
"=",
"\"stepfilled\"",
",",
"normed",
"=",
"True",
",",
"*",
"*",
"kwargs",
")",
"plt",
".",
"ylim",
"(",
"0",
",",
"1.1",
"*",
"h",
"[",
"0",
"]",
".",
"max",
"(",
")",
")",
"else",
":",
"bound",
"=",
"0.0001",
"low",
"=",
"self",
".",
"rv",
".",
"ppf",
"(",
"bound",
")",
"high",
"=",
"self",
".",
"rv",
".",
"ppf",
"(",
"1",
"-",
"bound",
")",
"if",
"hasattr",
"(",
"self",
".",
"rv",
".",
"dist",
",",
"\"pmf\"",
")",
":",
"low",
"=",
"int",
"(",
"low",
")",
"high",
"=",
"int",
"(",
"high",
")",
"vals",
"=",
"list",
"(",
"range",
"(",
"low",
",",
"high",
"+",
"1",
")",
")",
"plt",
".",
"plot",
"(",
"vals",
",",
"self",
".",
"rv",
".",
"pmf",
"(",
"vals",
")",
",",
"\"o\"",
",",
"*",
"*",
"kwargs",
")",
"else",
":",
"vals",
"=",
"np",
".",
"linspace",
"(",
"low",
",",
"high",
",",
"500",
")",
"plt",
".",
"plot",
"(",
"vals",
",",
"self",
".",
"rv",
".",
"pdf",
"(",
"vals",
")",
",",
"*",
"*",
"kwargs",
")",
"plt",
".",
"xlim",
"(",
"low",
"-",
"(",
"high",
"-",
"low",
")",
"*",
"0.1",
",",
"high",
"+",
"(",
"high",
"-",
"low",
")",
"*",
"0.1",
")",
"if",
"show",
":",
"self",
".",
"show",
"(",
")"
] |
Plot the distribution of the UncertainVariable. Continuous
distributions are plotted with a line plot and discrete distributions
are plotted with discrete circles.
Optional
--------
hist : bool
If true, a histogram is displayed
show : bool
If ``True``, the figure will be displayed after plotting the
distribution. If ``False``, an explicit call to ``plt.show()`` is
required to display the figure.
kwargs : any valid matplotlib.pyplot.plot kwarg
|
[
"Plot",
"the",
"distribution",
"of",
"the",
"UncertainVariable",
".",
"Continuous",
"distributions",
"are",
"plotted",
"with",
"a",
"line",
"plot",
"and",
"discrete",
"distributions",
"are",
"plotted",
"with",
"discrete",
"circles",
".",
"Optional",
"--------",
"hist",
":",
"bool",
"If",
"true",
"a",
"histogram",
"is",
"displayed",
"show",
":",
"bool",
"If",
"True",
"the",
"figure",
"will",
"be",
"displayed",
"after",
"plotting",
"the",
"distribution",
".",
"If",
"False",
"an",
"explicit",
"call",
"to",
"plt",
".",
"show",
"()",
"is",
"required",
"to",
"display",
"the",
"figure",
".",
"kwargs",
":",
"any",
"valid",
"matplotlib",
".",
"pyplot",
".",
"plot",
"kwarg"
] |
train
|
https://github.com/tisimst/mcerp/blob/2bb8260c9ad2d58a806847f1b627b6451e407de1/mcerp/__init__.py#L652-L698
|
shoeffner/cvloop
|
cvloop/functions.py
|
DrawHat.load_hat
|
def load_hat(self, path): # pylint: disable=no-self-use
"""Loads the hat from a picture at path.
Args:
path: The path to load from
Returns:
The hat data.
"""
hat = cv2.imread(path, cv2.IMREAD_UNCHANGED)
if hat is None:
raise ValueError('No hat image found at `{}`'.format(path))
b, g, r, a = cv2.split(hat)
return cv2.merge((r, g, b, a))
|
python
|
def load_hat(self, path): # pylint: disable=no-self-use
"""Loads the hat from a picture at path.
Args:
path: The path to load from
Returns:
The hat data.
"""
hat = cv2.imread(path, cv2.IMREAD_UNCHANGED)
if hat is None:
raise ValueError('No hat image found at `{}`'.format(path))
b, g, r, a = cv2.split(hat)
return cv2.merge((r, g, b, a))
|
[
"def",
"load_hat",
"(",
"self",
",",
"path",
")",
":",
"# pylint: disable=no-self-use",
"hat",
"=",
"cv2",
".",
"imread",
"(",
"path",
",",
"cv2",
".",
"IMREAD_UNCHANGED",
")",
"if",
"hat",
"is",
"None",
":",
"raise",
"ValueError",
"(",
"'No hat image found at `{}`'",
".",
"format",
"(",
"path",
")",
")",
"b",
",",
"g",
",",
"r",
",",
"a",
"=",
"cv2",
".",
"split",
"(",
"hat",
")",
"return",
"cv2",
".",
"merge",
"(",
"(",
"r",
",",
"g",
",",
"b",
",",
"a",
")",
")"
] |
Loads the hat from a picture at path.
Args:
path: The path to load from
Returns:
The hat data.
|
[
"Loads",
"the",
"hat",
"from",
"a",
"picture",
"at",
"path",
"."
] |
train
|
https://github.com/shoeffner/cvloop/blob/3ddd311e9b679d16c8fd36779931380374de343c/cvloop/functions.py#L173-L186
|
shoeffner/cvloop
|
cvloop/functions.py
|
DrawHat.find_faces
|
def find_faces(self, image, draw_box=False):
"""Uses a haarcascade to detect faces inside an image.
Args:
image: The image.
draw_box: If True, the image will be marked with a rectangle.
Return:
The faces as returned by OpenCV's detectMultiScale method for
cascades.
"""
frame_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
faces = self.cascade.detectMultiScale(
frame_gray,
scaleFactor=1.3,
minNeighbors=5,
minSize=(50, 50),
flags=0)
if draw_box:
for x, y, w, h in faces:
cv2.rectangle(image, (x, y),
(x + w, y + h), (0, 255, 0), 2)
return faces
|
python
|
def find_faces(self, image, draw_box=False):
"""Uses a haarcascade to detect faces inside an image.
Args:
image: The image.
draw_box: If True, the image will be marked with a rectangle.
Return:
The faces as returned by OpenCV's detectMultiScale method for
cascades.
"""
frame_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
faces = self.cascade.detectMultiScale(
frame_gray,
scaleFactor=1.3,
minNeighbors=5,
minSize=(50, 50),
flags=0)
if draw_box:
for x, y, w, h in faces:
cv2.rectangle(image, (x, y),
(x + w, y + h), (0, 255, 0), 2)
return faces
|
[
"def",
"find_faces",
"(",
"self",
",",
"image",
",",
"draw_box",
"=",
"False",
")",
":",
"frame_gray",
"=",
"cv2",
".",
"cvtColor",
"(",
"image",
",",
"cv2",
".",
"COLOR_RGB2GRAY",
")",
"faces",
"=",
"self",
".",
"cascade",
".",
"detectMultiScale",
"(",
"frame_gray",
",",
"scaleFactor",
"=",
"1.3",
",",
"minNeighbors",
"=",
"5",
",",
"minSize",
"=",
"(",
"50",
",",
"50",
")",
",",
"flags",
"=",
"0",
")",
"if",
"draw_box",
":",
"for",
"x",
",",
"y",
",",
"w",
",",
"h",
"in",
"faces",
":",
"cv2",
".",
"rectangle",
"(",
"image",
",",
"(",
"x",
",",
"y",
")",
",",
"(",
"x",
"+",
"w",
",",
"y",
"+",
"h",
")",
",",
"(",
"0",
",",
"255",
",",
"0",
")",
",",
"2",
")",
"return",
"faces"
] |
Uses a haarcascade to detect faces inside an image.
Args:
image: The image.
draw_box: If True, the image will be marked with a rectangle.
Return:
The faces as returned by OpenCV's detectMultiScale method for
cascades.
|
[
"Uses",
"a",
"haarcascade",
"to",
"detect",
"faces",
"inside",
"an",
"image",
"."
] |
train
|
https://github.com/shoeffner/cvloop/blob/3ddd311e9b679d16c8fd36779931380374de343c/cvloop/functions.py#L188-L211
|
uber-archive/h1-python
|
h1/client.py
|
HackerOneClient.find_resources
|
def find_resources(self, rsrc_type, sort=None, yield_pages=False, **kwargs):
"""Find instances of `rsrc_type` that match the filter in `**kwargs`"""
return rsrc_type.find(self, sort=sort, yield_pages=yield_pages, **kwargs)
|
python
|
def find_resources(self, rsrc_type, sort=None, yield_pages=False, **kwargs):
"""Find instances of `rsrc_type` that match the filter in `**kwargs`"""
return rsrc_type.find(self, sort=sort, yield_pages=yield_pages, **kwargs)
|
[
"def",
"find_resources",
"(",
"self",
",",
"rsrc_type",
",",
"sort",
"=",
"None",
",",
"yield_pages",
"=",
"False",
",",
"*",
"*",
"kwargs",
")",
":",
"return",
"rsrc_type",
".",
"find",
"(",
"self",
",",
"sort",
"=",
"sort",
",",
"yield_pages",
"=",
"yield_pages",
",",
"*",
"*",
"kwargs",
")"
] |
Find instances of `rsrc_type` that match the filter in `**kwargs`
|
[
"Find",
"instances",
"of",
"rsrc_type",
"that",
"match",
"the",
"filter",
"in",
"**",
"kwargs"
] |
train
|
https://github.com/uber-archive/h1-python/blob/c91aec6a26887e453106af39e96ec6d5c7b00c9d/h1/client.py#L111-L113
|
edelooff/sqlalchemy-json
|
sqlalchemy_json/track.py
|
TrackedObject.changed
|
def changed(self, message=None, *args):
"""Marks the object as changed.
If a `parent` attribute is set, the `changed()` method on the parent
will be called, propagating the change notification up the chain.
The message (if provided) will be debug logged.
"""
if message is not None:
self.logger.debug('%s: %s', self._repr(), message % args)
self.logger.debug('%s: changed', self._repr())
if self.parent is not None:
self.parent.changed()
elif isinstance(self, Mutable):
super(TrackedObject, self).changed()
|
python
|
def changed(self, message=None, *args):
"""Marks the object as changed.
If a `parent` attribute is set, the `changed()` method on the parent
will be called, propagating the change notification up the chain.
The message (if provided) will be debug logged.
"""
if message is not None:
self.logger.debug('%s: %s', self._repr(), message % args)
self.logger.debug('%s: changed', self._repr())
if self.parent is not None:
self.parent.changed()
elif isinstance(self, Mutable):
super(TrackedObject, self).changed()
|
[
"def",
"changed",
"(",
"self",
",",
"message",
"=",
"None",
",",
"*",
"args",
")",
":",
"if",
"message",
"is",
"not",
"None",
":",
"self",
".",
"logger",
".",
"debug",
"(",
"'%s: %s'",
",",
"self",
".",
"_repr",
"(",
")",
",",
"message",
"%",
"args",
")",
"self",
".",
"logger",
".",
"debug",
"(",
"'%s: changed'",
",",
"self",
".",
"_repr",
"(",
")",
")",
"if",
"self",
".",
"parent",
"is",
"not",
"None",
":",
"self",
".",
"parent",
".",
"changed",
"(",
")",
"elif",
"isinstance",
"(",
"self",
",",
"Mutable",
")",
":",
"super",
"(",
"TrackedObject",
",",
"self",
")",
".",
"changed",
"(",
")"
] |
Marks the object as changed.
If a `parent` attribute is set, the `changed()` method on the parent
will be called, propagating the change notification up the chain.
The message (if provided) will be debug logged.
|
[
"Marks",
"the",
"object",
"as",
"changed",
"."
] |
train
|
https://github.com/edelooff/sqlalchemy-json/blob/4e5df0d61dc09ed9a52e24ab291a1f1e14aa95cc/sqlalchemy_json/track.py#L25-L39
|
edelooff/sqlalchemy-json
|
sqlalchemy_json/track.py
|
TrackedObject.register
|
def register(cls, origin_type):
"""Decorator for mutation tracker registration.
The provided `origin_type` is mapped to the decorated class such that
future calls to `convert()` will convert the object of `origin_type`
to an instance of the decorated class.
"""
def decorator(tracked_type):
"""Adds the decorated class to the `_type_mapping` dictionary."""
cls._type_mapping[origin_type] = tracked_type
return tracked_type
return decorator
|
python
|
def register(cls, origin_type):
"""Decorator for mutation tracker registration.
The provided `origin_type` is mapped to the decorated class such that
future calls to `convert()` will convert the object of `origin_type`
to an instance of the decorated class.
"""
def decorator(tracked_type):
"""Adds the decorated class to the `_type_mapping` dictionary."""
cls._type_mapping[origin_type] = tracked_type
return tracked_type
return decorator
|
[
"def",
"register",
"(",
"cls",
",",
"origin_type",
")",
":",
"def",
"decorator",
"(",
"tracked_type",
")",
":",
"\"\"\"Adds the decorated class to the `_type_mapping` dictionary.\"\"\"",
"cls",
".",
"_type_mapping",
"[",
"origin_type",
"]",
"=",
"tracked_type",
"return",
"tracked_type",
"return",
"decorator"
] |
Decorator for mutation tracker registration.
The provided `origin_type` is mapped to the decorated class such that
future calls to `convert()` will convert the object of `origin_type`
to an instance of the decorated class.
|
[
"Decorator",
"for",
"mutation",
"tracker",
"registration",
"."
] |
train
|
https://github.com/edelooff/sqlalchemy-json/blob/4e5df0d61dc09ed9a52e24ab291a1f1e14aa95cc/sqlalchemy_json/track.py#L42-L53
|
edelooff/sqlalchemy-json
|
sqlalchemy_json/track.py
|
TrackedObject.convert
|
def convert(cls, obj, parent):
"""Converts objects to registered tracked types
This checks the type of the given object against the registered tracked
types. When a match is found, the given object will be converted to the
tracked type, its parent set to the provided parent, and returned.
If its type does not occur in the registered types mapping, the object
is returned unchanged.
"""
replacement_type = cls._type_mapping.get(type(obj))
if replacement_type is not None:
new = replacement_type(obj)
new.parent = parent
return new
return obj
|
python
|
def convert(cls, obj, parent):
"""Converts objects to registered tracked types
This checks the type of the given object against the registered tracked
types. When a match is found, the given object will be converted to the
tracked type, its parent set to the provided parent, and returned.
If its type does not occur in the registered types mapping, the object
is returned unchanged.
"""
replacement_type = cls._type_mapping.get(type(obj))
if replacement_type is not None:
new = replacement_type(obj)
new.parent = parent
return new
return obj
|
[
"def",
"convert",
"(",
"cls",
",",
"obj",
",",
"parent",
")",
":",
"replacement_type",
"=",
"cls",
".",
"_type_mapping",
".",
"get",
"(",
"type",
"(",
"obj",
")",
")",
"if",
"replacement_type",
"is",
"not",
"None",
":",
"new",
"=",
"replacement_type",
"(",
"obj",
")",
"new",
".",
"parent",
"=",
"parent",
"return",
"new",
"return",
"obj"
] |
Converts objects to registered tracked types
This checks the type of the given object against the registered tracked
types. When a match is found, the given object will be converted to the
tracked type, its parent set to the provided parent, and returned.
If its type does not occur in the registered types mapping, the object
is returned unchanged.
|
[
"Converts",
"objects",
"to",
"registered",
"tracked",
"types"
] |
train
|
https://github.com/edelooff/sqlalchemy-json/blob/4e5df0d61dc09ed9a52e24ab291a1f1e14aa95cc/sqlalchemy_json/track.py#L56-L71
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.