Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
visualizer-embed
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Owen Graham
visualizer-embed
Commits
668ac649
Verified
Commit
668ac649
authored
May 23, 2022
by
Owen Graham
Browse files
Options
Downloads
Patches
Plain Diff
Reorder Python functions
parent
2130f4fb
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
visualizer.py
+74
-74
74 additions, 74 deletions
visualizer.py
with
74 additions
and
74 deletions
visualizer.py
+
74
−
74
View file @
668ac649
...
...
@@ -106,12 +106,6 @@ def boxplot():
return
render_template
(
'
boxplot.html
'
)
def
read_stations
():
"""
Read amrdcrecords.json.
"""
with
open
(
'
amrdcrecords.json
'
)
as
f
:
return
json
.
load
(
f
)
@app.route
(
'
/record/years
'
)
def
record_years
():
"""
Return a list of years for the selected station.
"""
...
...
@@ -120,54 +114,12 @@ def record_years():
return
jsonify
(
years
)
@app.route
(
'
/plot/boxplot
'
)
def
plot_boxplot
():
"""
Boxplot one station/measurement @ multiple years.
"""
station_id
=
get_param
(
'
station
'
)
year1
=
get_param
(
'
year1
'
,
to
=
year_type
)
year2
=
get_param
(
'
year2
'
,
to
=
year_type
)
meas
=
get_param
(
'
measurement
'
,
to
=
meas_type
)
station
=
get_station
(
station_id
)
plot_data
=
[]
start_year
,
end_year
=
sorted
([
year1
,
year2
])
years
=
range
(
start_year
,
end_year
+
1
)
maximum
=
minimum
=
None
def
at_index
(
row
):
"""
Get the selected field of `row`.
"""
return
row
[
meas
.
field
]
for
year
in
years
:
data
=
read_data
(
station
,
year
)
selected_data
=
data
[:,
meas
.
field
]
plot_data
.
append
(
selected_data
)
year_max
=
max
(
data
,
key
=
at_index
)
year_min
=
min
(
data
,
key
=
at_index
)
if
maximum
is
None
:
maximum
,
minimum
=
year_max
,
year_min
else
:
maximum
=
max
(
maximum
,
year_max
,
key
=
at_index
)
minimum
=
min
(
minimum
,
year_min
,
key
=
at_index
)
fig
,
axes
=
plt
.
subplots
()
fig
.
set_figheight
(
6
)
fig
.
set_figwidth
(
12
)
axes
.
boxplot
(
plot_data
,
positions
=
years
)
axes
.
set_ylabel
(
meas
.
title
)
axes
.
grid
(
True
)
axes
.
set_title
(
(
f
'
Max
{
meas
.
title
}
:
{
maximum
[
meas
.
field
]
}
, Date: (
{
maximum
[
0
]
}
).
'
f
'
Min
{
meas
.
title
}
:
{
minimum
[
meas
.
field
]
}
, Date: (
{
minimum
[
0
]
}
).
'
),
fontsize
=
'
small
'
,
)
name
=
station
[
'
name
'
]
plt
.
suptitle
(
f
'
{
meas
.
title
}
measurements,
{
name
}
Station,
'
f
'
{
start_year
}
-
{
end_year
}
.
'
)
return
savefig_response
(
fig
)
@app.route
(
'
/record/link
'
)
def
record_link
():
"""
Return the source link for the selected dataset.
"""
station
=
get_station
(
get_param
(
'
station
'
))
year
=
get_param
(
'
year
'
,
to
=
year_type
)
return
jsonify
(
get_link
(
station
,
year
))
@app.route
(
'
/plot
'
)
...
...
@@ -261,11 +213,54 @@ def plot_overlay():
return
savefig_response
(
fig
)
def
savefig_response
(
fig
):
"""
Make an image response with `fig.savefig()`.
"""
buf
=
BytesIO
()
fig
.
savefig
(
buf
,
format
=
'
png
'
)
return
Response
(
buf
.
getvalue
(),
mimetype
=
'
image/png
'
)
@app.route
(
'
/plot/boxplot
'
)
def
plot_boxplot
():
"""
Boxplot one station/measurement @ multiple years.
"""
station_id
=
get_param
(
'
station
'
)
year1
=
get_param
(
'
year1
'
,
to
=
year_type
)
year2
=
get_param
(
'
year2
'
,
to
=
year_type
)
meas
=
get_param
(
'
measurement
'
,
to
=
meas_type
)
station
=
get_station
(
station_id
)
plot_data
=
[]
start_year
,
end_year
=
sorted
([
year1
,
year2
])
years
=
range
(
start_year
,
end_year
+
1
)
maximum
=
minimum
=
None
def
at_index
(
row
):
"""
Get the selected field of `row`.
"""
return
row
[
meas
.
field
]
for
year
in
years
:
data
=
read_data
(
station
,
year
)
selected_data
=
data
[:,
meas
.
field
]
plot_data
.
append
(
selected_data
)
year_max
=
max
(
data
,
key
=
at_index
)
year_min
=
min
(
data
,
key
=
at_index
)
if
maximum
is
None
:
maximum
,
minimum
=
year_max
,
year_min
else
:
maximum
=
max
(
maximum
,
year_max
,
key
=
at_index
)
minimum
=
min
(
minimum
,
year_min
,
key
=
at_index
)
fig
,
axes
=
plt
.
subplots
()
fig
.
set_figheight
(
6
)
fig
.
set_figwidth
(
12
)
axes
.
boxplot
(
plot_data
,
positions
=
years
)
axes
.
set_ylabel
(
meas
.
title
)
axes
.
grid
(
True
)
axes
.
set_title
(
(
f
'
Max
{
meas
.
title
}
:
{
maximum
[
meas
.
field
]
}
, Date: (
{
maximum
[
0
]
}
).
'
f
'
Min
{
meas
.
title
}
:
{
minimum
[
meas
.
field
]
}
, Date: (
{
minimum
[
0
]
}
).
'
),
fontsize
=
'
small
'
,
)
name
=
station
[
'
name
'
]
plt
.
suptitle
(
f
'
{
meas
.
title
}
measurements,
{
name
}
Station,
'
f
'
{
start_year
}
-
{
end_year
}
.
'
)
return
savefig_response
(
fig
)
def
get_param
(
key
,
to
=
str
):
...
...
@@ -299,23 +294,17 @@ def year_type(s):
raise
ValueError
(
f
'
bad year arg:
{
s
!r}
'
)
def
get_link
(
station
,
year
):
"""
Get the link to a dataset.
Calls `abort(404)` if none is found.
"""
for
record
in
station
[
'
records
'
]:
if
record
[
'
year
'
]
==
year
:
return
record
[
'
url
'
]
abort
(
404
)
def
savefig_response
(
fig
):
"""
Make an image response with `fig.savefig()`.
"""
buf
=
BytesIO
()
fig
.
savefig
(
buf
,
format
=
'
png
'
)
return
Response
(
buf
.
getvalue
(),
mimetype
=
'
image/png
'
)
@app.route
(
'
/record/link
'
)
def
record_link
():
"""
Return the source link for the selected dataset.
"""
station
=
get_station
(
get_param
(
'
station
'
))
year
=
get_param
(
'
year
'
,
to
=
year_type
)
return
jsonify
(
get_link
(
station
,
year
))
def
read_stations
():
"""
Read amrdcrecords.json.
"""
with
open
(
'
amrdcrecords.json
'
)
as
f
:
return
json
.
load
(
f
)
def
get_station
(
station_id
,
stations
=
None
):
...
...
@@ -330,6 +319,17 @@ def get_station(station_id, stations=None):
abort
(
404
)
def
get_link
(
station
,
year
):
"""
Get the link to a dataset.
Calls `abort(404)` if none is found.
"""
for
record
in
station
[
'
records
'
]:
if
record
[
'
year
'
]
==
year
:
return
record
[
'
url
'
]
abort
(
404
)
def
get_resources
(
link
):
"""
Fetch the download links for a dataset.
"""
doc
=
jsonld
.
flatten
(
link
+
'
.jsonld
'
)
...
...
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment