Permalink
Browse files

[WIP] addressing python semantic checking warnings from issue #1442, …

…needs a bit more testing to make sure changes did not break anything
  • Loading branch information...
Scott-Graham-Bose committed Apr 9, 2018
1 parent 0105bbc commit 4262b0e22234d03b7bbad65c741d6cf9a452b73b
@@ -2227,8 +2227,8 @@ def load_templates(file, output_file=None):
output.write(' <title>%s</title>\n' % title)
output.write(' <ns>10</ns>\n')
output.write(' <text>')
for line in page:
output.write(line)
for page_line in page:
output.write(page_line)
output.write(' </text>\n')
output.write('</page>\n')
page = []
@@ -79,7 +79,7 @@ def _run(self, sentence):
output = [-1 for i in range(N)]
for n in range(N):
# make LDF examples
examples = [ lambda: self.makeExample(sentence,n,m) for m in range(-1,N) if n != m ]
examples = [ lambda: self.makeExample(sentence,n=n,m=m) for m in range(-1,N) if n != m ]

# truth
parN = sentence[n][1]
@@ -2,11 +2,22 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "ImportError",
"evalue": "dynamic module does not define init function (initpylibvw)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-1-ab02786e9878>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mmatplotlib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpyplot\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mplt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mvowpalwabbit\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpyvw\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmagic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mu'matplotlib inline'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/scottgraham/venvs/vw/lib/python2.7/site-packages/vowpalwabbit-8.4.0-py2.7-macosx-10.12-x86_64.egg/vowpalwabbit/pyvw.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0m__future__\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdivision\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mpylibvw\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0mSearchTask\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mImportError\u001b[0m: dynamic module does not define init function (initpylibvw)"
]
}
],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
@@ -18,9 +29,7 @@
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# Generate some count data that has poisson distribution \n",
@@ -44,9 +53,7 @@
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# Train log-transform model\n",
@@ -65,9 +72,7 @@
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# Generate predictions from the log-transform model\n",
@@ -89,9 +94,7 @@
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# Train original domain model using poisson regression\n",
@@ -109,9 +112,7 @@
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# Generate predictions from the poisson model\n",
@@ -123,9 +124,7 @@
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"data": {
@@ -186,23 +185,23 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 2",
"language": "python",
"name": "python3"
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}
@@ -12,7 +12,6 @@ def __init__(self, vw, sch, num_actions):

def __del__(self):
self.bogus_example.finish()
pass

