code stringlengths 52 7.75k | docs stringlengths 1 5.85k |
|---|---|
def login(self, username, password, application, application_url):
logger.debug(str((username, application, application_url)))
method = self._anaconda_client_api.authenticate
return self._create_worker(method, username, password, application,
applicati... | Login to anaconda cloud. |
def logout(self):
logger.debug('Logout')
method = self._anaconda_client_api.remove_authentication
return self._create_worker(method) | Logout from anaconda cloud. |
def load_repodata(self, filepaths, extra_data=None, metadata=None):
logger.debug(str((filepaths)))
method = self._load_repodata
return self._create_worker(method, filepaths, extra_data=extra_data,
metadata=metadata) | Load all the available pacakges information for downloaded repodata.
Files include repo.continuum.io, additional data provided (anaconda
cloud), and additional metadata and merge into a single set of packages
and apps. |
def prepare_model_data(self, packages, linked, pip=None,
private_packages=None):
logger.debug('')
return self._prepare_model_data(packages, linked, pip=pip,
private_packages=private_packages) | Prepare downloaded package info along with pip pacakges info. |
def set_domain(self, domain='https://api.anaconda.org'):
logger.debug(str((domain)))
config = binstar_client.utils.get_config()
config['url'] = domain
binstar_client.utils.set_config(config)
self._anaconda_client_api = binstar_client.utils.get_server_api(
to... | Reset current api domain. |
def packages(self, login=None, platform=None, package_type=None,
type_=None, access=None):
logger.debug('')
method = self._anaconda_client_api.user_packages
return self._create_worker(method, login=login, platform=platform,
package_typ... | Return all the available packages for a given user.
Parameters
----------
type_: Optional[str]
Only find packages that have this conda `type`, (i.e. 'app').
access : Optional[str]
Only find packages that have this access level (e.g. 'private',
'authen... |
def _multi_packages(self, logins=None, platform=None, package_type=None,
type_=None, access=None, new_client=True):
private_packages = {}
if not new_client:
time.sleep(0.3)
return private_packages
for login in logins:
data = ... | Return the private packages for a given set of usernames/logins. |
def multi_packages(self, logins=None, platform=None, package_type=None,
type_=None, access=None):
logger.debug('')
method = self._multi_packages
new_client = True
try:
# Only the newer versions have extra keywords like `access`
sel... | Return the private packages for a given set of usernames/logins. |
def country(from_key='name', to_key='iso'):
gc = GeonamesCache()
dataset = gc.get_dataset_by_key(gc.get_countries(), from_key)
def mapper(input):
# For country name inputs take the names mapping into account.
if 'name' == from_key:
input = mappings.country_names.get(input,... | Creates and returns a mapper function to access country data.
The mapper function that is returned must be called with one argument. In
the default case you call it with a name and it returns a 3-letter
ISO_3166-1 code, e. g. called with ``Spain`` it would return ``ESP``.
:param from_key: (optional) t... |
def get_cities(self):
if self.cities is None:
self.cities = self._load_data(self.cities, 'cities.json')
return self.cities | Get a dictionary of cities keyed by geonameid. |
def get_cities_by_name(self, name):
if name not in self.cities_by_names:
if self.cities_items is None:
self.cities_items = list(self.get_cities().items())
self.cities_by_names[name] = [dict({gid: city})
for gid, city in self.cities_items if city[... | Get a list of city dictionaries with the given name.
City names cannot be used as keys, as they are not unique. |
def _set_repo_urls_from_channels(self, channels):
repos = []
sys_platform = self._conda_api.get_platform()
for channel in channels:
url = '{0}/{1}/repodata.json.bz2'.format(channel, sys_platform)
repos.append(url)
return repos | Convert a channel into a normalized repo name including.
Channels are assumed in normalized url form. |
def _check_repos(self, repos):
self._checking_repos = []
self._valid_repos = []
for repo in repos:
worker = self.download_is_valid_url(repo)
worker.sig_finished.connect(self._repos_checked)
worker.repo = repo
self._checking_repos.append(r... | Check if repodata urls are valid. |
def _repos_checked(self, worker, output, error):
if worker.repo in self._checking_repos:
self._checking_repos.remove(worker.repo)
if output:
self._valid_repos.append(worker.repo)
if len(self._checking_repos) == 0:
self._download_repodata(self._valid... | Callback for _check_repos. |
def _repo_url_to_path(self, repo):
repo = repo.replace('http://', '')
repo = repo.replace('https://', '')
repo = repo.replace('/', '_')
return os.sep.join([self._data_directory, repo]) | Convert a `repo` url to a file path for local storage. |
def _download_repodata(self, checked_repos):
self._files_downloaded = []
self._repodata_files = []
self.__counter = -1
if checked_repos:
for repo in checked_repos:
path = self._repo_url_to_path(repo)
self._files_downloaded.append(path... | Dowload repodata. |
def _get_repodata_from_meta(self):
path = os.sep.join([self.ROOT_PREFIX, 'conda-meta'])
packages = os.listdir(path)
meta_repodata = {}
for pkg in packages:
if pkg.endswith('.json'):
filepath = os.sep.join([path, pkg])
with open(filepat... | Generate repodata from local meta files. |
def _repodata_downloaded(self, worker=None, output=None, error=None):
if worker:
self._files_downloaded.remove(worker.path)
if worker.path in self._files_downloaded:
self._files_downloaded.remove(worker.path)
if len(self._files_downloaded) == 0:
... | Callback for _download_repodata. |
def repodata_files(self, channels=None):
if channels is None:
channels = self.conda_get_condarc_channels()
repodata_urls = self._set_repo_urls_from_channels(channels)
repopaths = []
for repourl in repodata_urls:
fullpath = os.sep.join([self._repo_url_t... | Return the repodata paths based on `channels` and the `data_directory`.
There is no check for validity here. |
def update_repodata(self, channels=None):
norm_channels = self.conda_get_condarc_channels(channels=channels,
normalize=True)
repodata_urls = self._set_repo_urls_from_channels(norm_channels)
self._check_repos(repodata_urls) | Update repodata from channels or use condarc channels if None. |
def update_metadata(self):
if self._data_directory is None:
raise Exception('Need to call `api.set_data_directory` first.')
metadata_url = 'https://repo.continuum.io/pkgs/metadata.json'
filepath = os.sep.join([self._data_directory, 'metadata.json'])
worker = self.do... | Update the metadata available for packages in repo.continuum.io.
Returns a download worker. |
def check_valid_channel(self,
channel,
conda_url='https://conda.anaconda.org'):
if channel.startswith('https://') or channel.startswith('http://'):
url = channel
else:
url = "{0}/{1}".format(conda_url, channel)
... | Check if channel is valid. |
def _aws_get_instance_by_tag(region, name, tag, raw):
client = boto3.session.Session().client('ec2', region)
matching_reservations = client.describe_instances(Filters=[{'Name': tag, 'Values': [name]}]).get('Reservations', [])
instances = []
[[instances.append(_aws_instance_from_dict(region, instanc... | Get all instances matching a tag. |
def aws_get_instances_by_id(region, instance_id, raw=True):
client = boto3.session.Session().client('ec2', region)
try:
matching_reservations = client.describe_instances(InstanceIds=[instance_id]).get('Reservations', [])
except ClientError as exc:
if exc.response.get('Error', {}).get('C... | Returns instances mathing an id. |
def get_instances_by_name(name, sort_by_order=('cloud', 'name'), projects=None, raw=True, regions=None, gcp_credentials=None, clouds=SUPPORTED_CLOUDS):
matching_instances = all_clouds_get_instances_by_name(
name, projects, raw, credentials=gcp_credentials, clouds=clouds)
if regions:
matchin... | Get intsances from GCP and AWS by name. |
def get_os_version(instance):
if instance.cloud == 'aws':
client = boto3.client('ec2', instance.region)
image_id = client.describe_instances(InstanceIds=[instance.id])['Reservations'][0]['Instances'][0]['ImageId']
return '16.04' if '16.04' in client.describe_images(ImageIds=[image_id])[... | Get OS Version for instances. |
def get_volumes(instance):
if instance.cloud == 'aws':
client = boto3.client('ec2', instance.region)
devices = client.describe_instance_attribute(
InstanceId=instance.id, Attribute='blockDeviceMapping').get('BlockDeviceMappings', [])
volumes = client.describe_volumes(VolumeI... | Returns all the volumes of an instance. |
def get_persistent_address(instance):
if instance.cloud == 'aws':
client = boto3.client('ec2', instance.region)
try:
client.describe_addresses(PublicIps=[instance.ip_address])
return instance.ip_address
except botocore.client.ClientError as exc:
if ex... | Returns the public ip address of an instance. |
def main():
pip_packages = {}
for package in pip.get_installed_distributions():
name = package.project_name
version = package.version
full_name = "{0}-{1}-pip".format(name.lower(), version)
pip_packages[full_name] = {'version': version}
data = json.dumps(pip_packages)
... | Use pip to find pip installed packages in a given prefix. |
def _save(file, data, mode='w+'):
with open(file, mode) as fh:
fh.write(data) | Write all data to created file. Also overwrite previous file. |
def merge(obj):
merge = ''
for f in obj.get('static', []):
print 'Merging: {}'. format(f)
merge += _read(f)
def doless(f):
print 'Compiling LESS: {}'.format(f)
ret, tmp = commands.getstatusoutput('lesscpy '+f)
if ret == 0:
return tmp
else:
... | Merge contents.
It does a simply merge of all files defined under 'static' key.
If you have JS or CSS file with embeded django tags like {% url ... %} or
{% static ... %} you should declare them under 'template' key. This
function will render them and append to the merged output.
To use the rende... |
def jsMin(data, file):
print 'Minifying JS... ',
url = 'http://javascript-minifier.com/raw' #POST
req = urllib2.Request(url, urllib.urlencode({'input': data}))
try:
f = urllib2.urlopen(req)
response = f.read()
f.close()
print 'Final: {:.1f}%'.format(100.0*len(respons... | Minify JS data and saves to file.
Data should be a string will whole JS content, and file will be
overwrited if exists. |
def jpgMin(file, force=False):
if not os.path.isfile(file+'.original') or force:
data = _read(file, 'rb')
_save(file+'.original', data, 'w+b')
print 'Optmising JPG {} - {:.2f}kB'.format(file, len(data)/1024.0),
url = 'http://jpgoptimiser.com/optimise'
parts, headers = en... | Try to optimise a JPG file.
The original will be saved at the same place with '.original' appended to its name.
Once a .original exists the function will ignore this file unless force is True. |
def process(obj):
#merge all static and templates and less files
merged = merge(obj)
#save the full file if name defined
if obj.get('full'):
print 'Saving: {} ({:.2f}kB)'.format(obj['full'], len(merged)/1024.0)
_save(obj['full'], merged)
else:
print 'Full merged size: {... | Process each block of the merger object. |
def growthfromrange(rangegrowth, startdate, enddate):
_yrs = (pd.Timestamp(enddate) - pd.Timestamp(startdate)).total_seconds() /\
dt.timedelta(365.25).total_seconds()
return yrlygrowth(rangegrowth, _yrs) | Annual growth given growth from start date to end date. |
def equities(country='US'):
nasdaqblob, otherblob = _getrawdata()
eq_triples = []
eq_triples.extend(_get_nas_triples(nasdaqblob))
eq_triples.extend(_get_other_triples(otherblob))
eq_triples.sort()
index = [triple[0] for triple in eq_triples]
data = [triple[1:] for triple in eq_triples]
... | Return a DataFrame of current US equities.
.. versionadded:: 0.4.0
.. versionchanged:: 0.5.0
Return a DataFrame
Parameters
----------
country : str, optional
Country code for equities to return, defaults to 'US'.
Returns
-------
eqs : :class:`pandas.DataFrame`
... |
def straddle(self, strike, expiry):
_rows = {}
_prices = {}
for _opttype in _constants.OPTTYPES:
_rows[_opttype] = _relevant_rows(self.data, (strike, expiry, _opttype,),
"No key for {} strike {} {}".format(expiry, strike, _opttype))
_prices[_o... | Metrics for evaluating a straddle.
Parameters
------------
strike : numeric
Strike price.
expiry : date or date str (e.g. '2015-01-01')
Expiration date.
Returns
------------
metrics : DataFrame
Metrics for evaluating straddle. |
def get(equity):
_optmeta = pdr.data.Options(equity, 'yahoo')
_optdata = _optmeta.get_all_data()
return Options(_optdata) | Retrieve all current options chains for given equity.
.. versionchanged:: 0.5.0
Eliminate special exception handling.
Parameters
-------------
equity : str
Equity for which to retrieve options data.
Returns
-------------
optdata : :class:`~pynance.opt.core.Options`
... |
def _get_norms_of_rows(data_frame, method):
if method == 'vector':
norm_vector = np.linalg.norm(data_frame.values, axis=1)
elif method == 'last':
norm_vector = data_frame.iloc[:, -1].values
elif method == 'mean':
norm_vector = np.mean(data_frame.values, axis=1)
elif method =... | return a column vector containing the norm of each row |
def _candlestick_ax(df, ax):
quotes = df.reset_index()
quotes.loc[:, 'Date'] = mdates.date2num(quotes.loc[:, 'Date'].astype(dt.date))
fplt.candlestick_ohlc(ax, quotes.values) | # Alternatively: (but hard to get dates set up properly)
plt.xticks(range(len(df.index)), df.index, rotation=45)
fplt.candlestick2_ohlc(ax, df.loc[:, 'Open'].values, df.loc[:, 'High'].values,
df.loc[:, 'Low'].values, df.loc[:, 'Close'].values, width=0.2) |
def get(self, opttype, strike, expiry):
_optrow = _relevant_rows(self.data, (strike, expiry, opttype,),
"No key for {} strike {} {}".format(expiry, strike, opttype))
return _getprice(_optrow) | Price as midpoint between bid and ask.
Parameters
----------
opttype : str
'call' or 'put'.
strike : numeric
Strike price.
expiry : date-like
Expiration date. Can be a :class:`datetime.datetime` or
a string that :mod:`pandas` can i... |
def metrics(self, opttype, strike, expiry):
_optrow = _relevant_rows(self.data, (strike, expiry, opttype,),
"No key for {} strike {} {}".format(expiry, strike, opttype))
_index = ['Opt_Price', 'Time_Val', 'Last', 'Bid', 'Ask', 'Vol', 'Open_Int', 'Underlying_Price', 'Quote_Time']... | Basic metrics for a specific option.
Parameters
----------
opttype : str ('call' or 'put')
strike : numeric
Strike price.
expiry : date-like
Expiration date. Can be a :class:`datetime.datetime` or
a string that :mod:`pandas` can interpret as s... |
def strikes(self, opttype, expiry):
_relevant = _relevant_rows(self.data, (slice(None), expiry, opttype,),
"No key for {} {}".format(expiry, opttype))
_index = _relevant.index.get_level_values('Strike')
_columns = ['Price', 'Time_Val', 'Last', 'Bid', 'Ask', 'Vol', 'Open_... | Retrieve option prices for all strikes of a given type with a given expiration.
Parameters
----------
opttype : str ('call' or 'put')
expiry : date-like
Expiration date. Can be a :class:`datetime.datetime` or
a string that :mod:`pandas` can interpret as such, e.g... |
def exps(self, opttype, strike):
_relevant = _relevant_rows(self.data, (strike, slice(None), opttype,),
"No key for {} {}".format(strike, opttype))
_index = _relevant.index.get_level_values('Expiry')
_columns = ['Price', 'Time_Val', 'Last', 'Bid', 'Ask', 'Vol', 'Open_Int... | Prices for given strike on all available dates.
Parameters
----------
opttype : str ('call' or 'put')
strike : numeric
Returns
----------
df : :class:`pandas.DataFrame`
eq : float
Price of underlying.
qt : :class:`datetime.datetime`
... |
def growth(interval, pricecol, eqdata):
size = len(eqdata.index)
labeldata = eqdata.loc[:, pricecol].values[interval:] /\
eqdata.loc[:, pricecol].values[:(size - interval)]
df = pd.DataFrame(data=labeldata, index=eqdata.index[:(size - interval)],
columns=['Growth'], dtype='float... | Retrieve growth labels.
Parameters
--------------
interval : int
Number of sessions over which growth is measured. For example, if
the value of 32 is passed for `interval`, the data returned will
show the growth 32 sessions ahead for each data point.
eqdata : DataFrame
... |
def sma(eqdata, **kwargs):
if len(eqdata.shape) > 1 and eqdata.shape[1] != 1:
_selection = kwargs.get('selection', 'Adj Close')
_eqdata = eqdata.loc[:, _selection]
else:
_eqdata = eqdata
_window = kwargs.get('window', 20)
_outputcol = kwargs.get('outputcol', 'SMA')
ret =... | simple moving average
Parameters
----------
eqdata : DataFrame
window : int, optional
Lookback period for sma. Defaults to 20.
outputcol : str, optional
Column to use for output. Defaults to 'SMA'.
selection : str, optional
Column of eqdata on which to calculate sma. If... |
def ema(eqdata, **kwargs):
if len(eqdata.shape) > 1 and eqdata.shape[1] != 1:
_selection = kwargs.get('selection', 'Adj Close')
_eqdata = eqdata.loc[:, _selection]
else:
_eqdata = eqdata
_span = kwargs.get('span', 20)
_col = kwargs.get('outputcol', 'EMA')
_emadf = pd.Dat... | Exponential moving average with the given span.
Parameters
----------
eqdata : DataFrame
Must have exactly 1 column on which to calculate EMA
span : int, optional
Span for exponential moving average. Cf. `pandas.stats.moments.ewma
<http://pandas.pydata.org/pandas-docs/stable/ge... |
def ema_growth(eqdata, **kwargs):
_growth_outputcol = kwargs.get('outputcol', 'EMA Growth')
_ema_outputcol = 'EMA'
kwargs['outputcol'] = _ema_outputcol
_emadf = ema(eqdata, **kwargs)
return simple.growth(_emadf, selection=_ema_outputcol, outputcol=_growth_outputcol) | Growth of exponential moving average.
Parameters
----------
eqdata : DataFrame
span : int, optional
Span for exponential moving average. Defaults to 20.
outputcol : str, optional.
Column to use for output. Defaults to 'EMA Growth'.
selection : str, optional
Column of eqd... |
def volatility(eqdata, **kwargs):
if len(eqdata.shape) > 1 and eqdata.shape[1] != 1:
_selection = kwargs.get('selection', 'Adj Close')
_eqdata = eqdata.loc[:, _selection]
else:
_eqdata = eqdata
_window = kwargs.get('window', 20)
_colname = kwargs.get('outputcol', 'Risk')
... | Volatility (standard deviation) over the given window
Parameters
----------
eqdata : DataFrame
window : int, optional
Lookback period. Defaults to 20.
outputcol : str, optional
Name of column to be used in returned dataframe. Defaults to 'Risk'.
selection : str, optional
... |
def growth_volatility(eqdata, **kwargs):
_window = kwargs.get('window', 20)
_selection = kwargs.get('selection', 'Adj Close')
_outputcol = kwargs.get('outputcol', 'Growth Risk')
_growthdata = simple.growth(eqdata, selection=_selection)
return volatility(_growthdata, outputcol=_outputcol, window... | Return the volatility of growth.
Note that, like :func:`pynance.tech.simple.growth` but in contrast to
:func:`volatility`, :func:`growth_volatility`
applies directly to a dataframe like that returned by
:func:`pynance.data.retrieve.get`, not necessarily to a single-column dataframe.
Parameters
... |
def bollinger(eqdata, **kwargs):
_window = kwargs.get('window', 20)
_multiple = kwargs.get('multiple', 2.)
_selection = kwargs.get('selection', 'Adj Close')
# ensures correct name for output column of sma()
kwargs['outputcol'] = 'SMA'
_smadf = sma(eqdata, **kwargs)
_sigmas = eqdata.loc[... | Bollinger bands
Returns bolldf, smadf where bolldf is a DataFrame containing
Bollinger bands with columns 'Upper' and 'Lower' and smadf contains
the simple moving average.
Parameters
----------
eqdata : DataFrame
Must include a column specified in the `selection` parameter or,
... |
def ratio_to_ave(window, eqdata, **kwargs):
_selection = kwargs.get('selection', 'Volume')
_skipstartrows = kwargs.get('skipstartrows', 0)
_skipendrows = kwargs.get('skipendrows', 0)
_outputcol = kwargs.get('outputcol', 'Ratio to Ave')
_size = len(eqdata.index)
_eqdata = eqdata.loc[:, _sele... | Return values expressed as ratios to the average over some number
of prior sessions.
Parameters
----------
eqdata : DataFrame
Must contain a column with name matching `selection`, or, if
`selection` is not specified, a column named 'Volume'
window : int
Interval over which t... |
def run(features, labels, regularization=0., constfeat=True):
n_col = (features.shape[1] if len(features.shape) > 1 else 1)
reg_matrix = regularization * np.identity(n_col, dtype='float64')
if constfeat:
reg_matrix[0, 0] = 0.
# http://stackoverflow.com/questions/27476933/numpy-linear-regres... | Run linear regression on the given data.
.. versionadded:: 0.5.0
If a regularization parameter is provided, this function
is a simplification and specialization of ridge
regression, as implemented in `scikit-learn
<http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Ridge.html#sk... |
def cal(self, opttype, strike, exp1, exp2):
assert pd.Timestamp(exp1) < pd.Timestamp(exp2)
_row1 = _relevant_rows(self.data, (strike, exp1, opttype,),
"No key for {} strike {} {}".format(exp1, strike, opttype))
_row2 = _relevant_rows(self.data, (strike, exp2, opttype,),
... | Metrics for evaluating a calendar spread.
Parameters
------------
opttype : str ('call' or 'put')
Type of option on which to collect data.
strike : numeric
Strike price.
exp1 : date or date str (e.g. '2015-01-01')
Earlier expiration date.
... |
def featurize(equity_data, n_sessions, **kwargs):
#Benchmarking
#>>> s = 'from __main__ import data\nimport datetime as dt\n'
#>>> timeit.timeit('data.featurize(data.get("ge", dt.date(1960, 1, 1),
# dt.date(2014, 12, 31)), 256)', setup=s, number=1)
#1.6771750450134277
columns = kwar... | Generate a raw (unnormalized) feature set from the input data.
The value at `column` on the given date is taken
as a feature, and each row contains values for n_sessions
Parameters
-----------
equity_data : DataFrame
data from which to generate features
n_sessions : int
number ... |
def decorate(fn, *args, **kwargs):
def _wrapper(*_args, **kwargs):
_ret = fn(*_args, **kwargs)
if isinstance(_ret, tuple):
return _ret + args
if len(args) == 0:
return _ret
return (_ret,) + args
for key, value in kwargs.items():
_wrapper.__dic... | Return a new function that replicates the behavior of the input
but also returns an additional value. Used for creating functions
of the proper type to pass to `labeledfeatures()`.
Parameters
----------
fn : function
*args : any
Additional parameters that the returned function will ret... |
def expand(fn, col, inputtype=pd.DataFrame):
if inputtype == pd.DataFrame:
if isinstance(col, int):
def _wrapper(*args, **kwargs):
return fn(args[0].iloc[:, col], *args[1:], **kwargs)
return _wrapper
def _wrapper(*args, **kwargs):
return fn(ar... | Wrap a function applying to a single column to make a function
applying to a multi-dimensional dataframe or ndarray
Parameters
----------
fn : function
Function that applies to a series or vector.
col : str or int
Index of column to which to apply `fn`.
inputtype : class or ty... |
def has_na(eqdata):
if isinstance(eqdata, pd.DataFrame):
_values = eqdata.values
else:
_values = eqdata
return len(_values[pd.isnull(_values)]) > 0 | Return false if `eqdata` contains no missing values.
Parameters
----------
eqdata : DataFrame or ndarray
Data to check for missing values (NaN, None)
Returns
----------
answer : bool
False iff `eqdata` contains no missing values. |
def add_const(features):
content = np.empty((features.shape[0], features.shape[1] + 1), dtype='float64')
content[:, 0] = 1.
if isinstance(features, np.ndarray):
content[:, 1:] = features
return content
content[:, 1:] = features.iloc[:, :].values
cols = ['Constant'] + features.co... | Prepend the constant feature 1 as first feature and return the modified
feature set.
Parameters
----------
features : ndarray or DataFrame |
def fromcols(selection, n_sessions, eqdata, **kwargs):
_constfeat = kwargs.get('constfeat', True)
_outcols = ['Constant'] if _constfeat else []
_n_rows = len(eqdata.index)
for _col in selection:
_outcols += map(partial(_concat, strval=' ' + _col), range(-n_sessions + 1, 1))
_features = ... | Generate features from selected columns of a dataframe.
Parameters
----------
selection : list or tuple of str
Columns to be used as features.
n_sessions : int
Number of sessions over which to create features.
eqdata : DataFrame
Data from which to generate feature set. Mus... |
def ln_growth(eqdata, **kwargs):
if 'outputcol' not in kwargs:
kwargs['outputcol'] = 'LnGrowth'
return np.log(growth(eqdata, **kwargs)) | Return the natural log of growth.
See also
--------
:func:`growth` |
def ret(eqdata, **kwargs):
if 'outputcol' not in kwargs:
kwargs['outputcol'] = 'Return'
result = growth(eqdata, **kwargs)
result.values[:, :] -= 1.
return result | Generate a DataFrame where the sole column, 'Return',
is the return for the equity over the given number of sessions.
For example, if 'XYZ' has 'Adj Close' of `100.0` on 2014-12-15 and
`90.0` 4 *sessions* later on 2014-12-19, then the 'Return' value
for 2014-12-19 will be `-0.1`.
Parameters
... |
def mse(predicted, actual):
diff = predicted - actual
return np.average(diff * diff, axis=0) | Mean squared error of predictions.
.. versionadded:: 0.5.0
Parameters
----------
predicted : ndarray
Predictions on which to measure error. May
contain a single or multiple column but must
match `actual` in shape.
actual : ndarray
Actual values against which to mea... |
def is_bday(date, bday=None):
_date = Timestamp(date)
if bday is None:
bday = CustomBusinessDay(calendar=USFederalHolidayCalendar())
return _date == (_date + bday) - bday | Return true iff the given date is a business day.
Parameters
----------
date : :class:`pandas.Timestamp`
Any value that can be converted to a pandas Timestamp--e.g.,
'2012-05-01', dt.datetime(2012, 5, 1, 3)
bday : :class:`pandas.tseries.offsets.CustomBusinessDay`
Defaults to `C... |
def compare(eq_dfs, columns=None, selection='Adj Close'):
content = np.empty((eq_dfs[0].shape[0], len(eq_dfs)), dtype=np.float64)
rel_perf = pd.DataFrame(content, eq_dfs[0].index, columns, dtype=np.float64)
for i in range(len(eq_dfs)):
rel_perf.iloc[:, i] = eq_dfs[i].loc[:, selection] / eq_dfs[... | Get the relative performance of multiple equities.
.. versionadded:: 0.5.0
Parameters
----------
eq_dfs : list or tuple of DataFrame
Performance data for multiple equities over
a consistent time frame.
columns : iterable of str, default None
Labels to use for the columns of... |
def info(self):
print("Expirations:")
_i = 0
for _datetime in self.data.index.levels[1].to_pydatetime():
print("{:2d} {}".format(_i, _datetime.strftime('%Y-%m-%d')))
_i += 1
print("Stock: {:.2f}".format(self.data.iloc[0].loc['Underlying_Price']))
... | Show expiration dates, equity price, quote time.
Returns
-------
self : :class:`~pynance.opt.core.Options`
Returns a reference to the calling object to allow
chaining.
expiries : :class:`pandas.tseries.index.DatetimeIndex`
Examples
--------
... |
def tolist(self):
return [_todict(key, self.data.loc[key, :]) for key in self.data.index] | Return the array as a list of rows.
Each row is a `dict` of values. Facilitates inserting data into a database.
.. versionadded:: 0.3.1
Returns
-------
quotes : list
A list in which each entry is a dictionary representing
a single options quote. |
def _generate_username(self):
while True:
# Generate a UUID username, removing dashes and the last 2 chars
# to make it fit into the 30 char User.username field. Gracefully
# handle any unlikely, but possible duplicate usernames.
username = str(uuid.uuid4... | Generate a unique username |
def update_model_cache(table_name):
model_cache_info = ModelCacheInfo(table_name, uuid.uuid4().hex)
model_cache_backend.share_model_cache_info(model_cache_info) | Updates model cache by generating a new key for the model |
def invalidate_model_cache(sender, instance, **kwargs):
logger.debug('Received post_save/post_delete signal from sender {0}'.format(sender))
if django.VERSION >= (1, 8):
related_tables = set(
[f.related_model._meta.db_table for f in sender._meta.get_fields()
if f.related_mo... | Signal receiver for models to invalidate model cache of sender and related models.
Model cache is invalidated by generating new key for each model.
Parameters
~~~~~~~~~~
sender
The model class
instance
The actual instance being saved. |
def invalidate_m2m_cache(sender, instance, model, **kwargs):
logger.debug('Received m2m_changed signals from sender {0}'.format(sender))
update_model_cache(instance._meta.db_table)
update_model_cache(model._meta.db_table) | Signal receiver for models to invalidate model cache for many-to-many relationship.
Parameters
~~~~~~~~~~
sender
The model class
instance
The instance whose many-to-many relation is updated.
model
The class of the objects that are added to, removed from or cleared from the r... |
def get_params(self):
value = self._get_lookup(self.operator, self.value)
self.params.append(self.value)
return self.params | returns a list |
def get_wheres(self):
self.wheres.append(u"%s %s"
% (lookup_cast(operator) % self.db_field,
self.operator))
return self.wheres | returns a list |
def generate_key(self):
sql = self.sql()
key, created = self.get_or_create_model_key()
if created:
db_table = self.model._meta.db_table
logger.debug('created new key {0} for model {1}'.format(key, db_table))
model_cache_info = ModelCacheInfo(db_table,... | Generate cache key for the current query. If a new key is created for the model it is
then shared with other consumers. |
def sql(self):
clone = self.query.clone()
sql, params = clone.get_compiler(using=self.db).as_sql()
return sql % params | Get sql for the current query. |
def get_or_create_model_key(self):
model_cache_info = model_cache_backend.retrieve_model_cache_info(self.model._meta.db_table)
if not model_cache_info:
return uuid.uuid4().hex, True
return model_cache_info.table_key, False | Get or create key for the model.
Returns
~~~~~~~
(model_key, boolean) tuple |
def invalidate_model_cache(self):
logger.info('Invalidating cache for table {0}'.format(self.model._meta.db_table))
if django.VERSION >= (1, 8):
related_tables = set(
[f.related_model._meta.db_table for f in self.model._meta.get_fields()
if ((f.one_t... | Invalidate model cache by generating new key for the model. |
def cache_backend(self):
if not hasattr(self, '_cache_backend'):
if hasattr(django.core.cache, 'caches'):
self._cache_backend = django.core.cache.caches[_cache_name]
else:
self._cache_backend = django.core.cache.get_cache(_cache_name)
ret... | Get the cache backend
Returns
~~~~~~~
Django cache backend |
def import_file(filename):
pathname, filename = os.path.split(filename)
modname = re.match(
r'(?P<modname>\w+)\.py', filename).group('modname')
file, path, desc = imp.find_module(modname, [pathname])
try:
imp.load_module(modname, file, path, desc)
finally:
file.close() | Import a file that will trigger the population of Orca.
Parameters
----------
filename : str |
def check_is_table(func):
@wraps(func)
def wrapper(**kwargs):
if not orca.is_table(kwargs['table_name']):
abort(404)
return func(**kwargs)
return wrapper | Decorator that will check whether the "table_name" keyword argument
to the wrapped function matches a registered Orca table. |
def check_is_column(func):
@wraps(func)
def wrapper(**kwargs):
table_name = kwargs['table_name']
col_name = kwargs['col_name']
if not orca.is_table(table_name):
abort(404)
if col_name not in orca.get_table(table_name).columns:
abort(404)
retu... | Decorator that will check whether the "table_name" and "col_name"
keyword arguments to the wrapped function match a registered Orca
table and column. |
def check_is_injectable(func):
@wraps(func)
def wrapper(**kwargs):
name = kwargs['inj_name']
if not orca.is_injectable(name):
abort(404)
return func(**kwargs)
return wrapper | Decorator that will check whether the "inj_name" keyword argument to
the wrapped function matches a registered Orca injectable. |
def schema():
tables = orca.list_tables()
cols = {t: orca.get_table(t).columns for t in tables}
steps = orca.list_steps()
injectables = orca.list_injectables()
broadcasts = orca.list_broadcasts()
return jsonify(
tables=tables, columns=cols, steps=steps, injectables=injectables,
... | All tables, columns, steps, injectables and broadcasts registered with
Orca. Includes local columns on tables. |
def table_info(table_name):
table = orca.get_table(table_name).to_frame()
buf = StringIO()
table.info(verbose=True, buf=buf)
info = buf.getvalue()
return info, 200, {'Content-Type': 'text/plain'} | Return the text result of table.info(verbose=True). |
def table_preview(table_name):
preview = orca.get_table(table_name).to_frame().head()
return (
preview.to_json(orient='split', date_format='iso'),
200,
{'Content-Type': 'application/json'}) | Returns the first five rows of a table as JSON. Inlcudes all columns.
Uses Pandas' "split" JSON format. |
def table_describe(table_name):
desc = orca.get_table(table_name).to_frame().describe()
return (
desc.to_json(orient='split', date_format='iso'),
200,
{'Content-Type': 'application/json'}) | Return summary statistics of a table as JSON. Includes all columns.
Uses Pandas' "split" JSON format. |
def table_definition(table_name):
if orca.table_type(table_name) == 'dataframe':
return jsonify(type='dataframe')
filename, lineno, source = \
orca.get_raw_table(table_name).func_source_data()
html = highlight(source, PythonLexer(), HtmlFormatter())
return jsonify(
type='... | Get the source of a table function.
If a table is registered DataFrame and not a function then all that is
returned is {'type': 'dataframe'}.
If the table is a registered function then the JSON returned has keys
"type", "filename", "lineno", "text", and "html". "text" is the raw
text of the functi... |
def table_groupbyagg(table_name):
table = orca.get_table(table_name)
# column to aggregate
column = request.args.get('column', None)
if not column or column not in table.columns:
abort(400)
# column or index level to group by
by = request.args.get('by', None)
level = request.a... | Perform a groupby on a table and return an aggregation on a single column.
This depends on some request parameters in the URL.
"column" and "agg" must always be present, and one of "by" or "level"
must be present. "column" is the table column on which aggregation will
be performed, "agg" is the aggrega... |
def column_preview(table_name, col_name):
col = orca.get_table(table_name).get_column(col_name).head(10)
return (
col.to_json(orient='split', date_format='iso'),
200,
{'Content-Type': 'application/json'}) | Return the first ten elements of a column as JSON in Pandas'
"split" format. |
def column_definition(table_name, col_name):
col_type = orca.get_table(table_name).column_type(col_name)
if col_type != 'function':
return jsonify(type=col_type)
filename, lineno, source = \
orca.get_raw_column(table_name, col_name).func_source_data()
html = highlight(source, Pyt... | Get the source of a column function.
If a column is a registered Series and not a function then all that is
returned is {'type': 'series'}.
If the column is a registered function then the JSON returned has keys
"type", "filename", "lineno", "text", and "html". "text" is the raw
text of the functio... |
def column_describe(table_name, col_name):
col_desc = orca.get_table(table_name).get_column(col_name).describe()
return (
col_desc.to_json(orient='split'),
200,
{'Content-Type': 'application/json'}) | Return summary statistics of a column as JSON.
Uses Pandas' "split" JSON format. |
def column_csv(table_name, col_name):
csv = orca.get_table(table_name).get_column(col_name).to_csv(path=None)
return csv, 200, {'Content-Type': 'text/csv'} | Return a column as CSV using Pandas' default CSV output. |
def injectable_repr(inj_name):
i = orca.get_injectable(inj_name)
return jsonify(type=str(type(i)), repr=repr(i)) | Returns the type and repr of an injectable. JSON response has
"type" and "repr" keys. |
def injectable_definition(inj_name):
inj_type = orca.injectable_type(inj_name)
if inj_type == 'variable':
return jsonify(type='variable')
else:
filename, lineno, source = \
orca.get_injectable_func_source_data(inj_name)
html = highlight(source, PythonLexer(), HtmlFo... | Get the source of an injectable function.
If an injectable is a registered Python variable and not a function
then all that is returned is {'type': 'variable'}.
If the column is a registered function then the JSON returned has keys
"type", "filename", "lineno", "text", and "html". "text" is the raw
... |
def list_broadcasts():
casts = [{'cast': b[0], 'onto': b[1]} for b in orca.list_broadcasts()]
return jsonify(broadcasts=casts) | List all registered broadcasts as a list of objects with
keys "cast" and "onto". |
def broadcast_definition(cast_name, onto_name):
if not orca.is_broadcast(cast_name, onto_name):
abort(404)
b = orca.get_broadcast(cast_name, onto_name)
return jsonify(
cast=b.cast, onto=b.onto, cast_on=b.cast_on, onto_on=b.onto_on,
cast_index=b.cast_index, onto_index=b.onto_in... | Return the definition of a broadcast as an object with keys
"cast", "onto", "cast_on", "onto_on", "cast_index", and "onto_index".
These are the same as the arguments to the ``broadcast`` function. |
def step_definition(step_name):
if not orca.is_step(step_name):
abort(404)
filename, lineno, source = \
orca.get_step(step_name).func_source_data()
html = highlight(source, PythonLexer(), HtmlFormatter())
return jsonify(filename=filename, lineno=lineno, text=source, html=html) | Get the source of a step function. Returned object has keys
"filename", "lineno", "text" and "html". "text" is the raw
text of the function, "html" has been marked up by Pygments. |
def _add_log_handler(
handler, level=None, fmt=None, datefmt=None, propagate=None):
if not fmt:
fmt = US_LOG_FMT
if not datefmt:
datefmt = US_LOG_DATE_FMT
handler.setFormatter(logging.Formatter(fmt=fmt, datefmt=datefmt))
if level is not None:
handler.setLevel(level... | Add a logging handler to Orca.
Parameters
----------
handler : logging.Handler subclass
level : int, optional
An optional logging level that will apply only to this stream
handler.
fmt : str, optional
An optional format string that will be used for the log
messages.
... |
def log_to_stream(level=None, fmt=None, datefmt=None):
_add_log_handler(
logging.StreamHandler(), fmt=fmt, datefmt=datefmt, propagate=False) | Send log messages to the console.
Parameters
----------
level : int, optional
An optional logging level that will apply only to this stream
handler.
fmt : str, optional
An optional format string that will be used for the log
messages.
datefmt : str, optional
... |
def log_to_file(filename, level=None, fmt=None, datefmt=None):
_add_log_handler(
logging.FileHandler(filename), fmt=fmt, datefmt=datefmt) | Send log output to the given file.
Parameters
----------
filename : str
level : int, optional
An optional logging level that will apply only to this stream
handler.
fmt : str, optional
An optional format string that will be used for the log
messages.
datefmt : st... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.