pyThermoDB 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. pyThermoDB/__init__.py +55 -0
  2. pyThermoDB/api/__init__.py +1 -0
  3. pyThermoDB/api/manage.py +68 -0
  4. pyThermoDB/app.py +212 -0
  5. pyThermoDB/builder/__init__.py +6 -0
  6. pyThermoDB/builder/comp_tools.py +174 -0
  7. pyThermoDB/builder/compbuilder.py +1112 -0
  8. pyThermoDB/builder/compexporter.py +207 -0
  9. pyThermoDB/config/__init__.py +12 -0
  10. pyThermoDB/config/constants.py +8 -0
  11. pyThermoDB/config/deps.py +33 -0
  12. pyThermoDB/config/main.py +1 -0
  13. pyThermoDB/config/reference.yml +183 -0
  14. pyThermoDB/config/setting.py +21 -0
  15. pyThermoDB/config/symbols.yml +43 -0
  16. pyThermoDB/core/__init__.py +14 -0
  17. pyThermoDB/core/table_util.py +206 -0
  18. pyThermoDB/core/tabledata.py +574 -0
  19. pyThermoDB/core/tableequation.py +1618 -0
  20. pyThermoDB/core/tablematrixdata.py +1665 -0
  21. pyThermoDB/core/tablematrixequation.py +1110 -0
  22. pyThermoDB/data/CO2 hydrogenation - General Data.csv +8 -0
  23. pyThermoDB/data/CO2 hydrogenation - The Molar Heat Capacities of Gases in the Ideal Gas (Zero-Pressure) State.csv +8 -0
  24. pyThermoDB/data/CO2 hydrogenation - Vapor Pressure.csv +8 -0
  25. pyThermoDB/data/__init__.py +3 -0
  26. pyThermoDB/data/ref.py +10 -0
  27. pyThermoDB/docs/__init__.py +19 -0
  28. pyThermoDB/docs/tableref.py +1015 -0
  29. pyThermoDB/docs/thermo.py +3908 -0
  30. pyThermoDB/handlers/__init__.py +0 -0
  31. pyThermoDB/handlers/errors.py +32 -0
  32. pyThermoDB/loader/__init__.py +6 -0
  33. pyThermoDB/loader/customref.py +677 -0
  34. pyThermoDB/manager/__init__.py +12 -0
  35. pyThermoDB/manager/main.py +181 -0
  36. pyThermoDB/manager/managedata.py +1550 -0
  37. pyThermoDB/models/__init__.py +45 -0
  38. pyThermoDB/models/conditions.py +31 -0
  39. pyThermoDB/models/configs.py +11 -0
  40. pyThermoDB/models/eq.py +68 -0
  41. pyThermoDB/models/property.py +24 -0
  42. pyThermoDB/models/ref.py +77 -0
  43. pyThermoDB/models/references.py +206 -0
  44. pyThermoDB/models/rules.py +4 -0
  45. pyThermoDB/models/tables.py +63 -0
  46. pyThermoDB/references/__init__.py +32 -0
  47. pyThermoDB/references/builder.py +291 -0
  48. pyThermoDB/references/checker.py +5184 -0
  49. pyThermoDB/references/config.py +122 -0
  50. pyThermoDB/references/content.py +143 -0
  51. pyThermoDB/references/databook.py +523 -0
  52. pyThermoDB/references/main.py +169 -0
  53. pyThermoDB/references/reference.py +353 -0
  54. pyThermoDB/references/reference_mapper.py +549 -0
  55. pyThermoDB/references/symbols_controller.py +201 -0
  56. pyThermoDB/references/table.py +391 -0
  57. pyThermoDB/thermodb.py +3265 -0
  58. pyThermoDB/transformer/__init__.py +8 -0
  59. pyThermoDB/transformer/transdata.py +72 -0
  60. pyThermoDB/transformer/transmatrixdata.py +96 -0
  61. pyThermoDB/utils/__init__.py +54 -0
  62. pyThermoDB/utils/component_data_extractor.py +283 -0
  63. pyThermoDB/utils/component_utils.py +382 -0
  64. pyThermoDB/utils/convertor.py +111 -0
  65. pyThermoDB/utils/core_utils.py +45 -0
  66. pyThermoDB/utils/data_utils.py +176 -0
  67. pyThermoDB/utils/equation_parser.py +470 -0
  68. pyThermoDB/utils/extractor.py +500 -0
  69. pyThermoDB/utils/file_manager.py +49 -0
  70. pyThermoDB/utils/logger.py +27 -0
  71. pyThermoDB/utils/prop_utils.py +45 -0
  72. pyThermoDB/utils/reference_utils.py +285 -0
  73. pyThermoDB/utils/result_generator.py +31 -0
  74. pyThermoDB/utils/utility.py +34 -0
  75. pyThermoDB/utils/validators.py +20 -0
  76. pythermodb-1.0.0.dist-info/METADATA +439 -0
  77. pythermodb-1.0.0.dist-info/RECORD +80 -0
  78. pythermodb-1.0.0.dist-info/WHEEL +5 -0
  79. pythermodb-1.0.0.dist-info/licenses/LICENSE +39 -0
  80. pythermodb-1.0.0.dist-info/top_level.txt +1 -0
