102 lines
2.6 KiB
Python
102 lines
2.6 KiB
Python
import pandas as pd
|
|
import plotly.graph_objects as go
|
|
import sys
|
|
import os
|
|
|
|
|
|
# --- KONFIGURATION & DATEIPFAD ---
|
|
|
|
# Prüfen, ob ein Pfad als Argument übergeben wurde, sonst Default nutzen
|
|
|
|
if len(sys.argv) > 1:
|
|
csv_dateipfad = sys.argv[1]
|
|
else:
|
|
csv_dateipfad = '/output/relations.csv'
|
|
|
|
# Sicherstellen, dass die Datei existiert
|
|
|
|
if not os.path.exists(csv_dateipfad):
|
|
print(f"Fehler: Die Datei '{csv_dateipfad}'wurde nicht gefunden.")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
# --- DATEN LADEN ---
|
|
try:
|
|
|
|
# Wir laden die CSV (Trennzeichen ; ist im deutschen Excel-Raum Standard)
|
|
df =pd.read_csv(csv_dateipfad, sep=None, engine='python')
|
|
print(f"Daten erfolgreich geladen: {len(df)} Zeile aus '{csv_dateipfad}'.")
|
|
|
|
except Exception as e:
|
|
print(f"Fehler beim Lesen der CSV: {e}")
|
|
sys.exit(1)
|
|
|
|
|
|
# --- DATEN AGGREGIEREN ---
|
|
# Wir zählen die Kommunikationspfade zwischen den Abteilungen
|
|
|
|
''' df_agg = df.groupby(['source_department', 'destination_department']).size().reset_index(name='weight') '''
|
|
df_agg = df.groupby(['source_department', 'destination_displayname']).size().reset_index(name='weight')
|
|
|
|
# Liste aller Departments für die Knoten-Beschriftung
|
|
''' all_nodes = list(pd.concat([df_agg['source_department'], df_agg['destination_department']]).unique()) '''
|
|
all_nodes = list(pd.concat([df_agg['source_department'], df_agg['destination_displayname']]).unique())
|
|
|
|
node_map = {name: i for i, name in enumerate(all_nodes)}
|
|
|
|
# Mapping auf Indizes
|
|
source_indices = df_agg['source_department'].map(node_map)
|
|
target_indices = df_agg['destination_displayname'].map(node_map)
|
|
''' target_indices = df_agg['destination_department'].map(node_map) '''
|
|
|
|
weights = df_agg['weight']
|
|
|
|
|
|
# --- FARBGESTALTUNG ---
|
|
# Wir färben die "Source"-Abteilungen (dein Team) anders ein als die "Ziele"
|
|
node_colors = ["#1f77b4"
|
|
if node in df['source_department'].unique() else "#9467bd" for node in all_nodes]
|
|
|
|
# --- VISUALISIERUNG ---
|
|
fig = go.Figure(data=[go.Sankey(
|
|
node=dict(
|
|
pad=20,
|
|
thickness=30,
|
|
line=dict(color="black", width=0.5),
|
|
label=all_nodes,
|
|
color=node_colors
|
|
),
|
|
|
|
link=dict(
|
|
source=source_indices,
|
|
target=target_indices,
|
|
value=weights,
|
|
color="rgba(200, 200, 200, 0.5)"
|
|
# Transparente graue Pfade
|
|
|
|
)
|
|
|
|
)])
|
|
|
|
|
|
|
|
fig.update_layout(
|
|
title_text=f"Organisatorische Netzwerkanalyse: Abteilungs-Flüsse<br><sup>Quelle: {csv_dateipfad}</sup>",
|
|
font_size=12,
|
|
height=800
|
|
)
|
|
|
|
# --- AUSGABE ---
|
|
output_filename = "netzwerk_analyse_lokal.html"
|
|
|
|
fig.write_html(output_filename)
|
|
|
|
print(f"Analyse abgeschlossen. Interaktives Diagramm gespeichert unter: {output_filename}")
|
|
|
|
# Automatisches Öffnen im Standardbrowser
|
|
import webbrowser
|
|
|
|
webbrowser.open('file://'+ os.path.realpath(output_filename))
|
|
|