biclusters / app.py
DanOPT
Added app.py and requirements.txt
63c633d
Raw History Blame Contribute Delete
8.48 kB
import gradio as gr
import numpy as np
from sklearn.datasets import make_biclusters
import matplotlib.pyplot as plt
import tempfile
def generate_biclusters(n_rows, n_cols, n_clusters, noise=0.0, minval=10, maxval=100, random_state=None):
# Generate biclusters
shape = (n_rows, n_cols)
# Unshuffled
X_unshuffled, rows_unshuffled, columns_unshuffled = make_biclusters(
shape=shape, n_clusters=n_clusters, noise=noise, minval=minval, maxval=maxval,
shuffle=False, random_state=random_state
)
# Shuffled
X_shuffled, rows_shuffled, columns_shuffled = make_biclusters(
shape=shape, n_clusters=n_clusters, noise=noise, minval=minval, maxval=maxval,
shuffle=True, random_state=random_state
)
# Plot
fig, axes = plt.subplots(1, 2, figsize=(20, 8))
# Plot the unshuffled bicluster array
im1 = axes[0].imshow(X_unshuffled, cmap='viridis', aspect='auto')
axes[0].set_title("Unshuffled Bicluster Array")
axes[0].set_xlabel("Columns")
axes[0].set_ylabel("Rows")
plt.colorbar(im1, ax=axes[0])
# Plot the shuffled bicluster array
im2 = axes[1].imshow(X_shuffled, cmap='viridis', aspect='auto')
axes[1].set_title("Shuffled Bicluster Array")
axes[1].set_xlabel("Columns")
axes[1].set_ylabel("Rows")
plt.colorbar(im2, ax=axes[1])
# Save the plot to a temporary file
temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
plt.savefig(temp_file.name)
plt.close()
# Prepare data for output
data_list_unshuffled = X_unshuffled.tolist()
data_list_shuffled = X_shuffled.tolist()
rows_list_unshuffled = rows_unshuffled.tolist()
columns_list_unshuffled = columns_unshuffled.tolist()
rows_list_shuffled = rows_shuffled.tolist()
columns_list_shuffled = columns_shuffled.tolist()
return temp_file.name, data_list_unshuffled, data_list_shuffled, rows_list_unshuffled, columns_list_unshuffled, rows_list_shuffled, columns_list_shuffled
# Define the Gradio interface
with gr.Blocks(title="Biclusters Dataset") as demo:
gr.Markdown("# Dataset Generator: Biclusters\n\n"
"This tool allows you to generate a constant block diagonal structure array for biclustering using scikit-learn's make_biclusters() function.\n\n"
"You can specify the shape, number of clusters, and other parameters.\n\n"
"The tool will visualize both unshuffled and shuffled versions of the generated data and provide the underlying data and cluster information.\n\n"
"It will also provide you the underlying Python code for the function call.\n\n"
"## Parameters Description:\n"
"- **Number of Rows**: The number of rows in the result array.\n"
"- **Number of Columns**: The number of columns in the result array.\n"
"- **Number of Clusters**: The number of biclusters to generate.\n"
"- **Noise**: The standard deviation of the gaussian noise (default is 0.0).\n"
"- **Minimum Value**: Minimum value of a bicluster (default is 10).\n"
"- **Maximum Value**: Maximum value of a bicluster (default is 100).\n"
"- **Random State**: Seed for reproducible output (default is 0).\n\n"
"## Plot Explanation:\n"
"- The left plot shows the unshuffled data, and the right plot shows the shuffled data.\n"
"- Colors represent the values in the bicluster arrays.\n\n"
"For more details on the parameters, visit the [scikit-learn: make_biclusters() documentation](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_biclusters.html).\n\n")
with gr.Row():
n_rows = gr.Number(value=10, label="Number of Rows", step=1)
n_cols = gr.Number(value=20, label="Number of Columns", step=1)
n_clusters = gr.Number(value=2, label="Number of Clusters", step=1)
with gr.Row():
noise = gr.Number(value=0.0, label="Noise")
minval = gr.Number(value=10, label="Minimum Value")
maxval = gr.Number(value=100, label="Maximum Value")
with gr.Row():
random_state = gr.Number(value=None, label="Random State (None for random)")
with gr.Row():
generate_btn = gr.Button("Generate")
with gr.Row():
output_image = gr.Image(label="Plot")
# Add textbox for displaying original make_biclusters Python code
with gr.Row():
sklearn_code_output = gr.Textbox(label="Python Code", lines=23, interactive=False)
with gr.Row():
output_data_unshuffled = gr.Dataframe(label="Unshuffled Bicluster Array")
output_data_shuffled = gr.Dataframe(label="Shuffled Bicluster Array")
with gr.Row():
output_cols_unshuffled = gr.Dataframe(label="Unshuffled Column Clusters")
output_cols_shuffled = gr.Dataframe(label="Shuffled Column Clusters")
with gr.Row():
output_rows_unshuffled = gr.Dataframe(label="Unshuffled Row Clusters")
output_rows_shuffled = gr.Dataframe(label="Shuffled Row Clusters")
# Function to generate the original scikit-learn make_biclusters code
def generate_sklearn_code(n_rows, n_cols, n_clusters, noise, minval, maxval, random_state):
random_state_str = 'None' if random_state is None else random_state
code = (
f"from sklearn.datasets import make_biclusters\n\n"
f"# Unshuffled\n"
f"X_unshuffled, rows_unshuffled, columns_unshuffled = make_biclusters(\n"
f" shape=({n_rows}, {n_cols}),\n"
f" n_clusters={n_clusters},\n"
f" noise={noise},\n"
f" minval={minval},\n"
f" maxval={maxval},\n"
f" shuffle=False,\n"
f" random_state={random_state_str}\n"
f")\n\n"
f"# Shuffled\n"
f"X_shuffled, rows_shuffled, columns_shuffled = make_biclusters(\n"
f" shape=({n_rows}, {n_cols}),\n"
f" n_clusters={n_clusters},\n"
f" noise={noise},\n"
f" minval={minval},\n"
f" maxval={maxval},\n"
f" shuffle=True,\n"
f" random_state={random_state_str}\n"
f")"
)
return code
# Logic to handle input and call generate_biclusters
def handle_output(n_rows, n_cols, n_clusters, noise, minval, maxval, random_state):
# Input validation
if n_rows <= 0 or n_cols <= 0 or n_clusters <= 0:
return "Error: Number of rows, columns, and clusters must be positive integers.", None, None, None, None, None, None, ""
if n_clusters > min(n_rows, n_cols):
return "Error: Number of clusters cannot exceed the number of rows or columns.", None, None, None, None, None, None, ""
if minval >= maxval:
return "Error: Minimum value must be less than maximum value.", None, None, None, None, None, None, ""
# Convert random_state to integer if provided
random_state = int(random_state) if random_state is not None else None
try:
# Generate biclusters
image, data_unshuffled, data_shuffled, rows_unshuffled, cols_unshuffled, rows_shuffled, cols_shuffled = generate_biclusters(
n_rows=int(n_rows), n_cols=int(n_cols), n_clusters=int(n_clusters),
noise=float(noise), minval=float(minval), maxval=float(maxval),
random_state=random_state
)
# Generate the original make_biclusters function call
sklearn_code = generate_sklearn_code(n_rows, n_cols, n_clusters, noise, minval, maxval, random_state)
return image, data_unshuffled, data_shuffled, rows_unshuffled, cols_unshuffled, rows_shuffled, cols_shuffled, sklearn_code
except Exception as e:
return f"An error occurred: {str(e)}", None, None, None, None, None, None, ""
generate_btn.click(
fn=handle_output,
inputs=[n_rows, n_cols, n_clusters, noise, minval, maxval, random_state],
outputs=[output_image, output_data_unshuffled, output_data_shuffled, output_rows_unshuffled, output_cols_unshuffled, output_rows_shuffled, output_cols_shuffled, sklearn_code_output]
)
# Launch the app
demo.launch()