optimize dashboard
This commit is contained in:
@@ -629,3 +629,111 @@ plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# %% Dashboard
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
import seaborn as sns
|
||||
from datetime import datetime
|
||||
import numpy as np
|
||||
|
||||
# Load the data
|
||||
file_path = '/home/shahin/Lab/Doktorarbeit/Barcelona/Data/Join_edssandsub.tsv'
|
||||
df = pd.read_csv(file_path, sep='\t')
|
||||
|
||||
# Rename columns to remove 'result.' prefix and handle spaces
|
||||
column_mapping = {}
|
||||
for col in df.columns:
|
||||
if col.startswith('result.'):
|
||||
new_name = col.replace('result.', '')
|
||||
# Handle spaces in column names (replace with underscores if needed)
|
||||
new_name = new_name.replace(' ', '_')
|
||||
column_mapping[col] = new_name
|
||||
df = df.rename(columns=column_mapping)
|
||||
|
||||
# Convert MedDatum to datetime
|
||||
df['MedDatum'] = pd.to_datetime(df['MedDatum'])
|
||||
|
||||
# Check what columns actually exist in the dataset
|
||||
print("Available columns:")
|
||||
print(df.columns.tolist())
|
||||
print("\nFirst few rows:")
|
||||
print(df.head())
|
||||
|
||||
# Hardcode specific patient names
|
||||
patient_names = ['6ccda8c6']
|
||||
|
||||
# Define the functional systems (columns to plot) - adjust based on actual column names
|
||||
functional_systems = ['EDSS', 'Visual', 'Sensory', 'Motor', 'Brainstem', 'Cerebellar', 'Autonomic', 'Bladder', 'Intellectual']
|
||||
|
||||
# Create subplots horizontally (2 columns, adjust rows as needed)
|
||||
num_plots = len(functional_systems)
|
||||
num_cols = 2
|
||||
num_rows = (num_plots + num_cols - 1) // num_cols # Ceiling division
|
||||
|
||||
fig, axes = plt.subplots(num_rows, num_cols, figsize=(15, 4*num_rows), sharex=True)
|
||||
if num_plots == 1:
|
||||
axes = [axes]
|
||||
elif num_rows == 1:
|
||||
axes = axes
|
||||
else:
|
||||
axes = axes.flatten()
|
||||
|
||||
# Plot for the hardcoded patient
|
||||
for i, system in enumerate(functional_systems):
|
||||
# Filter data for this specific patient
|
||||
patient_data = df[df['unique_id'] == patient_names[0]].sort_values('MedDatum')
|
||||
|
||||
# Check if patient data exists
|
||||
if patient_data.empty:
|
||||
print(f"No data found for patient: {patient_names[0]}")
|
||||
continue
|
||||
|
||||
# Check if the system column exists in the data
|
||||
if system in patient_data.columns:
|
||||
# Plot the specific functional system
|
||||
if not patient_data[system].isna().all():
|
||||
axes[i].plot(patient_data['MedDatum'], patient_data[system], marker='o', linewidth=2, label=system)
|
||||
axes[i].set_ylabel('Score')
|
||||
axes[i].set_title(f'Functional System: {system}')
|
||||
axes[i].grid(True, alpha=0.3)
|
||||
axes[i].legend()
|
||||
else:
|
||||
axes[i].set_title(f'Functional System: {system} (No data)')
|
||||
axes[i].set_ylabel('Score')
|
||||
axes[i].grid(True, alpha=0.3)
|
||||
else:
|
||||
# Try to find column with similar name (case insensitive)
|
||||
found_column = None
|
||||
for col in df.columns:
|
||||
if system.lower() in col.lower():
|
||||
found_column = col
|
||||
break
|
||||
|
||||
if found_column:
|
||||
print(f"Found similar column: {found_column}")
|
||||
if not patient_data[found_column].isna().all():
|
||||
axes[i].plot(patient_data['MedDatum'], patient_data[found_column], marker='o', linewidth=2, label=found_column)
|
||||
axes[i].set_ylabel('Score')
|
||||
axes[i].set_title(f'Functional System: {system} (found as: {found_column})')
|
||||
axes[i].grid(True, alpha=0.3)
|
||||
axes[i].legend()
|
||||
else:
|
||||
axes[i].set_title(f'Functional System: {system} (Column not found)')
|
||||
axes[i].set_ylabel('Score')
|
||||
axes[i].grid(True, alpha=0.3)
|
||||
|
||||
# Hide empty subplots
|
||||
for i in range(len(functional_systems), len(axes)):
|
||||
axes[i].set_visible(False)
|
||||
|
||||
# Set x-axis label for the last row only
|
||||
for i in range(len(functional_systems)):
|
||||
if i >= len(axes) - num_cols: # Last row
|
||||
axes[i].set_xlabel('Date')
|
||||
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
|
||||
##
|
||||
|
||||
Reference in New Issue
Block a user