Skip to content

Conversation

drbenvincent
Copy link
Collaborator

@drbenvincent drbenvincent commented Aug 30, 2025

At the moment this is a bit of an experiment. I'm trying out a number of different ideas for refactoring of the experiment class. Just to test out the idea I'm focussing on the InterruptedTimeSeries class.

Main things I've done are:

  1. Extract the core causal inference algorithm from __init__ to the algorithm method. This is not only more pythonic, but it also gives us a very nice and mostly readable method that captures the core logic of this quasi-experimental method.
  2. Extract data preparation from __init__ to the _build_data method. Increases modularity, testability, and tidies things up.
  3. The data related class attributes were previously scattered all over the place, but now these are neatly collected up into self.data which is an xarray.Dataset. This keeps things tidy but also aids discoverability of the information that people want.
  4. The resulting __init__ is nice and minimal. We still automatically trigger the model fitting, by calling self.algorithm, but there is the potential to not do this if we want to enable a more traditional Bayesian workflow where we build a model and do prior/prior predictive checks before fitting the model. But I'm not doing that in this refactor because it's a major workflow/API change.
  5. For this pre/post quasi experimental design I've also moved away from separating pre and post treatment periods. We just have self.impact for example which has an a period dimension. So if we want the post intervention impact, we can get that by result.impact.sel(period=="post"). Mostly this will be invisible to the user, but for those doing manual interrogation of results then there might be slight changes in the API to document in the notebooks. I'm not wedded to this, and we could always have temporary accessor properties to replicate previous behaviour, which we could then deprecate.
  6. Refactored plotting.
    a. I've separated computation/processing of results and the plotting. So we have get_plot_data_bayesian and get_plot_data_ols which both return data frames. Now the plot functions only ingest these data frames
    b. We now just have one plot method, and this deals with bayesian vs ols models with conditional logic. The motivation for that was to avoid massive duplication because the plots for each were so similar.
    c. What I have not yet done is to make the plot function only ingest the raw dataframe. At the moment it still gets a bunch of self attributes, but it would probably be better for the plot functions to just operate on data objects. I think the next step here would be to make this data an xarray.Dataset rather than a dataframe for greater flexibility (i.e. you can add meta data), but it also comes with some good save/load functionality from xarray. This plot refactoring is inspired by what seems to work quite well on some client projects.

📚 Documentation preview 📚: https://causalpy--524.org.readthedocs.build/en/524/

Copy link

codecov bot commented Aug 30, 2025

Codecov Report

❌ Patch coverage is 99.00990% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.20%. Comparing base (23e0f3e) to head (1cf6e1d).

Files with missing lines Patch % Lines
causalpy/experiments/interrupted_time_series.py 99.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #524   +/-   ##
=======================================
  Coverage   95.19%   95.20%           
=======================================
  Files          28       28           
  Lines        2457     2462    +5     
=======================================
+ Hits         2339     2344    +5     
  Misses        118      118           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant