mostlyai 0.3.5__tar.gz → 0.3.6__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mostlyai
3
- Version: 0.3.5
3
+ Version: 0.3.6
4
4
  Summary: The official Python client for the MOSTLY AI platform.
5
5
  Home-page: https://app.mostly.ai/
6
6
  License: Proprietary
@@ -13,11 +13,11 @@ Classifier: Programming Language :: Python :: 3.9
13
13
  Classifier: Programming Language :: Python :: 3.10
14
14
  Classifier: Programming Language :: Python :: 3.11
15
15
  Classifier: Programming Language :: Python :: 3.12
16
- Requires-Dist: httpx (>=0.25.1,<0.26.0)
16
+ Requires-Dist: httpx (>=0.25.0)
17
17
  Requires-Dist: pandas (>=1.5.3,<3.0.0)
18
- Requires-Dist: pyarrow (>=14.0.2,<15.0.0)
18
+ Requires-Dist: pyarrow (>=14.0.0)
19
19
  Requires-Dist: pydantic (>=2.4.2,<3.0.0)
20
- Requires-Dist: rich (>=13.7.0,<14.0.0)
20
+ Requires-Dist: rich (>=13.7.0)
21
21
  Description-Content-Type: text/markdown
22
22
 
23
23
  # MOSTLY AI - GenAI for Tabular Data
@@ -44,7 +44,7 @@ pip install mostlyai
44
44
  ## Basic Usage
45
45
  ```python
46
46
  from mostlyai import MostlyAI
47
- mostly = MostlyAI(api_key='your_api_key')
47
+ mostly = MostlyAI(api_key='your_api_key')
48
48
  g = mostly.train(data) # train a generator on your data
49
49
  sd = mostly.generate(g) # generate a synthetic dataset
50
50
  syn = sd.data() # consume synthetic as pd.DataFrame
@@ -113,6 +113,13 @@ sd.data()
113
113
  sd.download(file, format)
114
114
  ```
115
115
 
116
+ ### Synthetic Probes
117
+ ```python
118
+ sp = mostly.probe(g, seed=seed)
119
+ sp = mostly.probe(g, size=size)
120
+ sp = mostly.probe(g, config=config)
121
+ ```
122
+
116
123
  ### Sharing
117
124
 
118
125
  ```python
@@ -22,7 +22,7 @@ pip install mostlyai
22
22
  ## Basic Usage
23
23
  ```python
24
24
  from mostlyai import MostlyAI
25
- mostly = MostlyAI(api_key='your_api_key')
25
+ mostly = MostlyAI(api_key='your_api_key')
26
26
  g = mostly.train(data) # train a generator on your data
27
27
  sd = mostly.generate(g) # generate a synthetic dataset
28
28
  syn = sd.data() # consume synthetic as pd.DataFrame
@@ -91,6 +91,13 @@ sd.data()
91
91
  sd.download(file, format)
92
92
  ```
93
93
 
94
+ ### Synthetic Probes
95
+ ```python
96
+ sp = mostly.probe(g, seed=seed)
97
+ sp = mostly.probe(g, size=size)
98
+ sp = mostly.probe(g, config=config)
99
+ ```
100
+
94
101
  ### Sharing
95
102
 
96
103
  ```python
@@ -0,0 +1,3 @@
1
+ from mostlyai.api import MostlyAI
2
+
3
+ __all__ = ["MostlyAI"]
@@ -1,5 +1,5 @@
1
1
  from pathlib import Path
2
- from typing import Any, Optional, Union
2
+ from typing import Any, Optional, Union, Literal
3
3
 
4
4
  import pandas as pd
5
5
  import rich
@@ -13,13 +13,18 @@ from mostlyai.model import (
13
13
  Generator,
14
14
  PermissionLevel,
15
15
  ProgressStatus,
16
+ SyntheticDataset,
16
17
  )
17
18
  from mostlyai.shares import _MostlySharesClient
