---
jupyter:
  orphan: true
  jupytext:
    notebook_metadata_filter: all,-language_info
    split_at_heading: true
    text_representation:
      extension: .Rmd
      format_name: rmarkdown
      format_version: '1.2'
      jupytext_version: 1.13.7
  kernelspec:
    display_name: Python 3
    language: python
    name: python3
---

# A notebook that should use a module

```{python}
import numpy as np
import matplotlib.pyplot as plt
import nibabel as nib
```

A copy/paste of the code from [on_modules](on_modules.Rmd):

```{python}
def vol_means(image_fname):
    img = nib.load(image_fname)
    data = img.get_fdata()
    means = []
    for i in range(data.shape[-1]):
        vol = data[..., i]
        means.append(np.mean(vol))
    return np.array(means)
```

```{python}
def detect_outliers(some_values, n_stds=2):
    overall_mean = np.mean(some_values)
    overall_std = np.std(some_values)
    thresh = overall_std * n_stds
    is_outlier = (some_values - overall_mean) < -thresh
    return np.where(is_outlier)[0]
```

We apply this code to another image:

```{python}
# Load the function to fetch the data file we need.
import nipraxis
# Fetch the data file.
another_data_fname = nipraxis.fetch_file('ds114_sub009_t2r1.nii')
# Show the file name of the fetched data
another_data_fname
```

```{python}
more_means = vol_means(another_data_fname)
plt.plot(more_means)
```

Apply the code:

```{python}
detect_outliers(more_means)
```

Oh no!   It didn't work?  What's the problem?

Back to [on_modules](on_modules.Rmd)

## Back again


Now we've worked out a better solution:

```{python}
import volmeans
```

```{python}
more_means_again = volmeans.vol_means(another_data_fname)
volmeans.detect_outliers_fixed(more_means_again)
```
