Skip to content

Commit

Permalink
Merge pull request #18 from calico/issue-17
Browse files Browse the repository at this point in the history
Issue 17 - plot fix to include time zero fit when showing v_inter and v_final
  • Loading branch information
shackett authored Apr 19, 2024
2 parents ee0bbfb + e91f067 commit e45cb30
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 38 deletions.
65 changes: 42 additions & 23 deletions R/impulse_plotting.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
#' @return a ggplot
#'
#' @export
kinetics_plotting <- function(augmented_timecourses,
saturation = 0.9,
max_time,
fit_timepoints = 100) {
kinetics_plotting <- function(
augmented_timecourses,
saturation = 0.9,
max_time,
fit_timepoints = 100
) {

stopifnot("tbl_df" %in% class(augmented_timecourses))
stopifnot("tc_id" %in% colnames(augmented_timecourses))
Expand All @@ -20,8 +22,7 @@ kinetics_plotting <- function(augmented_timecourses,
present_variables <- intersect(reserved_variables,
colnames(augmented_timecourses))
if (length(present_variables) == 0) {
stop ("no aesthetic variables present; aesthetic variables are: ",
paste(reserved_variables, collapse = ", "))
cli::cli_abort("no aesthetic variables present; aesthetic variables are: {.field {reserved_variables}}")
}

checkmate::assertNumber(saturation, lower = 0.5, upper = 1)
Expand Down Expand Up @@ -51,6 +52,19 @@ kinetics_plotting <- function(augmented_timecourses,
tidyr::unnest_legacy(fitted_kinetics)

possible_nest_vars <- c("rate", "t_rise", "t_fall", "v_inter", "v_final", "tzero_offset")
missing_nest_vars <- setdiff(possible_nest_vars, colnames(fitted_kinetics))

if (length(missing_nest_vars) > 0) {
for (x in missing_nest_vars) {
if (x == "tzero_offset") {
fitted_kinetics <- fitted_kinetics %>% dplyr::mutate(tzero_offset = 0)
} else {
fitted_kinetics <- fitted_kinetics %>%
dplyr::mutate(!!rlang::sym(x) := NA_real_)
}
}
}

timepoints <- seq(from = 0, max_time, length.out = fit_timepoints)

fitted_values <- fitted_kinetics %>%
Expand All @@ -68,7 +82,7 @@ kinetics_plotting <- function(augmented_timecourses,
dplyr::filter(model == best_model),
fitted_values,
saturation = saturation
)
)

kinetics_plot <- kinetics_plot +
geom_rect(data = kinetic_intervals$asympote_aesthetics_df,
Expand Down Expand Up @@ -110,12 +124,14 @@ kinetics_plotting <- function(augmented_timecourses,

}

kinetics_plot
return(kinetics_plot)
}

kinetic_aesthetics <- function(fitted_kinetics,
fitted_values,
saturation = 0.9) {
kinetic_aesthetics <- function(
fitted_kinetics,
fitted_values,
saturation = 0.9
) {

# find fitted kinetic value at timing coefficient
time_aesthetics_df <- fitted_kinetics %>%
Expand All @@ -132,22 +148,27 @@ kinetic_aesthetics <- function(fitted_kinetics,

# find satuation time for assymptote
asympote_aesthetics_df <- fitted_kinetics %>%
dplyr::select(tc_id, model, rate, t_rise, t_fall, v_inter, v_final) %>%
tidyr::gather(par, value, -tc_id, -model, -rate, -v_inter, -v_final) %>%
dplyr::select(tc_id, model, rate, t_rise, t_fall, v_inter, v_final, tzero_offset) %>%
tidyr::gather(par, value, -tc_id, -model, -rate, -v_inter, -v_final, -tzero_offset) %>%
dplyr::filter(!is.na(value)) %>%
# match t_rise to v_inter and t_fall to v_final
dplyr::mutate(assymp = dplyr::case_when(par == "t_rise" ~ v_inter,
par == "t_fall" ~ v_final)) %>%
dplyr::mutate(assymp = dplyr::case_when(par == "t_rise" ~ v_inter + tzero_offset ,
par == "t_fall" ~ v_final + tzero_offset)) %>%
dplyr::mutate(assymp_type = dplyr::case_when(
par == "t_rise" ~ "v_inter",
par == "t_fall" ~ "v_final")) %>%
# derive saturation time for x-axis
dplyr::mutate(t_saturation_start = saturation_time(1 - saturation,
value,
rate),
t_saturation_end = saturation_time(saturation,
value,
rate)) %>%
dplyr::mutate(
t_saturation_start = saturation_time(
1 - saturation,
value,
rate
),
t_saturation_end = saturation_time(
saturation,
value,
rate
)) %>%
dplyr::select(tc_id, model, assymp_type, assymp,
t_saturation_start, t_saturation_end)

Expand All @@ -156,7 +177,5 @@ kinetic_aesthetics <- function(fitted_kinetics,
}

saturation_time <- function(saturation, c_time, rate) {

c_time + log(saturation / (1 - saturation)) / rate

}
20 changes: 18 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ sigmoid_impulse_plot <- function(timecourse_parameters) {
scale_x_continuous("Time") +
theme_bw() +
theme(text = element_text(size = 15), legend.position = "top")
}
sigmoid_impulse_plot(data_frame(t_rise = 25, rate = 0.25, v_inter = 3, v_final = -3, t_fall = 45))
sigmoid_impulse_plot(tibble(t_rise = 25, rate = 0.25, v_inter = 3, v_final = -3, t_fall = 45))
```

### sigmoid
Expand Down Expand Up @@ -100,3 +99,20 @@ Once conda and impulse are installed, A conda environment configured with Tensor
```{r, eval=FALSE}
auto_config_tf()
```

Alternatively, create a minimal environment with:

```{bash venv_setup, eval = FALSE}
<<PATH TO PYTHON BINARY>> -m venv .venv
source .venv/bin/activate
pip install --upgrade pip wheel setuptools ipykernel
pip install -r requirements.txt
```

Where, requirements.txt is just:

```{bash requirements, eval = FALSE}
tensorflow==2.9.*
numpy==1.23.*
tensorflow-probability==0.17.*
```
53 changes: 40 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ value(s) and asymptote(s).

The core functionality of **impulse** is:

- Simulate timecourse parameters and resulting timecourses
- Fit sigmoid and impulse models to timecourses with or without priors
on kinetic parameters
- Compare sigmoid and impulse models
- Visualize measurements and parametric fits
- Simulate timecourse parameters and resulting timecourses
- Fit sigmoid and impulse models to timecourses with or without priors
on kinetic parameters
- Compare sigmoid and impulse models
- Visualize measurements and parametric fits

## The models

Expand All @@ -33,21 +33,31 @@ between timecourses and kinetic paramters, but the kinetic parameters
are generally more meaningful since they indicate the timing and
magnitdue of responses.

A sigmoid with parameters {t\_rise = 25, v\_inter = 3, rate = 0.25} and
an impulse with two additional parameters {t\_fall = 45, v\_final = -3}
are shown. The t\_rise of 25 indicates a half-max time of 25 and
v\_inter of 3 indicates saturation at 3. In the impulse model there is a
second response with a half-max time of 45 and final asymptote at -3.
A sigmoid with parameters {t_rise = 25, v_inter = 3, rate = 0.25} and an
impulse with two additional parameters {t_fall = 45, v_final = -3} are
shown. The t_rise of 25 indicates a half-max time of 25 and v_inter of 3
indicates saturation at 3. In the impulse model there is a second
response with a half-max time of 45 and final asymptote at -3.

![](man/figures/README-sigmoid_impulse_compare-1.png)<!-- -->

### sigmoid

![Sigmoid](https://github.com/calico/impulse/blob/master/man/figures/sigmoid.png)
<figure>
<img
src="https://github.com/calico/impulse/blob/master/man/figures/sigmoid.png"
alt="Sigmoid" />
<figcaption aria-hidden="true">Sigmoid</figcaption>
</figure>

### implulse

![Impulse](https://github.com/calico/impulse/blob/master/man/figures/impulse.png)
<figure>
<img
src="https://github.com/calico/impulse/blob/master/man/figures/impulse.png"
alt="Impulse" />
<figcaption aria-hidden="true">Impulse</figcaption>
</figure>

## *Impulse* functionality

Expand All @@ -65,7 +75,7 @@ impulse models since there are natural constraints on parameter values
which should hold (non-negative rates, non-negative times, rise before
fall). When these constraints are violated, a good fit may occur, but
interpretability of timing and effect sizes will be lost. The vignette
*setting\_priors* describes how to formulate the priors and can be used
*setting_priors* describes how to formulate the priors and can be used
to guide the tuning of parameters for other application.

## Installation
Expand Down Expand Up @@ -98,3 +108,20 @@ with TensorFlow can be created using:
``` r
auto_config_tf()
```

Alternatively, create a minimal environment with:

``` bash
<<PATH TO PYTHON BINARY>> -m venv .venv
source .venv/bin/activate
pip install --upgrade pip wheel setuptools ipykernel
pip install -r requirements.txt
```
Where, requirements.txt is just:
``` bash
tensorflow==2.9.*
numpy==1.23.*
tensorflow-probability==0.17.*
```
Binary file modified man/figures/README-sigmoid_impulse_compare-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e45cb30

Please sign in to comment.