18
- from mostlyai.synthetic_datasets import _MostlySyntheticDatasetsClient
19
+ from mostlyai.synthetic_datasets import (
20
+ _MostlySyntheticDatasetsClient,
21
+ _MostlySyntheticProbesClient,
22
+ )
19
23
  from mostlyai.utils import (
20
24
  ShareableResource,
21
- _get_subject_table_names,
22
25
  _read_table_from_path,
26
+ _harmonize_sd_config,
27
+ Seed,
23
28
  )
24
29
 
25
30
 
@@ -52,6 +57,7 @@ class MostlyAI(_MostlyBaseClient):
52
57
  self.connectors = _MostlyConnectorsClient(**client_kwargs)
53
58
  self.generators = _MostlyGeneratorsClient(**client_kwargs)
54
59
  self.synthetic_datasets = _MostlySyntheticDatasetsClient(**client_kwargs)
60
+ self.synthetic_probes = _MostlySyntheticProbesClient(**client_kwargs)
55
61
  self.shares = _MostlySharesClient(**client_kwargs)
56
62
 
57
63
  def connect(self, config: dict[str, Any]) -> Connector:
@@ -64,9 +70,8 @@ class MostlyAI(_MostlyBaseClient):
64
70
  The structures of the `config`, `secrets` and `ssl` parameters depend on the connector `type`:
65
71
 
66
72
  - Cloud storage:
67
- - \t
68
- ```yaml
69
- type: AZURE_STORAGE
73
+ ```yaml
74
+ - type: AZURE_STORAGE
70
75
  config:
71
76
  accountName: string
72
77
  clientId: string (required for auth via service principal)
@@ -74,126 +79,106 @@ class MostlyAI(_MostlyBaseClient):
74
79
  secrets:
75
80
  accountKey: string (required for regular auth)
76
81
  clientSecret: string (required for auth via service principal)
77
- ```
78
- - \t
79
- ```yaml
80
- type: GOOGLE_CLOUD_STORAGE
82
+
83
+ - type: GOOGLE_CLOUD_STORAGE
81
84
  config:
82
85
  secrets:
83
86
  keyFile: string
84
- ```
85
- - \t
86
- ```yaml
87
- type: S3_STORAGE
87
+
88
+ - type: S3_STORAGE
88
89
  config:
89
90
  accessKey: string
91
+ endpointUrl: string (only needed for S3-compatible storage services other than AWS)
90
92
  secrets:
91
93
  secretKey: string
92
- ```
94
+ ```
93
95
  - Database:
