convexengineering / gpkit

Geometric programming for engineers
http://gpkit.readthedocs.org
MIT License
206 stars 40 forks source link

savetxt() errors with python3 #1415

Closed 1ozturkbe closed 4 years ago

1ozturkbe commented 5 years ago

Here's the error message. Should be easy to fix.

In [10]: sol.savetxt()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-fff6eb73d01a> in <module>
----> 1 sol.savetxt()

c:\users\berk\dropbox (mit)\mit graduate school\code\gpkit\gpkit\solution_array.py in savetxt(self, filename, printmodel, **kwargs)
    508         with open(filename, "w") as f:
    509             if printmodel and self.model:
--> 510                 f.write(str(self.model).encode("utf-8"))
    511             f.write(self.table(**kwargs).encode("utf-8"))
    512

TypeError: write() argument must be str, not bytes

Since GPkit claims to be python2/3 compatible, we should perhaps consider running tests for both versions to avoid future issues.

1ozturkbe commented 5 years ago

Removing the encode fixes the problem. Sol'n in the savetxtfix branch.

bqpd commented 5 years ago

unfortunately that fix doesn't work for all cases, I'll add the tests to show this :-/

On Thu, Aug 15, 2019, 17:53 Berk Ozturk notifications@github.com wrote:

Removing the encode fixes the problem. Sol'n in the savetxtfix branch.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/convexengineering/gpkit/issues/1415?email_source=notifications&email_token=AALKAGBATGNVEELSJPXWRNTQEXF53A5CNFSM4IMCFHC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4NDI7A#issuecomment-521811068, or mute the thread https://github.com/notifications/unsubscribe-auth/AALKAGHA3WT5R5H5U4FS53TQEXF53ANCNFSM4IMCFHCQ .

1ozturkbe commented 5 years ago

Alright, you know best!

whoburg commented 5 years ago

unfortunately that fix doesn't work for all cases, I'll add the tests to show this :-/

woot woot re adding tests!

bqpd commented 5 years ago

Hmm, I can't reproduce this error; what model were you trying to save?

1ozturkbe commented 5 years ago

I have to clean up a lot of code before I can tell you where this occurred... I'm working on about 4 different branches of 4 different repos. I will get back soon.

bqpd commented 5 years ago

(weekly ping)

1ozturkbe commented 5 years ago

I can no longer replicate this, and don't have time to dig into it... I will reopen if it shows up.

1ozturkbe commented 5 years ago

I managed to reproduce this. Look at commit 43ac0c of turbofan, diffix branch. If you run engine_validation in py3, you get the following:

Solving took 14 GP solves and 2.16 seconds.
Checking for relaxed constants...
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
~\Dropbox (MIT)\MIT Graduate School\Code\turbofan\turbofan\engine_validation.py in <module>
    794     sol = m.localsolve(verbosity=2, x0=x0)
    795     post_process(sol)
--> 796     sol.savetxt()
    797
    798     #print out various percent differences in TSFC and engine areas

c:\users\berk\dropbox (mit)\mit graduate school\code\gpkit\gpkit\solution_array.py in savetxt(self, filename, printmodel, **kwargs)
    510         with open(filename, "w") as f:
    511             if printmodel and self.model:
--> 512                 f.write(str(self.model))
    513             f.write(self.table(**kwargs))
    514

c:\users\berk\appdata\local\programs\python\python37\lib\encodings\cp1252.py in encode(self, input, final)
     17 class IncrementalEncoder(codecs.IncrementalEncoder):
     18     def encode(self, input, final=False):
---> 19         return codecs.charmap_encode(input,self.errors,encoding_table)[0]
     20
     21 class IncrementalDecoder(codecs.IncrementalDecoder):

UnicodeEncodeError: 'charmap' codec can't encode character '\u03c0' in position 7048: character maps to <undefined>

Not the same as the original error, but I had seen something similar somewhere else.

bqpd commented 5 years ago

(note to self, it's actually commit https://github.com/convexengineering/turbofan/pull/22/commits/443ac0c6b0fb50628a7c9035638f368c85bf9ccf)

bqpd commented 5 years ago

Alack it runs fine on my computer.

bqpd commented 5 years ago

Let's try and debug this on your computer early next week.