Skip to content

Commit 884be94

Browse files
author
Thierry RAMORASOAVINA
committed
List all possible variable types of class Variable
1 parent d47193a commit 884be94

File tree

5 files changed

+260
-2
lines changed

5 files changed

+260
-2
lines changed

doc/samples/samples.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,82 @@ Samples
5656
kh.build_dictionary_from_data_table(
5757
data_table_path, dictionary_name, dictionary_file_path
5858
)
59+
.. autofunction:: create_dictionary_domain
60+
.. code-block:: python
61+
62+
# Imports
63+
import os
64+
from khiops import core as kh
65+
66+
# Creates a Root dictionary
67+
root_dictionary = kh.Dictionary(
68+
json_data={"name": "dict_from_scratch", "root": True, "key": ["Id"]}
69+
)
70+
71+
# Starts with simple variables to declare
72+
simple_variables = [
73+
{"name": "Id", "type": "Categorical"},
74+
{"name": "Num", "type": "Numerical"},
75+
{"name": "text", "type": "Text"},
76+
{"name": "hour", "type": "Time"},
77+
{"name": "date", "type": "Date"},
78+
{"name": "ambiguous_ts", "type": "Timestamp"},
79+
{"name": "ts", "type": "TimestampTZ"},
80+
]
81+
for var_spec in simple_variables:
82+
var = kh.Variable()
83+
var.name = var_spec["name"]
84+
var.type = var_spec["type"]
85+
root_dictionary.add_variable(var)
86+
87+
# Creates a second dictionary (not linked yet)
88+
second_dictionary = kh.Dictionary(
89+
json_data={"name": "Service", "key": ["Id", "id_product"]}
90+
)
91+
second_dictionary.add_variable(
92+
kh.Variable(json_data={"name": "Id", "type": "Categorical"})
93+
)
94+
second_dictionary.add_variable(
95+
kh.Variable(json_data={"name": "id_product", "type": "Categorical"})
96+
)
97+
# Creates a third dictionary (not linked yet)
98+
third_dictionary = kh.Dictionary(json_data={"name": "Address", "key": ["Id"]})
99+
third_dictionary.add_variable(
100+
kh.Variable(json_data={"name": "StreetNumber", "type": "Numerical"})
101+
)
102+
third_dictionary.add_variable(
103+
kh.Variable(json_data={"name": "StreetName", "type": "Categorical"})
104+
)
105+
third_dictionary.add_variable(
106+
kh.Variable(json_data={"name": "id_city", "type": "Categorical"})
107+
)
108+
109+
# Adds the variables used in a Multi-tables context in the first dictionary.
110+
# They link the root dictionary to the additional ones
111+
root_dictionary.add_variable(
112+
kh.Variable(json_data={"name": "Services", "type": "Table(Service)"})
113+
)
114+
root_dictionary.add_variable(
115+
kh.Variable(json_data={"name": "Address", "type": "Entity(Address)"})
116+
)
117+
118+
# Creates a DictionaryDomain (set of dictionaries)
119+
dictionary_domain = kh.DictionaryDomain()
120+
dictionary_domain.add_dictionary(root_dictionary)
121+
dictionary_domain.add_dictionary(second_dictionary)
122+
dictionary_domain.add_dictionary(third_dictionary)
123+
124+
output_dir = os.path.join("kh_samples", "create_dictionary_domain")
125+
dictionary_file_path = os.path.join(output_dir, "dict_from_scratch.kdic")
126+
127+
# Create the output directory if needed
128+
if not os.path.isdir(output_dir):
129+
os.mkdir(output_dir)
130+
131+
# Writes the dictionary domain to a file
132+
with open(dictionary_file_path, "w") as report_file:
133+
report_file_writer = KhiopsOutputWriter(report_file)
134+
dictionary_domain.write(report_file_writer)
59135
.. autofunction:: detect_data_table_format
60136
.. code-block:: python
61137

