Skip to content

Commit a36de2b

Browse files
committed
julia init in math_science stuff
1 parent d029598 commit a36de2b

File tree

4 files changed

+450
-0
lines changed

4 files changed

+450
-0
lines changed

notebooks/math_science/NonlinearDynamics.ipynb

+186
Large diffs are not rendered by default.

notebooks/math_science/Project.toml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name = "MathScience"
2+
3+
[deps]
4+
Agents = "46ada45e-f475-11e8-01d0-f70cc89e6671"
5+
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
6+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
7+
ChaosTools = "608a59af-f2a3-5ad4-90b4-758bdf3122a7"
8+
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
9+
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
10+
DelayDiffEq = "bcd4f6db-9728-5f36-b5f7-82caef46ccdb"
11+
DelayEmbeddings = "5732040d-69e3-5649-938a-b6b4f237613f"
12+
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
13+
Dierckx = "39dd38d3-220a-591b-8e3c-4c3a8c710a94"
14+
DiffEqCallbacks = "459566f4-90b8-5000-8ac3-15dfb0a30def"
15+
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
16+
DrWatson = "634d3b9d-ee7a-5ddf-bec9-22491ea816e1"
17+
DynamicalBilliards = "4986ee89-4ee5-5cef-b6b8-e49ba721d7a5"
18+
DynamicalSystems = "61744808-ddfa-5f27-97ff-6e42cc95d634"
19+
DynamicalSystemsBase = "6e36e845-645a-534a-86f2-f5d4aa5a06b4"
20+
Entropies = "ed8fcbec-b94c-44b6-89df-898894ad9591"
21+
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
22+
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
23+
GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a"
24+
GraphMakie = "1ecd5474-83a3-4783-bb4f-06765db800d2"
25+
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
26+
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
27+
InteractiveDynamics = "ec714cd0-5f51-11eb-0b6e-452e7367ff84"
28+
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
29+
JLD = "4138dd39-2aa7-5051-a626-17a0bb65d9c8"
30+
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
31+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
32+
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
33+
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
34+
Neighborhood = "645ca80c-8b79-4109-87ea-e1f58159d116"
35+
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
36+
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
37+
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
38+
PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee"
39+
RecurrenceAnalysis = "639c3291-70d9-5ea2-8c5b-839eba1ee399"
40+
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
41+
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"
42+
Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc"
43+
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
44+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
45+
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
46+
TerminalLoggers = "5d786b92-1e48-4d6f-9151-6b4477ca9bed"
47+
TimeseriesSurrogates = "c804724b-8c18-5caa-8579-6025a0767c70"
48+
TransferEntropy = "ea221983-52f3-5440-99c7-13ea201cd633"
49+
50+
[compat]
51+
Agents = "5"
52+
ChaosTools = "~2.9.0"
53+
DelayEmbeddings = "~2.4"
54+
DynamicalSystemsBase = "~2.8"
55+
Entropies = "~1.1"
56+
RecurrenceAnalysis = "~1.8"
57+
julia = "1.5"

notebooks/math_science/colorscheme.jl

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
struct CyclicContainer <: AbstractVector{String}
2+
c::Vector{String}
3+
n::Int
4+
end
5+
CyclicContainer(c) = CyclicContainer(c, 0)
6+
7+
Base.length(c::CyclicContainer) = length(c.c)
8+
Base.size(c::CyclicContainer) = size(c.c)
9+
Base.getindex(c::CyclicContainer, i) = c.c[mod1(i, length(c.c))]
10+
function Base.getindex(c::CyclicContainer)
11+
c.n += 1
12+
c[c.n]
13+
end
14+
Base.iterate(c::CyclicContainer, i = 1) = iterate(c.c, i)
15+
16+
COLORS = [
17+
"#6D44D0",
18+
"#2CB3BF",
19+
"#1B1B1B",
20+
"#DA5210",
21+
"#866373",
22+
"#03502A"
23+
]
24+
25+
COLORSCHEME = COLORS
26+
27+
CCOLORS = CyclicContainer(COLORS)
28+
NONBLACK = CyclicContainer(COLORS[[1,2,4,5]])
29+
BLACK = COLORS[3]
30+
31+
LINESTYLES = CyclicContainer(["-", "--", "-.", ":",])
32+
33+
figx = 16 # default width correspoding to full text width, in inches
34+
figy = 5 # default height corresponding to 1 row of plots, in inches
35+
36+
export COLORSCHEME, COLORS, CCOLORS, LINESTYLES, figx, figy