def _run(self, your_own_input_example):
pass
@@ -242,41 +241,38 @@ def predict(examples, my_tag, oracle, condition=None, allowed=None, learner_id=0
# P.set_input_at(n, examples[n])
# else: # non-LDF
# P.set_input(examples)
if True: # TODO: get rid of this
if oracle is None: pass
elif isinstance(oracle, list):
assert 0 not in oracle, 'multiclass labels are from 1..., please do not use zero or bad things will happen!'
if len(oracle) > 0:
P.set_oracles(oracle)
elif isinstance(oracle, int):
assert oracle > 0, 'multiclass labels are from 1..., please do not use zero or bad things will happen!'
P.set_oracle(oracle)
else:
raise TypeError('expecting oracle to be a list or an integer')

if condition is not None:
if not isinstance(condition, list): condition = [condition]
for c in condition:
if not isinstance(c, tuple): raise TypeError('item ' + str(c) + ' in condition list is malformed')
if len(c) == 2 and isinstance(c[0], int) and isinstance(c[1], str) and len(c[1]) == 1:
P.add_condition(max(0, c[0]), c[1])
elif len(c) == 3 and isinstance(c[0], int) and isinstance(c[1], int) and isinstance(c[2], str) and len(c[2]) == 1:
P.add_condition_range(max(0,c[0]), max(0,c[1]), c[2])
else:
raise TypeError('item ' + str(c) + ' in condition list malformed')

if allowed is None: pass
elif isinstance(allowed, list):
assert 0 not in allowed, 'multiclass labels are from 1..., please do not use zero or bad things will happen!'
P.set_alloweds(allowed)
else: raise TypeError('allowed argument wrong type')

if learner_id != 0: P.set_learner_id(learner_id)

p = P.predict()
return p
if oracle is None: pass
elif isinstance(oracle, list):
assert 0 not in oracle, 'multiclass labels are from 1..., please do not use zero or bad things will happen!'
if len(oracle) > 0:
P.set_oracles(oracle)
elif isinstance(oracle, int):
assert oracle > 0, 'multiclass labels are from 1..., please do not use zero or bad things will happen!'
P.set_oracle(oracle)
else:
raise TypeError("'examples' should be a pyvw example (or a pylibvw example), or a list of said things")
raise TypeError('expecting oracle to be a list or an integer')

if condition is not None:
if not isinstance(condition, list): condition = [condition]
for c in condition:
if not isinstance(c, tuple): raise TypeError('item ' + str(c) + ' in condition list is malformed')
if len(c) == 2 and isinstance(c[0], int) and isinstance(c[1], str) and len(c[1]) == 1:
P.add_condition(max(0, c[0]), c[1])
elif len(c) == 3 and isinstance(c[0], int) and isinstance(c[1], int) and isinstance(c[2], str) and len(c[2]) == 1:
P.add_condition_range(max(0,c[0]), max(0,c[1]), c[2])
else:
raise TypeError('item ' + str(c) + ' in condition list malformed')

if allowed is None: pass
elif isinstance(allowed, list):
assert 0 not in allowed, 'multiclass labels are from 1..., please do not use zero or bad things will happen!'
P.set_alloweds(allowed)
else: raise TypeError('allowed argument wrong type')

if learner_id != 0: P.set_learner_id(learner_id)

p = P.predict()
return p

sch.predict = predict
num_actions = sch.get_num_actions()
@@ -380,7 +380,7 @@ def __repr__(self):

def __del__(self):
if hasattr(self, 'vw_') and self.vw_ is not None:
self.vw_.__del__()
del self.vw_

def get_params(self, deep=True):
"""This returns the set of vw and estimator parameters currently in use"""
@@ -507,6 +507,22 @@ def __init__(self, **params):

super(VWClassifier, self).__init__(**params)

def predict(self, X):
"""Predict class labels for samples in X.
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Samples.
Returns
-------
C : array, shape = [n_samples]
Predicted class label per sample.
"""

return ThresholdingLinearClassifierMixin.predict(self, X=X)

def decision_function(self, X):
"""Predict confidence scores for samples.
The confidence score for a sample is the signed distance of that
@@ -103,7 +103,6 @@ def raw_input_keypress(str):
seed=open(args.seed,'r')
except:
print 'Warning: could not read from %s'%args.seed
pass

try:
unlabeled=open(args.unlabeled_dataset,'r')
@@ -117,15 +116,13 @@ def raw_input_keypress(str):
human = open(args.human, 'r')
except:
print 'Warning: could not read human examples from %s' % args.human
pass

output=None
if args.output is not None:
try:
output=open(args.output,'w')
except:
print 'Warning: could not write to %s'%args.output
pass


# Create a socket
@@ -280,6 +280,9 @@ def validation_metric_vw(self):
fpr, tpr, _ = roc_curve(self.y_true_holdout, y_pred_holdout_proba)
loss = -auc(fpr, tpr)

else:
raise KeyError('Invalide outer loss function')

self.logger.info('parameter suffix: %s' % self.param_suffix)
self.logger.info('loss value: %.6f' % loss)

@@ -316,6 +319,8 @@ def objective(kwargs):
algo = tpe.suggest
elif self.searcher == 'rand':
algo = rand.suggest
else:
raise KeyError('Invalid searcher')

logging.debug("starting hypersearch...")
best_params = fmin(objective, space=self.space, trials=self.trials, algo=algo, max_evals=self.max_evals)
@@ -52,7 +52,6 @@ class vw_lda(object):
self.hashed_lda_model = ""
self.hash_values = "hash_values.csv"
self.max_file_count = 10000
pass


def create_hash(self, doc):

0 comments on commit 4262b0e

Please sign in to comment.