94
- - \t
95
- ```yaml
96
- type: BIGQUERY
97
- config:
98
- secrets:
99
- keyFile: string
100
- ```
101
- - \t
102
- ```yaml
103
- type: DATABRICKS
104
- config:
105
- host: string
106
- httpPath: string
107
- catalog: string
108
- clientId: string (required for auth via service principal)
109
- tenantId: string (required for auth via service principal)
110
- secrets:
111
- accessToken: string (required for regular auth)
112
- clientSecret: string (required for auth via service principal)
113
- ```
114
- - \t
115
- ```yaml
116
- type: HIVE
117
- config:
118
- host: string
119
- port: integer, default: 10000
120
- username: string (required for regular auth)
121
- kerberos_enabled: boolean, default: false
122
- kerberos_principal: string (required if kerberos_enabled)
123
- kerberos_krb5_conf: string (required if kerberos_enabled)
124
- secrets:
125
- password: string (required for regular auth)
126
- kerberos_keytab: base64-encoded string (required if kerberos_enabled)
127
- ```
128
- - \t
129
- ```yaml
130
- type: MARIADB
131
- config:
132
- host: string
133
- port: integer, default: 3306
134
- username: string
135
- secrets:
136
- password: string
137
- ```
138
- - \t
139
- ```yaml
140
- type: MSSQL
141
- config:
142
- host: string
143
- port: integer, default: 1433
144
- username: string
145
- database: string
146
- secrets:
147
- password: string
148
- ```
149
- - \t
150
- ```yaml
151
- type: MYSQL
152
- config:
153
- host: string
154
- port: integer, default: 3306
155
- username: string
156
- secrets:
157
- password: string
158
- ```
159
- - \t
160
- ```yaml
161
- type: ORACLE
162
- config:
163
- host: string
164
- port: integer, default: 1521
165
- username: string
166
- connectionType: enum {SID, SERVICE_NAME}, default: SID
167
- database: string, default: ORCL
168
- secrets:
169
- password: string
170
- ```
171
- - \t
172
- ```yaml
173
- type: POSTGRES
174
- config:
175
- host: string
176
- port: integer, default: 5432
177
- username: string
178
- database: string
179
- secrets:
180
- password: string
181
- ssl:
182
- rootCertificate: string
183
- sslCertificate: string
184
- sslCertificateKey: string
185
- ```
186
- - \t
187
- ```yaml
188
- type: SNOWFLAKE
189
- config:
190
- account: string
191
- username: string
192
- warehouse: string, default: COMPUTE_WH
193
- database: string
194
- secrets:
195
- password: string
196
- ```
96
+ ```yaml
97
+ - type: BIGQUERY
98
+ config:
99
+ secrets:
100
+ keyFile: string
101
+
102
+ - type: DATABRICKS
103
+ config:
104
+ host: string
105
+ httpPath: string
106
+ catalog: string
107
+ clientId: string (required for auth via service principal)
108
+ tenantId: string (required for auth via service principal)
109
+ secrets:
110
+ accessToken: string (required for regular auth)
111
+ clientSecret: string (required for auth via service principal)
112
+
113
+ - type: HIVE
114
+ config:
115
+ host: string
116
+ port: integer, default: 10000
117
+ username: string (required for regular auth)
118
+ kerberos_enabled: boolean, default: false
119
+ kerberos_principal: string (required if kerberos_enabled)
120
+ kerberos_krb5_conf: string (required if kerberos_enabled)
121
+ secrets:
122
+ password: string (required for regular auth)
123
+ kerberos_keytab: base64-encoded string (required if kerberos_enabled)
124
+
125
+ - type: MARIADB
126
+ config:
127
+ host: string
128
+ port: integer, default: 3306
129
+ username: string
130
+ secrets:
131
+ password: string
132
+
133
+ - type: MSSQL
134
+ config:
135
+ host: string
136
+ port: integer, default: 1433
137
+ username: string
138
+ database: string
139
+ secrets:
140
+ password: string
141
+
142
+ - type: MYSQL
143
+ config:
144
+ host: string
145
+ port: integer, default: 3306
146
+ username: string
147
+ secrets:
148
+ password: string
149
+
150
+ - type: ORACLE
151
+ config:
152
+ host: string
153
+ port: integer, default: 1521
154
+ username: string
155
+ connectionType: enum {SID, SERVICE_NAME}, default: SID
156
+ database: string, default: ORCL
157
+ secrets:
158
+ password: string
159
+
160
+ - type: POSTGRES
161
+ config:
162
+ host: string
163
+ port: integer, default: 5432
164
+ username: string
165
+ database: string
166
+ secrets:
167
+ password: string
168
+ ssl:
169
+ rootCertificate: string
170
+ sslCertificate: string
171
+ sslCertificateKey: string
172
+
173
+ - type: SNOWFLAKE
174
+ config:
175
+ account: string
176
+ username: string
177
+ warehouse: string, default: COMPUTE_WH
178
+ database: string
179
+ secrets:
180
+ password: string
181
+ ```
197
182
 
198
183
  :return: The created connector.
199
184
  """
@@ -210,7 +195,8 @@ class MostlyAI(_MostlyBaseClient):
210
195
  name: Optional[str] = None,
211
196
  start: bool = True,
212
197
  wait: bool = True,
213
- ):
198
+ progress_bar: bool = True,
199
+ ) -> Generator:
214
200
  """
