Update app.py
Browse files
app.py
CHANGED
|
@@ -56,6 +56,17 @@ SOFT_EXCLUDE_PATTERNS = ["ideal_", "noisy_", "sign_ideal_", "sign_noisy_"]
|
|
| 56 |
|
| 57 |
_ASSET_CACHE: Dict[str, pd.DataFrame] = {}
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
def safe_parse(value):
|
| 61 |
"""Safely parse stringified Python literals."""
|
|
@@ -66,7 +77,6 @@ def safe_parse(value):
|
|
| 66 |
return value
|
| 67 |
return value
|
| 68 |
|
| 69 |
-
|
| 70 |
def adjacency_features(adj_value) -> Dict[str, float]:
|
| 71 |
"""Derive graph statistics from an adjacency matrix."""
|
| 72 |
parsed = safe_parse(adj_value)
|
|
@@ -98,7 +108,6 @@ def adjacency_features(adj_value) -> Dict[str, float]:
|
|
| 98 |
"adj_degree_std": np.nan,
|
| 99 |
}
|
| 100 |
|
| 101 |
-
|
| 102 |
def qasm_features(qasm_value) -> Dict[str, float]:
|
| 103 |
"""Extract lightweight text statistics from QASM."""
|
| 104 |
if not isinstance(qasm_value, str) or not qasm_value.strip():
|
|
@@ -123,7 +132,6 @@ def qasm_features(qasm_value) -> Dict[str, float]:
|
|
| 123 |
"qasm_measure_count": float(measure_count),
|
| 124 |
}
|
| 125 |
|
| 126 |
-
|
| 127 |
def enrich_dataframe(df: pd.DataFrame) -> pd.DataFrame:
|
| 128 |
"""Add derived numeric features and compute error targets."""
|
| 129 |
df = df.copy()
|
|
@@ -143,9 +151,8 @@ def enrich_dataframe(df: pd.DataFrame) -> pd.DataFrame:
|
|
| 143 |
df[error_col] = df[noisy_col] - df[ideal_col]
|
| 144 |
return df
|
| 145 |
|
| 146 |
-
|
| 147 |
def load_single_dataset() -> pd.DataFrame:
|
| 148 |
-
"""Fetch
|
| 149 |
key = "amplitude_damping"
|
| 150 |
if key not in _ASSET_CACHE:
|
| 151 |
logger.info("Loading dataset: %s", key)
|
|
@@ -155,7 +162,6 @@ def load_single_dataset() -> pd.DataFrame:
|
|
| 155 |
_ASSET_CACHE[key] = df
|
| 156 |
return _ASSET_CACHE[key]
|
| 157 |
|
| 158 |
-
|
| 159 |
def get_available_feature_columns(df: pd.DataFrame) -> List[str]:
|
| 160 |
"""Retrieve filtered list of numerical feature columns."""
|
| 161 |
numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
|
|
@@ -168,7 +174,6 @@ def get_available_feature_columns(df: pd.DataFrame) -> List[str]:
|
|
| 168 |
features.append(col)
|
| 169 |
return sorted(features)
|
| 170 |
|
| 171 |
-
|
| 172 |
def default_feature_selection(features: List[str]) -> List[str]:
|
| 173 |
"""Provide a curated list of default structural features."""
|
| 174 |
preferred = [
|
|
@@ -179,7 +184,6 @@ def default_feature_selection(features: List[str]) -> List[str]:
|
|
| 179 |
selected = [f for f in preferred if f in features]
|
| 180 |
return selected[:10] if selected else features[:10]
|
| 181 |
|
| 182 |
-
|
| 183 |
def make_regression_figure(y_true: np.ndarray, y_pred: np.ndarray, basis: str) -> plt.Figure:
|
| 184 |
"""Generate diagnostic regression plots."""
|
| 185 |
fig, axs = plt.subplots(1, 2, figsize=(14, 6))
|
|
@@ -202,7 +206,6 @@ def make_regression_figure(y_true: np.ndarray, y_pred: np.ndarray, basis: str) -
|
|
| 202 |
fig.tight_layout()
|
| 203 |
return fig
|
| 204 |
|
| 205 |
-
|
| 206 |
def train_regressor(
|
| 207 |
feature_columns: List[str],
|
| 208 |
test_size: float,
|
|
@@ -267,8 +270,8 @@ def train_regressor(
|
|
| 267 |
|
| 268 |
return fig_z, metrics_text, fig_x, fig_y
|
| 269 |
|
| 270 |
-
|
| 271 |
# ======================= EXPLORER FUNCTIONS =======================
|
|
|
|
| 272 |
def build_dataset_profile(df: pd.DataFrame) -> str:
|
| 273 |
"""Generate Markdown summary of the loaded dataset."""
|
| 274 |
return (
|
|
@@ -278,7 +281,6 @@ def build_dataset_profile(df: pd.DataFrame) -> str:
|
|
| 278 |
f"**Classes / Noise:** amplitude_damping"
|
| 279 |
)
|
| 280 |
|
| 281 |
-
|
| 282 |
def refresh_explorer(dataset_key: str, split_name: str):
|
| 283 |
"""Update Explorer tab components."""
|
| 284 |
df = load_single_dataset()
|
|
@@ -312,6 +314,7 @@ def refresh_explorer(dataset_key: str, split_name: str):
|
|
| 312 |
summary_box,
|
| 313 |
)
|
| 314 |
|
|
|
|
| 315 |
|
| 316 |
CUSTOM_CSS = """
|
| 317 |
.gradio-container {
|
|
@@ -366,7 +369,7 @@ with gr.Blocks(title=APP_TITLE) as demo:
|
|
| 366 |
metrics = gr.Markdown()
|
| 367 |
|
| 368 |
with gr.TabItem("📖 Guide"):
|
| 369 |
-
gr.Markdown(
|
| 370 |
|
| 371 |
gr.Markdown("---")
|
| 372 |
gr.Markdown(
|
|
@@ -409,6 +412,5 @@ with gr.Blocks(title=APP_TITLE) as demo:
|
|
| 409 |
)
|
| 410 |
demo.load(sync_features, [dataset_dropdown], [feature_picker])
|
| 411 |
|
| 412 |
-
|
| 413 |
if __name__ == "__main__":
|
| 414 |
demo.launch(theme=gr.themes.Soft(), css=CUSTOM_CSS)
|
|
|
|
| 56 |
|
| 57 |
_ASSET_CACHE: Dict[str, pd.DataFrame] = {}
|
| 58 |
|
| 59 |
+
# ========================= HELPERS =========================
|
| 60 |
+
|
| 61 |
+
def load_guide_content() -> str:
|
| 62 |
+
"""
|
| 63 |
+
Read the GUIDE.md file from the root directory.
|
| 64 |
+
"""
|
| 65 |
+
try:
|
| 66 |
+
with open("GUIDE.md", "r", encoding="utf-8") as f:
|
| 67 |
+
return f.read()
|
| 68 |
+
except FileNotFoundError:
|
| 69 |
+
return "### ⚠️ GUIDE.md not found in the root directory."
|
| 70 |
|
| 71 |
def safe_parse(value):
|
| 72 |
"""Safely parse stringified Python literals."""
|
|
|
|
| 77 |
return value
|
| 78 |
return value
|
| 79 |
|
|
|
|
| 80 |
def adjacency_features(adj_value) -> Dict[str, float]:
|
| 81 |
"""Derive graph statistics from an adjacency matrix."""
|
| 82 |
parsed = safe_parse(adj_value)
|
|
|
|
| 108 |
"adj_degree_std": np.nan,
|
| 109 |
}
|
| 110 |
|
|
|
|
| 111 |
def qasm_features(qasm_value) -> Dict[str, float]:
|
| 112 |
"""Extract lightweight text statistics from QASM."""
|
| 113 |
if not isinstance(qasm_value, str) or not qasm_value.strip():
|
|
|
|
| 132 |
"qasm_measure_count": float(measure_count),
|
| 133 |
}
|
| 134 |
|
|
|
|
| 135 |
def enrich_dataframe(df: pd.DataFrame) -> pd.DataFrame:
|
| 136 |
"""Add derived numeric features and compute error targets."""
|
| 137 |
df = df.copy()
|
|
|
|
| 151 |
df[error_col] = df[noisy_col] - df[ideal_col]
|
| 152 |
return df
|
| 153 |
|
|
|
|
| 154 |
def load_single_dataset() -> pd.DataFrame:
|
| 155 |
+
"""Fetch and cache the dataset."""
|
| 156 |
key = "amplitude_damping"
|
| 157 |
if key not in _ASSET_CACHE:
|
| 158 |
logger.info("Loading dataset: %s", key)
|
|
|
|
| 162 |
_ASSET_CACHE[key] = df
|
| 163 |
return _ASSET_CACHE[key]
|
| 164 |
|
|
|
|
| 165 |
def get_available_feature_columns(df: pd.DataFrame) -> List[str]:
|
| 166 |
"""Retrieve filtered list of numerical feature columns."""
|
| 167 |
numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
|
|
|
|
| 174 |
features.append(col)
|
| 175 |
return sorted(features)
|
| 176 |
|
|
|
|
| 177 |
def default_feature_selection(features: List[str]) -> List[str]:
|
| 178 |
"""Provide a curated list of default structural features."""
|
| 179 |
preferred = [
|
|
|
|
| 184 |
selected = [f for f in preferred if f in features]
|
| 185 |
return selected[:10] if selected else features[:10]
|
| 186 |
|
|
|
|
| 187 |
def make_regression_figure(y_true: np.ndarray, y_pred: np.ndarray, basis: str) -> plt.Figure:
|
| 188 |
"""Generate diagnostic regression plots."""
|
| 189 |
fig, axs = plt.subplots(1, 2, figsize=(14, 6))
|
|
|
|
| 206 |
fig.tight_layout()
|
| 207 |
return fig
|
| 208 |
|
|
|
|
| 209 |
def train_regressor(
|
| 210 |
feature_columns: List[str],
|
| 211 |
test_size: float,
|
|
|
|
| 270 |
|
| 271 |
return fig_z, metrics_text, fig_x, fig_y
|
| 272 |
|
|
|
|
| 273 |
# ======================= EXPLORER FUNCTIONS =======================
|
| 274 |
+
|
| 275 |
def build_dataset_profile(df: pd.DataFrame) -> str:
|
| 276 |
"""Generate Markdown summary of the loaded dataset."""
|
| 277 |
return (
|
|
|
|
| 281 |
f"**Classes / Noise:** amplitude_damping"
|
| 282 |
)
|
| 283 |
|
|
|
|
| 284 |
def refresh_explorer(dataset_key: str, split_name: str):
|
| 285 |
"""Update Explorer tab components."""
|
| 286 |
df = load_single_dataset()
|
|
|
|
| 314 |
summary_box,
|
| 315 |
)
|
| 316 |
|
| 317 |
+
# ========================= INTERFACE =========================
|
| 318 |
|
| 319 |
CUSTOM_CSS = """
|
| 320 |
.gradio-container {
|
|
|
|
| 369 |
metrics = gr.Markdown()
|
| 370 |
|
| 371 |
with gr.TabItem("📖 Guide"):
|
| 372 |
+
gr.Markdown(load_guide_content())
|
| 373 |
|
| 374 |
gr.Markdown("---")
|
| 375 |
gr.Markdown(
|
|
|
|
| 412 |
)
|
| 413 |
demo.load(sync_features, [dataset_dropdown], [feature_picker])
|
| 414 |
|
|
|
|
| 415 |
if __name__ == "__main__":
|
| 416 |
demo.launch(theme=gr.themes.Soft(), css=CUSTOM_CSS)
|