Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/echoshader' into realtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Sohambutala committed Sep 4, 2024
2 parents f766946 + 31c1105 commit 2f14c9b
Show file tree
Hide file tree
Showing 4 changed files with 505 additions and 21 deletions.
73 changes: 56 additions & 17 deletions echodataflow/extensions/panel_app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import io
import matplotlib
import panel as pn
import param
import diskcache as dc
import holoviews as hv
import xarray as xr
import echoshader
import echopype.colormap
from holoviews import opts

class EchogramPanel(param.Parameterized):
multi_freq = param.Parameter()
Expand All @@ -14,15 +17,15 @@ class EchogramPanel(param.Parameterized):

def __init__(self, **params):
super().__init__(**params)
self.multi_freq = pn.Row()
self.multi_freq = pn.Column()
self.tricolor = pn.Row()
self.track = pn.Column()
self.tile_select = pn.widgets.Select()

@param.depends('multi_freq', 'tricolor', 'track', 'tile_select')
def view(self):
return {
"multi_freq": pn.Row(self.multi_freq),
"multi_freq": pn.Column(self.multi_freq),
"tricolor": pn.Row(self.tricolor),
"track": pn.Column(self.tile_select, self.track),
}
Expand All @@ -32,39 +35,72 @@ def view(self):
# Initialize DiskCache
cache = dc.Cache('~/eshader_cache')

def extract_frequency(channel):
import re
match = re.search(r'ES(\d+)', channel)
return int(match.group(1)) if match else float('inf')

def update_panel_from_cache():
global panel_object
print("Updating panel from cache...")
ds_MVBS = None
try:
if panel_object is None:
panel_object = EchogramPanel()
else:
del panel_object
panel_object = EchogramPanel()
desired_order = [120, 38, 18]
ek500_cmap = matplotlib.colormaps["ep.ek500"]
clipping = {
'min': tuple(ek500_cmap.get_under()),
'max': tuple(ek500_cmap.get_over()),
'NaN': tuple(ek500_cmap.get_bad()),
}

zarr_path = cache.get('zarr_path')
channel_multi_freq = cache.get('channel_multi_freq')
channel_tricolor = cache.get('channel_tricolor')
print(channel_tricolor)
tile_select = cache.get('tile_select')

print(channel_multi_freq)
print(channel_tricolor)
print(tile_select)

print(f'Loading data from {zarr_path}')

print("tri")
if zarr_path is not None:
ds_MVBS = xr.open_zarr(zarr_path)

if ds_MVBS is not None and channel_multi_freq is not None:
egram_all = ds_MVBS.sel(echo_range=slice(None, 400)).eshader.echogram(
channel=channel_multi_freq,
vmin=-70,
vmax=-36,
cmap="viridis",
opts=hv.opts.Image(width=800)
)
panel_object.multi_freq[:] = [pn.pane.HoloViews(egram_all)]
ds_MVBS = xr.open_zarr(zarr_path).compute()

