Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #61

Merged
merged 17 commits into from
Dec 30, 2016
Merged

Refactor #61

merged 17 commits into from
Dec 30, 2016

Conversation

bqpd
Copy link
Contributor

@bqpd bqpd commented Dec 22, 2016

as part of digging into the gpfit code I cleaned up some logic in fit.py...

@mjburton11, could you check this still works with your stuff?

@whoburg, ready for review.

@bqpd bqpd changed the base branch from master to smaequality December 22, 2016 06:06
@mjburton11
Copy link

mjburton11 commented Dec 22, 2016

@bqpd, the renaming of u to u_fit caused some problems in evaluate_fit.py that I was able to fix. (pull my latest commit on this branch) However, running gassolar/environment/wind_fit.py in commit convexengineering/gassolar@d65428c gives this error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/Users/mjburton11/MIT/GPKIT/gpkit-projects/gas_solar_trade/gassolar/environment/wind_fit.py in <module>()
    100                     print "rms iter=%d" % i
    101                 np.random.seed(i)
--> 102                 cns, rm = fit(X, Y, 4, "SMA")
    103                 rms.append(rm)
    104                 if rm > 0.05:

/Users/mjburton11/anaconda/lib/python2.7/site-packages/gpfit/fit.pyc in fit(xdata, ydata, K, ftype)
    105     elif ftype == "SMA":
    106         # constraint of the form w^alpha >= c1*u1^exp1 + c2*u2^exp2 +....
--> 107         lhs, rhs = w**alpha, monos.sum()
    108         print_SMA(A, B, alpha, d, K)
    109     elif ftype == "MA":

/Users/mjburton11/MIT/GPKIT/gpkit/gpkit/nomials/array.pyc in sum(self, *args, **kwargs)
    199             i = it.multi_index
    200             it.iternext()
--> 201             cs.extend(mag(self[i].cs).tolist())
    202             exps.extend(self[i].exps)
    203         if units:

AttributeError: 'float' object has no attribute 'cs'

@bqpd
Copy link
Contributor Author

bqpd commented Dec 22, 2016

The bottom part of the error message seems to be missing...

@bqpd
Copy link
Contributor Author

bqpd commented Dec 28, 2016

@mjburton11, what's the full error message above?

@bqpd
Copy link
Contributor Author

bqpd commented Dec 28, 2016

Oh I got it. Fixing...

@mjburton11
Copy link

haha just updated the comment...

@bqpd
Copy link
Contributor Author

bqpd commented Dec 28, 2016

@mjburton11, I think the latest commit fixes the bug

@mjburton11
Copy link

@bqpd, now getting the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/mjburton11/MIT/GPKIT/gpkit-projects/gas_solar_trade/gassolar/environment/wind_fit.py in <module>()
    144     colnames = np.append(colnames, "alpha")
    145     colnames = np.insert(colnames, 0, "latitude")
--> 146     df.columns = colnames
    147     # df.to_csv("windaltfitdata1.csv")

/Users/mjburton11/anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in __setattr__(self, name, value)
   2159         try:
   2160             object.__getattribute__(self, name)
-> 2161             return object.__setattr__(self, name, value)
   2162         except AttributeError:
   2163             pass

pandas/src/properties.pyx in pandas.lib.AxisProperty.__set__ (pandas/lib.c:42548)()

/Users/mjburton11/anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in _set_axis(self, axis, labels)
    411 
    412     def _set_axis(self, axis, labels):
--> 413         self._data.set_axis(axis, labels)
    414         self._clear_item_cache()
    415 

/Users/mjburton11/anaconda/lib/python2.7/site-packages/pandas/core/internals.pyc in set_axis(self, axis, new_labels)
   2217         if new_len != old_len:
   2218             raise ValueError('Length mismatch: Expected axis has %d elements, '
-> 2219                              'new values have %d elements' % (old_len, new_len))
   2220 
   2221         self.axes[axis] = new_labels

ValueError: Length mismatch: Expected axis has 8 elements, new values have 14 elements

@bqpd
Copy link
Contributor Author

bqpd commented Dec 28, 2016

Actually I think this is better fixed in gpkit...

@mjburton11
Copy link

Wait.. this is a pandas error...

@bqpd
Copy link
Contributor Author

bqpd commented Dec 28, 2016

Yeah not sure about that pandas error :p. Worked on mine, got a latex error instead

