mostlyai 5.7.0__tar.gz → 5.8.0__tar.gz
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.
- {mostlyai-5.7.0 → mostlyai-5.8.0}/PKG-INFO +1 -1
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/__init__.py +1 -1
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/auto_detect.py +8 -5
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/metadata_objects.py +1 -1
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/connectors.py +90 -2
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/step_finalize_generation.py +89 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/step_generate_data.py +19 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/generators.py +58 -8
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/routes.py +5 -21
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/client/api.py +2 -3
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/domain.py +39 -14
- {mostlyai-5.7.0 → mostlyai-5.8.0}/pyproject.toml +1 -1
- {mostlyai-5.7.0 → mostlyai-5.8.0}/.gitignore +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/LICENSE +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/README.md +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/__init__.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/base.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/context.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/conversions.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/db/__init__.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/db/base.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/db/bigquery.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/db/databricks.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/db/hive.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/db/mssql.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/db/mysql.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/db/oracle.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/db/postgresql.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/db/redshift.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/db/snowflake.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/db/sqlite.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/db/types_coercion.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/dtype.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/exceptions.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/file/__init__.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/file/base.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/file/container/__init__.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/file/container/aws.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/file/container/azure.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/file/container/bucket_based.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/file/container/gcs.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/file/container/minio.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/file/table/__init__.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/file/table/csv.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/file/table/feather.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/file/table/json.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/file/table/parquet.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/file/utils.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/language_model.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/non_context.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/progress_callback.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/pull.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/pull_context.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/pull_utils.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/push.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/util/__init__.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/util/common.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_data/util/kerberos.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/__init__.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/cli.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/__init__.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/jobs.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/migration.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/plan.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/step_analyze_training_data.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/step_create_data_report.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/step_create_model_report.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/step_deliver_data.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/step_encode_training_data.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/step_finalize_training.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/step_generate_model_report_data.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/step_pull_training_data.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/step_train_model.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/progress.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/server.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/storage.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/synthetic_datasets.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/client/__init__.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/client/_base_utils.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/client/_naming_conventions.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/client/_utils.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/client/artifacts.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/client/base.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/client/connectors.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/client/datasets.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/client/exceptions.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/client/generators.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/client/synthetic_datasets.py +0 -0
- {mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/py.typed +0 -0
|
@@ -49,4 +49,4 @@ For more information, visit: https://mostly-ai.github.io/mostlyai/
|
|
|
49
49
|
from mostlyai.sdk.client.api import MostlyAI
|
|
50
50
|
|
|
51
51
|
__all__ = ["MostlyAI"]
|
|
52
|
-
__version__ = "5.
|
|
52
|
+
__version__ = "5.8.0" # Do not set this manually. Use poetry version [params].
|
|
@@ -32,7 +32,7 @@ PK_POSSIBLE_VIRTUAL_DTYPES = (VirtualVarchar, VirtualInteger)
|
|
|
32
32
|
_LOG = logging.getLogger(__name__)
|
|
33
33
|
|
|
34
34
|
|
|
35
|
-
def auto_detect_encoding_types_and_pk(table: DataTable) -> tuple[dict, str | None]:
|
|
35
|
+
def auto_detect_encoding_types_and_pk(table: DataTable) -> tuple[dict[str, ModelEncodingType], str | None]:
|
|
36
36
|
# sub-select only the columns which got the default tabular categorical encoding type
|
|
37
37
|
columns_to_auto_detect = [
|
|
38
38
|
c for c, enc in table.encoding_types.items() if enc == ModelEncodingType.tabular_categorical
|
|
@@ -79,7 +79,7 @@ def auto_detect_primary_key(sample: pd.DataFrame) -> str | None:
|
|
|
79
79
|
return None
|
|
80
80
|
|
|
81
81
|
|
|
82
|
-
def auto_detect_encoding_type(x: pd.Series):
|
|
82
|
+
def auto_detect_encoding_type(x: pd.Series) -> ModelEncodingType:
|
|
83
83
|
x = x.dropna()
|
|
84
84
|
x = x.astype(str)
|
|
85
85
|
x = x[x.str.strip() != ""] # filter out empty and whitespace-only strings
|
|
@@ -96,8 +96,11 @@ def auto_detect_encoding_type(x: pd.Series):
|
|
|
96
96
|
if x.str.match(LAT_LONG_REGEX).all():
|
|
97
97
|
return ModelEncodingType.tabular_lat_long
|
|
98
98
|
|
|
99
|
-
# if more than 5% of rows contain unique values
|
|
100
|
-
if len(x
|
|
101
|
-
|
|
99
|
+
# if more than 5% of rows contain unique values
|
|
100
|
+
if len(x) >= 100 and x.value_counts().eq(1).reindex(x).mean() > 0.05:
|
|
101
|
+
if x.str.len().nunique() == 1: # if all values are of the same length -> character encoding
|
|
102
|
+
return ModelEncodingType.tabular_character
|
|
103
|
+
else: # if values are of different lengths -> text encoding
|
|
104
|
+
return ModelEncodingType.language_text
|
|
102
105
|
|
|
103
106
|
return ModelEncodingType.tabular_categorical
|
|
@@ -164,7 +164,7 @@ class ConstraintSchema(CustomBaseModel):
|
|
|
164
164
|
|
|
165
165
|
class TableSchema(CustomBaseModel):
|
|
166
166
|
name: Annotated[Nullable[str], Field(description="The name of the table.")] = None
|
|
167
|
-
|
|
167
|
+
total_rows: Annotated[
|
|
168
168
|
Nullable[int],
|
|
169
169
|
Field(
|
|
170
170
|
description="The total number of rows in the table.",
|
|
@@ -12,15 +12,20 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
+
import logging
|
|
15
16
|
from io import BytesIO
|
|
16
17
|
from pathlib import Path
|
|
17
18
|
|
|
18
19
|
import pandas as pd
|
|
19
20
|
from fastapi import HTTPException
|
|
20
21
|
|
|
22
|
+
from mostlyai.sdk._data.auto_detect import auto_detect_encoding_types_and_pk
|
|
23
|
+
from mostlyai.sdk._data.base import Schema
|
|
21
24
|
from mostlyai.sdk._data.conversions import create_container_from_connector
|
|
22
|
-
from mostlyai.sdk._data.
|
|
23
|
-
from mostlyai.sdk._data.
|
|
25
|
+
from mostlyai.sdk._data.db.base import SqlAlchemyContainer
|
|
26
|
+
from mostlyai.sdk._data.file.utils import make_data_table_from_container, read_data_table_from_path
|
|
27
|
+
from mostlyai.sdk._data.metadata_objects import ColumnSchema, ConstraintSchema, TableSchema
|
|
28
|
+
from mostlyai.sdk._data.util.common import encrypt, get_passphrase, run_with_timeout_unsafe
|
|
24
29
|
from mostlyai.sdk._local.storage import write_connector_to_json
|
|
25
30
|
from mostlyai.sdk.domain import (
|
|
26
31
|
Connector,
|
|
@@ -33,6 +38,8 @@ from mostlyai.sdk.domain import (
|
|
|
33
38
|
ConnectorWriteDataConfig,
|
|
34
39
|
)
|
|
35
40
|
|
|
41
|
+
_LOG = logging.getLogger(__name__)
|
|
42
|
+
|
|
36
43
|
|
|
37
44
|
def create_connector(home_dir: Path, config: ConnectorConfig, test_connection: bool = True) -> Connector:
|
|
38
45
|
config = encrypt_connector_config(config)
|
|
@@ -121,3 +128,84 @@ def query_data_from_connector(connector: Connector, sql: str) -> pd.DataFrame:
|
|
|
121
128
|
return data_container.query(sql)
|
|
122
129
|
except Exception as e:
|
|
123
130
|
raise HTTPException(status_code=400, detail=str(e))
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _data_tables_to_table_schemas(
|
|
134
|
+
schema: Schema,
|
|
135
|
+
ordered_table_names: list[str],
|
|
136
|
+
ordered_locations: list[str],
|
|
137
|
+
) -> list[TableSchema]:
|
|
138
|
+
table_schemas = []
|
|
139
|
+
for table_name, location in zip(ordered_table_names, ordered_locations):
|
|
140
|
+
table = schema.tables[table_name]
|
|
141
|
+
# auto-detect encoding types
|
|
142
|
+
encoding_types, primary_key = auto_detect_encoding_types_and_pk(table)
|
|
143
|
+
_LOG.info(f"auto-detected {encoding_types=} and {primary_key=} for table {table_name}")
|
|
144
|
+
table.encoding_types |= encoding_types
|
|
145
|
+
# fetch table schema
|
|
146
|
+
columns = [
|
|
147
|
+
ColumnSchema(
|
|
148
|
+
name=col,
|
|
149
|
+
original_data_type=str(table.dtypes[col].wrapped),
|
|
150
|
+
default_model_encoding_type=table.encoding_types[col].value,
|
|
151
|
+
)
|
|
152
|
+
for col in table.columns
|
|
153
|
+
]
|
|
154
|
+
relations = schema.subset(relations_to=[table_name]).relations
|
|
155
|
+
constraints = [
|
|
156
|
+
ConstraintSchema(foreign_key=rel.child.column, referenced_table=rel.parent.table) for rel in relations
|
|
157
|
+
]
|
|
158
|
+
# fetch table row count - but time-box to 10secs
|
|
159
|
+
total_rows = run_with_timeout_unsafe(lambda: table.row_count, timeout=10)
|
|
160
|
+
# instantiate TableSchema
|
|
161
|
+
table_schema = TableSchema(
|
|
162
|
+
name=table_name,
|
|
163
|
+
total_rows=total_rows,
|
|
164
|
+
primary_key=table.primary_key or primary_key,
|
|
165
|
+
columns=columns,
|
|
166
|
+
constraints=constraints,
|
|
167
|
+
location=location,
|
|
168
|
+
)
|
|
169
|
+
table_schemas.append(table_schema)
|
|
170
|
+
|
|
171
|
+
return table_schemas
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _collapse_table_schemas(table_schemas: list[TableSchema]) -> TableSchema:
|
|
175
|
+
n = len(table_schemas)
|
|
176
|
+
assert n > 0
|
|
177
|
+
if n == 1:
|
|
178
|
+
return table_schemas[0]
|
|
179
|
+
else:
|
|
180
|
+
table_schema = table_schemas[0]
|
|
181
|
+
table_schema.children = table_schemas[1:]
|
|
182
|
+
return table_schema
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def fetch_location_schema(connector: Connector, location: str, include_children: bool = False) -> TableSchema:
|
|
186
|
+
container = create_container_from_connector(connector)
|
|
187
|
+
meta = container.set_location(location)
|
|
188
|
+
if isinstance(container, SqlAlchemyContainer):
|
|
189
|
+
db_schema, table_name = meta["db_schema"], meta["table_name"]
|
|
190
|
+
if include_children:
|
|
191
|
+
container.filtered_tables = container.get_children(table_name)
|
|
192
|
+
else:
|
|
193
|
+
container.filtered_tables = [table_name]
|
|
194
|
+
container.fetch_schema()
|
|
195
|
+
schema = container.schema
|
|
196
|
+
ordered_table_names = container.filtered_tables
|
|
197
|
+
ordered_locations = [".".join([db_schema, table_name]) for table_name in ordered_table_names]
|
|
198
|
+
else:
|
|
199
|
+
location = meta["location"]
|
|
200
|
+
table = read_data_table_from_path(container)
|
|
201
|
+
schema = Schema(tables={table.name: table})
|
|
202
|
+
ordered_table_names = [table.name]
|
|
203
|
+
ordered_locations = [location]
|
|
204
|
+
|
|
205
|
+
table_schemas = _data_tables_to_table_schemas(
|
|
206
|
+
schema=schema,
|
|
207
|
+
ordered_table_names=ordered_table_names,
|
|
208
|
+
ordered_locations=ordered_locations,
|
|
209
|
+
)
|
|
210
|
+
table_schema = _collapse_table_schemas(table_schemas)
|
|
211
|
+
return table_schema
|
|
@@ -218,6 +218,87 @@ def filter_and_order_columns(data: pd.DataFrame, table_name: str, schema: Schema
|
|
|
218
218
|
return data[keep_cols]
|
|
219
219
|
|
|
220
220
|
|
|
221
|
+
def merge_extra_seed_into_output(
|
|
222
|
+
*,
|
|
223
|
+
table_name: str,
|
|
224
|
+
pqt_path: Path,
|
|
225
|
+
csv_path: Path | None,
|
|
226
|
+
schema: Schema,
|
|
227
|
+
) -> None:
|
|
228
|
+
"""
|
|
229
|
+
merge extra seed columns into already-written output files.
|
|
230
|
+
|
|
231
|
+
handles both subject tables (1:1 row alignment) and sequential tables
|
|
232
|
+
(merge by context key to handle row expansion from sequence completion).
|
|
233
|
+
"""
|
|
234
|
+
# get workspace_dir from schema table container path
|
|
235
|
+
table = schema.tables[table_name]
|
|
236
|
+
workspace_dir = table.container.path.parent # SyntheticData -> workspace_dir
|
|
237
|
+
extra_seed = load_extra_seed_columns(workspace_dir)
|
|
238
|
+
if extra_seed is None:
|
|
239
|
+
return
|
|
240
|
+
|
|
241
|
+
_LOG.info(f"merging extra seed columns into output for table {table_name}")
|
|
242
|
+
|
|
243
|
+
table = schema.tables[table_name]
|
|
244
|
+
|
|
245
|
+
context_key = next((fk for fk in table.foreign_keys if fk.is_context), None)
|
|
246
|
+
context_key_col = context_key.column if context_key else None
|
|
247
|
+
|
|
248
|
+
parquet_files = sorted(list(pqt_path.glob("*.parquet")))
|
|
249
|
+
if not parquet_files:
|
|
250
|
+
_LOG.warning(f"no parquet files found for table {table_name} at {pqt_path}")
|
|
251
|
+
return
|
|
252
|
+
|
|
253
|
+
if context_key_col:
|
|
254
|
+
# sequential table: use row index within each context group to align seed data
|
|
255
|
+
for file_path in parquet_files:
|
|
256
|
+
chunk_data = pd.read_parquet(file_path)
|
|
257
|
+
chunk_data["__row_idx__"] = chunk_data.groupby(context_key_col).cumcount()
|
|
258
|
+
|
|
259
|
+
chunk_data = pd.merge(chunk_data, extra_seed, on=[context_key_col, "__row_idx__"], how="left")
|
|
260
|
+
chunk_data = chunk_data.drop(columns=["__row_idx__"])
|
|
261
|
+
chunk_data.to_parquet(file_path, index=False)
|
|
262
|
+
else:
|
|
263
|
+
# subject table: use 1:1 row alignment
|
|
264
|
+
row_offset = 0
|
|
265
|
+
for file_path in parquet_files:
|
|
266
|
+
chunk_data = pd.read_parquet(file_path)
|
|
267
|
+
chunk_size = len(chunk_data)
|
|
268
|
+
|
|
269
|
+
chunk_extra = extra_seed.iloc[row_offset : row_offset + chunk_size].reset_index(drop=True)
|
|
270
|
+
if len(chunk_extra) == chunk_size:
|
|
271
|
+
chunk_data = pd.concat([chunk_data, chunk_extra], axis=1)
|
|
272
|
+
chunk_data.to_parquet(file_path, index=False)
|
|
273
|
+
elif len(chunk_extra) != chunk_size:
|
|
274
|
+
_LOG.warning(
|
|
275
|
+
f"extra seed columns chunk mismatch for {table_name}: expected {chunk_size}, got {len(chunk_extra)}"
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
row_offset += chunk_size
|
|
279
|
+
|
|
280
|
+
if csv_path:
|
|
281
|
+
_LOG.info(f"regenerating csv file for {table_name} with extra seed columns")
|
|
282
|
+
csv_file = csv_path / f"{table_name}.csv"
|
|
283
|
+
if csv_file.exists():
|
|
284
|
+
csv_file.unlink()
|
|
285
|
+
|
|
286
|
+
for file_path in parquet_files:
|
|
287
|
+
chunk_data = pd.read_parquet(file_path)
|
|
288
|
+
csv_post = CsvDataTable(path=csv_file, name=table_name)
|
|
289
|
+
csv_post.write_data(chunk_data, if_exists="append")
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def load_extra_seed_columns(workspace_dir: Path) -> pd.DataFrame | None:
|
|
293
|
+
"""load extra seed columns for a table if they exist."""
|
|
294
|
+
extra_seed_path = workspace_dir / "ExtraSeedColumns" / "seed_extra.parquet"
|
|
295
|
+
|
|
296
|
+
if extra_seed_path.exists():
|
|
297
|
+
_LOG.info(f"loading extra seed columns from {workspace_dir}")
|
|
298
|
+
return pd.read_parquet(extra_seed_path)
|
|
299
|
+
return None
|
|
300
|
+
|
|
301
|
+
|
|
221
302
|
def write_batch_outputs(
|
|
222
303
|
data: pd.DataFrame, table_name: str, batch_counter: int, pqt_path: Path, csv_path: Path | None
|
|
223
304
|
) -> None:
|
|
@@ -513,6 +594,14 @@ def finalize_table_generation(
|
|
|
513
594
|
csv_path=csv_path,
|
|
514
595
|
)
|
|
515
596
|
|
|
597
|
+
# merge extra seed columns as a separate post-processing step
|
|
598
|
+
merge_extra_seed_into_output(
|
|
599
|
+
table_name=target_table_name,
|
|
600
|
+
pqt_path=pqt_path,
|
|
601
|
+
csv_path=csv_path,
|
|
602
|
+
schema=generated_data_schema,
|
|
603
|
+
)
|
|
604
|
+
|
|
516
605
|
|
|
517
606
|
def export_data_to_excel(delivery_dir: Path, output_dir: Path):
|
|
518
607
|
output_dir.mkdir(parents=True, exist_ok=True)
|
|
@@ -92,6 +92,25 @@ def execute_step_generate_data(
|
|
|
92
92
|
imputation = config.imputation
|
|
93
93
|
fairness = config.fairness
|
|
94
94
|
|
|
95
|
+
# extract and save extra seed columns before generation
|
|
96
|
+
if sample_seed is not None:
|
|
97
|
+
model_columns = [c.name for c in tgt_g_table.columns if c.included]
|
|
98
|
+
extra_columns = [c for c in sample_seed.columns if c not in model_columns]
|
|
99
|
+
|
|
100
|
+
if extra_columns:
|
|
101
|
+
extra_seed_dir = workspace_dir / "ExtraSeedColumns"
|
|
102
|
+
extra_seed_dir.mkdir(parents=True, exist_ok=True)
|
|
103
|
+
|
|
104
|
+
extra_seed_data = sample_seed[extra_columns].copy()
|
|
105
|
+
context_fk = next((fk for fk in tgt_g_table.foreign_keys if fk.is_context), None)
|
|
106
|
+
if context_fk and context_fk.column in sample_seed.columns:
|
|
107
|
+
# for sequential tables, save context key + row index to align seed data after sequence completion
|
|
108
|
+
extra_seed_data[context_fk.column] = sample_seed[context_fk.column].astype("string[pyarrow]")
|
|
109
|
+
extra_seed_data["__row_idx__"] = extra_seed_data.groupby(context_fk.column).cumcount()
|
|
110
|
+
|
|
111
|
+
extra_seed_data.to_parquet(extra_seed_dir / "seed_extra.parquet")
|
|
112
|
+
sample_seed = sample_seed[[c for c in sample_seed.columns if c in model_columns]]
|
|
113
|
+
|
|
95
114
|
# call GENERATE
|
|
96
115
|
engine.generate(
|
|
97
116
|
ctx_data=ctx_data,
|
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
|
|
15
15
|
from pathlib import Path
|
|
16
16
|
|
|
17
|
+
from fastapi import HTTPException
|
|
18
|
+
|
|
19
|
+
from mostlyai.sdk._local import connectors
|
|
17
20
|
from mostlyai.sdk._local.execution.plan import (
|
|
18
21
|
FINALIZE_TRAINING_TASK_STEPS,
|
|
19
22
|
TRAINING_TASK_REPORT_STEPS,
|
|
@@ -24,6 +27,7 @@ from mostlyai.sdk._local.execution.plan import (
|
|
|
24
27
|
)
|
|
25
28
|
from mostlyai.sdk._local.storage import (
|
|
26
29
|
get_model_label,
|
|
30
|
+
read_connector_from_json,
|
|
27
31
|
read_generator_from_json,
|
|
28
32
|
write_connector_to_json,
|
|
29
33
|
write_generator_to_json,
|
|
@@ -37,6 +41,7 @@ from mostlyai.sdk.domain import (
|
|
|
37
41
|
Generator,
|
|
38
42
|
GeneratorConfig,
|
|
39
43
|
JobProgress,
|
|
44
|
+
ModelEncodingType,
|
|
40
45
|
ModelType,
|
|
41
46
|
ProgressStatus,
|
|
42
47
|
ProgressStep,
|
|
@@ -49,9 +54,9 @@ from mostlyai.sdk.domain import (
|
|
|
49
54
|
|
|
50
55
|
|
|
51
56
|
def create_generator(home_dir: Path, config: GeneratorConfig) -> Generator:
|
|
52
|
-
# handle file uploads -> create_connectors
|
|
53
57
|
for t in config.tables or []:
|
|
54
58
|
if t.data is not None:
|
|
59
|
+
# handle file uploads -> create_connectors
|
|
55
60
|
connector = Connector(
|
|
56
61
|
**{
|
|
57
62
|
"name": "FILE_UPLOAD",
|
|
@@ -66,14 +71,59 @@ def create_generator(home_dir: Path, config: GeneratorConfig) -> Generator:
|
|
|
66
71
|
t.data = None
|
|
67
72
|
t.source_connector_id = connector.id
|
|
68
73
|
t.location = str(fn.absolute())
|
|
69
|
-
if t.columns is None:
|
|
70
|
-
t.columns = [
|
|
71
|
-
SourceColumnConfig(
|
|
72
|
-
name=c,
|
|
73
|
-
)
|
|
74
|
-
for c in list(df.columns)
|
|
75
|
-
]
|
|
76
74
|
write_connector_to_json(home_dir / "connectors" / connector.id, connector)
|
|
75
|
+
else:
|
|
76
|
+
connector = read_connector_from_json(home_dir / "connectors" / t.source_connector_id)
|
|
77
|
+
|
|
78
|
+
try:
|
|
79
|
+
table_schema = connectors.fetch_location_schema(connector, t.location)
|
|
80
|
+
except Exception:
|
|
81
|
+
raise HTTPException(
|
|
82
|
+
status_code=400,
|
|
83
|
+
detail=f"Cannot create generator due to failure to fetch schema of `{t.name}`."
|
|
84
|
+
f" Please check whether source_connector_id and location are correct.",
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
# replace auto-detected LANGUAGE_TEXT columns with TABULAR_CHARACTER only for LOCAL mode
|
|
88
|
+
auto_detected_columns: dict[str, ModelEncodingType] = {
|
|
89
|
+
c.name: (
|
|
90
|
+
ModelEncodingType.tabular_character
|
|
91
|
+
if c.default_model_encoding_type == ModelEncodingType.language_text
|
|
92
|
+
else ModelEncodingType(c.default_model_encoding_type)
|
|
93
|
+
)
|
|
94
|
+
for c in table_schema.columns
|
|
95
|
+
}
|
|
96
|
+
# auto-detect columns if not provided
|
|
97
|
+
if t.columns is None:
|
|
98
|
+
t.columns = [
|
|
99
|
+
SourceColumnConfig(
|
|
100
|
+
name=name,
|
|
101
|
+
model_encoding_type=model_encoding_type,
|
|
102
|
+
)
|
|
103
|
+
for name, model_encoding_type in auto_detected_columns.items()
|
|
104
|
+
]
|
|
105
|
+
else:
|
|
106
|
+
for c in t.columns:
|
|
107
|
+
if c.name not in auto_detected_columns:
|
|
108
|
+
raise HTTPException(
|
|
109
|
+
status_code=400,
|
|
110
|
+
detail=f"Column `{c.name}` does not exist in the table.",
|
|
111
|
+
)
|
|
112
|
+
if c.model_encoding_type == ModelEncodingType.auto:
|
|
113
|
+
c.model_encoding_type = auto_detected_columns[c.name]
|
|
114
|
+
|
|
115
|
+
# auto-detect primary key if not provided
|
|
116
|
+
auto_detected_primary_key = table_schema.primary_key
|
|
117
|
+
foreign_keys = [fk.column for fk in t.foreign_keys or []]
|
|
118
|
+
included_columns = [c.name for c in t.columns]
|
|
119
|
+
if (
|
|
120
|
+
t.primary_key is None
|
|
121
|
+
and auto_detected_primary_key not in foreign_keys
|
|
122
|
+
and auto_detected_primary_key in included_columns
|
|
123
|
+
):
|
|
124
|
+
t.primary_key = auto_detected_primary_key
|
|
125
|
+
# reinitialize the config to validate updated config after auto detection
|
|
126
|
+
config = GeneratorConfig(**config.model_dump())
|
|
77
127
|
|
|
78
128
|
# create generator
|
|
79
129
|
generator = Generator(
|
|
@@ -28,7 +28,6 @@ from starlette.background import BackgroundTask
|
|
|
28
28
|
|
|
29
29
|
from mostlyai import sdk
|
|
30
30
|
from mostlyai.sdk._data.conversions import create_container_from_connector
|
|
31
|
-
from mostlyai.sdk._data.file.utils import read_data_table_from_path
|
|
32
31
|
from mostlyai.sdk._local import connectors, generators, synthetic_datasets
|
|
33
32
|
from mostlyai.sdk._local.execution.jobs import execute_probing_job
|
|
34
33
|
from mostlyai.sdk._local.generators import create_generator as create_generator_model
|
|
@@ -219,26 +218,11 @@ class Routes:
|
|
|
219
218
|
async def location_schema(id: str, location: str):
|
|
220
219
|
connector_dir = self.home_dir / "connectors" / id
|
|
221
220
|
connector = read_connector_from_json(connector_dir)
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
container.fetch_schema()
|
|
228
|
-
table = container.schema.tables.get(table_name)
|
|
229
|
-
else:
|
|
230
|
-
table = read_data_table_from_path(container)
|
|
231
|
-
if not table:
|
|
232
|
-
return JSONResponse(status_code=400, content=f"Table at {location} not found")
|
|
233
|
-
columns = [
|
|
234
|
-
{
|
|
235
|
-
"name": col,
|
|
236
|
-
"originalDataType": str(table.dtypes[col].wrapped),
|
|
237
|
-
"defaultModelEncodingType": table.encoding_types[col].value,
|
|
238
|
-
}
|
|
239
|
-
for col in table.columns
|
|
240
|
-
]
|
|
241
|
-
return JSONResponse(status_code=200, content=columns)
|
|
221
|
+
try:
|
|
222
|
+
table_schema = connectors.fetch_location_schema(connector, location, include_children=False)
|
|
223
|
+
return table_schema.columns
|
|
224
|
+
except Exception as e:
|
|
225
|
+
return JSONResponse(status_code=400, content=str(e))
|
|
242
226
|
|
|
243
227
|
@self.router.post("/connectors/{id}/read-data")
|
|
244
228
|
async def read_data(id: str, config: ConnectorReadDataConfig = Body(...)) -> StreamingResponse:
|
|
@@ -699,7 +699,7 @@ class MostlyAI(_MostlyBaseClient):
|
|
|
699
699
|
generator (Generator | str): The generator instance or its UUID.
|
|
700
700
|
config (SyntheticDatasetConfig | dict | None): Configuration for the synthetic dataset.
|
|
701
701
|
size (int | dict[str, int] | None): Sample size(s) for the subject table(s).
|
|
702
|
-
seed (Seed | dict[str, Seed] | None): Either a single Seed for the subject table, or a dictionary with table names as keys and Seeds as values. Seed can either be a DataFrame or a path to a CSV or PARQUET file. Check generator details (`generator.tables[i].columns[j].value_range`) for possible value ranges.
|
|
702
|
+
seed (Seed | dict[str, Seed] | None): Either a single Seed for the subject table, or a dictionary with table names as keys and Seeds as values. Seed can either be a DataFrame or a path to a CSV or PARQUET file. Check generator details (`generator.tables[i].columns[j].value_range`) for possible value ranges. Note: Extra columns in seed data (not part of the trained generator) will be retained and merged into the output.
|
|
703
703
|
name (str | None): Name of the synthetic dataset.
|
|
704
704
|
start (bool): Whether to start generation immediately. Default is True.
|
|
705
705
|
wait (bool): Whether to wait for generation to finish. Default is True.
|
|
@@ -779,8 +779,7 @@ class MostlyAI(_MostlyBaseClient):
|
|
|
779
779
|
Args:
|
|
780
780
|
generator (Generator | str): The generator instance or its UUID.
|
|
781
781
|
size (int | dict[str, int] | None): Sample size(s) for the subject table(s). Default is 1, if no seed is provided.
|
|
782
|
-
seed (Seed | dict[str, Seed] | None): Either a single Seed for the subject table, or a dictionary with table names as keys and Seeds as values. Seed can either be a DataFrame or a path to a CSV or PARQUET file. Check generator details (`generator.tables[i].columns[j].value_range`) for possible value ranges. Seed data may contain null values; columns listed in `imputation.columns` will have nulls generated, while unlisted columns keep nulls as-is.
|
|
783
|
-
config (SyntheticProbeConfig | dict | None): Configuration for the probe.
|
|
782
|
+
seed (Seed | dict[str, Seed] | None): Either a single Seed for the subject table, or a dictionary with table names as keys and Seeds as values. Seed can either be a DataFrame or a path to a CSV or PARQUET file. Check generator details (`generator.tables[i].columns[j].value_range`) for possible value ranges. Seed data may contain null values; columns listed in `imputation.columns` will have nulls generated, while unlisted columns keep nulls as-is. Note: Extra columns in seed data (not part of the trained generator) will be retained and merged into the output.
|
|
784
783
|
return_type (Literal["auto", "dict"]): The type of the return value. "dict" will always provide a dictionary of DataFrames. "auto" will return a single DataFrame for a single-table generator, and a dictionary of DataFrames for a multi-table generator. Default is "auto".
|
|
785
784
|
|
|
786
785
|
Returns:
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
15
|
# generated by datamodel-codegen:
|
|
16
|
-
# timestamp: 2025-11-
|
|
16
|
+
# timestamp: 2025-11-19T10:29:35+00:00
|
|
17
17
|
|
|
18
18
|
from __future__ import annotations
|
|
19
19
|
|
|
@@ -3173,18 +3173,26 @@ class SourceTableConfig(CustomBaseModel):
|
|
|
3173
3173
|
@model_validator(mode="after")
|
|
3174
3174
|
def add_model_configuration(self):
|
|
3175
3175
|
# Check if the table has a tabular and/or a language model
|
|
3176
|
-
columns = self.columns or []
|
|
3177
3176
|
keys = [fk.column for fk in self.foreign_keys or []]
|
|
3178
3177
|
if self.primary_key:
|
|
3179
3178
|
keys.append(self.primary_key)
|
|
3180
|
-
model_columns = [c for c in columns if c.name not in keys]
|
|
3181
|
-
if model_columns:
|
|
3182
|
-
|
|
3183
|
-
has_tabular_model =
|
|
3184
|
-
has_language_model =
|
|
3185
|
-
|
|
3179
|
+
model_columns = [c for c in self.columns if c.name not in keys] if self.columns is not None else None
|
|
3180
|
+
if model_columns is None:
|
|
3181
|
+
# auto detection haven't been run yet, so we assume both models are present to retain model configurations given by the user
|
|
3182
|
+
has_tabular_model = True
|
|
3183
|
+
has_language_model = True
|
|
3184
|
+
elif len(model_columns) == 0:
|
|
3185
|
+
# this table doesn't have any columns other than PK/FKs
|
|
3186
3186
|
has_tabular_model = True
|
|
3187
3187
|
has_language_model = False
|
|
3188
|
+
else:
|
|
3189
|
+
enc_types = [c.model_encoding_type or ModelEncodingType.auto for c in model_columns]
|
|
3190
|
+
has_tabular_model = any(
|
|
3191
|
+
enc_type.startswith(ModelType.tabular) or enc_type == ModelEncodingType.auto for enc_type in enc_types
|
|
3192
|
+
)
|
|
3193
|
+
has_language_model = any(
|
|
3194
|
+
enc_type.startswith(ModelType.language) or enc_type == ModelEncodingType.auto for enc_type in enc_types
|
|
3195
|
+
)
|
|
3188
3196
|
# Always train tabular model for tables with a primary key or linked tables to model sequences
|
|
3189
3197
|
if self.primary_key or (self.foreign_keys and any(fk.is_context for fk in self.foreign_keys)):
|
|
3190
3198
|
has_tabular_model = True
|
|
@@ -3194,13 +3202,18 @@ class SourceTableConfig(CustomBaseModel):
|
|
|
3194
3202
|
if self.language_model_configuration and not has_language_model:
|
|
3195
3203
|
self.language_model_configuration = None
|
|
3196
3204
|
# Add default model configurations if none were provided
|
|
3197
|
-
if has_tabular_model
|
|
3205
|
+
if has_tabular_model:
|
|
3198
3206
|
default_model = "MOSTLY_AI/Medium"
|
|
3199
|
-
self.tabular_model_configuration
|
|
3200
|
-
|
|
3207
|
+
if not self.tabular_model_configuration:
|
|
3208
|
+
self.tabular_model_configuration = ModelConfiguration(model=default_model)
|
|
3209
|
+
elif not self.tabular_model_configuration.model:
|
|
3210
|
+
self.tabular_model_configuration.model = default_model
|
|
3211
|
+
if has_language_model:
|
|
3201
3212
|
default_model = "MOSTLY_AI/LSTMFromScratch-3m"
|
|
3202
|
-
self.language_model_configuration
|
|
3203
|
-
|
|
3213
|
+
if not self.language_model_configuration:
|
|
3214
|
+
self.language_model_configuration = ModelConfiguration(model=default_model, max_sequence_window=None)
|
|
3215
|
+
elif not self.language_model_configuration.model:
|
|
3216
|
+
self.language_model_configuration.model = default_model
|
|
3204
3217
|
# language models atm do not support max_sequence_window; thus set configuration to None
|
|
3205
3218
|
self.language_model_configuration.max_sequence_window = None
|
|
3206
3219
|
return self
|
|
@@ -3235,7 +3248,7 @@ class SourceTableConfig(CustomBaseModel):
|
|
|
3235
3248
|
|
|
3236
3249
|
@model_validator(mode="after")
|
|
3237
3250
|
def validate_keys_exists_in_columns(self):
|
|
3238
|
-
if self.columns:
|
|
3251
|
+
if self.columns is not None:
|
|
3239
3252
|
column_names = {col.name for col in self.columns}
|
|
3240
3253
|
pk = self.primary_key
|
|
3241
3254
|
if pk and pk not in column_names:
|
|
@@ -3253,6 +3266,18 @@ class SourceTableConfig(CustomBaseModel):
|
|
|
3253
3266
|
raise ValueError(f"Column '{primary_key}' is both a primary key and a foreign key.")
|
|
3254
3267
|
return self
|
|
3255
3268
|
|
|
3269
|
+
@model_validator(mode="after")
|
|
3270
|
+
def validate_data_or_connector_is_provided(self):
|
|
3271
|
+
if self.data is None and (self.source_connector_id is None or self.location is None):
|
|
3272
|
+
raise ValueError(
|
|
3273
|
+
"At least one input source must be provided: either `data`, or both `source_connector_id` and `location`."
|
|
3274
|
+
)
|
|
3275
|
+
elif self.data is not None and (self.source_connector_id is not None or self.location is not None):
|
|
3276
|
+
raise ValueError(
|
|
3277
|
+
"Only one input source is allowed: either `data`, or both `source_connector_id` and `location`."
|
|
3278
|
+
)
|
|
3279
|
+
return self
|
|
3280
|
+
|
|
3256
3281
|
|
|
3257
3282
|
class SourceTablePatchConfig(CustomBaseModel):
|
|
3258
3283
|
"""
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/step_analyze_training_data.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/step_encode_training_data.py
RENAMED
|
File without changes
|
|
File without changes
|
{mostlyai-5.7.0 → mostlyai-5.8.0}/mostlyai/sdk/_local/execution/step_generate_model_report_data.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|