pyThermoDB/__init__.py ADDED
@@ -0,0 +1,55 @@
1
+ from .config import __version__, __author__, __description__
2
+ from .core import (
3
+ TableEquation,
4
+ TableMatrixEquation,
5
+ TableData,
6
+ TableMatrixData
7
+ )
8
+ from .docs import ThermoDB
9
+ from .builder import CompBuilder
10
+ from .loader import CustomRef
11
+ from .manager import ManageData
12
+ from .app import (
13
+ init,
14
+ ref,
15
+ build_thermodb,
16
+ load_thermodb,
17
+ )
18
+ from .thermodb import (
19
+ build_component_thermodb,
20
+ build_components_thermodb,
21
+ build_component_thermodb_from_reference,
22
+ check_and_build_component_thermodb,
23
+ check_and_build_components_thermodb,
24
+ build_mixture_thermodb_from_reference,
25
+ check_and_build_mixture_thermodb,
26
+ ComponentThermoDB,
27
+ MixtureThermoDB
28
+ )
29
+
30
+ __all__ = [
31
+ '__version__',
32
+ '__author__',
33
+ '__description__',
34
+ 'ThermoDB',
35
+ 'CompBuilder',
36
+ 'TableData',
37
+ 'TableEquation',
38
+ 'TableMatrixData',
39
+ 'TableMatrixEquation',
40
+ 'init',
41
+ 'ref',
42
+ 'build_thermodb',
43
+ 'load_thermodb',
44
+ 'build_component_thermodb',
45
+ 'build_components_thermodb',
46
+ 'ManageData',
47
+ 'CustomRef',
48
+ 'build_component_thermodb_from_reference',
49
+ 'check_and_build_component_thermodb',
50
+ 'check_and_build_components_thermodb',
51
+ 'build_mixture_thermodb_from_reference',
52
+ 'check_and_build_mixture_thermodb',
53
+ 'ComponentThermoDB',
54
+ 'MixtureThermoDB'
55
+ ]
@@ -0,0 +1 @@
1
+ from .manage import Manage
@@ -0,0 +1,68 @@
1
+ # import packages/modules
2
+ # externals
3
+ import requests
4
+ # internal
5
+
6
+
7
+ class Manage:
8
+ def __init__(self, api_url, databook_id, table_id):
9
+ self.api_url = api_url
10
+ self.databook_id = databook_id
11
+ self.table_id = table_id
12
+
13
+ def component_list(self) -> list:
14
+ # parameters
15
+ reqtype = 'read'
16
+ id = str(self.databook_id).strip()
17
+ fid = str(self.table_id).strip()
18
+ parameters = f"?reqtype={reqtype}&id={id}&fid={fid}"
19
+ # all data
20
+ get_all = self.api_url + parameters
21
+ response = requests.get(get_all)
22
+
23
+ if response.status_code == 200:
24
+ payload = response.json()
25
+ data = payload['records']
26
+ # header
27
+ header = data[0]
28
+ # records
29
+ records = data[1:]
30
+ # find the index
31
+ for i, item in enumerate(header):
32
+ if item.lower() == "name":
33
+ name_id = int(i)
34
+ # component names
35
+ component_names = [record[name_id] for record in records]
36
+ return component_names
37
+ else:
38
+ print("error", response.status_code)
39
+ return []
40
+
41
+ def component_info(self, component_name: str, search_col_name="Name"):
42
+ '''
43
+ get component info
44
+ '''
45
+ # parameters
46
+ reqtype = 'search'
47
+ id = str(self.databook_id).strip()
48
+ fid = str(self.table_id).strip()
49
+ # query
50
+ query = {
51
+ "reqtype": reqtype,
52
+ "id": id,
53
+ "fid": fid,
54
+ "search_col_name": search_col_name,
55
+ "name": component_name
56
+ }
57
+
58
+ # post
59
+ response = requests.post(self.api_url, json=query, headers={
60
+ "Content-Type": "application/json"})
61
+
62
+ if response.status_code == 200:
63
+ payload = response.json()
64
+ data = payload
65
+ return data
66
+ else:
67
+ print("error", response.status_code)
68
+ return []
pyThermoDB/app.py ADDED
@@ -0,0 +1,212 @@
1
+ # import packages/modules
2
+ import logging
3
+ import os
4
+ from typing import (
5
+ Optional,
6
+ Dict,
7
+ List,
8
+ )
9
+ # local
10
+ from .docs import (
11
+ ThermoDB,
12
+ TableReference,
13
+ )
14
+ from .builder import CompBuilder
15
+ from .loader import CustomRef
16
+ from .models import CustomReference
17
+
18
+
19
+ # NOTE: logger
20
+ logger = logging.getLogger(__name__)
21
+
22
+
23
+ def init(
24
+ custom_reference: Optional[
25
+ CustomReference | str
26
+ ] = None
27
+ ) -> ThermoDB:
28
+ '''
29
+ Initialize thermodb app to check and build thermodynamic data and equations.
30
+
31
+ Parameters
32
+ ----------
33
+ custom_reference : Optional[CustomReference | str], optional
34
+ set-up external reference (custom reference) dict for databook and tables (check examples)
35
+
36
+ Returns
37
+ -------
38
+ ThermoDB : object
39
+ ThermoDB object used for checking and building data and equation objects
40
+
41
+ Notes
42
+ ------
43
+ ### Set-up external reference dict for databook and tables
44
+
45
+ - `format ref_external = {'reference':[yml files], 'tables':[csv files]}`
46
+
47
+ ### Examples
48
+
49
+ ```python
50
+ # custom ref
51
+ custom_reference = {
52
+ 'reference': [yml_path],
53
+ 'tables': [csv_path_1, csv_path_2]
54
+ }
55
+
56
+ # init app
57
+ tdb = ptdb.init(custom_reference=custom_reference)
58
+ ```
59
+ '''
60
+ try:
61
+ # NOTE: init vars
62
+ # check new custom ref
63
+ check_ref = False
64
+ # init class
65
+ CustomRefC = None
66
+
67
+ # SECTION: init class
68
+ if custom_reference:
69
+ # NOTE: check both str and dict
70
+ if not isinstance(custom_reference, (str, dict)):
71
+ logger.error(
72
+ "`custom_reference` must be a string or dictionary!")
73
+ raise TypeError(
74
+ "`custom_reference` must be a string or dictionary!")
75
+
76
+ # NOTE: check if string (yml file)
77
+ if isinstance(custom_reference, str):
78
+ # set dict
79
+ custom_reference = {'reference': [custom_reference]}
80
+
81
+ # NOTE: check dict values
82
+ if not all(isinstance(v, list) for v in custom_reference.values()):
83
+ logger.error(
84
+ "All values in `custom_reference` dictionary must be lists!")
85
+ raise TypeError(
86
+ "All values in `custom_reference` dictionary must be lists!")
87
+
88
+ # NOTE: init class
89
+ CustomRefC = CustomRef(custom_reference)
90
+
91
+ # NOTE: initialize reference
92
+ check_ref = CustomRefC.init_ref()
93
+
94
+ # SECTION: check ref
95
+ if check_ref:
96
+ return ThermoDB(custom_ref=CustomRefC)
97
+ else:
98
+ return ThermoDB()
99
+ except Exception as e:
100
+ raise Exception(f"Initializing app failed! {e}")
101
+
102
+
103
+ def ref(
104
+ custom_reference: Optional[
105
+ Dict[str, List[str]]
106
+ ] = None
107
+ ) -> TableReference:
108
+ '''
109
+ Checking references (custom reference) object including databook and tables to display data
110
+
111
+ Parameters
112
+ ----------
113
+ custom_reference : dict
114
+ set-up external reference dict for databook and tables, format `ref_external = {'yml':[yml files], 'csv':[csv files]}`
115
+
116
+ Returns
117
+ -------
118
+ TableReferenceC : object
119
+ TableReference object used for checking references
120
+
121
+ Notes
122
+ ------
123
+ ### Check external reference dict for databook and tables
124
+
125
+ - `format ref_external = {'yml':[yml files], 'csv':[csv files]}`
126
+ - `format ref_external = {'reference':[yml files], 'tables':[csv files]}`
127
+
128
+ ### Examples
129
+
130
+ ```python
131
+ # custom ref
132
+ custom_reference = {
133
+ 'reference': [yml_path],
134
+ 'tables': [csv_path_1, csv_path_2]
135
+ }
136
+
137
+ # init app
138
+ tdb = ptdb.ref(custom_reference=custom_reference)
139
+ ```
140
+ '''
141
+ try:
142
+ # check new custom ref
143
+ check_ref = False
144
+ if custom_reference:
145
+ CustomRefC = CustomRef(custom_reference)
146
+ # check ref
147
+ check_ref = CustomRefC.init_ref()
148
+
149
+ # check
150
+ if check_ref:
151
+ return TableReference(custom_ref=CustomRefC)
152
+ else:
153
+ # init
154
+ return TableReference()
155
+
156
+ except Exception as e:
157
+ raise Exception(f'Building reference failed! {e}')
158
+
159
+
160
+ def build_thermodb(
161
+ thermodb_name: Optional[str] = None,
162
+ message: Optional[str] = None
163
+ ) -> CompBuilder:
164
+ '''
165
+ Build thermodb object to check and build thermodynamic data and equations
166
+
167
+ Parameters
168
+ ----------
169
+ thermodb_name : str
170
+ name of the thermodb object
171
+ - `thermodb_name` : str, name of the thermodb object
172
+ message : str, optional
173
+ a short description of the thermodb object, by default None
174
+
175
+ Returns
176
+ -------
177
+ CompBuilder : object
178
+ CompBuilder object used for building thermodynamic data and equations
179
+ '''
180
+ try:
181
+ # NOTE: init class
182
+ return CompBuilder(
183
+ thermodb_name=thermodb_name,
184
+ message=message
185
+ )
186
+ except Exception as e:
187
+ raise Exception("Building thermodb failed!, ", e)
188
+
189
+
190
+ def load_thermodb(thermodb_file: str) -> CompBuilder:
191
+ '''
192
+ Load thermodb object to read thermodynamic data and equations
193
+
194
+ Parameters
195
+ ----------
196
+ thermodb_file : str
197
+ thermodb filename path
198
+
199
+ Returns
200
+ -------
201
+ CompBuilder : object
202
+ CompBuilder object used for loading thermodynamic data and equations
203
+ '''
204
+ try:
205
+ # NOTE: check file exists
206
+ if not os.path.isfile(thermodb_file):
207
+ raise FileNotFoundError(f"File '{thermodb_file}' does not exist!")
208
+
209
+ # NOTE: init class
210
+ return CompBuilder.load(thermodb_file)
211
+ except Exception as e:
212
+ raise Exception("Loading thermodb failed!, ", e)
@@ -0,0 +1,6 @@
1
+ # export
2
+ from .compbuilder import CompBuilder
3
+
4
+ __all_ = [
5
+ 'CompBuilder',
6
+ ]
@@ -0,0 +1,174 @@
1
+ # import libs
2
+ import logging
3
+ from typing import List, Dict, Any, Optional
4
+ # local
5
+ from pyThermoDB.core import TableEquation, TableData
6
+ from ..references.symbols_controller import SymbolController
7
+
8
+ # NOTE: logger
9
+ logger = logging.getLogger(__name__)
10
+
11
+
12
+ class CompTools:
13
+ """
14
+ Component Tools for handling thermodynamic data and functions (equations).
15
+ """
16
+
17
+ def __init__(self):
18
+ # LINK: Initialize SymbolController
19
+ self.symbol_controller = SymbolController()
20
+
21
+ def get_fn_structure(
22
+ self,
23
+ func: List[TableEquation]
24
+ ):
25
+ try:
26
+ # NOTE: init
27
+ res: Dict[str, Any] = {}
28
+
29
+ for fn in func:
30
+ # get info
31
+ db_name = fn.databook_name
32
+ tb_name = fn.table_name
33
+ eq_number = fn.eq_num
34
+ returns = fn.returns
35
+ return_symbols = fn.return_symbols
36
+ return_identifiers = fn.make_identifiers(
37
+ param_id="return",
38
+ mode="symbol"
39
+ )
40
+ args = fn.args
41
+ arg_symbols = fn.arg_symbols
42
+ arg_identifiers = fn.make_identifiers(
43
+ param_id="arg",
44
+ mode="symbol"
45
+ )
46
+ fn_body = fn.body
47
+ fn_body_integral = fn.body_integral
48
+
49
+ # set id
50
+ fn_id = f"{db_name}::{tb_name}"
51
+
52
+ # build structure
53
+ res[fn_id] = {
54
+ "function_numbers": eq_number,
55
+ "returns": returns,
56
+ "return_symbols": return_symbols,
57
+ "return_identifiers": return_identifiers,
58
+ "args": args,
59
+ "arg_symbols": arg_symbols,
60
+ "arg_identifiers": arg_identifiers,
61
+ "function_body": fn_body,
62
+ "function_body_integral": fn_body_integral
63
+ }
64
+
65
+ return res
66
+ except Exception as e:
67
+ logger.error(f"Error in getting functions' structure: {e}")
68
+ return None
69
+
70
+ def get_fn_identifier(
71
+ self,
72
+ func: List[TableEquation]
73
+ ) -> Optional[List[Dict[str, str]]]:
74
+ try:
75
+ res = [
76
+ {
77
+ f"{fn.databook_name}::{fn.table_name}": fn.make_identifiers(
78
+ param_id="return",
79
+ mode="symbol"
80
+ )[0]
81
+ }
82
+ for fn in func
83
+ ]
84
+
85
+ # res
86
+ return res
87
+ except Exception as e:
88
+ logger.error(f"Error in getting functions' identifiers: {e}")
89
+ return None
90
+
91
+ def get_data_structure(
92
+ self,
93
+ data: List[TableData]
94
+ ):
95
+ try:
96
+ # NOTE: init
97
+ res: Dict[str, Any] = {}
98
+
99
+ for dt in data:
100
+ # get info
101
+ db_name = dt.databook_name
102
+ tb_name = dt.table_name
103
+ prop_names = dt.property_names
104
+ symbols = dt.table_symbols
105
+
106
+ # set id
107
+ data_id = f"{db_name}::{tb_name}"
108
+
109
+ # build structure
110
+ res[data_id] = {
111
+ "property_names": prop_names,
112
+ "symbols": symbols
113
+ }
114
+
115
+ return res
116
+ except Exception as e:
117
+ logger.error(f"Error in getting data structure: {e}")
118
+ return None
119
+
120
+ def get_data_identifier(
121
+ self,
122
+ data: List[TableData]
123
+ ) -> Optional[List[Dict[str, List[str]]]]:
124
+ try:
125
+ res = [
126
+ {
127
+ f"{dt.databook_name}::{dt.table_name}": dt.table_symbols
128
+ }
129
+ for dt in data
130
+ ]
131
+
132
+ # res
133
+ return res
134
+ except Exception as e:
135
+ logger.error(f"Error in getting data identifiers: {e}")
136
+ return None
137
+
138
+ def get_data_id_labels(
139
+ self,
140
+ data: List[TableData]
141
+ ) -> List[Dict[str, str]]:
142
+ '''
143
+ Get all available symbol labels
144
+
145
+ Parameters
146
+ ----------
147
+ data : List[TableData]
148
+ List of TableData objects to extract symbols from.
149
+
150
+ Returns
151
+ -------
152
+ List[Dict[str, str]]
153
+ A list of dictionaries containing symbol and their labels.
154
+ '''
155
+ try:
156
+ # SECTION: get TableData symbols
157
+ symbols = self.get_data_identifier(data)
158
+ if not symbols:
159
+ return []
160
+
161
+ # SECTION: find labels using SymbolController
162
+ res: List[Dict[str, str]] = []
163
+
164
+ for symbol_dict in symbols:
165
+ for symbol_list in symbol_dict.values():
166
+ for symbol in symbol_list:
167
+ label = self.symbol_controller.map_symbol_to_property_name(
168
+ symbol)
169
+ res.append({symbol: label})
170
+
171
+ return res
172
+ except Exception as e:
173
+ logger.error(f"Error listing properties: {e}")
174
+ return []