Skip to content

[feat]: SPR Timeseries - #375

Open
JZahner1 wants to merge 10 commits into
nmfs-ost:sprfrom
JZahner1:spr
Open

JZahner1 wants to merge 10 commits into
nmfs-ost:sprfrom
JZahner1:spr

Conversation

@JZahner1

Copy link
Copy Markdown
Collaborator

plot_spr allows for plotting a timeseries of spawning potential ratio (SPR) quantities.

Users may specify one of three options to customize what quantity is plotted:

  • spr - spawning potential ratio
  • fishing_intensity - fishing intensity, or 1-SPR
  • spr_ratio - the quantity associated with the SPR derived quantity. For some model (e.g., SS3), this quantity may be customizable by the user.

Some models may only plot spr_ratio with uncertainty intervals.

Dynamically plot timeseries of SPR quantities. Valid quantities include SPR, fishing intensity (1-SPR), or SPR Ratio (a potentially user defined SPR quantity.
Allow user to specify y-axis label in `plot_spr`. This is important so users can ammend the default labels, esepcially when SPR quantities are user defined.
@Schiano-NOAA
Schiano-NOAA self-requested a review September 28, 2026 15:53
@Schiano-NOAA Schiano-NOAA added the enhancement New feature or request label Sep 28, 2026
@Schiano-NOAA Schiano-NOAA linked an issue Sep 28, 2026 that may be closed by this pull request
@Schiano-NOAA Schiano-NOAA changed the title SPR Timeseries [feat]: SPR Timeseries Sep 28, 2026
@sbreitbart-NOAA
sbreitbart-NOAA self-requested a review September 28, 2026 16:01
1-spr is stored as "spr_report" within the SPR_SERIES module. Removed the explicit recalculation of it, and added "spr_report" explicitly to the label filter.
@JZahner1

Copy link
Copy Markdown
Collaborator Author

@Schiano-NOAA Do we want to add SPR tables to this PR?

@sbreitbart-NOAA sbreitbart-NOAA left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, @JZahner1! I've added some comments about how to update the section where the plot is exported. I'll let @Schiano-NOAA review the rest of the plot. Happy to answer questions here or in a meeting if that's easier.

Comment thread R/plot_spr.R
Comment thread R/plot_spr.R
Comment thread R/plot_spr.R Outdated
@Schiano-NOAA

Copy link
Copy Markdown
Collaborator

@Schiano-NOAA Do we want to add SPR tables to this PR?

@JZahner1 Just the plot please! If you want to make a table, it should be in a separate PR however I am not sure that's on our planned development list.

JZahner1 and others added 2 commits September 29, 2026 15:32
Co-authored-by: Sophie Breitbart <sophie.breitbart@noaa.gov>
Comment thread R/plot_spr.R Outdated
) {


quantity <- match.arg(quantity)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please adjust this so there is some default selection

Suggested change
quantity <- match.arg(quantity)
if (length(quantity) > 1 quantity <- "spr" else quantity <- match.arg(quantity)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or alternative put a warning that there is no selected quantity so when it fails if the user doesn't put something in there, they know what is going on

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Schiano-NOAA Im pretty sure that R defaults to using the first element if a vector is set as a default parameter value. So if no value is provided, "spr" is used. I can certainly add that to the function documentation. Restricting multiple selections is a good idea though.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay i was wondering if it did. I was testing it in line and and it didn't select one in the swtich but might behave differently within a function

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah. match.arg doesn't really work outside of a function

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

match.arg will take the first element of the vector provided in the function call specifications as the default. There is an internal argument to match.arg to not allow multiple selections, i.e., several.ok = FALSE.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kellijohnson-NOAA thanks for sharing! Does it throw an error if multiple options are provided but several.ok=FALSE or does it default back to the first valid option?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It errors

calculate_stat <- function(x, type = c("mean", "median", "sd")) {
      # 1. Validate the user input against the default vector options above
      type <- match.arg(type, several.ok = FALSE)
      
      # 2. Execute code based on the validated choice
      switch(type,
             mean   = mean(x, na.rm = TRUE),
             median = median(x, na.rm = TRUE),
             sd     = sd(x, na.rm = TRUE))
    }

calculate_stat(1:10)
[1] 5.5

calculate_stat(1:10, type = c("mean", "median"))
Error in match.arg(type, several.ok = FALSE) : 'arg' must be of length 1

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbreitbart-NOAA @Schiano-NOAA is this behavior we want (e.g., throwing an error when multiple options are input), or do we want to fallback to the default "spr" quantity? Both seem equally reasonable to me.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm tough call. I think it would be good to fall back to the default and provide a warning message that it has > 1 selected so setting to default or you could set it to the first option provided.

Comment thread R/plot_spr.R Outdated
Comment thread R/plot_spr.R Outdated
dir = figures_dir,
scale_amount = scale_amount,
unit_label = unit_label
scale_amount = scale_amount

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see scale_amount set as an argument in the function and I don't think we want it?

Suggested change
scale_amount = scale_amount
scale_amount = 1

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stopped me from creating an rda

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally commented (made a code "suggestion") where I tried to "suggest" removing the unit_label line. It may have gotten wonky and erased the scale_amount line. In any case, to be clear, the scale_amount line was originally = scale_amount and should remain that way (unless I'm missing something, @Schiano-NOAA ). The unit_label line can be deleted. So, it should look like this:

      dat = dat,
      dir = figures_dir,
      scale_amount = scale_amount
    )
  }

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scale_amount isn't defined as a function input (i guess you can pass it as an extra parameter), so I dont think we can use do scale_amount=scale_amount here. Plus, for SPR, there are no units, so theres nothing to "scale" anyway.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it should be changed to 1 as Josh did ⬆️

Co-authored-by: Sam (Schiano) Bredeck <125507018+Schiano-NOAA@users.noreply.github.com>
Comment thread tests/testthat/test-plot_spawning_potential_ratio.R
Comment thread tests/testthat/test-plot_spawning_potential_ratio.R

@Schiano-NOAA Schiano-NOAA left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! 🥳 Thank you for resolving the issues as I reviewed. Once all comments are addressed/resolved, I will merge the PR in. Thank you so much for your work on this.

Edit: see other comment about BAM

@Schiano-NOAA Schiano-NOAA left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My apologies there is one more thing. This is not functional with BAM output. BAM contains "spr" so I am not sure why it's not working. I will send you an example file to test and work on

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] SPR time series (AM)

4 participants