gamsapi 52.5.0__cp312-cp312-win_amd64.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.
- gams/__init__.py +27 -0
- gams/_version.py +1 -0
- gams/connect/__init__.py +28 -0
- gams/connect/agents/__init__.py +24 -0
- gams/connect/agents/_excel/__init__.py +32 -0
- gams/connect/agents/_excel/excelagent.py +312 -0
- gams/connect/agents/_excel/workbook.py +155 -0
- gams/connect/agents/_sqlconnectors/__init__.py +42 -0
- gams/connect/agents/_sqlconnectors/_accesshandler.py +211 -0
- gams/connect/agents/_sqlconnectors/_databasehandler.py +250 -0
- gams/connect/agents/_sqlconnectors/_mysqlhandler.py +168 -0
- gams/connect/agents/_sqlconnectors/_postgreshandler.py +131 -0
- gams/connect/agents/_sqlconnectors/_pyodbchandler.py +112 -0
- gams/connect/agents/_sqlconnectors/_sqlalchemyhandler.py +74 -0
- gams/connect/agents/_sqlconnectors/_sqlitehandler.py +262 -0
- gams/connect/agents/_sqlconnectors/_sqlserverhandler.py +179 -0
- gams/connect/agents/concatenate.py +440 -0
- gams/connect/agents/connectagent.py +743 -0
- gams/connect/agents/csvreader.py +675 -0
- gams/connect/agents/csvwriter.py +151 -0
- gams/connect/agents/domainwriter.py +143 -0
- gams/connect/agents/excelreader.py +756 -0
- gams/connect/agents/excelwriter.py +467 -0
- gams/connect/agents/filter.py +223 -0
- gams/connect/agents/gamsreader.py +112 -0
- gams/connect/agents/gamswriter.py +239 -0
- gams/connect/agents/gdxreader.py +109 -0
- gams/connect/agents/gdxwriter.py +146 -0
- gams/connect/agents/labelmanipulator.py +303 -0
- gams/connect/agents/projection.py +539 -0
- gams/connect/agents/pythoncode.py +71 -0
- gams/connect/agents/rawcsvreader.py +248 -0
- gams/connect/agents/rawexcelreader.py +312 -0
- gams/connect/agents/schema/CSVReader.yaml +92 -0
- gams/connect/agents/schema/CSVWriter.yaml +44 -0
- gams/connect/agents/schema/Concatenate.yaml +52 -0
- gams/connect/agents/schema/DomainWriter.yaml +25 -0
- gams/connect/agents/schema/ExcelReader.yaml +121 -0
- gams/connect/agents/schema/ExcelWriter.yaml +78 -0
- gams/connect/agents/schema/Filter.yaml +74 -0
- gams/connect/agents/schema/GAMSReader.yaml +20 -0
- gams/connect/agents/schema/GAMSWriter.yaml +47 -0
- gams/connect/agents/schema/GDXReader.yaml +23 -0
- gams/connect/agents/schema/GDXWriter.yaml +32 -0
- gams/connect/agents/schema/LabelManipulator.yaml +99 -0
- gams/connect/agents/schema/Projection.yaml +24 -0
- gams/connect/agents/schema/PythonCode.yaml +6 -0
- gams/connect/agents/schema/RawCSVReader.yaml +34 -0
- gams/connect/agents/schema/RawExcelReader.yaml +42 -0
- gams/connect/agents/schema/SQLReader.yaml +75 -0
- gams/connect/agents/schema/SQLWriter.yaml +103 -0
- gams/connect/agents/sqlreader.py +301 -0
- gams/connect/agents/sqlwriter.py +276 -0
- gams/connect/connectdatabase.py +275 -0
- gams/connect/connectvalidator.py +93 -0
- gams/connect/errors.py +34 -0
- gams/control/__init__.py +136 -0
- gams/control/database.py +2231 -0
- gams/control/execution.py +1900 -0
- gams/control/options.py +2792 -0
- gams/control/workspace.py +1198 -0
- gams/core/__init__.py +24 -0
- gams/core/cfg/__init__.py +26 -0
- gams/core/cfg/_cfgmcc.cp312-win_amd64.pyd +0 -0
- gams/core/cfg/cfgmcc.py +519 -0
- gams/core/dct/__init__.py +26 -0
- gams/core/dct/_dctmcc.cp312-win_amd64.pyd +0 -0
- gams/core/dct/dctmcc.py +574 -0
- gams/core/embedded/__init__.py +26 -0
- gams/core/embedded/gamsemb.py +1024 -0
- gams/core/emp/__init__.py +24 -0
- gams/core/emp/emplexer.py +89 -0
- gams/core/emp/empyacc.py +281 -0
- gams/core/gdx/__init__.py +26 -0
- gams/core/gdx/_gdxcc.cp312-win_amd64.pyd +0 -0
- gams/core/gdx/gdxcc.py +866 -0
- gams/core/gev/__init__.py +26 -0
- gams/core/gev/_gevmcc.cp312-win_amd64.pyd +0 -0
- gams/core/gev/gevmcc.py +855 -0
- gams/core/gmd/__init__.py +26 -0
- gams/core/gmd/_gmdcc.cp312-win_amd64.pyd +0 -0
- gams/core/gmd/gmdcc.py +917 -0
- gams/core/gmo/__init__.py +26 -0
- gams/core/gmo/_gmomcc.cp312-win_amd64.pyd +0 -0
- gams/core/gmo/gmomcc.py +2046 -0
- gams/core/idx/__init__.py +26 -0
- gams/core/idx/_idxcc.cp312-win_amd64.pyd +0 -0
- gams/core/idx/idxcc.py +510 -0
- gams/core/numpy/__init__.py +29 -0
- gams/core/numpy/_gams2numpy.cp312-win_amd64.pyd +0 -0
- gams/core/numpy/gams2numpy.py +1048 -0
- gams/core/opt/__init__.py +26 -0
- gams/core/opt/_optcc.cp312-win_amd64.pyd +0 -0
- gams/core/opt/optcc.py +840 -0
- gams/engine/__init__.py +204 -0
- gams/engine/api/__init__.py +13 -0
- gams/engine/api/auth_api.py +7653 -0
- gams/engine/api/cleanup_api.py +751 -0
- gams/engine/api/default_api.py +887 -0
- gams/engine/api/hypercube_api.py +2629 -0
- gams/engine/api/jobs_api.py +5229 -0
- gams/engine/api/licenses_api.py +2220 -0
- gams/engine/api/namespaces_api.py +7783 -0
- gams/engine/api/usage_api.py +5627 -0
- gams/engine/api/users_api.py +5931 -0
- gams/engine/api_client.py +804 -0
- gams/engine/api_response.py +21 -0
- gams/engine/configuration.py +601 -0
- gams/engine/exceptions.py +216 -0
- gams/engine/models/__init__.py +86 -0
- gams/engine/models/bad_input.py +89 -0
- gams/engine/models/cleanable_job_result.py +104 -0
- gams/engine/models/cleanable_job_result_page.py +113 -0
- gams/engine/models/engine_license.py +107 -0
- gams/engine/models/files_not_found.py +93 -0
- gams/engine/models/forwarded_token_response.py +112 -0
- gams/engine/models/generic_key_value_pair.py +89 -0
- gams/engine/models/hypercube.py +160 -0
- gams/engine/models/hypercube_page.py +111 -0
- gams/engine/models/hypercube_summary.py +91 -0
- gams/engine/models/hypercube_token.py +97 -0
- gams/engine/models/identity_provider.py +107 -0
- gams/engine/models/identity_provider_ldap.py +121 -0
- gams/engine/models/identity_provider_oauth2.py +146 -0
- gams/engine/models/identity_provider_oauth2_scope.py +89 -0
- gams/engine/models/identity_provider_oauth2_with_secret.py +152 -0
- gams/engine/models/identity_provider_oidc.py +133 -0
- gams/engine/models/identity_provider_oidc_with_secret.py +143 -0
- gams/engine/models/inex.py +91 -0
- gams/engine/models/invitation.py +136 -0
- gams/engine/models/invitation_quota.py +106 -0
- gams/engine/models/invitation_token.py +87 -0
- gams/engine/models/job.py +165 -0
- gams/engine/models/job_no_text_entry.py +138 -0
- gams/engine/models/job_no_text_entry_page.py +111 -0
- gams/engine/models/license.py +91 -0
- gams/engine/models/log_piece.py +96 -0
- gams/engine/models/message.py +87 -0
- gams/engine/models/message_and_token.py +99 -0
- gams/engine/models/message_with_webhook_id.py +89 -0
- gams/engine/models/model_auth_token.py +87 -0
- gams/engine/models/model_configuration.py +125 -0
- gams/engine/models/model_default_instance.py +99 -0
- gams/engine/models/model_default_user_instance.py +98 -0
- gams/engine/models/model_hypercube_job.py +106 -0
- gams/engine/models/model_hypercube_usage.py +130 -0
- gams/engine/models/model_instance_info.py +116 -0
- gams/engine/models/model_instance_info_full.py +123 -0
- gams/engine/models/model_instance_pool_info.py +112 -0
- gams/engine/models/model_job_labels.py +179 -0
- gams/engine/models/model_job_usage.py +133 -0
- gams/engine/models/model_pool_usage.py +124 -0
- gams/engine/models/model_usage.py +115 -0
- gams/engine/models/model_user.py +96 -0
- gams/engine/models/model_userinstance_info.py +119 -0
- gams/engine/models/model_userinstancepool_info.py +95 -0
- gams/engine/models/model_version.py +91 -0
- gams/engine/models/models.py +120 -0
- gams/engine/models/namespace.py +104 -0
- gams/engine/models/namespace_quota.py +96 -0
- gams/engine/models/namespace_with_permission.py +96 -0
- gams/engine/models/not_found.py +91 -0
- gams/engine/models/password_policy.py +97 -0
- gams/engine/models/perm_and_username.py +89 -0
- gams/engine/models/quota.py +117 -0
- gams/engine/models/quota_exceeded.py +97 -0
- gams/engine/models/status_code_meaning.py +89 -0
- gams/engine/models/stream_entry.py +89 -0
- gams/engine/models/system_wide_license.py +92 -0
- gams/engine/models/text_entries.py +87 -0
- gams/engine/models/text_entry.py +101 -0
- gams/engine/models/time_span.py +95 -0
- gams/engine/models/time_span_pool_worker.py +99 -0
- gams/engine/models/token_forward_error.py +87 -0
- gams/engine/models/user.py +127 -0
- gams/engine/models/user_group_member.py +96 -0
- gams/engine/models/user_groups.py +108 -0
- gams/engine/models/vapid_info.py +87 -0
- gams/engine/models/webhook.py +138 -0
- gams/engine/models/webhook_parameterized_event.py +99 -0
- gams/engine/py.typed +0 -0
- gams/engine/rest.py +258 -0
- gams/magic/__init__.py +32 -0
- gams/magic/gams_magic.py +142 -0
- gams/magic/interactive.py +402 -0
- gams/tools/__init__.py +30 -0
- gams/tools/errors.py +34 -0
- gams/tools/toolcollection/__init__.py +24 -0
- gams/tools/toolcollection/alg/__init__.py +24 -0
- gams/tools/toolcollection/alg/rank.py +51 -0
- gams/tools/toolcollection/data/__init__.py +24 -0
- gams/tools/toolcollection/data/csvread.py +444 -0
- gams/tools/toolcollection/data/csvwrite.py +311 -0
- gams/tools/toolcollection/data/exceldump.py +47 -0
- gams/tools/toolcollection/data/sqlitewrite.py +276 -0
- gams/tools/toolcollection/gdxservice/__init__.py +24 -0
- gams/tools/toolcollection/gdxservice/gdxencoding.py +104 -0
- gams/tools/toolcollection/gdxservice/gdxrename.py +94 -0
- gams/tools/toolcollection/linalg/__init__.py +24 -0
- gams/tools/toolcollection/linalg/cholesky.py +57 -0
- gams/tools/toolcollection/linalg/eigenvalue.py +56 -0
- gams/tools/toolcollection/linalg/eigenvector.py +58 -0
- gams/tools/toolcollection/linalg/invert.py +55 -0
- gams/tools/toolcollection/linalg/ols.py +138 -0
- gams/tools/toolcollection/tooltemplate.py +321 -0
- gams/tools/toolcollection/win32/__init__.py +24 -0
- gams/tools/toolcollection/win32/excelmerge.py +93 -0
- gams/tools/toolcollection/win32/exceltalk.py +76 -0
- gams/tools/toolcollection/win32/msappavail.py +49 -0
- gams/tools/toolcollection/win32/shellexecute.py +54 -0
- gams/tools/tools.py +116 -0
- gams/transfer/__init__.py +35 -0
- gams/transfer/_abcs/__init__.py +37 -0
- gams/transfer/_abcs/container_abcs.py +433 -0
- gams/transfer/_internals/__init__.py +63 -0
- gams/transfer/_internals/algorithms.py +436 -0
- gams/transfer/_internals/casepreservingdict.py +124 -0
- gams/transfer/_internals/constants.py +270 -0
- gams/transfer/_internals/domainviolation.py +103 -0
- gams/transfer/_internals/specialvalues.py +172 -0
- gams/transfer/containers/__init__.py +26 -0
- gams/transfer/containers/_container.py +1794 -0
- gams/transfer/containers/_io/__init__.py +28 -0
- gams/transfer/containers/_io/containers.py +164 -0
- gams/transfer/containers/_io/gdx.py +1029 -0
- gams/transfer/containers/_io/gmd.py +872 -0
- gams/transfer/containers/_mixins/__init__.py +26 -0
- gams/transfer/containers/_mixins/ccc.py +1274 -0
- gams/transfer/syms/__init__.py +33 -0
- gams/transfer/syms/_methods/__init__.py +24 -0
- gams/transfer/syms/_methods/tables.py +120 -0
- gams/transfer/syms/_methods/toDict.py +115 -0
- gams/transfer/syms/_methods/toList.py +83 -0
- gams/transfer/syms/_methods/toValue.py +60 -0
- gams/transfer/syms/_mixins/__init__.py +32 -0
- gams/transfer/syms/_mixins/equals.py +626 -0
- gams/transfer/syms/_mixins/generateRecords.py +499 -0
- gams/transfer/syms/_mixins/pivot.py +313 -0
- gams/transfer/syms/_mixins/pve.py +627 -0
- gams/transfer/syms/_mixins/sa.py +27 -0
- gams/transfer/syms/_mixins/sapve.py +27 -0
- gams/transfer/syms/_mixins/saua.py +27 -0
- gams/transfer/syms/_mixins/sauapve.py +199 -0
- gams/transfer/syms/_mixins/spve.py +1528 -0
- gams/transfer/syms/_mixins/ve.py +936 -0
- gams/transfer/syms/container_syms/__init__.py +31 -0
- gams/transfer/syms/container_syms/_alias.py +984 -0
- gams/transfer/syms/container_syms/_equation.py +333 -0
- gams/transfer/syms/container_syms/_parameter.py +973 -0
- gams/transfer/syms/container_syms/_set.py +604 -0
- gams/transfer/syms/container_syms/_universe_alias.py +461 -0
- gams/transfer/syms/container_syms/_variable.py +321 -0
- gamsapi-52.5.0.dist-info/METADATA +150 -0
- gamsapi-52.5.0.dist-info/RECORD +257 -0
- gamsapi-52.5.0.dist-info/WHEEL +5 -0
- gamsapi-52.5.0.dist-info/licenses/LICENSE +22 -0
- gamsapi-52.5.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
#
|
|
2
|
+
# GAMS - General Algebraic Modeling System Python API
|
|
3
|
+
#
|
|
4
|
+
# Copyright (c) 2017-2026 GAMS Development Corp. <support@gams.com>
|
|
5
|
+
# Copyright (c) 2017-2026 GAMS Software GmbH <support@gams.com>
|
|
6
|
+
#
|
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
# furnished to do so, subject to the following conditions:
|
|
13
|
+
#
|
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
# copies or substantial portions of the Software.
|
|
16
|
+
#
|
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
# SOFTWARE.
|
|
24
|
+
#
|
|
25
|
+
|
|
26
|
+
import gams.transfer as gt
|
|
27
|
+
from gams.core.embedded import ECGAMSDatabase
|
|
28
|
+
from gams.core.numpy import Gams2Numpy
|
|
29
|
+
from gams.connect.agents.connectagent import ConnectAgent
|
|
30
|
+
import pandas as pd
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class GAMSReader(ConnectAgent):
|
|
34
|
+
def __init__(self, cdb, inst, agent_index):
|
|
35
|
+
super().__init__(cdb, inst, agent_index)
|
|
36
|
+
if not (self._cdb.ecdb and isinstance(self._cdb.ecdb, ECGAMSDatabase)):
|
|
37
|
+
self._connect_error("GAMSReader is running without GAMS context.")
|
|
38
|
+
self._parse_options(self._inst)
|
|
39
|
+
|
|
40
|
+
def _parse_options(self, inst):
|
|
41
|
+
self._symbols = inst["symbols"]
|
|
42
|
+
self._trace = inst["trace"]
|
|
43
|
+
|
|
44
|
+
def execute(self):
|
|
45
|
+
if self._trace > 0:
|
|
46
|
+
self._log_instructions(self._inst, self._inst_raw)
|
|
47
|
+
self._describe_container(self._cdb.container, "Connect Container (before):")
|
|
48
|
+
|
|
49
|
+
cdb_empty = len(self._cdb.container) == 0
|
|
50
|
+
if cdb_empty:
|
|
51
|
+
gms = self._cdb.container
|
|
52
|
+
else:
|
|
53
|
+
gms = gt.Container(system_directory=self._system_directory)
|
|
54
|
+
if self._symbols == "all":
|
|
55
|
+
gms.read(self._cdb.ecdb.db._gmd)
|
|
56
|
+
else:
|
|
57
|
+
sym_names = [sym["name"] for sym in self._symbols]
|
|
58
|
+
self._symbols_exist_gmd(self._cdb.ecdb.db._gmd, sym_names)
|
|
59
|
+
gms.read(self._cdb.ecdb.db._gmd, symbols=sym_names)
|
|
60
|
+
if self._trace > 1:
|
|
61
|
+
self._cdb.print_log(f"GAMS symbols: {gms.listSymbols()}\n")
|
|
62
|
+
# Renaming
|
|
63
|
+
for sym in self._symbols:
|
|
64
|
+
if sym["newName"] is not None:
|
|
65
|
+
if sym["newName"] in gms:
|
|
66
|
+
self._connect_error(
|
|
67
|
+
f"Can not read >{sym['name']}< as >{sym['newName']}< because a symbol with this name was read already."
|
|
68
|
+
)
|
|
69
|
+
gms.renameSymbol(sym["name"], sym["newName"])
|
|
70
|
+
|
|
71
|
+
# For symbols with None records, empty df is assigned
|
|
72
|
+
for _, sym in gms:
|
|
73
|
+
self._transform_sym_none_to_empty(sym)
|
|
74
|
+
|
|
75
|
+
if self._trace > 0:
|
|
76
|
+
self._describe_container(gms, "GAMS Container:")
|
|
77
|
+
if self._trace > 2:
|
|
78
|
+
for name, sym in gms.data.items():
|
|
79
|
+
self._cdb.print_log(f"GAMS Container symbol={name}:\n {sym.records}\n")
|
|
80
|
+
|
|
81
|
+
if not cdb_empty:
|
|
82
|
+
# Copy from gms to container
|
|
83
|
+
self._symbols_exist_cdb(gms.listSymbols())
|
|
84
|
+
self._cdb.container.read(gms)
|
|
85
|
+
|
|
86
|
+
# Change order of '*' categories to GMD UEL order
|
|
87
|
+
g2np = Gams2Numpy(self._system_directory)
|
|
88
|
+
gmd_uels = {
|
|
89
|
+
k: v for v, k in enumerate(g2np.gmdGetUelList(self._cdb.ecdb.db._gmd))
|
|
90
|
+
}
|
|
91
|
+
for sym_name in gms.listSymbols():
|
|
92
|
+
sym = self._cdb.container[sym_name]
|
|
93
|
+
if (
|
|
94
|
+
sym.dimension > 1
|
|
95
|
+
and not isinstance(sym, gt.Alias)
|
|
96
|
+
and isinstance(sym.records, pd.DataFrame)
|
|
97
|
+
):
|
|
98
|
+
for pos, d in enumerate(sym.domain[1:]):
|
|
99
|
+
if isinstance(d, str):
|
|
100
|
+
col = sym.records[sym.records.columns[pos + 1]]
|
|
101
|
+
cat_sorted = sorted(col.cat.categories, key=gmd_uels.get)
|
|
102
|
+
sym.records[sym.records.columns[pos + 1]] = col.astype(
|
|
103
|
+
pd.CategoricalDtype(categories=cat_sorted, ordered=True)
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
if self._trace > 2:
|
|
107
|
+
for name, sym in self._cdb.container.data.items():
|
|
108
|
+
self._cdb.print_log(
|
|
109
|
+
f"Connect Container symbol={name}:\n {sym.records}\n"
|
|
110
|
+
)
|
|
111
|
+
if self._trace > 0:
|
|
112
|
+
self._describe_container(self._cdb.container, "Connect Container (after):")
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
#
|
|
2
|
+
# GAMS - General Algebraic Modeling System Python API
|
|
3
|
+
#
|
|
4
|
+
# Copyright (c) 2017-2026 GAMS Development Corp. <support@gams.com>
|
|
5
|
+
# Copyright (c) 2017-2026 GAMS Software GmbH <support@gams.com>
|
|
6
|
+
#
|
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
# furnished to do so, subject to the following conditions:
|
|
13
|
+
#
|
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
# copies or substantial portions of the Software.
|
|
16
|
+
#
|
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
# SOFTWARE.
|
|
24
|
+
#
|
|
25
|
+
|
|
26
|
+
from gams.connect.agents.connectagent import ConnectAgent
|
|
27
|
+
from gams.core.embedded import ECGAMSDatabase
|
|
28
|
+
from gams.core.gmd import *
|
|
29
|
+
import gams.transfer as gt
|
|
30
|
+
import pandas as pd
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class GAMSWriter(ConnectAgent):
|
|
34
|
+
|
|
35
|
+
def __init__(self, cdb, inst, agent_index):
|
|
36
|
+
super().__init__(cdb, inst, agent_index)
|
|
37
|
+
if not (self._cdb.ecdb and isinstance(self._cdb.ecdb, ECGAMSDatabase)):
|
|
38
|
+
self._connect_error("GAMSWriter is running without GAMS context.")
|
|
39
|
+
self._parse_options(self._inst)
|
|
40
|
+
|
|
41
|
+
def _parse_options(self, inst):
|
|
42
|
+
self._symbols = inst["symbols"]
|
|
43
|
+
self._duplicate_records = inst["duplicateRecords"]
|
|
44
|
+
self._trace = inst["trace"]
|
|
45
|
+
self._merge_type = inst["mergeType"]
|
|
46
|
+
self._domain_check_type = inst["domainCheckType"]
|
|
47
|
+
self._write_all = self._symbols == "all"
|
|
48
|
+
|
|
49
|
+
def execute(self):
|
|
50
|
+
if self._trace > 0:
|
|
51
|
+
self._log_instructions(self._inst, self._inst_raw)
|
|
52
|
+
self._describe_container(self._cdb.container, "Connect Container:")
|
|
53
|
+
|
|
54
|
+
elif self._cdb.ecdb.arguments.startswith(
|
|
55
|
+
"@connectOut"
|
|
56
|
+
): # we run with GAMS cmd parameter connectOut
|
|
57
|
+
self._connect_error("GAMSWriter not available for connectOut scripts.")
|
|
58
|
+
|
|
59
|
+
drmap = {"none": False, "first": "first", "last": "last"}
|
|
60
|
+
write_container = self._cdb.container
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# collect target symbol names
|
|
64
|
+
sym_map = {} # newName->sym
|
|
65
|
+
if self._write_all:
|
|
66
|
+
sym_map = {s: {"name": s} for s in write_container.listSymbols()}
|
|
67
|
+
else:
|
|
68
|
+
for sym in self._symbols:
|
|
69
|
+
sym_map[self._dict_get(sym, "newName", sym["name"])] = sym
|
|
70
|
+
|
|
71
|
+
# check that target symbols exist in GAMS database
|
|
72
|
+
target_sym_names = list(sym_map.keys())
|
|
73
|
+
for sym_t in target_sym_names.copy():
|
|
74
|
+
try:
|
|
75
|
+
self._symbols_exist_gmd(self._cdb.ecdb.db._gmd, sym_t)
|
|
76
|
+
except:
|
|
77
|
+
missing = True
|
|
78
|
+
else: # if no exception was raised, we have a symbol in the GAMS database and can continue with the next symbol
|
|
79
|
+
continue
|
|
80
|
+
|
|
81
|
+
# the symbol was not found in the GAMS database and we need to check the GMD object containing symbols with unknown dimension
|
|
82
|
+
if gmdHandleToPtr(self._cdb.ecdb._gmdud) is not None:
|
|
83
|
+
rc = new_intp()
|
|
84
|
+
symPtr = gmdFindSymbolPy(self._cdb.ecdb._gmdud, sym_t, rc)
|
|
85
|
+
rc_val = intp_value(rc)
|
|
86
|
+
delete_intp(rc)
|
|
87
|
+
if rc_val:
|
|
88
|
+
missing = False
|
|
89
|
+
sym_type = self._cdb.container[sym_map[sym_t]["name"]]._gams_type
|
|
90
|
+
dimension = self._cdb.container[sym_map[sym_t]["name"]].dimension
|
|
91
|
+
|
|
92
|
+
rc, user_info, _, _ = gmdSymbolInfo(self._cdb.ecdb._gmdud, symPtr, GMD_USERINFO)
|
|
93
|
+
self._cdb._ecdb._check_for_gmd_error(rc, self._cdb.ecdb._gmdud)
|
|
94
|
+
rc, _, _, sym_text = gmdSymbolInfo(self._cdb.ecdb._gmdud, symPtr, GMD_EXPLTEXT)
|
|
95
|
+
self._cdb._ecdb._check_for_gmd_error(rc, self._cdb.ecdb._gmdud)
|
|
96
|
+
|
|
97
|
+
rc = new_intp()
|
|
98
|
+
gmdAddSymbolPy(
|
|
99
|
+
self._cdb.ecdb._gmd,
|
|
100
|
+
sym_t,
|
|
101
|
+
dimension,
|
|
102
|
+
sym_type,
|
|
103
|
+
user_info,
|
|
104
|
+
sym_text,
|
|
105
|
+
rc,
|
|
106
|
+
)
|
|
107
|
+
rc_val = intp_value(rc)
|
|
108
|
+
delete_intp(rc)
|
|
109
|
+
self._cdb._ecdb._check_for_gmd_error(rc_val)
|
|
110
|
+
if missing:
|
|
111
|
+
if self._write_all:
|
|
112
|
+
target_sym_names.remove(sym_t)
|
|
113
|
+
if self._trace > 0:
|
|
114
|
+
self._cdb.print_log(
|
|
115
|
+
f"Skipping symbol '{sym_t}' since it does not exist in the GAMS database."
|
|
116
|
+
)
|
|
117
|
+
else:
|
|
118
|
+
self._symbols_exist_gmd(self._cdb.ecdb.db._gmd, sym_t)
|
|
119
|
+
|
|
120
|
+
if self._write_all:
|
|
121
|
+
if self._trace > 2:
|
|
122
|
+
for name in target_sym_names:
|
|
123
|
+
sym = write_container[name]
|
|
124
|
+
self._cdb.print_log(
|
|
125
|
+
f"Connect Container symbol={name}:\n {sym.records}\n"
|
|
126
|
+
)
|
|
127
|
+
if self._duplicate_records != "all":
|
|
128
|
+
# copy Connect container to avoid altering the Connect database
|
|
129
|
+
write_container = gt.Container(
|
|
130
|
+
self._cdb.container, system_directory=self._system_directory
|
|
131
|
+
)
|
|
132
|
+
write_container.dropDuplicateRecords(
|
|
133
|
+
symbols=target_sym_names,
|
|
134
|
+
keep=drmap[self._duplicate_records]
|
|
135
|
+
)
|
|
136
|
+
if not write_container.hasDuplicateRecords(symbols=target_sym_names):
|
|
137
|
+
write_container.write(self._cdb.ecdb.db._gmd, symbols=target_sym_names, eps_to_zero=False)
|
|
138
|
+
gmd_list = target_sym_names
|
|
139
|
+
else:
|
|
140
|
+
dup_name_list = [
|
|
141
|
+
name
|
|
142
|
+
for name in target_sym_names
|
|
143
|
+
if write_container[name].hasDuplicateRecords()
|
|
144
|
+
]
|
|
145
|
+
self._connect_error(
|
|
146
|
+
f"Following symbols have duplicate records: {dup_name_list}. Consider setting 'duplicateRecords' to 'first', 'last', or 'none'."
|
|
147
|
+
)
|
|
148
|
+
else:
|
|
149
|
+
symbols_raw = self._symbols.copy()
|
|
150
|
+
for s in self._symbols:
|
|
151
|
+
self._update_sym_inst(s, self._inst)
|
|
152
|
+
|
|
153
|
+
# Since we can't copy invalid symbols (=symbols with duplicates) we need to resolve the duplicates in the Connect container
|
|
154
|
+
all_dr = any(sym["duplicateRecords"] != "all" for sym in self._symbols)
|
|
155
|
+
if all_dr:
|
|
156
|
+
# copy Connect container to avoid altering the Connect database
|
|
157
|
+
write_container = gt.Container(
|
|
158
|
+
self._cdb.container, system_directory=self._system_directory
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
sym_names = []
|
|
162
|
+
for sym, sym_raw in zip(self._symbols, symbols_raw):
|
|
163
|
+
if self._trace > 0:
|
|
164
|
+
self._log_instructions(
|
|
165
|
+
sym, sym_raw, description=f"Write symbol >{sym['name']}<:"
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
sym_name = sym["name"]
|
|
169
|
+
self._symbols_exist_cdb(sym_name, should_exist=True)
|
|
170
|
+
|
|
171
|
+
if sym["duplicateRecords"] != "all":
|
|
172
|
+
write_container[sym_name].dropDuplicateRecords(
|
|
173
|
+
keep=drmap[sym["duplicateRecords"]]
|
|
174
|
+
)
|
|
175
|
+
if not write_container[sym_name].hasDuplicateRecords():
|
|
176
|
+
sym_names.append(sym_name)
|
|
177
|
+
else:
|
|
178
|
+
self._connect_error(
|
|
179
|
+
f"Symbol '{sym_name}' has duplicate records. Consider setting 'duplicateRecords' to 'first', 'last', or 'none'."
|
|
180
|
+
)
|
|
181
|
+
gms = gt.Container(system_directory=self._system_directory)
|
|
182
|
+
gms.read(write_container, sym_names)
|
|
183
|
+
|
|
184
|
+
# Apply original categories of * domains in new Container
|
|
185
|
+
for sym_name in gms.listSymbols():
|
|
186
|
+
ssym = write_container[sym_name]
|
|
187
|
+
tsym = gms[sym_name]
|
|
188
|
+
if ssym.records is None:
|
|
189
|
+
continue
|
|
190
|
+
for d, tdl, sdl in zip(
|
|
191
|
+
tsym.domain, tsym.domain_labels, ssym.domain_labels
|
|
192
|
+
):
|
|
193
|
+
if isinstance(d, str):
|
|
194
|
+
tsym.records[tdl] = tsym.records[tdl].astype(
|
|
195
|
+
pd.CategoricalDtype(
|
|
196
|
+
categories=ssym.records[sdl].cat.categories,
|
|
197
|
+
ordered=True,
|
|
198
|
+
)
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
# Renaming
|
|
202
|
+
if self._trace > 1:
|
|
203
|
+
self._cdb.print_log(f"GAMS symbols: {gms.listSymbols()}\n")
|
|
204
|
+
for sym in self._symbols:
|
|
205
|
+
if sym["newName"] is not None:
|
|
206
|
+
gms.renameSymbol(sym["name"], sym["newName"])
|
|
207
|
+
|
|
208
|
+
if self._trace > 0:
|
|
209
|
+
self._describe_container(gms, "GAMS Container:")
|
|
210
|
+
if self._trace > 2:
|
|
211
|
+
for name, sym in gms.data.items():
|
|
212
|
+
self._cdb.print_log(f"GAMS Container symbol={name}: {sym.records}")
|
|
213
|
+
gms.write(self._cdb.ecdb.db._gmd, eps_to_zero=False)
|
|
214
|
+
gmd_list = gms.listSymbols()
|
|
215
|
+
|
|
216
|
+
# Build the modSymList with merge type and domain check type
|
|
217
|
+
merge_type_lookup = {"replace": 0, "merge": 1, "default": 2}
|
|
218
|
+
domain_check_type_lookup = {"filtered": 0, "checked": 1, "default": 2}
|
|
219
|
+
rc = new_intp()
|
|
220
|
+
self._cdb.ecdb._modSymList = {}
|
|
221
|
+
for sym_name in gmd_list:
|
|
222
|
+
sym_ptr = gmdFindSymbolPy(self._cdb.ecdb.db._gmd, sym_name, rc)
|
|
223
|
+
if not intp_value(rc):
|
|
224
|
+
self._connect_error(gmdGetLastError(self._cdb.ecdb.db._gmd)[1])
|
|
225
|
+
ret = gmdSymbolInfo(self._cdb.ecdb.db._gmd, sym_ptr, GMD_NUMBER)
|
|
226
|
+
if not ret[0]:
|
|
227
|
+
self._connect_error(gmdGetLastError(self._cdb.ecdb.db._gmd)[1])
|
|
228
|
+
if self._write_all:
|
|
229
|
+
mt = self._merge_type
|
|
230
|
+
dct = self._domain_check_type
|
|
231
|
+
else:
|
|
232
|
+
sym = sym_map[sym_name]
|
|
233
|
+
mt = sym["mergeType"]
|
|
234
|
+
dct = sym["domainCheckType"]
|
|
235
|
+
self._cdb.ecdb._modSymList[ret[1]] = (
|
|
236
|
+
merge_type_lookup[mt],
|
|
237
|
+
domain_check_type_lookup[dct],
|
|
238
|
+
)
|
|
239
|
+
delete_intp(rc)
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
#
|
|
2
|
+
# GAMS - General Algebraic Modeling System Python API
|
|
3
|
+
#
|
|
4
|
+
# Copyright (c) 2017-2026 GAMS Development Corp. <support@gams.com>
|
|
5
|
+
# Copyright (c) 2017-2026 GAMS Software GmbH <support@gams.com>
|
|
6
|
+
#
|
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
# furnished to do so, subject to the following conditions:
|
|
13
|
+
#
|
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
# copies or substantial portions of the Software.
|
|
16
|
+
#
|
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
# SOFTWARE.
|
|
24
|
+
#
|
|
25
|
+
|
|
26
|
+
import os
|
|
27
|
+
from gams.connect.agents.connectagent import ConnectAgent
|
|
28
|
+
from gams.core.numpy import Gams2Numpy
|
|
29
|
+
import gams.transfer as gt
|
|
30
|
+
import pandas as pd
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class GDXReader(ConnectAgent):
|
|
34
|
+
def __init__(self, cdb, inst, agent_index):
|
|
35
|
+
super().__init__(cdb, inst, agent_index)
|
|
36
|
+
self._parse_options(self._inst)
|
|
37
|
+
|
|
38
|
+
def _parse_options(self, inst):
|
|
39
|
+
self._symbols = inst["symbols"]
|
|
40
|
+
self._gdx_file = os.path.abspath(inst["file"])
|
|
41
|
+
self._trace = inst["trace"]
|
|
42
|
+
|
|
43
|
+
def execute(self):
|
|
44
|
+
if self._trace > 0:
|
|
45
|
+
self._log_instructions(self._inst, self._inst_raw)
|
|
46
|
+
self._describe_container(self._cdb.container, "Connect Container (before):")
|
|
47
|
+
|
|
48
|
+
cdb_empty = len(self._cdb.container) == 0
|
|
49
|
+
if cdb_empty:
|
|
50
|
+
gdx = self._cdb.container
|
|
51
|
+
else:
|
|
52
|
+
gdx = gt.Container(system_directory=self._system_directory)
|
|
53
|
+
if self._symbols == "all":
|
|
54
|
+
gdx.read(self._gdx_file)
|
|
55
|
+
else:
|
|
56
|
+
sym_names = [sym["name"] for sym in self._symbols]
|
|
57
|
+
self._symbols_exist_gdx(self._gdx_file, sym_names)
|
|
58
|
+
gdx.read(self._gdx_file, symbols=sym_names)
|
|
59
|
+
if self._trace > 1:
|
|
60
|
+
self._cdb.print_log(f"GDX symbols: {gdx.listSymbols()}\n")
|
|
61
|
+
# Renaming
|
|
62
|
+
for sym in self._symbols:
|
|
63
|
+
if sym["newName"] is not None:
|
|
64
|
+
if sym["newName"] in gdx:
|
|
65
|
+
self._connect_error(
|
|
66
|
+
f"Can not read >{sym['name']}< as >{sym['newName']}< because a symbol with this name was read already."
|
|
67
|
+
)
|
|
68
|
+
gdx.renameSymbol(sym["name"], sym["newName"])
|
|
69
|
+
|
|
70
|
+
# For symbols with None records, empty df is assigned
|
|
71
|
+
for _, sym in gdx:
|
|
72
|
+
self._transform_sym_none_to_empty(sym)
|
|
73
|
+
|
|
74
|
+
if self._trace > 0:
|
|
75
|
+
self._describe_container(gdx, "GDX Container:")
|
|
76
|
+
if self._trace > 2:
|
|
77
|
+
for name, sym in gdx.data.items():
|
|
78
|
+
self._cdb.print_log(f"GDX Container symbol={name}:\n {sym.records}\n")
|
|
79
|
+
|
|
80
|
+
if not cdb_empty:
|
|
81
|
+
# Copy from gdx to container
|
|
82
|
+
self._symbols_exist_cdb(gdx.listSymbols())
|
|
83
|
+
self._cdb.container.read(gdx)
|
|
84
|
+
|
|
85
|
+
# Change order of '*' categories to GDX UEL order
|
|
86
|
+
g2np = Gams2Numpy(self._system_directory)
|
|
87
|
+
gdx_uels = {k: v for v, k in enumerate(g2np.gdxGetUelList(self._gdx_file))}
|
|
88
|
+
for sym_name in gdx.listSymbols():
|
|
89
|
+
sym = self._cdb.container[sym_name]
|
|
90
|
+
if (
|
|
91
|
+
sym.dimension > 1
|
|
92
|
+
and not isinstance(sym, gt.Alias)
|
|
93
|
+
and isinstance(sym.records, pd.DataFrame)
|
|
94
|
+
):
|
|
95
|
+
for pos, d in enumerate(sym.domain[1:]):
|
|
96
|
+
if isinstance(d, str):
|
|
97
|
+
col = sym.records[sym.records.columns[pos + 1]]
|
|
98
|
+
cat_sorted = sorted(col.cat.categories, key=gdx_uels.get)
|
|
99
|
+
sym.records[sym.records.columns[pos + 1]] = col.astype(
|
|
100
|
+
pd.CategoricalDtype(categories=cat_sorted, ordered=True)
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
if self._trace > 2:
|
|
104
|
+
for name, sym in self._cdb.container.data.items():
|
|
105
|
+
self._cdb.print_log(
|
|
106
|
+
f"Connect Container symbol={name}:\n {sym.records}\n"
|
|
107
|
+
)
|
|
108
|
+
if self._trace > 0:
|
|
109
|
+
self._describe_container(self._cdb.container, "Connect Container (after):")
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
#
|
|
2
|
+
# GAMS - General Algebraic Modeling System Python API
|
|
3
|
+
#
|
|
4
|
+
# Copyright (c) 2017-2026 GAMS Development Corp. <support@gams.com>
|
|
5
|
+
# Copyright (c) 2017-2026 GAMS Software GmbH <support@gams.com>
|
|
6
|
+
#
|
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
# furnished to do so, subject to the following conditions:
|
|
13
|
+
#
|
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
# copies or substantial portions of the Software.
|
|
16
|
+
#
|
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
# SOFTWARE.
|
|
24
|
+
#
|
|
25
|
+
|
|
26
|
+
import os
|
|
27
|
+
from gams.connect.agents.connectagent import ConnectAgent
|
|
28
|
+
import gams.transfer as gt
|
|
29
|
+
import pandas as pd
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class GDXWriter(ConnectAgent):
|
|
33
|
+
|
|
34
|
+
def __init__(self, cdb, inst, agent_index):
|
|
35
|
+
super().__init__(cdb, inst, agent_index)
|
|
36
|
+
self._parse_options(self._inst)
|
|
37
|
+
|
|
38
|
+
def _parse_options(self, inst):
|
|
39
|
+
self._symbols = inst["symbols"]
|
|
40
|
+
self._duplicate_records = inst["duplicateRecords"]
|
|
41
|
+
inst["file"] = os.path.abspath(inst["file"])
|
|
42
|
+
self._gdx_file = inst["file"]
|
|
43
|
+
self._trace = inst["trace"]
|
|
44
|
+
self._write_all = self._symbols == "all"
|
|
45
|
+
|
|
46
|
+
def execute(self):
|
|
47
|
+
if self._trace > 0:
|
|
48
|
+
self._log_instructions(self._inst, self._inst_raw)
|
|
49
|
+
self._describe_container(self._cdb.container, "Connect Container:")
|
|
50
|
+
|
|
51
|
+
drmap = {"none": False, "first": "first", "last": "last"}
|
|
52
|
+
write_container = self._cdb.container
|
|
53
|
+
if self._write_all:
|
|
54
|
+
if self._trace > 2:
|
|
55
|
+
for name, sym in write_container.data.items():
|
|
56
|
+
self._cdb.print_log(
|
|
57
|
+
f"Connect Container symbol={name}:\n {sym.records}\n"
|
|
58
|
+
)
|
|
59
|
+
if self._duplicate_records != "all":
|
|
60
|
+
# copy Connect container to avoid altering the Connect database
|
|
61
|
+
write_container = gt.Container(
|
|
62
|
+
self._cdb.container, system_directory=self._system_directory
|
|
63
|
+
)
|
|
64
|
+
write_container.dropDuplicateRecords(
|
|
65
|
+
keep=drmap[self._duplicate_records]
|
|
66
|
+
)
|
|
67
|
+
if not write_container.hasDuplicateRecords():
|
|
68
|
+
write_container.write(self._gdx_file, eps_to_zero=False)
|
|
69
|
+
else:
|
|
70
|
+
dup_name_list = [
|
|
71
|
+
name
|
|
72
|
+
for name, sym in write_container.data.items()
|
|
73
|
+
if sym.hasDuplicateRecords()
|
|
74
|
+
]
|
|
75
|
+
self._connect_error(
|
|
76
|
+
f"Following symbols have duplicate records: {dup_name_list}. Consider setting 'duplicateRecords' to 'first', 'last', or 'none'."
|
|
77
|
+
)
|
|
78
|
+
else:
|
|
79
|
+
symbols_raw = self._symbols.copy()
|
|
80
|
+
for s in self._symbols:
|
|
81
|
+
self._update_sym_inst(s, self._inst)
|
|
82
|
+
|
|
83
|
+
# Since we can't copy invalid symbols (=symbols with duplicates) we need to resolve the duplicates in the Connect container
|
|
84
|
+
all_dr = any(sym["duplicateRecords"] != "all" for sym in self._symbols)
|
|
85
|
+
if all_dr:
|
|
86
|
+
# copy Connect container to avoid altering the Connect database
|
|
87
|
+
write_container = gt.Container(
|
|
88
|
+
self._cdb.container, system_directory=self._system_directory
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
sym_names = []
|
|
92
|
+
for sym, sym_raw in zip(self._symbols, symbols_raw):
|
|
93
|
+
if self._trace > 0:
|
|
94
|
+
self._log_instructions(
|
|
95
|
+
sym, sym_raw, description=f"Write symbol >{sym['name']}<:"
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
sym_name = sym["name"]
|
|
99
|
+
self._symbols_exist_cdb(sym_name, should_exist=True)
|
|
100
|
+
|
|
101
|
+
if sym["duplicateRecords"] != "all":
|
|
102
|
+
write_container[sym_name].dropDuplicateRecords(
|
|
103
|
+
keep=drmap[sym["duplicateRecords"]]
|
|
104
|
+
)
|
|
105
|
+
if not write_container[sym_name].hasDuplicateRecords():
|
|
106
|
+
sym_names.append(sym_name)
|
|
107
|
+
else:
|
|
108
|
+
self._connect_error(
|
|
109
|
+
f"Symbol '{sym_name}' has duplicate records. Consider setting 'duplicateRecords' to 'first', 'last', or 'none'."
|
|
110
|
+
)
|
|
111
|
+
gdx = gt.Container(system_directory=self._system_directory)
|
|
112
|
+
gdx.read(write_container, sym_names)
|
|
113
|
+
|
|
114
|
+
# Apply original categories of * domains in new Container
|
|
115
|
+
for sym_name in gdx.listSymbols():
|
|
116
|
+
ssym = write_container[sym_name]
|
|
117
|
+
tsym = gdx[sym_name]
|
|
118
|
+
if ssym.records is None:
|
|
119
|
+
continue
|
|
120
|
+
for d, tdl, sdl in zip(
|
|
121
|
+
tsym.domain, tsym.domain_labels, ssym.domain_labels
|
|
122
|
+
):
|
|
123
|
+
if isinstance(d, str):
|
|
124
|
+
tsym.records[tdl] = tsym.records[tdl].astype(
|
|
125
|
+
pd.CategoricalDtype(
|
|
126
|
+
categories=ssym.records[sdl].cat.categories,
|
|
127
|
+
ordered=True,
|
|
128
|
+
)
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
# Renaming
|
|
132
|
+
if self._trace > 1:
|
|
133
|
+
self._cdb.print_log(f"GDX symbols: {gdx.listSymbols()}\n")
|
|
134
|
+
for sym in self._symbols:
|
|
135
|
+
if sym["newName"] is not None:
|
|
136
|
+
gdx.renameSymbol(sym["name"], sym["newName"])
|
|
137
|
+
|
|
138
|
+
if self._trace > 0:
|
|
139
|
+
self._describe_container(gdx, "GDX Container:")
|
|
140
|
+
if self._trace > 2:
|
|
141
|
+
for name, sym in gdx.data.items():
|
|
142
|
+
self._cdb.print_log(
|
|
143
|
+
f"GDX Container symbol={name}:\n {sym.records}\n"
|
|
144
|
+
)
|
|
145
|
+
self._cdb.print_log(f" Valid: {sym.isValid(verbose=True)}\n")
|
|
146
|
+
gdx.write(self._gdx_file, eps_to_zero=False)
|