notebooks/math_science/style.jl

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
include("colorscheme.jl")
2+
using PyPlot, DrWatson
3+
using3D()
4+
export rdspl, axis_zoomin!, add_identifiers!, nice_arrow!, figx, figy
5+
6+
DrWatson._wsave(s, fig::Figure) = fig.savefig(s, dpi = 600, transparent = false)
7+
8+
"""
9+
rdspl(x, n = 3)
10+
Round `x` with `n` sigdigits for display purposes.
11+
"""
12+
rdspl(x::Real, n = 3) = round(x, sigdigits=n)
13+
rdspl(x::AbstractVector, n = 3) = Tuple((round.(Float64.(x); sigdigits=n)))
14+
15+
PyPlot.rc("lines", lw = 3)
16+
PyPlot.rc("errorbar", capsize = 6)
17+
PyPlot.rc("axes", grid = true)
18+
PyPlot.rc("grid", color = "0.75", alpha = 0.75)
19+
PyPlot.rc("axes", axisbelow=true) # makes grid behind other plotting elements
20+
21+
MATHFONTSIZE = 34
22+
PyPlot.rc("font", size = 28) # set default fontsize
23+
PyPlot.rc("xtick", labelsize = 24)
24+
PyPlot.rc("ytick", labelsize = 24)
25+
PyPlot.rc("axes", labelsize = MATHFONTSIZE)
26+
PyPlot.rc("legend", fontsize = 30, handlelength = 1)
27+
# PyPlot.rc("font", family = "Times New Roman") # Serif main font
28+
PyPlot.rc("font", family = "DejaVu Sans") # sans main font
29+
# PyPlot.rc("mathtext", rm = "sanserif", fontset="dejavusans") # sans math font
30+
PyPlot.rc("mathtext", rm = "serif", fontset="cm") # serif math font
31+
32+
for z in ("x", "y")
33+
PyPlot.rc("$(z)tick.major", size = 7, width = 1.2)
34+
PyPlot.rc("$(z)tick.minor", size = 3, visible = false)
35+
end
36+
37+
PyPlot.rc("figure", figsize = (figx, figy))
38+
PyPlot.rc("savefig", dpi = 600, transparent = true, format = "png")
39+
40+
# set default color cycle
41+
PyPlot.rc("axes", prop_cycle = matplotlib.cycler(color=COLORS))
42+
43+
if false # test font
44+
figure(figsize=(10,8)); plot(rand(10), label = "\$a=\\int_0^\\infty xdx\$")
45+
xlabel("x label"); ylabel("\$H_2\$"); legend(); tight_layout()
46+
end
47+
48+
if false # test color scheme
49+
fig = figure(figsize = (20, 15)) # show colors
50+
ax1 = subplot(231)
51+
ax2 = subplot(232)
52+
ax3 = subplot(233)
53+
ax4 = subplot(223)
54+
ax5 = subplot(224)
55+
lw = 60
56+
L = length(COLORSCHEME)
57+
for (i, c) in enumerate(COLORS)
58+
chsv = matplotlib.colors.rgb_to_hsv(matplotlib.colors.to_rgb(c))
59+
ax1.plot([0, 1], [0, 0] .+ i, color = c, lw = lw)
60+
ax1.set_title("color")
61+
ax2.plot([0, 1], [0, 0] .+ i, color = string(chsv[3]), lw = lw)
62+
ax2.set_title("brightness")
63+
ax3.plot([0, 1], [0, 0] .+ i, color = string(chsv[2]), lw = lw)
64+
ax3.set_title("saturation")
65+
x = 0:0.05:5π
66+
ax4.plot(x, rand(1:3) .* cos.(x .+ i/2) .+ rand(length(x))/5; color=c, lw = 2)
67+
ax5.bar(collect(1:4) .+ (i-1)/L, 0.5rand(4) .+ 0.5, 1/L; color=c)
68+
end
69+
fig = tight_layout()
70+
end
71+
72+
bbox = Dict(:boxstyle => "round,pad=0.3", :facecolor=>"white", :alpha => 1.0)
73+
74+
"`add_identifiers!(fig = gcf(), axs = fig.get_axes(); xloc, yloc)`"
75+
function add_identifiers!(fig = gcf(), axs = fig.get_axes();
76+
xloc = 0.975, yloc = 1
77+
)
78+
for (i, ax) in enumerate(axs)
79+
l = collect('a':'z')[i]
80+
if ax.name "3d"
81+
ax.text(xloc, yloc, "$(l)"; transform = ax.transAxes,
82+
bbox = bbox, zorder = 99, va = "top")
83+
end
84+
end
85+
end
86+
87+
"""
88+
nice_arrow!(ax, xc, yc, xspan, yspan;
89+
style = "<->", tex = "", xo = 0.2, yo = -0.2, color = "C0"
90+
)
91+
"""
92+
function nice_arrow!(ax, xc, yc, xspan, yspan;
93+
style = "<->", tex = "", xo = 0.2, yo = -0.2, color = "C0")
94+
95+
ax.annotate("";
96+
xy=(xc-xspan/2, yc - yspan/2), xycoords="data",
97+
xytext=(xc+xspan/2, yc + yspan/2), textcoords="data",
98+
arrowprops = Dict(
99+
:arrowstyle=>style,
100+
:connectionstyle=>"arc3",
101+
:lw=>1.5, :color => color),
102+
zorder = 99, color
103+
)
104+
if tex != ""
105+
ax.text(xc + xo, yc + yo, tex; va = :center, color)
106+
end
107+
end
108+
109+
"""
110+
axis_zoomin!(zoomin, origin, zbox, rbox, co = "C0"; kwargs...)
111+
Create a zoomin box connecting two axes, the `zoomin` and `origin`.
112+
The zoom-in box is in the `origin` axis, while the `zoomin` axis is the
113+
box. `rbox` is the enclosing box of the `zoomin` axes, while `zbox`
114+
is the small "zoom-in" box of the `origin` axis. They must be in the form
115+
((x1, y1), (x2, y2)). `co` is color `α` the alpha setting.
116+
"""
117+
function axis_zoomin!(zoomin, origin, zbox, rbox, co = "C0";
118+
connect_lines = true, lw = 2.0, α = 1.0, dir = :right, set_lims::Bool=true)
119+
# plot box in zoomin axis
120+
line, = zoomin.plot(
121+
[rbox[1][1], rbox[2][1], rbox[2][1], rbox[1][1], rbox[1][1]],
122+
[rbox[1][2], rbox[1][2], rbox[2][2], rbox[2][2], rbox[1][2]],
123+
color=co, lw = lw, alpha = α
124+
)
125+
line.set_clip_on(false)
126+
# plot box in origin axis
127+
line, = origin.plot(
128+
[zbox[1][1], zbox[2][1], zbox[2][1], zbox[1][1], zbox[1][1]],
129+
[zbox[1][2], zbox[1][2], zbox[2][2], zbox[2][2], zbox[1][2]],
130+
color=co, lw = lw, alpha = α
131+
)
132+
line.set_clip_on(false)
133+
134+
# xyA is zoomin axis, xyB is origin axis
135+
if connect_lines
136+
if dir == :right
137+
for e in 1:2
138+
con = matplotlib.patches.ConnectionPatch(
139+
xyA = (rbox[1][1], rbox[e][2]), xyB=(zbox[2][1], zbox[e][2]),
140+
coordsA="data", coordsB="data",
141+
axesA = zoomin, axesB=origin, color=co, lw = lw, alpha = α)
142+
con.set_clip_on(false)
143+
zoomin.add_artist(con)
144+
end
145+
elseif dir == :top
146+
for e in 1:2
147+
con = matplotlib.patches.ConnectionPatch(
148+
xyA = (rbox[e][1], rbox[1][2]), xyB=(zbox[e][1], zbox[2][2]),
149+
coordsA="data", coordsB="data",
150+
axesA = zoomin, axesB=origin, color=co, lw = lw, alpha = α)
151+
con.set_clip_on(false)
152+
zoomin.add_artist(con)
153+
end
154+
end
155+
end
156+
# Set limits for zoom axis
157+
if set_lims
158+
zoomin.set_xlim(zbox[1][1], zbox[2][1])
159+
zoomin.set_ylim(zbox[1][2], zbox[2][2])
160+
end
161+
# remove axis for zoomin
162+
zoomin.axis("off")
163+
end
164+
165+
function darken_color(color, amount=0.5)
166+
c = matplotlib.colors.to_rgb(color)
167+
c = matplotlib.colors.rgb_to_hsv(c)
168+
c = (c[1], c[2], clamp(amount*c[3], 0, 1))
169+
return matplotlib.colors.hsv_to_rgb(c)
170+
end
171+

0 commit comments

Comments
 (0)