215
201
  Train a generator
216
202
 
@@ -219,6 +205,7 @@ class MostlyAI(_MostlyBaseClient):
219
205
  :param name: Optional. The name of the generator.
220
206
  :param start: If true, then training is started right away. Default: true.
221
207
  :param wait: If true, then the function only returns once training has finished. Default: true.
208
+ :param progress_bar: If true, then the progress bar will be displayed, in case of wait=True
222
209
  :return: The created generator.
223
210
  """
224
211
  if data is not None and config is not None:
@@ -244,7 +231,7 @@ class MostlyAI(_MostlyBaseClient):
244
231
  g.training.start()
245
232
  rich.print("Started generator training")
246
233
  if start and wait:
247
- g = g.training.wait()
234
+ g = g.training.wait(progress_bar=progress_bar)
248
235
  if g.training_status == ProgressStatus.done:
249
236
  rich.print(
250
237
  ":tada: [bold green]Your generator is ready![/] "
@@ -257,16 +244,15 @@ class MostlyAI(_MostlyBaseClient):
257
244
  self,
258
245
  generator: Union[Generator, str, None] = None,
259
246
  size: Union[int, dict[str, int], None] = None,
260
- seed: Union[
261
- pd.DataFrame, str, Path, dict[str, Union[pd.DataFrame, str, Path]], None
262
- ] = None,
247
+ seed: Union[Seed, dict[str, Seed], None] = None,
263
248
  config: Union[dict, None] = None,
264
249
  name: Optional[str] = None,
265
250
  start: bool = True,
266
251
  wait: bool = True,
267
- ):
252
+ progress_bar: bool = True,
253
+ ) -> SyntheticDataset:
268
254
  """
269
- Train a generator
255
+ Generate synthetic data
270
256
 
271
257
  :param generator: The generator instance or its UUID, that is to be used for generating synthetic data.
272
258
  :param config: The configuration parameters of the synthetic dataset to be created. See SyntheticDataset.config for the structure of the parameters.
@@ -275,40 +261,10 @@ class MostlyAI(_MostlyBaseClient):
275
261
  :param name: Optional. The name of the synthetic dataset.
276
262
  :param start: If true, then generation is started right away. Default: true.
277
263
  :param wait: If true, then the function only returns once generation has finished. Default: true.
264
+ :param progress_bar: If true, then the progress bar will be displayed, in case of wait=True
278
265
  :return: The created synthetic dataset.
