zhiminy/EvalEng huggingface dataset:
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Fix unexpected pytest failing
Root cause: External Dependency Breakage

- See last commit: https://github.com/understandable-machine-intelligence-lab/Quantus/runs/6899421836?check_suite_focus=true
- Possible solution: https://discuss.streamlit.io/t/typeerror-descriptors-cannot-not-be-created-directly/25639/19

Fixed in https://github.com/understandable-machine-intelligence-lab/Quantus/pull/126
Repository: understandable-machine-intelligence-lab/Quantus
Issue: [function missing] baseline_replacement_by_patch
Root cause: Unimplemented Feature Gap

Hey,

When I use the code, I have an error. It says:  "AttributeError: module 'quantus' has no attribute 'baseline_replacement_by_patch'"

I indeed did not find such a function. But it exists in your tutorial https://github.com/understandable-machine-intelligence-lab/Quantus/blob/main/tutorials/tutorial_basic_example_all_metrics.ipynb

Could you please add it?

Thank you in advance,

Hanwei

Hey! Fixed in commit: https://github.com/understandable-machine-intelligence-lab/Quantus/commit/7accbd2423120e8fa2331761b8c966325661cc07

Thanks so much @hanwei0912
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Let's make a Metric Comparison Matrix
Root cause: Unimplemented Feature Gap

Let's make a Metric Cimparison metric, similar to Captum's: https://captum.ai/docs/algorithms_comparison_matrix)https://captum.ai/docs/algorithms_comparison_matrix.

Tasks:
- [ ] Create a MetricMatrix.md file
- [ ] Start adding things like:
     - Short description e.g., a one-liner describing the metric
     - Score range (tuple) e.g., [-1, 1]
     - Bounded (bool) e.g., y/n
     - Time Complexity
     - Category
     - Task/ Data/ Model - Agnostic?
     - Stochastic vs 
     - etc etc ....
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Add support for TF saved model / TF hub models
Root cause: Unimplemented Feature Gap

In TF it's common practise to use `tf.saved_model` format, for (de)serialising trained models. This format has however a bit 
other APIs than `tf.keras.Model`. It would be great to have Quantus support it.

Also whilst on it, imo it would make sense to also support https://tfhub.dev/, since this is also a popular format for sharing pre-trained TF models

