forked from spyder-ide/spyder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscientific_startup.py
153 lines (129 loc) · 4.71 KB
/
scientific_startup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
#
# Copyright © 2011 Pierre Raybaut
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)
"""
Scientific Python startup script
Requires NumPy, SciPy and Matplotlib
"""
# Need a temporary print function that is Python version agnostic.
import sys
def exec_print(string="", end_space=False):
if sys.version[0] == '2':
if end_space:
exec("print '" + string + "',")
else:
exec("print '" + string + "'")
else:
if end_space:
exec("print('" + string + "', end=' ')")
else:
exec("print('" + string + "')")
__has_numpy = True
__has_scipy = True
__has_matplotlib = True
#==============================================================================
# Pollute the namespace but also provide MATLAB-like experience
#==============================================================================
try:
from pylab import * #analysis:ignore
# Enable Matplotlib's interactive mode:
ion()
except ImportError:
pass
# Import modules following official guidelines:
try:
import numpy as np
except ImportError:
__has_numpy = False
try:
import scipy as sp
except ImportError:
__has_scipy = False
try:
import matplotlib as mpl
import matplotlib.pyplot as plt #analysis:ignore
except ImportError:
__has_matplotlib = False
#==============================================================================
# Print what modules have been imported
#==============================================================================
__imports = ""
if __has_numpy:
__imports += "Imported NumPy %s" % np.__version__
if __has_scipy:
__imports += ", SciPy %s" % sp.__version__
if __has_matplotlib:
__imports += ", Matplotlib %s" % mpl.__version__
exec_print("")
if __imports:
exec_print(__imports)
import os
if os.environ.get('QT_API') != 'pyside':
try:
import guiqwt
import guiqwt.pyplot as plt_
import guidata
plt_.ion()
exec_print("+ guidata %s, guiqwt %s" % (guidata.__version__,
guiqwt.__version__))
except (ImportError, AssertionError):
exec_print()
#==============================================================================
# Add help about the "scientific" command
#==============================================================================
def setscientific():
"""Set 'scientific' in __builtin__"""
infos = ""
if __has_numpy:
infos += """
This is a standard Python interpreter with preloaded tools for scientific
computing and visualization. It tries to import the following modules:
>>> import numpy as np # NumPy (multidimensional arrays, linear algebra, ...)"""
if __has_scipy:
infos += """
>>> import scipy as sp # SciPy (signal and image processing library)"""
if __has_matplotlib:
infos += """
>>> import matplotlib as mpl # Matplotlib (2D/3D plotting library)
>>> import matplotlib.pyplot as plt # Matplotlib's pyplot: MATLAB-like syntax
>>> from pylab import * # Matplotlib's pylab interface
>>> ion() # Turned on Matplotlib's interactive mode"""
try:
import guiqwt #analysis:ignore
infos += """
>>> import guidata # GUI generation for easy dataset editing and display
>>> import guiqwt # Efficient 2D data-plotting features
>>> import guiqwt.pyplot as plt_ # guiqwt's pyplot: MATLAB-like syntax
>>> plt_.ion() # Turned on guiqwt's interactive mode"""
except ImportError:
pass
if __has_numpy:
infos += "\n"
infos += """
Within Spyder, this interpreter also provides:
* special commands (e.g. %ls, %cd, %pwd, %clear)
- %ls: List files in the current directory
- %cd dir: Change to directory dir
- %pwd: Show current directory
- %clear x: Remove variable x from namespace
"""
try:
# Python 2
import __builtin__ as builtins
except ImportError:
# Python 3
import builtins
try:
from site import _Printer
except ImportError:
# Python 3.4
from _sitebuiltins import _Printer
builtins.scientific = _Printer("scientific", infos)
setscientific()
exec_print('Type "scientific" for more details.')
#==============================================================================
# Delete temp vars
#==============================================================================
del setscientific, __has_numpy, __has_scipy, __has_matplotlib, __imports, exec_print