Skip to content

Commit

Permalink
Merge pull request #32 from biaslyze-dev/cleanup-notebooks
Browse files Browse the repository at this point in the history
Remove unused or old notebooks
  • Loading branch information
tsterbak committed Aug 11, 2023
2 parents b02418a + acc45eb commit c732bc8
Show file tree
Hide file tree
Showing 15 changed files with 720 additions and 9,265 deletions.
8 changes: 4 additions & 4 deletions biaslyze/_plotly_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ def _build_data_lookup(results):
return lookup


def _plot_dashboard(results, num_keywords: int = 10):
def _plot_dashboard(results, num_keywords: int = 10, port: int = 8090):
"""Plot a dashboard of the results as interactive boxplots.
Args:
results: The results.
num_keywords: The number of keywords to plot.
port: The port to run the dashboard on.
"""
concepts = [res.concept for res in results.concept_results]
data_lookup = _build_data_lookup(results)
Expand Down Expand Up @@ -119,7 +120,6 @@ def generate_histogram(dataf):
fig.add_trace(
go.Histogram(
x=plot_data,
# name=keyword,
marker=dict(color=pink2blue_colormap[3]),
nbinsx=100,
)
Expand Down Expand Up @@ -196,7 +196,7 @@ def update_box_plot(concept_idx, method):
template="plotly_white",
showlegend=False,
# width=100*4,
height=50 * num_keywords,
height=45 * num_keywords,
hoverlabel=dict(bgcolor="white", font_size=16, font_family="Rockwell"),
)
return fig
Expand Down Expand Up @@ -329,7 +329,7 @@ def display_selected_text(click_data, relayout_data, concept_idx, method):

app.run_server(
mode="inline",
port=8090,
port=port,
dev_tools_ui=True,
dev_tools_hot_reload=True,
threaded=True,
Expand Down
19 changes: 16 additions & 3 deletions biaslyze/results/counterfactual_detection_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from biaslyze._plotly_dashboard import _plot_dashboard
from biaslyze._plotting import _plot_box_plot, _plot_histogram_dashboard
from biaslyze.utils import is_port_in_use


class CounterfactualSample:
Expand Down Expand Up @@ -145,9 +146,21 @@ def report(self):
f"""Concept: {concept_result.concept}\t\tMax-Mean Counterfactual Score: {np.abs(concept_result.scores.mean()).max():.5f}\t\tMax-Std Counterfactual Score: {concept_result.scores.std().max():.5f}"""
)

def dashboard(self, num_keywords: int = 10):
"""Start a dash dashboard with interactive box plots."""
_plot_dashboard(self, num_keywords=num_keywords)
def dashboard(self, num_keywords: int = 10, port: int = 8090):
"""Start a dash dashboard with interactive box plots.
Args:
num_keywords: The number of keywords per concept to show in the dashboard.
port: The port to run the dashboard on.
"""
next_free_port = 0
while is_port_in_use(port+next_free_port):
next_free_port += 1
if next_free_port > 0:
warnings.warn(
f"Port {port} is already in use. Using next free port {port+next_free_port} instead."
)
_plot_dashboard(self, num_keywords=num_keywords, port=port+next_free_port)

def __visualize_counterfactual_scores(
self, concept: str, top_n: Optional[int] = None
Expand Down
17 changes: 16 additions & 1 deletion biaslyze/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Contains several utility functions for the biaslyze package."""
import dill
import socket


def load_results(path: str):
Expand All @@ -8,4 +9,18 @@ def load_results(path: str):
For previously with CounterfactualBiasDetector.save() saved results.
"""
with open(path, "rb") as f:
return dill.load(f)
return dill.load(f)


def is_port_in_use(port: int) -> bool:
"""Check if a port is already in use.
Args:
port: The port to check.
"""
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("127.0.0.1", port))
return False # Port is available
except OSError:
return True # Port is already in use
28 changes: 12 additions & 16 deletions docs/sources/biaslyze/results/counterfactual_detection_results.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


## CounterfactualDetectionResult
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/results/counterfactual_detection_results.py/#L66)
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/results/counterfactual_detection_results.py/#L67)
```python
CounterfactualDetectionResult(
concept_results: List[CounterfactualConceptResult]
Expand All @@ -24,7 +24,7 @@ The result of a counterfactual bias detection run.


### .save
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/results/counterfactual_detection_results.py/#L76)
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/results/counterfactual_detection_results.py/#L77)
```python
.save(
path: str
Expand Down Expand Up @@ -54,7 +54,7 @@ results = load_results(path)


### .report
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/results/counterfactual_detection_results.py/#L137)
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/results/counterfactual_detection_results.py/#L138)
```python
.report()
```
Expand All @@ -66,32 +66,28 @@ Details:
For each concept, the maximum mean and maximum standard deviation of the counterfactual scores is shown.

### .dashboard
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/results/counterfactual_detection_results.py/#L148)
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/results/counterfactual_detection_results.py/#L149)
```python
.dashboard(
num_keywords: int = 10
num_keywords: int = 10, port: int = 8090
)
```

---
Start a dash dashboard with interactive box plots.

### .visualize_counterfactual_score_by_sample
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/results/counterfactual_detection_results.py/#L278)
```python
.visualize_counterfactual_score_by_sample(
concept: str
)
```

---
Visualize the counterfactual scores for each sample for a given concept.
**Args**

* **num_keywords** : The number of keywords per concept to show in the dashboard.
* **port** : The port to run the dashboard on.


----


## CounterfactualConceptResult
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/results/counterfactual_detection_results.py/#L50)
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/results/counterfactual_detection_results.py/#L51)
```python
CounterfactualConceptResult(
concept: str, scores: pd.DataFrame, omitted_keywords: List[str],
Expand All @@ -107,7 +103,7 @@ The result of a counterfactual bias detection run for a single concept.


## CounterfactualSample
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/results/counterfactual_detection_results.py/#L15)
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/results/counterfactual_detection_results.py/#L16)
```python
CounterfactualSample(
text: str, orig_keyword: str, keyword: str, concept: str, tokenized: List[str],
Expand Down
2 changes: 1 addition & 1 deletion docs/sources/biaslyze/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


### load_results
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/utils.py/#L5)
[source](https://github.com/biaslyze-dev/biaslyze/blob/main/biaslyze/utils.py/#L6)
```python
.load_results(
path: str
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c732bc8

Please sign in to comment.