khiops/core/dictionary.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,13 @@ class Variable:
971971
True if the variable is used.
972972
type : str
973973
Variable type.
974+
It can be either native (``Categorical``, ``Numerical``, ``Time``,
975+
``Date``, ``Timestamp``, ``TimestampTZ``, ``Text``),
976+
internal (``TextList``, ``Structure``)
977+
- See https://khiops.org/11.0.0-b.0/api-docs/kdic/text-list-rules/
978+
- See https://khiops.org/api-docs/kdic/structures-introduction/
979+
or relational (``Entity`` - 0-1 relationship, ``Table`` - 0-n relationship)
980+
- See https://khiops.org/tutorials/kdic_multi_table/
974981
object_type : str
975982
Type complement for the ``Table`` and ``Entity`` types.
976983
structure_type : str
@@ -989,6 +996,11 @@ class Variable:
989996
List of variable comments.
990997
meta_data : `MetaData`
991998
Variable metadata.
999+
1000+
Examples
1001+
--------
1002+
See the following function of the ``samples.py`` documentation script:
1003+
- `samples.create_dictionary()`
9921004
"""
9931005

9941006
def __init__(self, json_data=None):

khiops/samples/samples.ipynb

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,95 @@
5757
")"
5858
]
5959
},
60+
{
61+
"cell_type": "markdown",
62+
"metadata": {},
63+
"source": [
64+
"### `create_dictionary_domain()`\n\n",
65+
"Creates a dictionary domain (a set of dictionaries) from scratch\n with all the possible variable types\n \n"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"metadata": {},
72+
"outputs": [],
73+
"source": [
74+
"# Imports\n",
75+
"import os\n",
76+
"from khiops import core as kh\n",
77+
"\n",
78+
"# Creates a Root dictionary\n",
79+
"root_dictionary = kh.Dictionary(\n",
80+
" json_data={\"name\": \"dict_from_scratch\", \"root\": True, \"key\": [\"Id\"]}\n",
81+
")\n",
82+
"\n",
83+
"# Starts with simple variables to declare\n",
84+
"simple_variables = [\n",
85+
" {\"name\": \"Id\", \"type\": \"Categorical\"},\n",
86+
" {\"name\": \"Num\", \"type\": \"Numerical\"},\n",
87+
" {\"name\": \"text\", \"type\": \"Text\"},\n",
88+
" {\"name\": \"hour\", \"type\": \"Time\"},\n",
89+
" {\"name\": \"date\", \"type\": \"Date\"},\n",
90+
" {\"name\": \"ambiguous_ts\", \"type\": \"Timestamp\"},\n",
91+
" {\"name\": \"ts\", \"type\": \"TimestampTZ\"},\n",
92+
"]\n",
93+
"for var_spec in simple_variables:\n",
94+
" var = kh.Variable()\n",
95+
" var.name = var_spec[\"name\"]\n",
96+
" var.type = var_spec[\"type\"]\n",
97+
" root_dictionary.add_variable(var)\n",
98+
"\n",
99+
"# Creates a second dictionary (not linked yet)\n",
100+
"second_dictionary = kh.Dictionary(\n",
101+
" json_data={\"name\": \"Service\", \"key\": [\"Id\", \"id_product\"]}\n",
102+
")\n",
103+
"second_dictionary.add_variable(\n",
104+
" kh.Variable(json_data={\"name\": \"Id\", \"type\": \"Categorical\"})\n",
105+
")\n",
106+
"second_dictionary.add_variable(\n",
107+
" kh.Variable(json_data={\"name\": \"id_product\", \"type\": \"Categorical\"})\n",
108+
")\n",
109+
"# Creates a third dictionary (not linked yet)\n",
110+
"third_dictionary = kh.Dictionary(json_data={\"name\": \"Address\", \"key\": [\"Id\"]})\n",
111+
"third_dictionary.add_variable(\n",
112+
" kh.Variable(json_data={\"name\": \"StreetNumber\", \"type\": \"Numerical\"})\n",
113+
")\n",
114+
"third_dictionary.add_variable(\n",
115+
" kh.Variable(json_data={\"name\": \"StreetName\", \"type\": \"Categorical\"})\n",
116+
")\n",
117+
"third_dictionary.add_variable(\n",
118+
" kh.Variable(json_data={\"name\": \"id_city\", \"type\": \"Categorical\"})\n",
119+
")\n",
120+
"\n",
121+
"# Adds the variables used in a Multi-tables context in the first dictionary.\n",
122+
"# They link the root dictionary to the additional ones\n",
123+
"root_dictionary.add_variable(\n",
124+
" kh.Variable(json_data={\"name\": \"Services\", \"type\": \"Table(Service)\"})\n",
125+
")\n",
126+
"root_dictionary.add_variable(\n",
127+
" kh.Variable(json_data={\"name\": \"Address\", \"type\": \"Entity(Address)\"})\n",
128+
")\n",
129+
"\n",
130+
"# Creates a DictionaryDomain (set of dictionaries)\n",
131+
"dictionary_domain = kh.DictionaryDomain()\n",
132+
"dictionary_domain.add_dictionary(root_dictionary)\n",
133+
"dictionary_domain.add_dictionary(second_dictionary)\n",
134+
"dictionary_domain.add_dictionary(third_dictionary)\n",
135+
"\n",
136+
"output_dir = os.path.join(\"kh_samples\", \"create_dictionary_domain\")\n",
137+
"dictionary_file_path = os.path.join(output_dir, \"dict_from_scratch.kdic\")\n",
138+
"\n",
139+
"# Create the output directory if needed\n",
140+
"if not os.path.isdir(output_dir):\n",
141+
" os.mkdir(output_dir)\n",
142+
"\n",
143+
"# Writes the dictionary domain to a file\n",
144+
"with open(dictionary_file_path, \"w\") as report_file:\n",
145+
" report_file_writer = KhiopsOutputWriter(report_file)\n",
146+
" dictionary_domain.write(report_file_writer)"
147+
]
148+
},
60149
{
61150
"cell_type": "markdown",
62151
"metadata": {},
@@ -2339,4 +2428,4 @@
23392428
"metadata": {},
23402429
"nbformat": 4,
23412430
"nbformat_minor": 2
2342-
}
2431+
}

khiops/samples/samples.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import khiops
1515
import os
1616
from khiops import core as kh
17+
from khiops.core import KhiopsOutputWriter
1718

1819
# Disable open files without encoding because samples are simple code snippets
1920
# pylint: disable=unspecified-encoding
@@ -65,6 +66,85 @@ def build_dictionary_from_data_table():
6566
)
6667

6768

69+
def create_dictionary_domain():
70+
"""Creates a dictionary domain (a set of dictionaries) from scratch
71+
with all the possible variable types
72+
"""
73+
# Imports
74+
import os
75+
from khiops import core as kh
76+
77+
# Creates a Root dictionary
78+
root_dictionary = kh.Dictionary(
79+
json_data={"name": "dict_from_scratch", "root": True, "key": ["Id"]}
80+
)
81+
82+
# Starts with simple variables to declare
83+
simple_variables = [
84+
{"name": "Id", "type": "Categorical"},
85+
{"name": "Num", "type": "Numerical"},
86+
{"name": "text", "type": "Text"},
87+
{"name": "hour", "type": "Time"},
88+
{"name": "date", "type": "Date"},
89+
{"name": "ambiguous_ts", "type": "Timestamp"},
90+
{"name": "ts", "type": "TimestampTZ"},
91+
]
92+
for var_spec in simple_variables:
93+
var = kh.Variable()
94+
var.name = var_spec["name"]
95+
var.type = var_spec["type"]
96+
root_dictionary.add_variable(var)
97+
98+
# Creates a second dictionary (not linked yet)
99+
second_dictionary = kh.Dictionary(
100+
json_data={"name": "Service", "key": ["Id", "id_product"]}
101+
)
102+
second_dictionary.add_variable(
103+
kh.Variable(json_data={"name": "Id", "type": "Categorical"})
104+
)
105+
second_dictionary.add_variable(
106+
kh.Variable(json_data={"name": "id_product", "type": "Categorical"})
107+
)
108+
# Creates a third dictionary (not linked yet)
109+
third_dictionary = kh.Dictionary(json_data={"name": "Address", "key": ["Id"]})
110+
third_dictionary.add_variable(
111+
kh.Variable(json_data={"name": "StreetNumber", "type": "Numerical"})
112+
)
113+
third_dictionary.add_variable(
114+
kh.Variable(json_data={"name": "StreetName", "type": "Categorical"})
115+
)
116+
third_dictionary.add_variable(
117+
kh.Variable(json_data={"name": "id_city", "type": "Categorical"})
118+
)
119+
120+
# Adds the variables used in a Multi-tables context in the first dictionary.
121+
# They link the root dictionary to the additional ones
122+
root_dictionary.add_variable(
123+
kh.Variable(json_data={"name": "Services", "type": "Table(Service)"})
124+
)
125+
root_dictionary.add_variable(
126+
kh.Variable(json_data={"name": "Address", "type": "Entity(Address)"})
127+
)
128+
129+
# Creates a DictionaryDomain (set of dictionaries)
130+
dictionary_domain = kh.DictionaryDomain()
131+
dictionary_domain.add_dictionary(root_dictionary)
132+
dictionary_domain.add_dictionary(second_dictionary)
133+
dictionary_domain.add_dictionary(third_dictionary)
134+
135+
output_dir = os.path.join("kh_samples", "create_dictionary_domain")
136+
dictionary_file_path = os.path.join(output_dir, "dict_from_scratch.kdic")
137+
138+
# Create the output directory if needed
139+
if not os.path.isdir(output_dir):
140+
os.mkdir(output_dir)
141+
142+
# Writes the dictionary domain to a file
143+
with open(dictionary_file_path, "w") as report_file:
144+
report_file_writer = KhiopsOutputWriter(report_file)
145+
dictionary_domain.write(report_file_writer)
146+
147+
68148
def detect_data_table_format():
69149
"""Detects the format of a data table with and without a dictionary file
70150
@@ -1987,6 +2067,7 @@ def build_deployed_dictionary():
19872067
exported_samples = [
19882068
get_khiops_version,
19892069
build_dictionary_from_data_table,
2070+
create_dictionary_domain,
19902071
detect_data_table_format,
19912072
check_database,
19922073
export_dictionary_files,

khiops/samples/samples_sklearn.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,4 +1036,4 @@
10361036
"metadata": {},
10371037
"nbformat": 4,
10381038
"nbformat_minor": 2
1039-
}
1039+
}

0 commit comments

Comments
 (0)