279
266
  """
280
- if config is None:
281
- config = {}
282
- if isinstance(generator, Generator):
283
- config["generatorId"] = str(generator.id)
284
- elif generator is not None:
285
- config["generatorId"] = str(generator)
286
- elif "generatorId" not in config:
287
- raise ValueError(
288
- "Either a generator or a configuration with a generatorId must be provided."
289
- )
290
-
291
- if "tables" not in config:
292
- g = self.generators.get(config["generatorId"])
293
- subject_tables = _get_subject_table_names(g.config())
294
- config["tables"] = [
295
- {
296
- "name": table,
297
- "configuration": {
298
- "sampleSize": (
299
- size.get(table) if isinstance(size, dict) else size
300
- ),
301
- "sampleSeedData": (
302
- seed.get(table) if isinstance(seed, dict) else seed
303
- ),
304
- },
305
- }
306
- for table in subject_tables
307
- ]
308
-
309
- if name is not None:
310
- config |= {"name": name}
311
-
267
+ config = _harmonize_sd_config(generator, size, seed, config, name)
312
268
  sd = self.synthetic_datasets.create(config)
313
269
  rich.print(
314
270
  f"Created synthetic dataset "
@@ -320,7 +276,7 @@ class MostlyAI(_MostlyBaseClient):
320
276
  sd.generation.start()
321
277
  rich.print("Started synthetic dataset generation")
322
278
  if start and wait:
323
- sd = sd.generation.wait()
279
+ sd = sd.generation.wait(progress_bar=progress_bar)
324
280
  if sd.generation_status == ProgressStatus.done:
325
281
  rich.print(
326
282
  ":tada: [bold green]Your synthetic dataset is ready![/] "
@@ -329,6 +285,30 @@ class MostlyAI(_MostlyBaseClient):
329
285
  )
330
286
  return sd
331
287
 
288
+ def probe(
289
+ self,
290
+ generator: Union[Generator, str, None] = None,
291
+ size: Union[int, dict[str, int], None] = None,
292
+ seed: Union[Seed, dict[str, Seed], None] = None,
293
+ config: Union[dict, None] = None,
294
+ return_type: Literal["auto", "dict"] = "auto",
295
+ ) -> Union[pd.DataFrame, dict[str, pd.DataFrame]]:
296
+ """
297
+ Probe a generator
298
+
299
+ :param generator: The generator instance or its UUID, that is to be used for generating synthetic data.
300
+ :param config: The configuration parameters of the synthetic dataset to be created. See SyntheticDataset.config for the structure of the parameters.
301
+ :param size: Optional. Either a single integer, or a dictionary of integers. Used for specifying the sample_size of the subject table(s).
302
+ :param seed: Optional. Either a single pandas DataFrame data, or a path to a CSV or PARQUET file, or list of samples, or a dictionary of those. Used for seeding the subject table(s).
303
+ :return: The created synthetic probe.
304
+ """
305
+ config = _harmonize_sd_config(generator, size, seed, config)
306
+ dfs = self.synthetic_probes.create(config)
307
+ if return_type == "auto" and len(dfs) == 1:
308
+ return list(dfs.values())[0]
309
+ else:
310
+ return dfs
311
+
332
312
  # SHARES
333
313
 
334
314
  def share(
@@ -6,7 +6,7 @@ import pandas as pd
6
6
  from mostlyai.base import DELETE, GET, PATCH, POST, Paginator, _MostlyBaseClient
7
7
  from mostlyai.model import Generator, JobProgress
8
8
  from mostlyai.shares import _MostlySharesMixin
9
- from mostlyai.utils import _convert_df_to_base64, _job_wait, _read_table_from_path
9
+ from mostlyai.utils import _convert_to_base64, _job_wait, _read_table_from_path
10
10
 
11
11
 
12
12
  class _MostlyGeneratorsClient(_MostlyBaseClient, _MostlySharesMixin):
@@ -61,12 +61,12 @@ class _MostlyGeneratorsClient(_MostlyBaseClient, _MostlySharesMixin):
61
61
  if "data" in table and table["data"] is not None:
62
62
  if isinstance(table["data"], (str, Path)):
63
63
  name, df = _read_table_from_path(table["data"])
64
- table["data"] = _convert_df_to_base64(df)
64
+ table["data"] = _convert_to_base64(df)
65
65
  if "name" not in table:
66
66
  table["name"] = name
67
67
  del df
68
68
  elif isinstance(table["data"], pd.DataFrame):
69
- table["data"] = _convert_df_to_base64(table["data"])
69
+ table["data"] = _convert_to_base64(table["data"])
70
70
  else:
71
71
  raise ValueError("data must be a DataFrame or a file path")
72
72
 
@@ -108,7 +108,9 @@ class _MostlyGeneratorsClient(_MostlyBaseClient, _MostlySharesMixin):
108
108
  )
109
109
  return response
110
110
 
111
- def _training_wait(self, generator_id: str, interval: float) -> Generator:
112
- _job_wait(lambda: self._training_progress(generator_id), interval)
111
+ def _training_wait(
112
+ self, generator_id: str, progress_bar: bool, interval: float
113
+ ) -> Generator:
114
+ _job_wait(lambda: self._training_progress(generator_id), interval, progress_bar)
113
115
  generator = self.get(generator_id)
114
116
  return generator