# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# # In Class Notebook, Week 14

# You can always paste the URL of this notebook (https://github.com/UIUC-iSchool-DataViz/is445_spring2022/blob/master/week14/inClass_week14.ipynb ) into the nbviewer interface for a plain-text rendering:
#
# https://kokes.github.io/nbviewer.js/viewer.html

import numpy as np
import pandas as pd
import ipyvolume

x,y,z = np.random.random((3,100))

ipyvolume.quickscatter(x,y,z,size=1, marker='sphere')

from sys import path

path

path.append('/Users/jnaiman/Downloads/')
path

from solverlibs import read_in_galaxy_data

galaxy_file ='/Users/jnaiman/Downloads/galaxyFiles/outarrsnap_001_fac1n3.txt'
t_h, r_h, v_h, e_h, N, part_type = read_in_galaxy_data(galaxy_file)
# t_h = time
# r_h = x,y,z positional vector for each timestep and each particle
# v_h = vx,vy,vz for each particle, each timestep
# e_h = energy (physics element)
# N = number of particles
# part_type = type of particle

r_h.shape

N

np.unique(part_type)

# +
tstep = 50

# r_h [particles, x/y/z, time step out of 500]
x,y,z = r_h[:,0,tstep], r_h[:,1,tstep], r_h[:,2,tstep]
# -

ipyvolume.quickscatter(x,y,z,size=1,marker='sphere')

# +
# ipyvolume.quiver?
# -

ipyvolume.figure()
for i in range(r_h.shape[0]): # loop over all particles
    ipyvolume.quiver(r_h[i,0,:250], # ith particle, x position, up to 100 time steps
                    r_h[i,1,:250], # ith particle, y position, up to 100 time steps
                    r_h[i,2,:250], # ith particle, z position, up to 100 time steps
                    v_h[i,0,:250], # ith particle, velocity in the x-direction, up to 100 timesteps
                    v_h[i,1,:250], # ith particle, vy, up to 100
                    v_h[i,2,:250], size=1) # ith particle, vz, up to 100
ipyvolume.show()

v_h.shape

# +
step = 50 # plot every "step"th timestep
Nt = v_h.shape[2] # 500, or full, timesteps

ipyvolume.figure()
for i in range(r_h.shape[0]): # loop over all particles
    ipyvolume.quiver(r_h[i,0,0:Nt:step], # ith particle, x position, 0 to Nt (500 timesteps) in steps of "step"
                    r_h[i,1,0:Nt:step], # ith particle, y position, 0 to Nt (500 timesteps) in steps of "step"
                    r_h[i,2,0:Nt:step], # ith particle, z position, 0 to Nt (500 timesteps) in steps of "step"
                    v_h[i,0,0:Nt:step], # ith particle, velocity in the x-direction, 0 to Nt (500 timesteps) in steps of "step"
                    v_h[i,1,0:Nt:step], # ith particle, vy, 0 to Nt (500 timesteps) in steps of "step"
                    v_h[i,2,0:Nt:step], size=1) # ith particle, vz, 0 to Nt (500 timesteps) in steps of "step"
ipyvolume.show()
# -

# ## 3d animations with ipyvolume

# all particles, x position, all timesteps
r_h[:,0,:].shape

r_h[:,0,:].T.shape # 'swaps' the number of particles and timesteps (Transpose of a matrix)

# +
step = 10 # only plot with every 10th time step

r = r_h[:,:,0:Nt:step] # all particles, x/y/z, subsampled 
v = v_h[:,:,0:Nt:step]

# r = r_h[:,:,Nt:0:-step] # all particles, x/y/z, subsampled 
# v = v_h[:,:,Nt:0:-step]
# -

r_h.shape, r.shape, r[:,0,:].T.shape

# +
ipyvolume.figure()

s = ipyvolume.scatter(r[:,0,:].T, r[:,1,:].T, r[:,2,:].T,
                     marker='sphere')

ani = ipyvolume.animation_control(s,interval=200) # animate our scatter plot with 200 milisec invervals

ipyvolume.show()
# -

# ## Adding widgets

ipyvolume.figure()
s = ipyvolume.scatter(r[:,0,25], r[:,1,25], r[:,2,25],marker='sphere', color='blue')
ipyvolume.show()

import ipywidgets

color = ipywidgets.ColorPicker()

color

s.color

color.value

ipywidgets.jslink((s,'color'),(color,'value'))

ipyvolume.figure()
s2 = ipyvolume.scatter(r[:,0,25], r[:,1,25], r[:,2,25],marker='sphere', color='blue')

ipywidgets.jslink((s2,'color'),(color,'value')) # linking

#ipywidgets.VBox([ipyvolume.gcc(),color])
ipywidgets.HBox([ipyvolume.gcc(),color])

s2.size

size = ipywidgets.FloatSlider(min=0,max=30, step=0.1)

size

ipywidgets.jslink((s2,'size'),(size,'value'))

controls = ipywidgets.VBox([color,size])
ipywidgets.HBox([ipyvolume.gcc(),controls])

myOutBox = ipywidgets.HBox([ipyvolume.gcc(),controls])

ipyvolume.embed.layout = myOutBox.children[1].layout # just trust me or try it without it!
ipyvolume.embed.layout.min_width = "400px"

ipyvolume.embed.embed_html("firstPage.html",myOutBox, offline=True, devmode=False)

# !open firstPage.html

# +
# for windows I *think* it is "start"
# -

# ## Widgets + 3d animation

step = 10 # animate every 10th data point
# position vector
r = r_h[:,:,0:Nt:step]

# +
ipyvolume.figure()

s3 = ipyvolume.scatter(r[:,0,:].T, r[:,1,:].T, r[:,2,:].T, marker='sphere')
# -

size3  = ipywidgets.FloatSlider(min=0,max=30,step=0.1, description='Particle Size')
color3 = ipywidgets.ColorPicker()

# linking widgets with scatter plot traits
ipywidgets.jslink((s3,'size'),(size3,'value'))
ipywidgets.jslink((s3,'color'),(color3,'value'))

ani = ipyvolume.animation_control(s3,interval=200)

ipywidgets.VBox([ipyvolume.gcc(),size3,color3])

# also save to a webpage
boxOut = ipywidgets.VBox([ipyvolume.gcc(),size3,color3])
ipyvolume.embed.layout = boxOut.children[1].layout
ipyvolume.embed.layout.min_width='400px'
ipyvolume.embed.embed_html("secondPage.html", boxOut, offline=True, devmode=False)

# !open secondPage.html

# better science dataset
galaxy_file_better = '/Users/jnaiman/Downloads/galaxyFiles/outarrsnap_001_fac1n2.txt'

t_h2, r_h2, v_h2, e_h2, N2, part_type2 = read_in_galaxy_data(galaxy_file_better)

r_h2.shape

step = 100 
step = 200

StarcoderdataJupyterScriptsDedupFiltered