Skip to content

Instantly share code, notes, and snippets.

@jbzdak
Created May 18, 2014 14:50
Show Gist options
  • Save jbzdak/e676dc8af17dbe233222 to your computer and use it in GitHub Desktop.
Save jbzdak/e676dc8af17dbe233222 to your computer and use it in GitHub Desktop.
Animate cascade to svg
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <codecell>
import numpy as np
import requests
import svgwrite
from svgwrite import mm
from IPython.display import SVG
# <markdowncell>
# Load data
# <codecell>
response = requests.get("https://s3.amazonaws.com/public-bucket-vzi0CDPFDE4ywJfvfylfpnDW/single-event")
# <codecell>
dtype = np.dtype([
("event_id", np.uint32),
('point', np.dtype("3float32")),
('energy', np.float32),
('particle_kinetic_energy', np.float32),
('timestamp_nanosecond', np.float32),
])
# <codecell>
event = np.fromstring(response.content, dtype=dtype)
# <headingcell level=1>
# Prepare picture
# <codecell>
dwg = svgwrite.Drawing(filename="/tmp/show-test.svg", debug=False)
dwg.viewbox(-40, -80, 2000, 400)
# <markdowncell>
# Background white fill
# <codecell>
max = np.max(event['point'], axis=0)*40
min = np.min(event['point'], axis=0)*40
rect = dwg.rect(insert=(int(min[2]), int(min[0])), size=(int(max[2]-min[2]), int(max[0]-min[0])))
rect['fill']='white'
dwg.add(rect)
# <codecell>
marker = dwg.marker(insert=(1, 5), size=(6, 6), orient="auto")
marker.viewbox(width=10, height=10)
path = dwg.path(d="M 0 0 L 10 5 L 0 10 z")
path.stroke(color="black", opacity=0.5)
path.fill(color="black", opacity=0.5)
marker.add(path)
dwg.defs.add(marker)
# <markdowncell>
# Draw axis
# <codecell>
line = dwg.line(start=(0, 0), end=(1000, 0))
line.stroke(color="black", width=0.2*mm, opacity=0.5)
line['marker-end'] = marker.get_funciri()
dwg.add(line)
for ii in range(50, 1000, 50):
line = dwg.line(start=(ii, -5), end=(ii, 5))
line.stroke(color="black", width=0.2*mm, opacity=0.5)
dwg.add(line)
dwg.add(dwg.text("Longitudinal axis", insert=(0, -10)))
line = dwg.line(start=(0, 50), end=(50, 50))
line.stroke(color="black", width=0.2*mm, opacity=0.5)
dwg.add(line)
line = dwg.line(start=(0, 45), end=(0, 55))
line.stroke(color="black", width=0.2*mm, opacity=0.5)
dwg.add(line)
line = dwg.line(start=(50, 45), end=(50, 55))
line.stroke(color="black", width=0.2*mm, opacity=0.5)
dwg.add(line)
#text= dwg.text("z", insert=(53, 80))
#dwg.add(text)
text= dwg.text("0", insert=(0, 70))
dwg.add(text)
text= dwg.text("50mm", insert=(53, 70))
dwg.add(text)
dwg.add(dwg.text("0", insert=(0, 15)))
# <codecell>
# def f(double):
# return "{:.2g}".format(double)
# <codecell>
g = dwg.g(id='scored', fill='red', )
g['fill-opacity']=0
g['stroke-opacity']=0
scored = dwg.add(g)
for e in event:
circle = dwg.circle(center=(e['point'][2]*40.0*mm, e['point'][0]*40.0*mm), r=.2*mm)
set_attr = svgwrite.animate.Set(debug=False)
set_attr['to']=1
set_attr.set_target("fill-opacity", "XML")
set_attr.set_timing(begin="{:2g}s".format(e['timestamp_nanosecond']*10+5))
circle.add(set_attr)
scored.add(circle)
# <codecell>
#SVG(data=dwg.tostring())
# <codecell>
dwg.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment