API reference
Generator
The generator module provides generator functions for generating realistic data. These generators wrap around common data sources such as frequency tables and numeric distributions.
from_frequency_table(csv_file_path, value_column=0, freq_column=1, encoding='utf-8', delimiter=',', rng=None)
Generate data from a frequency table. The frequency table must be provided in CSV format and contain at least two columns: one containing values to generate and one containing their assigned absolute frequencies. Values generated by this function will have a distribution similar to the frequencies listed in the input file. If the value and frequency column are provided as strings, then it is automatically assumed that the CSV file has a header row.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csv_file_path |
Union[str, PathLike[str]]
|
path to CSV file |
required |
value_column |
Union[str, int]
|
name or index of the value column |
0
|
freq_column |
Union[str, int]
|
name or index of the frequency column |
1
|
encoding |
str
|
character encoding of the CSV file |
'utf-8'
|
delimiter |
str
|
column delimiter of the CSV file |
','
|
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
Generator
|
function returning list with single series containing values generated from the input file |
Source code in gecko/generator.py
from_function(func, *args, **kwargs)
Generate data from an arbitrary function that returns a single value at a time.
Notes
This function should be used sparingly since it is not vectorized. Only use it for testing purposes or if performance is not important.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[P, str]
|
function to invoke to generate data from |
required |
*args |
object
|
positional arguments to pass to |
()
|
**kwargs |
object
|
keyword arguments to pass to |
{}
|
Returns:
Type | Description |
---|---|
Generator
|
function returning list with strings generated from custom function |
Source code in gecko/generator.py
from_multicolumn_frequency_table(csv_file_path, value_columns=0, freq_column=1, encoding='utf-8', delimiter=',', rng=None)
Generate data from a frequency table with multiple interdependent columns.. The frequency table must be provided in CSV format and contain at least two columns: one containing values to generate and one containing their assigned absolute frequencies. Values generated by this function will have a distribution similar to the frequencies listed in the input file. If the values and frequency column are provided as strings, then it is automatically assumed that the CSV file has a header row.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csv_file_path |
Union[str, PathLike[str]]
|
path to CSV file |
required |
value_columns |
Union[int, str, list[int], list[str]]
|
names or indices of the value columns |
0
|
freq_column |
Union[int, str]
|
name or index of the frequency column |
1
|
encoding |
str
|
character encoding of the CSV file |
'utf-8'
|
delimiter |
str
|
column delimiter of the CSV file |
','
|
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
Generator
|
function returning list with as many series as there are value columns specified containing values generated from the input file |
Source code in gecko/generator.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
|
from_normal_distribution(mean=0, sd=1, precision=6, rng=None)
Generate data from a normal distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mean |
float
|
mean of the normal distribution |
0
|
sd |
float
|
standard deviation of the normal distribution |
1
|
precision |
int
|
decimal precision of the numbers generated from the normal distribution |
6
|
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
Generator
|
function returning list with numbers drawn from a normal distribution formatted as strings |
Source code in gecko/generator.py
from_uniform_distribution(low=0, high=1, precision=6, rng=None)
Generate data from a uniform distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
low |
Union[int, float]
|
lower limit of uniform distribution (inclusive) |
0
|
high |
Union[int, float]
|
upper limit of uniform distribution (exclusive) |
1
|
precision |
int
|
decimal precision of the numbers generated from the uniform distribution |
6
|
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
Generator
|
function returning list with numbers drawn from a uniform distribution formatted as strings |
Source code in gecko/generator.py
to_data_frame(column_to_generator_dict, count)
Generate data frame by using multiple generators at once. Column names must be mapped to their respective generators. A generator can be assigned to one or multiple column names, but it must always match the amount of series that the generator returns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column_to_generator_dict |
dict[Union[str, tuple[str, ...]], Generator]
|
mapping of column names to generators |
required |
count |
int
|
amount of records to generate |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
data frame with columns and rows generated as specified |
Source code in gecko/generator.py
Mutator
The mutator module provides mutator functions for mutating data. These mutators implement common error sources such as typos based on keymaps, random edit errors and more.
mutate_data_frame(df_in, column_to_mutator_dict, rng=None)
Mutate a data frame by applying several mutators on select columns. This function takes a dictionary which has column names as keys and mutators as values. A column may be assigned a single mutator, a mutator with a probability, a list of mutators where each is applied with the same probability, and a list of weighted mutators where each is applied with its assigned probability.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df_in |
DataFrame
|
data frame to mutate |
required |
column_to_mutator_dict |
dict[Union[str, tuple[str, ...]], Union[Mutator, tuple[float, Mutator], list[Mutator], list[tuple[float, Mutator]]]]
|
mapping of column names to mutators |
required |
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
DataFrame
|
data frame with columns mutated as specified |
Source code in gecko/mutator.py
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 |
|
with_categorical_values(csv_file_path, value_column=0, encoding='utf-8', delimiter=',', rng=None)
Mutate data by replacing it with another from a list of categorical values. This mutator reads all unique values from a column within a CSV file. All strings within a series will be replaced with a different random value from this column. If the value column is provided as a string, then it is automatically assumed that the CSV file has a header row.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csv_file_path |
Union[PathLike, str]
|
path to CSV file |
required |
value_column |
Union[str, int]
|
name or index of value column |
0
|
encoding |
str
|
character encoding of the CSV file |
'utf-8'
|
delimiter |
str
|
column delimiter of the CSV file |
','
|
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
Mutator
|
function returning list with strings mutated by replacing values with a different one from a limited set of permitted values |
Source code in gecko/mutator.py
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 |
|
with_cldr_keymap_file(cldr_path, charset=None, rng=None)
Mutate data by randomly introducing typos. Potential typos are sourced from a Common Locale Data Repository (CLDR) keymap. Any character may be replaced with one of its horizontal or vertical neighbors on a keyboard. They may also be replaced with its upper- or lowercase variant. It is possible for a string to not be modified if a selected character has no possible replacements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cldr_path |
Union[PathLike, str]
|
path to CLDR keymap file |
required |
charset |
Optional[str]
|
string with characters that may be mutated |
None
|
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
Mutator
|
function returning list with strings mutated by applying typos according to keymap file |
Source code in gecko/mutator.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
|
with_delete(rng=None)
Mutate data by randomly deleting characters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
Mutator
|
function returning list with strings mutated by deleting random characters |
Source code in gecko/mutator.py
with_edit(p_insert=0.25, p_delete=0.25, p_substitute=0.25, p_transpose=0.25, charset=string.ascii_letters, rng=None)
Mutate data by randomly applying insertion, deletion, substitution or transposition of characters. This mutator works as a wrapper around the respective mutators for the mentioned individual operations. The charset of allowed characters is passed on to the insertion and substitution mutators. Each mutator receives its own isolated RNG which is derived from the RNG passed into this function. The probabilities of each mutator must sum up to 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p_insert |
float
|
probability of random character insertion on a string |
0.25
|
p_delete |
float
|
probability of random character deletion on a string |
0.25
|
p_substitute |
float
|
probability of random character substitution on a string |
0.25
|
p_transpose |
float
|
probability of random character transposition on a string |
0.25
|
charset |
str
|
string to sample random characters from for insertion and substitution |
ascii_letters
|
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
Mutator
|
function returning list with strings mutated by random edit operations |
Source code in gecko/mutator.py
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 |
|
with_function(func, *args, **kwargs)
Mutate data using an arbitrary function that mutates a single value at a time.
Notes
This function should be used sparingly since it is not vectorized. Only use it for testing purposes or if performance is not important.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[Concatenate[str, P], str]
|
function to invoke to mutate data with |
required |
*args |
object
|
positional arguments to pass to |
()
|
**kwargs |
object
|
keyword arguments to pass to |
{}
|
Returns:
Type | Description |
---|---|
Mutator
|
function returning list with strings mutated using custom function |
Source code in gecko/mutator.py
with_insert(charset=string.ascii_letters, rng=None)
Mutate data by inserting random characters. The characters are drawn from the provided charset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
charset |
str
|
string to sample random characters from |
ascii_letters
|
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
Mutator
|
function returning list with strings mutated by inserting random characters |
Source code in gecko/mutator.py
with_missing_value(value='', strategy='blank')
Mutate data by replacing select entries with a representative "missing" value.
Strings are selected for replacement depending on the chosen strategy.
If all
, then all strings in the series will be replaced with the missing value.
If blank
, then all strings that are either empty or consist of whitespace characters only will be replaced with
the missing value.
If empty
, then all strings that are empty will be replaced with the missing value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
str
|
"missing" value to replace select entries with |
''
|
strategy |
Literal['all', 'blank', 'empty']
|
|
'blank'
|
Returns:
Type | Description |
---|---|
Mutator
|
function returning list where select strings will be replaced with the "missing" value |
Source code in gecko/mutator.py
with_noop()
Mutate data by not mutating it at all. This mutator returns the input series as-is. You might use it to leave a certain percentage of records in a series untouched.
Returns:
Type | Description |
---|---|
Mutator
|
function returning list of strings as-is |
Source code in gecko/mutator.py
with_permute(rng=None)
Mutate data from series by permuting their contents. This function ensures that for each row there is at least one permutation happening.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
Mutator
|
function returning list with the entries in each series swapped |
Source code in gecko/mutator.py
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 |
|
with_phonetic_replacement_table(csv_file_path, source_column=0, target_column=1, flags_column=2, encoding='utf-8', delimiter=',', rng=None)
Mutate data by randomly replacing characters with others that sound similar.
The rules for similar-sounding character sequences are sourced from a CSV file.
This table must have at least three columns: a source, target and a flag column.
A source pattern is mapped to its target under the rules imposed by the provided flags.
These flags determine where such a replacement can take place within a string.
If no flags are defined, it is implied that this replacement can take place anywhere in a string.
Conversely, if ^
, $
, _
, or any combination of the three are set, it implies that a replacement
can only occur at the start, end or in the middle of a string.
If the source, target and flags column are provided as strings, then it is automatically assumed that the CSV file
has a header row.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csv_file_path |
Union[PathLike, str]
|
path to CSV file containing phonetic replacement rules |
required |
source_column |
Union[int, str]
|
name or index of source column |
0
|
target_column |
Union[int, str]
|
name or index of target column |
1
|
flags_column |
Union[int, str]
|
name or index of flag column |
2
|
encoding |
str
|
character encoding of the CSV file |
'utf-8'
|
delimiter |
str
|
column delimiter of the CSV file |
','
|
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
Mutator
|
function returning list with strings mutated by applying phonetic errors according to rules in CSV file |
Source code in gecko/mutator.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
|
with_replacement_table(csv_file_path, source_column=0, target_column=1, encoding='utf-8', delimiter=',', rng=None)
Mutate data by randomly substituting sequences from a replacement table. The table must have at least two columns: a source and a target value column. A source value may have multiple target values that it can map to. Strings that do not contain any possible source values are not mutated. It is possible for a string to not be modified if no target value could be picked for its assigned source value. This can only happen if a source value is mapped to multiple target values. In this case, each target value will be independently selected or not. If the source and target column are provided as strings, then it is automatically assumed that the CSV file has a header row.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csv_file_path |
Union[PathLike, str]
|
path to CSV file |
required |
source_column |
Union[str, int]
|
name or index of the source column |
0
|
target_column |
Union[str, int]
|
name or index of the target column |
1
|
encoding |
str
|
character encoding of the CSV file |
'utf-8'
|
delimiter |
str
|
column delimiter of the CSV file |
','
|
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
Mutator
|
function returning list with strings mutated by replacing characters as specified in CSV file |
Source code in gecko/mutator.py
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
|
with_substitute(charset=string.ascii_letters, rng=None)
Mutate data by replacing single characters with a new one. The characters are drawn from the provided charset.
Notes
It is possible for a character to be replaced by itself.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
charset |
str
|
string to sample random characters from |
ascii_letters
|
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
Mutator
|
function returning list with strings mutated by substituting random characters |
Source code in gecko/mutator.py
with_transpose(rng=None)
Mutate data by randomly swapping neighboring characters.
Notes
It is possible for the same two neighboring characters to be swapped.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rng |
Optional[Generator]
|
random number generator to use |
None
|
Returns:
Type | Description |
---|---|
Mutator
|
function returning list with strings mutated by transposing random neighboring characters |