![image](https://user-images.githubusercontent.com/44274784/184477424-bc28a483-6878-46e1-92c8-e7887ade3010.png)



Sounds good. Let's sync on it :)


So, as per official documentation:

A SavedModel contains a complete TensorFlow program, including trained parameters (i.e, [tf.Variable]
(https://www.tensorflow.org/api_docs/python/tf/Variable)s) and computation. 
It does not require the original model building code to run, which makes it 
useful for sharing or deploying with [TFLite](https://tensorflow.org/lite), [TensorFlow.js](https://js.tensorflow.org/), 
[TensorFlow Serving](https://www.tensorflow.org/tfx/serving/tutorials/Serving_REST_simple), or [TensorFlow Hub](https://tensorflow.org/hub).

Which basically means we get `Callable`, we can use to run inference, however no access to internals: like layers, activations and we can't modify it's behaviour. Because only (compiled) computational graph is saved.

I'd say it makes no sense to support this. So I'l close the issue, or rather cancel the feature request
Repository: understandable-machine-intelligence-lab/Quantus
Issue: `quantus.explain` shows zennit warning, when generating explanatins for TF model using tf-explain
Root cause: Validation Gap

If zennit and tf-explain installed, running e.g.
```
a_batch = quantus.explain(
    model,
    x_batch,
    y_batch,
    method='GradCam',
    gc_layer='test_conv'
 )   
```
prints  `UserWarning: Using quantus 'explain' function as an explainer without specifying 'attributor'in kwargs will produce a vanilla 'Gradient' explanation.`
Nothing really broken here, it is just misleading. After uninstalling zennit, the warning is gone

here's the PR https://github.com/understandable-machine-intelligence-lab/Quantus/pull/141
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Enable batch processing for TF-Explain
Root cause: External Dependency Breakage

Hi, 
currently calls from `quantus.explain()` to tf-explain look like this: 
```
explainer = tf_explain.core.gradients_inputs.GradientsInputs()
        explanation = (
            np.array(
                list(
                    map(
                        lambda x, y: explainer.explain(([x], None), model, y),
                        inputs,
                        targets,
                    )
                ),
                dtype=float,
            )
            / 255
        )
``` 
Instead it should be possible to just pass a batch, which should greatly speed up performance.
Or am I missing something?

So, I have checked out the source code of tf-explain. It does not support batch processing e.g. https://github.com/sicara/tf-explain/blob/master/tf_explain/core/vanilla_gradients.py#L49
Another lib we could use is https://github.com/keisen/tf-keras-vis. 
I will do an experiment and compare performance (gains). 


I'will close the issue, since there's nothing which could be done for it 🤷‍♂️
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Dependency conflict in requirements
Root cause: External Dependency Breakage

Hello,
when I was adding `quantus` to poetry project, it turned out there is a dependency conflict in `quantus` requirements:
```
Because quantus (0.1.6) depends on opencv-python (4.5.5.62) which depends on numpy (>=1.21.2), quantus (0.1.6) requires numpy (>=1.21.2).
And because quantus (0.1.6) depends on numpy (1.19.5), quantus is forbidden.
So, because no versions of quantus match >0.1.6,<0.2.0
and example-project depends on quantus (^0.1.6), version solving failed.
```
Using Python 3.9.7 and Poetry 1.2.0, MacOS 12.4

Problem probably comes from different numpy version [requirements of `opencv-python`](https://github.com/opencv/opencv-python/blob/4.x/pyproject.toml) for Linux and macOS.

Problem is solved in this [issue in poetry project](https://github.com/python-poetry/poetry/issues/6447).
Repository: understandable-machine-intelligence-lab/Quantus
Issue: divide by 0 in `quantus.normalise_func.normalise_by_negative`
Root cause: Validation Gap

![image](https://user-images.githubusercontent.com/44274784/189420951-5c1b10be-1f95-41ae-bc01-4810d3aaece1.png)

I think we should see if/how we can prevent this

It is gone after last release. I'm closing the issue
Repository: understandable-machine-intelligence-lab/Quantus
Issue: treatment of slices in baseline_replacement_by_indices is wrong
Root cause: Algorithmic Error

Slices currently will be simply expanded to to arrays by np.arange:
https://github.com/understandable-machine-intelligence-lab/Quantus/blob/6f7ced11a011b48d336c0aabfb558ca87ea4b815/quantus/helpers/utils.py#L629

But slices behave actually completely different when we use them to index numpy arrays:
Here's an example:

```
a = np.zeros((100, 100))

a_slice = a[slice(5, 10), slice(5, 10)]
print(f'{a_slice.shape = }')

a_indices = a[np.arange(5, 10), np.arange(5, 10)]
print(f'{a_indices.shape = }')
```
The output will be
```
a_slice.shape = (5, 5)
a_indices.shape = (5,)
```

The reason is, that when we simply expand them to integer arrays, then numpy takes it as a list of five coordinates.
Using slices we can index a nd-block, using arrays we only index the diagonal.
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Warn about sample-wise measurements potentially having different lengths
Root cause: Documentation Deficiency

Due to patches being generated non-overlappingly, and depending on attributions, the number of patches per input can differ between samples, leading to different number of measurements between samples.

While this is desired behavior, a warning about that would be great, however, this is not trivial when samples are evaluated instance-wise.

A similar issue happens for region perturbation, but a warning is more easily implemented here, due to the number of evaluations being a hyperparameter (although the same issue as for selectivity can still occur here even without warning)

Writing this issue mainly as a reminder to think about how to deal with this behavior and make it more transparent to the user.
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Extend methods in quantus.explain e.g., using captum and tf-explain
Root cause: Unimplemented Feature Gap

Make more base methods available

https://captum.ai/api/!

- [x] Extend captum methods
- [x] Add to constants.py (separation `captum`, `tf-explain`)
- [x] Add some parameters to the pytests

addressed in PR #206
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Feature request: `quantus.explain` kwarg to disable numpy conversion

Currently, `Quantus` [converts](https://github.com/understandable-machine-intelligence-lab/Quantus/blob/daa9ba313f97a5ca52a2125563399461ca5d3bf1/quantus/helpers/explanation_func.py#L391) explanations to `numpy` arrays. This is understandable, but maybe it could be optional with a default-value kwarg `convert_to_numpy=True`, such that its possible to work with tensors throughout?
 

As a side note, TensorFlow tensors can almost always be directly passed to NumPy functions.
We could also expect a noticeable speed-up if we avoid constantly copying tensors between the CPU and GPU. 
Last time I ran profiling, `np.asarray` was top 3 of `tottime`. I can't find the profiling file now, so I can't post if here, sorry.
Ideally, we could have a global config flag, e.g., `quantus.config.disable_numpy_conversion()`. (is related to https://github.com/understandable-machine-intelligence-lab/Quantus/issues/234)

And then e.g. `norm_func.py`
```
from functools import singledispatch

@singledispatch
def l2_norm(a: np.ndarrat):
    # just a fill-in example.
    return np.linalg.norm(a, l_ord=2)

try 
    import tensorflow as tf
    
     @l2_norm.register
     def l2_norm(a: tf.Tensor):
         return tf.linal.norm(a, ord=2)
except ModuleNotFoundError:
    pass
    
```

aaaaand then, after we register Torch and TensorFlow variants for every NumPy/SciPy function, we can work with tensors only, and they will stay on the GPU, and we avoid CPU copying overhead. 
Sounds like a lot of work, mb we should start with 1 metric, 1 explainer and all the functions used by them. Profile NumPy and torch/TensorFlow versions, and if we see a noticeable speed-up, we could re-work the whole library for one of the next major releases.

@vedal why did you want to be able to work with tensors directly?
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Input Invariance misses model input bias shift
Root cause: Algorithmic Error

### Description
Currently the InputInvariance metric just adds a bias to `x_batch`.
According to the original paper, you should then add an offset in the opposite direction to the model input bias.
Attribution methods are input invariant, if they don't change under this setup.

The current implementation misses the second half, so it is very unrealistic that the metric actually returns `True` for any setup.

Here's the reference:
https://arxiv.org/abs/1711.00867

### Minimum acceptance criteria
- Add offset to model input bias.
- Add at least one test case parametrization where `InputInvariance` returns `True`


Addressed in PR #225
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Find out how to render text heat maps in GitHub preview
Root cause: Documentation Deficiency

From top to down the views generated by:
- PyCharm
- Colab
- JupyterLab
- GitHub preview

![image](https://user-images.githubusercontent.com/44274784/195724278-57cc842f-40d3-4e2f-81d3-1646bef8535f.png)
![image](https://user-images.githubusercontent.com/44274784/195724312-657a20e1-31d4-409c-a690-e939a40db67f.png)
![image](https://user-images.githubusercontent.com/44274784/195724442-e5b89262-47b2-45fc-bcd0-021baa2fe5a1.png)
![image](https://user-images.githubusercontent.com/44274784/195724510-52b0c35e-a858-49e5-b688-24cbf8e17d0b.png)

Ideally, we want them all to be rendered in the same view. I think this could be caused by GitHub preview treating Jupyter notebook as HTML itself and applying some of GitHub's CSS onto it. It would be event better if we find a library to **reliably** render heat maps for us

so, here is the answer https://stackoverflow.com/questions/74046734/plot-text-saliency-map-in-jupyter-notebook

Fixed by @aaarrti !
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Fix UNKNOWN on codecov badge

### Description
- The codecov badge is correctly pointing the user to the report but it does not capture the coverage % but states unknown

### Steps to reproduce the behavior
- No need, just look at the README.md

### Minimum acceptance criteria
- Updated codecov.yml file
- Codecov badge shows the test coverage % instead of UNKNOWN
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Question: attributions preprocessing and `get_wrapped_model` only accessible when computing attributions in metric
Root cause: Unimplemented Feature Gap

Hi! 
Thanks alot again for this inpiring package!

### Description of the problem
I was trying to pre-compute softmax-attributions `quantus.explain` and evaluate them on a list of metrics through `quantus.evaluate`, and was confused by the design of `quantus` here. 

The main confusion is that: while Quantus gives you the option to evaluate pre-computed attributions on multiple metrics by passing an `a_batch` argument, there is no straight-forward to pre-compute softmax-attributions (cannot be done using `quantus.explain`). Instead, there are two ways with Quantus:

1. use `quantus.evaluate(..., model=model a_batch=None, softmax=True)`, which re-computes softmax-attributions for each metric.
2. leverage `quantus.helpers.utils.get_wrapped_model()` and compute the softmax-attributions manually, as is done in the metric base-class:[base.py:L384](https://github.com/understandable-machine-intelligence-lab/Quantus/blob/8c5a2379f6bc0c8ae30c65b7c4ce19fc967cb0da/quantus/metrics/base.py#L384)) in the following way:
```
a_batch = quantus.explain(model=quantus.helpers.utils.get_wrapped_model(model, softmax=True), x_batch, y_batch)

```

Both of these options are a bit unintuitive IMO, particularly since even the softmax-argument can be hard to spot as a metric-argument. I'd expect it as an argument for both `quantus.explain` or `quantus.evaluate`.

### Description of a solution

- one solution would be to have the softmax-argument accessible from `quantus.explain`? 
- also, for simplicity, maybe `quantus.evaluate` should not allow `a_batch=None` ? 
--------------

More general suggestion:
- attribution preprocessing before metrics should be available as separate modules which could be pipelined, like torchvision.transform or sklearn Pipeline. At the moment, attribution preprocessing, such as `normalise` or `interpolation`, and even attribution computation like softmax, are currently performed under-the-hood in each metric. 

I believe having a separate preprocessing-class, would make the computations of the library more transparent, such that the user can visualize and inspect the final attributions that into the metric.

-----------------------------

Note: a softmax-attributions feature may or may not be a necessary. I see that there are different recommendations mentioned for this, as in this thread: [comment 1](https://github.com/pytorch/captum/issues/407#issue-639582944) and [comment 2](https://github.com/pytorch/captum/issues/407#issuecomment-650684371). I see that captum, for example, only use softmax for [visualization](https://captum.ai/api/insights.html#captum.insights.AttributionVisualizer).
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Include private class methods in API documentation
GitHub labels: bug

### Description
- Private methods e.g., `__call__` methods are not included as per default in readthedocs.org generations
- See here: https://quantus.readthedocs.io/en/latest/docs_api/quantus.html

### Solution
- Ideas here: https://stackoverflow.com/questions/57610288/showing-only-private-methods-with-sphinx-autodoc

### Minimum acceptance criteria
- Private methods are exposed in the API documentation
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Metric arguments as keyword only
Root cause: Unimplemented Feature Gap

### Description of the problem
- Metric classes have many arguments. It is easy to get them wrong, when passing them as positional arguments.
### Description of a solution
- Make metric arguments keyword only.

### Minimum acceptance criteria
- This is a breaking change, and users could be impacted. Include it in major release with a proper warning.


A better solution would be https://github.com/understandable-machine-intelligence-lab/Quantus/issues/234
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Extend functionality of quantus.evaluate to be more general
Root cause: Unimplemented Feature Gap

### Description of the problem
- The current `quantus.evaluate()` function expects the `xai_methods` argument to have a structure which does not fit all possible explanation frameworks (even those wrapped by quantus, e.g., `zennit`). Specifically, it expects each explanation method to be defined either via a string or a callable. This is in contrast to the general implementation of quantus.explain, which basically just expects the explanation method to be simply defined by the `**kwargs`, and is therefore much more general.

- Similarly, some form of iteration over the `metric_call_kwargs` should also be supported. Again, this would fit better with the relatively recent API changes of metrics in quantus. For instance, a user my want to evaluate with `softmax=True` as well as `softmax=False`, or different `model_predict_kwargs`. In the future, more important arguments could also be added to the `__call__` method of metrics, so iteration over those might be desirable.

### Description of a solution
- [x] Make `xai_methods` a Union[Dict[str, Callable], Dict[str, Dict]], where the second option would be of the form `{method: explain_func_kwargs}`. 
- [x] Remove the then redundant `explain_func_kwargs` argument from quantus.evaluate. 
- [x] Add a third loop iterating over different `call_kwargs`
- [x] Make `call_kwargs` of type Dict[str, Dict]
- [x] Update Docs :)

### Minimum acceptance criteria
- This is not a very significant change, but it changes the API of `quantus.evaluate()`, so it needs to be communicated properly.
- Some consideration should be given to usability also. Does it make sense to keep some form of accessing xai_methods via string (i.e., List[str]) to simply make evaluation more comfortable?
- Pls review @annahedstroem


Looks great! Added you as a reviewer @leanderweber 

See: https://github.com/understandable-machine-intelligence-lab/Quantus/pull/214

Already completed.
Repository: understandable-machine-intelligence-lab/Quantus
Issue: blur_at_indices does not handle the output of create_patch_slice correctly
Root cause: Algorithmic Error

### Description
Basically the title. blur_at_indices does not deal correctly with multidimensional indices. Specifically, the lines for applying the kernel iterate incorrectly over multidimensional indices.

### Steps to reproduce the behavior
Run region_perturbation with the blur perturbation and Color-image data. Otherwise use default parameters.

### Minimum acceptance criteria
- Iterate over a flattened version of `arr_indices`
- Write test cases to catch this.
- @leanderweber will make a PR w.r.t. this soon.
- @annahedstroem please review then.
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Minor misleading error message in Robustness metrics

### Description
When failing to provide an explain_func for the robustness metrics, an error message states:
```AssertionError: Make sure 'explain_func' is a Callable that takes model, x_batch, y_batch and **kwargs as arguments.```

However, the correct signature for explain_func is `model, inputs, targets and **kwargs`.

This is important as these are internally used by their name, not only by position.

### Steps to reproduce the behavior
See description. This is very minor.

### Minimum acceptance criteria
- Change two words in the error message :)
- @annahedstroem


Already completed.
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Catch invalid metric output
Root cause: Validation Gap

### Description of the problem
Some metrics may output inf or nan values when inputting certain attributions.

### Description of a solution
Such behavior should be caught and, at the very least, the user should be warned that the output contains invalid values and may need to be sorted out.
Optimally, some information about what caused this could be given. E.g., 0 attributions, nan attributions were passed etc?

### Minimum acceptance criteria
- Above warning is implemented.
- No metric "quietly" outputs invalid values anymore.
- @annahedstroem
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Add simple interpretation assistance to metric docstrings
GitHub labels: documentation

good first issue

help wanted

### Description of the problem & solution
Currently, the docstring of each metric mostly explains what the metric computes, as well as the corresponding reference.
It may be helpful in practice to add a very simple guide for how to interpret the resulting scores as well.
E.g., just something like "Higher/Lower scores are better". I believe this would increase usability a lot, especially for users that are new to XAI quantification.

### Minimum acceptance criteria
- Add guidance for how outputs should be interpreted to each metric docstring,
- @annahedstroem
Repository: understandable-machine-intelligence-lab/Quantus
Issue: Can't calculate metrics for pre-calculated attributions
Root cause: Documentation Deficiency

### Description
When trying to follow the tutorial, for already calculated attributions: https://quantus.readthedocs.io/en/latest/getting_started/getting_started_example.html 
an assertion error is thrown.
![Screenshot from 2022-12-08 14-51-34](https://user-images.githubusercontent.com/50047927/206463222-0587c31f-8cfe-44c1-aa93-a8402d6980a7.png)

### Steps to reproduce the behavior
Train a model and calculate attributions with Captum using Integrated Gradients. When running:
metric = quantus.Continuity()
    scores = metric(model=net,
                         x_batch=input_batch,
                         y_batch=label_batch,
                         a_batch=int_grad_relevances
                         )
An Assertion error will be thrown that 'explain_func' needs to be a callable.

### Minimum acceptance criteria
It should be made possible again to calculate metrics without an 'explain_func' if attributions are pre-calculated.
I would be happy if someone could have a look at this (e.g. @annahedstroem) and see if they can reproduce this issue.


Hi @JacquelineBeinecke thanks for sharing your problem!

The reason why this error is thrown is because the `Continuity` metric relies on an explanation function in order to generate explanations on perturbed input, inaddition to precomputed attributions. See line 319 and onwards! https://github.com/understandable-machine-intelligence-lab/Quantus/blob/main/quantus/metrics/robustness/continuity.py




Hi, thanks for the explanation, that solved my problem :)