@mjburton11
Copy link

@bqpd, ok so when I generate the fit, K=4 SMA, it prints out 4 terms but the constraint it returns only has 2...

@bqpd
Copy link
Contributor Author

bqpd commented Dec 28, 2016

Yeah, the first two terms were zero. :-/

@bqpd
Copy link
Contributor Author

bqpd commented Dec 28, 2016

Which is what caused the bug.

@mjburton11
Copy link

Hmmm... that's not what I'm seeing.

@mjburton11
Copy link

Ok now I'm getting this error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/Users/mjburton11/MIT/GPKIT/gpkit-projects/gas_solar_trade/gassolar/environment/wind_fit.py in <module>()
    100                     print "rms iter=%d" % i
    101                 np.random.seed(i)
--> 102                 cns, rm = fit(X, Y, 4, "SMA")
    103                 rms.append(rm)
    104                 if rm > 0.05:

/Users/mjburton11/anaconda/lib/python2.7/site-packages/gpfit/fit.pyc in fit(xdata, ydata, K, ftype)
    105     elif ftype == "SMA":
    106         # constraint of the form w^alpha >= c1*u1^exp1 + c2*u2^exp2 +....
--> 107         lhs, rhs = w**alpha, monos.sum()
    108         print_SMA(A, B, alpha, d, K)
    109     elif ftype == "MA":

/Users/mjburton11/MIT/GPKIT/gpkit/gpkit/nomials/array.pyc in sum(self, *args, **kwargs)
    199             i = it.multi_index
    200             it.iternext()
--> 201             cs.extend(mag(self[i].cs).tolist())
    202             exps.extend(self[i].exps)
    203         if units:

AttributeError: 'float' object has no attribute 'cs'

@bqpd
Copy link
Contributor Author

bqpd commented Dec 28, 2016

Yup, that's the zeros.

@bqpd
Copy link
Contributor Author

bqpd commented Dec 28, 2016

