Skip to content

Commit

Permalink
table_init
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgiving committed Jun 1, 2024
1 parent 277ec01 commit 42e7be2
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ experiments/
*__pycache__
.DS_Store
.vscode
logs
logs
data
7 changes: 7 additions & 0 deletions report/argument_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from pathlib import Path

from tap import Tap


class ArgumentParser(Tap):
config: Path
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions report/log_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import json
from pathlib import Path
from typing import Dict


class LogParser:
def parse(self, log_path: Path) -> Dict[str, float]:
with Path(log_path).open() as log_buff:
log_data = log_buff.read()
log_data = log_data.replace('\'', '"')
return json.loads(log_data)
Empty file added report/tables/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions report/tables/highlight_rule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from enum import Enum
from typing import Dict


class HighlightRule(Enum):
MAX = 'max'
MIN = 'min'
NONE = 'none'


def get_highlight_rules() -> Dict[str, HighlightRule]:
return {
'log_name': HighlightRule.NONE,
'ari': HighlightRule.MAX,
'ami': HighlightRule.MAX,
'completeness': HighlightRule.MAX,
'homogeneity': HighlightRule.MAX,
'nmi': HighlightRule.MAX,
'v_measure': HighlightRule.MAX,
'accuracy': HighlightRule.MAX,
'time': HighlightRule.MIN,
'inertia': HighlightRule.MIN
}
41 changes: 41 additions & 0 deletions report/tables/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json
from typing import Dict

import pandas as pd

from report.log_parser import LogParser
from report.tables.highlight_rule import get_highlight_rules
from report.tables.saver import LatexSaver
from report.tables.styler import TableStyler
from report.tables.table_argument_parser import TableArgumentParser


def get_data_from_config(json_data: Dict) -> pd.DataFrame:
data = []
parser = LogParser()
for log_name, log_path in json_data['logs'].items():
log_data_dict = parser.parse(log_path)
log_data_dict = {'log_name': log_name, **log_data_dict}
data.append(log_data_dict)
return data


def main() -> None:
args = TableArgumentParser(underscores_to_dashes=True).parse_args()
with args.config.open() as file:
json_data = json.load(file)

args.save_path.mkdir(parents=True, exist_ok=True)
saver = LatexSaver(args.save_path / json_data['name'])

data = get_data_from_config(json_data)
data_frame = pd.DataFrame(data)

rules = get_highlight_rules()
styler = TableStyler(data_frame, json_data['columns'], rules).style()
saver.save(styler)



if __name__ == '__main__':
main()
23 changes: 23 additions & 0 deletions report/tables/saver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from abc import ABC
from pathlib import Path

from pandas.io.formats.style import Styler


class Saver(ABC):
def __init__(self, file_name: Path) -> None:
self._file_name = file_name
self._convert_path()

def save(styler: Styler) -> None:
...


class LatexSaver(Saver):

def _convert_path(self) -> None:
self._file_name = self._file_name.with_suffix('.tex')

def save(self, styler: Styler) -> None:
with self._file_name.open('w') as file:
styler.format(escape='latex', precision=2).to_latex(file, convert_css=True)
51 changes: 51 additions & 0 deletions report/tables/styler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from typing import Any, Dict, List

import pandas as pd
from pandas.io.formats.style import Styler

from report.tables.highlight_rule import HighlightRule

FORMAT_BOLD ='font-weight:bold;'


class TableStyler:
def __init__(self, data_frame: pd.DataFrame, columns: List[str], rules: Dict[str, Any]) -> None:
self._data_frame = data_frame
self._columns = columns
self._rules = rules

def _highlight_max(self, styler: Styler) -> Styler:
columns_for_highlight = [name for name, highlight_rule in self._rules.items() \
if highlight_rule is HighlightRule.MAX]
columns = list(set(self._columns).intersection(set(columns_for_highlight)))
return styler.highlight_max(subset=columns, props=FORMAT_BOLD)

def _highlight_min(self, styler: Styler) -> Styler:
columns_for_highlight = [name for name, highlight_rule in self._rules.items() \
if highlight_rule is HighlightRule.MIN]
columns = list(set(self._columns).intersection(set(columns_for_highlight)))
return styler.highlight_min(subset=columns, props=FORMAT_BOLD)

def _round_values(self, styler: Styler) -> Styler:
columns_for_rounding = [name for name, highlight_rule in self._rules.items() \
if highlight_rule is not HighlightRule.NONE]
columns = list(set(self._columns).intersection(set(columns_for_rounding)))
func = lambda value: f'{value:.2f}'
return styler.format(func, na_rep='N/A', subset=columns)

def _hide_index(self, styler: Styler) -> Styler:
return styler.hide()

def _highlight_index(self, styler: Styler) -> Styler:
return styler.applymap_index(lambda _: FORMAT_BOLD, axis='columns')

def style(self) -> Styler:
frame = self._data_frame[self._columns]
styler = frame.style

styler = self._highlight_max(styler)
styler = self._highlight_min(styler)
styler = self._hide_index(styler)
styler = self._highlight_index(styler)
styler = self._round_values(styler)
return styler
7 changes: 7 additions & 0 deletions report/tables/table_argument_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from pathlib import Path

from report.argument_parser import ArgumentParser


class TableArgumentParser(ArgumentParser):
save_path: Path = Path('./data/tables')

0 comments on commit 42e7be2

Please sign in to comment.