Last active
June 10, 2019 22:46
-
-
Save rlabbe/91409a59d4dfe038fb7ade6d8eebedb5 to your computer and use it in GitHub Desktop.
Make spyder act like I want
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rom subprocess import call, Popen | |
import sys | |
import matplotlib as mpl | |
import numpy | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
def npp(precision=4): | |
np.set_printoptions( | |
precision=precision, | |
threshold=1000, | |
suppress=True, | |
linewidth=120, | |
formatter={'float_kind': '{:f}'.format}) | |
def ca(): | |
plt.close('all') | |
def ppa(x, digits=3): | |
""" | |
Pretty print numpy array with the given floating point | |
precision | |
""" | |
np.savetxt(sys.stdout, x, f'%.{digits}f') | |
def rec_text(data, | |
filename, | |
program='c:\\program files\\sublime text 3\\subl.exe'): | |
with open(filename, 'w') as f: | |
pd.DataFrame.from_records(data).to_string(f) | |
if program is not None: | |
call([program, filename]) | |
def subl(data=None, ff: str='%.3f', name="c:\\tmp\\subl.txt"): | |
""" | |
convert numpy array into columned text file and open in sublime. | |
If data is a string, then it assumes it is a file name and opens | |
the file directly. | |
""" | |
if type(data) == str: | |
osubl(data) | |
if data is not None: | |
with open(name, 'w') as f: | |
pd.DataFrame.from_records(data).to_string(f, float_format=lambda x: ff % x) | |
call(['c:\\program files\\sublime text 3\\subl.exe', name]) | |
else: | |
call(['c:\\program files\\sublime text 3\\subl.exe']) | |
def osubl(name): | |
""" | |
open file in sublime | |
""" | |
call(['c:\\program files\\sublime text 3\\subl.exe', name]) | |
def excel(data, name='c:\\tmp\\excel.csv'): | |
""" convert numpy array into csv and open in Excel""" | |
with open(name, 'w') as f: | |
pd.DataFrame.from_records(data).to_csv(f) | |
Popen([r'C:\Program Files\Microsoft Office\Office15\EXCEL.EXE', name]) | |
def arr(s): | |
""" Use the matlab string method to create a 2d array. Works for ints | |
and floats only. dtype will be float if any value is a float, otherwise | |
dtype will be int. | |
Examples | |
-------- | |
>>> arr('1 2 3; 4 5 6') | |
array([[1, 2, 3], | |
[4, 5, 6]]) | |
>>> arr('3.') | |
array([[3.]] | |
""" | |
def num(x): | |
try: | |
return int(x) | |
except ValueError: | |
return float(x) | |
rows = s.split(';') | |
d = [[num(f) for f in r.lstrip().rstrip().split(' ')] for r in rows] | |
return np.array(d) | |
def pprec(rec: numpy.record) -> None: | |
""" | |
Pretty print a numpy record - each value gets printed as | |
key: value | |
on a separate line. | |
Input | |
----- | |
rec : numpy.record | |
""" | |
print(rec.pprint()) | |
# no exponentials in axis, I find it unreadable | |
mpl.rcParams['axes.formatter.useoffset'] = False | |
npp(4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment