Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

utils.py 1.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  1. """
  2. Just utility functions
  3. """
  4. from pathlib import Path
  5. from typing import *
  6. import pandas as pd
  7. from IPython.display import HTML, display
  8. from dataclasses import dataclass
  9. import numpy as np
  10. @dataclass
  11. class Table:
  12. @staticmethod
  13. def from_rows(rows: List[List[str]]):
  14. return Table(rows[0], rows[1:])
  15. headers: List[str]
  16. body: List[List[str]]
  17. caption: str = ''
  18. @property
  19. def rows(self) -> str:
  20. return "".join(['<tr><td>'+'</td><td>'.join(row)+'</td></tr>' for row in self.body])
  21. def _repr_html_(self):
  22. '''
  23. Nice JupyterLab table HTML representation
  24. :return:
  25. '''
  26. return f"<table border='2'> {'' if self.caption == '' else '<caption>' + self.caption + '</caption>'}" \
  27. f"<tr><th>{'</th><th>'.join(self.headers)}</th></tr>" \
  28. f"{self.rows}" \
  29. f"</table>"
  30. def show(df: pd.DataFrame, cols: int, rows: int = 3) -> pd.DataFrame:
  31. return df[df.columns[0:cols]].head(rows)
  32. def less_or_value(n: np.ndarray, max: float = 0.0, value: float = 0.0):
  33. k = n.copy()
  34. k[k >= max] = value
  35. return k
  36. def more_or_value(n: np.ndarray, min: float = 0.0, value: float = 0.0):
  37. k = n.copy()
  38. k[k <= min] = value
  39. return k
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...