22import logging
33import re
44
5- from tabulate import tabulate
5+ import numpy as np
6+ from rich .console import Console
7+ from rich .table import Table
8+
9+ from CodeEntropy .config .logging_config import LoggingConfig
610
711# Set up logger
812logger = logging .getLogger (__name__ )
13+ console = LoggingConfig .get_console ()
914
1015
1116class DataLogger :
12- def __init__ (self ):
17+ def __init__ (self , console = None ):
18+ self .console = console or Console ()
1319 self .molecule_data = []
1420 self .residue_data = []
21+ self .group_labels = {}
1522
1623 def save_dataframes_as_json (self , molecule_df , residue_df , output_file ):
1724 """Save multiple DataFrames into a single JSON file with separate keys"""
@@ -28,44 +35,75 @@ def clean_residue_name(self, resname):
2835 """Ensures residue names are stripped and cleaned before being stored"""
2936 return re .sub (r"[-–—]" , "" , str (resname ))
3037
31- def add_results_data (self , resname , level , entropy_type , value ):
38+ def add_results_data (self , group_id , level , entropy_type , value ):
3239 """Add data for molecule-level entries"""
33- resname = self .clean_residue_name (resname )
34- self .molecule_data .append ((resname , level , entropy_type , value ))
40+ self .molecule_data .append ((group_id , level , entropy_type , value ))
3541
36- def add_residue_data (self , resid , resname , level , entropy_type , value ):
42+ def add_residue_data (
43+ self , group_id , resname , level , entropy_type , frame_count , value
44+ ):
3745 """Add data for residue-level entries"""
3846 resname = self .clean_residue_name (resname )
39- self .residue_data .append ([resid , resname , level , entropy_type , value ])
47+ if isinstance (frame_count , np .ndarray ):
48+ frame_count = frame_count .tolist ()
49+ self .residue_data .append (
50+ [group_id , resname , level , entropy_type , frame_count , value ]
51+ )
52+
53+ def add_group_label (self , group_id , label , residue_count = None , atom_count = None ):
54+ """Store a mapping from group ID to a descriptive label and metadata"""
55+ self .group_labels [group_id ] = {
56+ "label" : label ,
57+ "residue_count" : residue_count ,
58+ "atom_count" : atom_count ,
59+ }
4060
4161 def log_tables (self ):
42- """Log both tables at once """
43- # Log molecule data
62+ """Display rich tables in terminal """
63+
4464 if self .molecule_data :
45- logger .info ("Molecule Data Table:" )
46- table_str = tabulate (
47- self .molecule_data ,
48- headers = ["Residue Name" , "Level" , "Type" , "Result (J/mol/K)" ],
49- tablefmt = "grid" ,
50- numalign = "center" ,
51- stralign = "center" ,
65+ table = Table (
66+ title = "Molecule Entropy Results" , show_lines = True , expand = True
5267 )
53- logger .info (f"\n { table_str } " )
68+ table .add_column ("Group ID" , justify = "center" , style = "bold cyan" )
69+ table .add_column ("Level" , justify = "center" , style = "magenta" )
70+ table .add_column ("Type" , justify = "center" , style = "green" )
71+ table .add_column ("Result (J/mol/K)" , justify = "center" , style = "yellow" )
72+
73+ for row in self .molecule_data :
74+ table .add_row (* [str (cell ) for cell in row ])
75+
76+ console .print (table )
5477
55- # Log residue data
5678 if self .residue_data :
57- logger .info ("Residue Data Table:" )
58- table_str = tabulate (
59- self .residue_data ,
60- headers = [
61- "Residue ID" ,
62- "Residue Name" ,
63- "Level" ,
64- "Type" ,
65- "Result (J/mol/K)" ,
66- ],
67- tablefmt = "grid" ,
68- numalign = "center" ,
69- stralign = "center" ,
79+ table = Table (title = "Residue Entropy Results" , show_lines = True , expand = True )
80+ table .add_column ("Group ID" , justify = "center" , style = "bold cyan" )
81+ table .add_column ("Residue Name" , justify = "center" , style = "cyan" )
82+ table .add_column ("Level" , justify = "center" , style = "magenta" )
83+ table .add_column ("Type" , justify = "center" , style = "green" )
84+ table .add_column ("Count" , justify = "center" , style = "green" )
85+ table .add_column ("Result (J/mol/K)" , justify = "center" , style = "yellow" )
86+
87+ for row in self .residue_data :
88+ table .add_row (* [str (cell ) for cell in row ])
89+
90+ console .print (table )
91+
92+ if self .group_labels :
93+ label_table = Table (
94+ title = "Group ID to Residue Label Mapping" , show_lines = True , expand = True
7095 )
71- logger .info (f"\n { table_str } " )
96+ label_table .add_column ("Group ID" , justify = "center" , style = "bold cyan" )
97+ label_table .add_column ("Residue Label" , justify = "center" , style = "green" )
98+ label_table .add_column ("Residue Count" , justify = "center" , style = "magenta" )
99+ label_table .add_column ("Atom Count" , justify = "center" , style = "yellow" )
100+
101+ for group_id , info in self .group_labels .items ():
102+ label_table .add_row (
103+ str (group_id ),
104+ info ["label" ],
105+ str (info .get ("residue_count" , "" )),
106+ str (info .get ("atom_count" , "" )),
107+ )
108+
109+ console .print (label_table )
0 commit comments