Skip to content

Commit

Permalink
feat: added visualizer to verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPatrie committed Aug 1, 2024
1 parent c53e4e2 commit 1b98524
Show file tree
Hide file tree
Showing 3 changed files with 327 additions and 60 deletions.
16 changes: 16 additions & 0 deletions bio_check/processing_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import numpy as np


def generate_color_gradient(self, simulator_names) -> list[str]:
"""Generate a gradient of colors from red to green to blue for a list of simulator names."""
num_simulators = len(simulator_names)

red_to_green = np.linspace([1, 0, 0], [0, 1, 0], num=int(np.ceil(num_simulators / 2)), endpoint=False)
green_to_blue = np.linspace([0, 1, 0], [0, 0, 1], num=int(np.ceil(num_simulators / 2 + 1)))

full_gradient = np.vstack([red_to_green, green_to_blue])[1:num_simulators + 1]

hex_colors = ['#{:02x}{:02x}{:02x}'.format(int(r * 255), int(g * 255), int(b * 255)) for r, g, b in full_gradient]

return hex_colors

67 changes: 64 additions & 3 deletions bio_check/verifier.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import os
from time import sleep

import numpy as np
import requests
from uuid import uuid4

import seaborn as sns
from matplotlib import pyplot as plt
from requests import Response
from requests.exceptions import RequestException
from requests_toolbelt.multipart.encoder import MultipartEncoder
from typing import *
from dataclasses import dataclass, asdict

from processing_tools import generate_color_gradient


@dataclass
class RequestError:
Expand Down Expand Up @@ -173,9 +178,65 @@ def get_verify_output(self, job_id: str) -> Union[Dict[str, Union[str, Dict]], R
except Exception as e:
return RequestError(error=str(e))

def visualize(self):
# TODO: get results and viz here
pass
def visualize(
self,
data: dict,
simulators: list[str],
output_start: int,
output_end: int,
num_points: int,
hue: str = 'simulators',
use_grid=False
) -> None:
"""Visualize simulation output data, not comparison data, with subplots for each species.
Args:
data (dict): simulation output data
simulators (list[str]): list of simulators
output_start (int): start time of simulation output recording.
output_end (int): end time of simulation output recording.
num_points (int): number of points in simulation output time series.
hue (str): hue upon which the linplot colors are based. Options are: `'simulators'` or `'species'`. Defaults to 'simulators'.
use_grid (bool): whether to use a grid for each subplot. Defaults to False.
"""
# grid plot params
species_data_content = data['content']['results']['results']
species_names = list(species_data_content.keys())
num_species = len(species_names)

# plot data params
t = np.linspace(output_start, output_end, num_points) # TODO: extract this dynamically.
simulator_colors = generate_color_gradient(simulators)

# TODO: extract simulator names dynamically as well.

fig, axes = plt.subplots(nrows=num_species, ncols=3, figsize=(20, 6 * num_species))

if num_species == 1:
axes = [axes]

# iterate over grid rows
for i, species_name in enumerate(species_names):
# iterate over grid cols
for j, simulator_name in enumerate(simulators):
ax = axes[i][j]
species_data = data['content']['results']['results'][species_name]
output_data = species_data.get('output_data')

if output_data:
# create one plot in each column mapped to each individual simulator output (for clarity :) )
simulator_output = output_data[simulator_name]
sns.lineplot(ax=ax, color=simulator_colors[j], data=simulator_output, label=f"{simulator_name}")

# set row title
ax.set_title(f"{species_name} simulation outputs for {simulator_name}")
ax.legend()
ax.grid(use_grid)

# TODO: adjust this
plt.tight_layout()
plt.show()

def export_csv(self):
pass
Expand Down
Loading

0 comments on commit 1b98524

Please sign in to comment.