I have created the following script to fetch certain tables from certain runs as artifacts. While the artifacts are found I cannot download them as I lack permissions although I am logged in. I tried with my project set in all 3 visibility settings
def fetch_and_plot_confusion_matrix_from_artifact(project_name, run_name, table_key="Training Confusion Matrix"):
"""
Fetches a confusion matrix stored as a table artifact in W&B and plots it.
Parameters:
- project_name: Full W&B project name (e.g., 'entity/project_name').
- run_name: The name of the run to fetch.
- table_key: The key in the summary field containing the confusion matrix table.
Returns:
- None (plots and saves the confusion matrix).
"""
api = wandb.Api()
try:
# Find the specific run by name
runs = api.runs(project_name)
for run in runs:
if run.name == run_name:
print(f"Found run: {run_name} (ID: {run.id})")
# Fetch the table metadata from the summary
table_metadata = run.summary.get(table_key)
if not table_metadata:
print(f"Table key '{table_key}' not found in summary.")
return
# Extract the artifact collection name and alias
artifact_collection = "Training Confusion Matrix" # Replace with your collection name
artifact_alias = "v0" # Replace with the desired alias (e.g., "v0", "latest")
print(f"Fetching artifact: {artifact_collection}:{artifact_alias}")
# Fetch the artifact explicitly
artifact = api.artifact(f"{project_name}/{artifact_collection}:{artifact_alias}")
artifact_dir = artifact.download()
# Locate the table JSON file and load its contents
table_json_path = os.path.join(artifact_dir, table_metadata["path"])
with open(table_json_path, "r") as f:
table_data = json.load(f)
# Extract the confusion matrix and class names
columns = table_data["columns"] # Columns of the table
data = table_data["data"] # Rows of the table
class_names = [row[0] for row in data] # First column contains class names
confusion_matrix = np.array([row[1:] for row in data]) # Remaining columns contain the matrix
print(f"Confusion Matrix:\n{confusion_matrix}")
print(f"Class Names: {class_names}")
# Plot the confusion matrix using your provided function
fig = plotNSaveConfusion(confusion_matrix, class_names, "reconstructed_confusion_matrix", "Reconstructed Confusion Matrix")
# Save and optionally display the figure
fig.write_html("reconstructed_confusion_matrix.html")
fig.show()
return
print(f"Run with name '{run_name}' not found in project '{project_name}'.")
except Exception as e:
print(f"Error fetching or plotting confusion matrix: {e}")
# Example usage
wandb.login()
project_name = "kuthesis/KU AI Thesis" # Replace with your W&B project name
run_name = "dry-bee-1130" # Replace with your W&B run name
fetch_and_plot_confusion_matrix_from_artifact(project_name, run_name, table_key="Training Confusion Matrix")
If you could offer any help I would appreciate it.