if ds_MVBS is not None and channel_tricolor is not None:
tricolor = ds_MVBS.sel(echo_range=slice(None, 400)).eshader.echogram(
channel_tricolor = sorted(channel_tricolor, key=lambda x: desired_order.index(extract_frequency(x)))
print(channel_tricolor)
tricolor = ds_MVBS.eshader.echogram(
channel=channel_tricolor,
vmin=-70,
vmax=-36,
rgb_composite=True,
opts=hv.opts.RGB(width=800)
opts=opts.RGB(width=800)
)
panel_object.tricolor[:] = [pn.pane.HoloViews(tricolor)]

print("Multi")
if ds_MVBS is not None and channel_multi_freq is not None:
egram_all = []
for ch in channel_tricolor:
egram = ds_MVBS.eshader.echogram(
channel=[ch],
vmin=-70,
vmax=-36,
cmap = "ep.ek500",
opts = opts.Image(clipping_colors=clipping, width=800),
)
egram_all.append(egram)
panel_object.multi_freq[:] = [pn.pane.HoloViews(e) for e in egram_all]

print("track")
if ds_MVBS is not None:
track = ds_MVBS.eshader.track(
tile='EsriOceanBase',
Expand All @@ -81,9 +117,12 @@ def update_panel_from_cache():
finally:
if ds_MVBS is not None:
ds_MVBS.close()
del ds_MVBS

# Periodically update the panel
pn.state.add_periodic_callback(update_panel_from_cache, 100000)
pn.state.add_periodic_callback(update_panel_from_cache, 138000)

update_panel_from_cache()

# Serve the Panel app
pn.serve(panel_object.view(), port=1800, websocket_origin="*", admin=True, show=True)
pn.serve(panel_object.view(), port=1800, websocket_origin="*", admin=True, show=False)
214 changes: 214 additions & 0 deletions echodataflow/extensions/panel_app_lasker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import matplotlib
import panel as pn
import param
import diskcache as dc
import holoviews as hv
import xarray as xr
import echoshader
from holoviews import opts
import random
import echopype.colormap
import asyncio

class EchogramPanel(param.Parameterized):
multi_freq = param.Parameter()
tricolor = param.Parameter()
track = param.Parameter()
tile_select = param.Parameter()

def __init__(self, **params):
super().__init__(**params)
self.multi_freq = pn.Column()
self.tricolor = pn.Row()
self.track = pn.Column()
self.tile_select = pn.widgets.Select()


@param.depends('multi_freq', 'tricolor', 'track', 'tile_select')
def view(self):
return {
"multi_freq": pn.Column(self.multi_freq),
"tricolor": pn.Row(self.tricolor),
"track": pn.Column(self.tile_select, self.track),
}

panel_object = EchogramPanel()



# Initialize DiskCache
cache = dc.Cache('~/eshader_cache')

zarr_path = cache.get('zarr_path')



def extract_frequency(channel):
import re
match = re.search(r'ES(\d+)', channel)
return int(match.group(1)) if match else float('inf')

channel_tricolor = cache.get('channel_tricolor')

desired_order = [120, 38, 18]

channel_tricolor = sorted(channel_tricolor, key=lambda x: desired_order.index(extract_frequency(x)))

# print(channel_tricolor)

channel_multi_freq = cache.get('channel_multi_freq')

desired_order = [18, 38, 120]

channel_multi_freq = sorted(channel_tricolor, key=lambda x: desired_order.index(extract_frequency(x)))


# print(channel_multi_freq)

ek500_cmap = matplotlib.colormaps["ep.ek500"]

clipping = {
'min': tuple(ek500_cmap.get_under()),
'max': tuple(ek500_cmap.get_over()),
'NaN': tuple(ek500_cmap.get_bad()),
}

from functools import partial

import numpy as np
import panel as pn

def update(ds_MVBS):
temp = xr.open_zarr(zarr_path).compute()
ds_MVBS["Sv"].data = temp["Sv"].data

print("Tri")
def tricolor_app():
ds_MVBS = xr.open_zarr(zarr_path).sel(echo_range=slice(None, 400)).compute()

tricolor = ds_MVBS.eshader.echogram(
channel=channel_tricolor,
vmin=-70,
vmax=-36,
rgb_composite=True,
opts=opts.RGB(width=800)
)

if ds_MVBS is not None and 'softmax' in list(ds_MVBS.keys()):
hv_ds = hv.Dataset(ds_MVBS["softmax"])
contours = hv.operation.contours(hv_ds.to(hv.Image, kdims=["ping_time", "echo_range"]), levels=[0.7, 0.8, 0.9])

if 'softmax' in list(ds_MVBS.keys()):

composed_plot = tricolor()*contours
composed_plot = composed_plot.opts(opts.Contours(cmap='kgy', tools=['hover'], legend_position="bottom_right"))
composed_plot = composed_plot.opts(xlabel='Ping Time')
composed_plot = composed_plot.opts(ylabel='Depth (m)')
plot = composed_plot
else:
tricolor = tricolor().opts(xlabel='Ping Time', ylabel='Depth (m)')
plot = tricolor

return plot

# panel_object.tricolor[:] = tricolor_app

update_button = pn.widgets.Button(name='Refresh data')

def update_plot(event=None):
return(tricolor_app())

view_tricolor = pn.Column(update_button,
pn.panel(pn.bind(update_plot, update_button), loading_indicator=True),
)



# multifrequency plot
print("Multi")
def multi_app():
ds_MVBS = xr.open_zarr(zarr_path).sel(echo_range=slice(None, 400)).compute()

if ds_MVBS is not None and 'softmax' in list(ds_MVBS.keys()):
hv_ds = hv.Dataset(ds_MVBS["softmax"])
contours = hv.operation.contours(hv_ds.to(hv.Image, kdims=["ping_time", "echo_range"]), levels=[0.7, 0.8, 0.9])

if ds_MVBS is not None and channel_multi_freq is not None:
egram = ds_MVBS.eshader.echogram(
channel=channel_multi_freq,
vmin=-70,
vmax=-36,
cmap = "ep.ek500",
opts = opts.Image(clipping_colors=clipping,
width=800,
xlim=(ds_MVBS.ping_time.min().values, ds_MVBS.ping_time.max().values),
xlabel='Ping Time'
)
)


if 'softmax' in list(ds_MVBS.keys()):

plot = egram()*contours.opts(opts.Contours(cmap='rainbow', tools=['hover'], legend_position="bottom_right"))

else:
plot = egram()
else:
print("no plot")

return plot

update_button = pn.widgets.Button(name='Refresh data')

def update_plot(event=None):
return(multi_app())

view_multi = pn.Column(update_button,
pn.panel(pn.bind(update_plot, update_button), loading_indicator=True),
)



# panel_object.multi_freq[:] = multi_app()


print("track")
def track_app():
ds_MVBS = xr.open_zarr(zarr_path).compute()

if ds_MVBS is not None:
track = ds_MVBS.eshader.track(
tile='EsriOceanBase',
opts=hv.opts.Path(width=600, height=350)
)
return track

update_button = pn.widgets.Button(name='Refresh data')

def update_plot(event=None):
return(track_app())

view_track = pn.Column(update_button,
pn.panel(pn.bind(update_plot, update_button), loading_indicator=True),
)


#panel_object.track[:] = [track_app()]


# print(panel_object.tile_select)
# if tile_select is not None:
# panel_object.tile_select.options = tile_select.options
# panel_object.tile_select.value = tile_select.value

#pn.serve({"tricolor": view}, port=1801, websocket_origin="*", admin=True, show=False)
pn.serve({"tricolor": view_tricolor, 'multi_freq': view_multi, 'track':view_track}, port=1800, websocket_origin="*", admin=True, show=False)


def all_app():
ds_MVBS = xr.open_zarr(zarr_path).compute()
d = {"tricolor": pn.Row(tricolor_app)}
return(d)



Loading

0 comments on commit 2f14c9b

Please sign in to comment.