making a gpkit pull (convexengineering/gpkit#1019) to address the summing issue. Not sure why the fit has zeros, though; did it not before, or was that just abstracted away before?

@mjburton11
Copy link

mjburton11 commented Dec 28, 2016

So sometimes it did have zeros, but with a different np.random seed I could often get a function without a zero...

@bqpd
Copy link
Contributor Author

bqpd commented Dec 28, 2016

Did those functions have a lower rmse, or is a 4th-order fit overkill, or is this some weird local minima thing?

@bqpd
Copy link
Contributor Author

bqpd commented Dec 28, 2016

@mjburton11, now implements proper summing on gpkit master, so you shouldn't see that 'float' object has no attribute 'cs' error anymore.

@mjburton11
Copy link

Ok I'm not getting that error anymore. But the printed fit is still different from the returned constraint:

In [1]: %run wind_fit.py
Fitting for 54 latitude
rms iter=0
w**0.00298794 = 0.045666 * (u_1)**3.05619 * (u_2)**3.31932
    + 0.00105499 * (u_1)**0.0966488 * (u_2)**27.3915
    + 0.634617 * (u_1)**-0.0265261 * (u_2)**0.0118065
    + 0.362956 * (u_1)**0.0510403 * (u_2)**-0.0161858
RMS Error: 0.013 after iter=0, Altitude 40000

...Pandas error...

In [3]: cns
Out[3]: gpkit.PosynomialInequality(w_fit**0.003 >= 0.363*u_fit_(0,)**0.051*u_fit_(1,)**-0.016 + 0.635*u_fit_(0,)**-0.027*u_fit_(1,)**0.012)

@bqpd
Copy link
Contributor Author

bqpd commented Dec 28, 2016

That looks like a refactor problem, yeah! For some reason it's dropping the two smaller terms.

@bqpd
Copy link
Contributor Author

bqpd commented Dec 29, 2016

Resolved by the latest commit, I think: was a numerical issue with small values of alpha

@bqpd
Copy link
Contributor Author

bqpd commented Dec 29, 2016

@mjburton11, re: evaluate_fit.py, what about having the constraints returned by fit have a method that accepts x data?

@bqpd
Copy link
Contributor Author

bqpd commented Dec 29, 2016

Doing so seems to also remove some of the iterations that were occurring in wind_fit.py without any visible change to the output:

windfitl54

@bqpd
Copy link
Contributor Author

bqpd commented Dec 29, 2016

Now with more refactor...

@bqpd
Copy link
Contributor Author

bqpd commented Dec 29, 2016

@mjburton11, @whoburg, this is ready for review.

@bqpd
Copy link
Contributor Author

bqpd commented Dec 29, 2016

@galbramc, the example seems unable to import gpfit? Or at least it has this pylint error: "Unable to import 'gpfit.fit'"

@bqpd
Copy link
Contributor Author

bqpd commented Dec 29, 2016

@galbramc, fixed it by pip installing gpfit in jenkins

@galbramc
Copy link

Hmm, didn't realize I had forgotten to install gpfit

@mjburton11
Copy link

I've run this through all my stuff and it works fine. I like that the evaluate function can be called from constraint. Much more elegant way to do that...

@bqpd
Copy link
Contributor Author

bqpd commented Dec 30, 2016 via email

@mjburton11
Copy link

Should I squash/merge?

@bqpd
Copy link
Contributor Author

bqpd commented Dec 30, 2016 via email

@mjburton11 mjburton11 merged commit b0f2b46 into smaequality Dec 30, 2016
@bqpd
Copy link
Contributor Author

bqpd commented Dec 30, 2016

Deleted branch.

@bqpd bqpd deleted the refactor branch December 30, 2016 22:42
Copy link
Collaborator

@whoburg whoburg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @bqpd, what a huge bunch of improvements! I finally had a chance to review and left a few comments / suggestions. Should we address those with issues / a new PR?

@@ -60,7 +60,7 @@ confidence=
# no Warning level messages displayed, use"--disable=all --enable=classes
# --disable=W"
#disable=import-star-module-level,old-octal-literal,oct-method,print-statement,unpacking-in-except,parameter-unpacking,backtick,old-raise-syntax,old-ne-operator,long-suffix,dict-view-method,dict-iter-method,metaclass-assignment,next-method-called,raising-string,indexing-exception,raw_input-builtin,long-builtin,file-builtin,execfile-builtin,coerce-builtin,cmp-builtin,buffer-builtin,basestring-builtin,apply-builtin,filter-builtin-not-iterating,using-cmp-argument,useless-suppression,range-builtin-not-iterating,suppressed-message,no-absolute-import,old-division,cmp-method,reload-builtin,zip-builtin-not-iterating,intern-builtin,unichr-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,input-builtin,round-builtin,hex-method,nonzero-method,map-builtin-not-iterating
disable=locally-disabled
disable=locally-disabled,superfluous-parens,wrong-import-position, fixme, duplicate-code, maybe-no-member
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In gpkit our pylint.sh script runs pylint with --disable=fixme. That seems better than disabling here, so that pylint plugins will show todos.

Also not sure I agree with disabling duplicate-code, especially while we're working towards refactor! But I know it's a pain until we get the counts to zero, so maybe we should address that by opening an issue re: duplicate-code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplicate code is in the tests directory, which could probably be allowed to have duplicate code...not that this is the right way to do that, either.


# Naming hint for function names
function-name-hint=[a-z_][a-z0-9_]{2,30}$
function-rgx=[A-Za-z_][a-z0-9_]{2,30}$
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what method needs caps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have done search-and-replace, I was so tired of pylint.


# Naming hint for class names
class-name-hint=[A-Z_][a-zA-Z0-9]+$
class-rgx=[t_]?|[A-Z_][a-zA-Z0-9]+$
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be better to just update our test classes and modules to follow the expected style?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely!

@@ -3,14 +3,16 @@
import numpy as np
from numpy import array, arange
from gpfit.lse_implicit import lse_implicit
from .seed import SEED

SEED = 33404
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious, why hard-code this instead of importing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the local import you can't run the test files as scripts!

@whoburg
Copy link
Collaborator

whoburg commented Jan 2, 2017

oh, this got merged to smaequality, not master. Shall I review over there? (timeframe may take ~1 week)

@bqpd
Copy link
Contributor Author

bqpd commented Jan 2, 2017

I think this mostly replaced smaequality! I'll check what the diff is.

@bqpd
Copy link
Contributor Author

bqpd commented Jan 2, 2017

Made a new issue for these pylint issues: #63

@bqpd
Copy link
Contributor Author

bqpd commented Jan 2, 2017

@whoburg, these changes were identical to those in smaequality, so I merged that branch to master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants