missing_value
knime2py.nodes.missing_value
Missing Value Handler.
Overview
This module generates Python code to handle missing values in a DataFrame based on KNIME's Missing Value policies. It fits into the knime2py generator pipeline by producing code that applies specified fill strategies to input tables and writes the results to the node's context.
Runtime Behavior
Inputs: - Reads a DataFrame from the context using the key format 'src_id:in_port'.
Outputs: - Writes the processed DataFrame back to the context with the key format 'node_id:out_port', where out_port defaults to '1'.
Key algorithms or mappings: - Implements fill strategies such as mean, median, mode, forward fill, backward fill, and fixed value fills based on the configuration parsed from settings.xml.
Edge Cases
- Handles empty or constant columns by skipping them.
- Safeguards against NaN values and class imbalance by providing fallback strategies.
Generated Code Dependencies
The generated code requires the following external libraries: - pandas
These dependencies are required by the generated code, not by this module.
Usage
Typically invoked by upstream KNIME nodes that require missing value handling. Example context access:
df = context['input_table:1']
Node Identity
KNIME factory id: - FACTORY = "org.knime.base.node.preproc.pmml.missingval.compute.MissingValueHandlerNodeFactory"
Configuration
Settings are defined in the MissingValueSettings dataclass, which includes:
- by_dtype: List of TypePolicy instances defining fill strategies per data type.
The parse_missing_value_settings function extracts these values from the
settings.xml file using XPath queries.
Limitations
Currently, this module does not support all KNIME fill strategies and may approximate behavior in some cases.
References
For more information, refer to the KNIME documentation and the following URL: https://hub.knime.com/knime/extensions/org.knime.features.base/latest/ org.knime.base.node.preproc.pmml.missingval.compute.MissingValueHandlerNodeFactory
first(root, xpath)
Return the first string value for xpath, stripped, or None.
If the xpath returns an element, prefer its @value, else its .text. If it returns a scalar (string/number), cast to str and strip.
Source code in src/knime2py/nodes/node_utils.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
first_el(root, xpath)
Return the first Element for xpath, or None (ignores non-Elements).
Source code in src/knime2py/nodes/node_utils.py
102 103 104 105 106 107 108 | |
all_values(root, xpath)
Return all values for xpath as stripped strings.
Source code in src/knime2py/nodes/node_utils.py
110 111 112 | |
iter_entries(root)
Yield (key, value) pairs for all KNIME
Source code in src/knime2py/nodes/node_utils.py
120 121 122 123 124 125 | |
strip_rule_quotes(s)
Remove surrounding quotes from a KNIME rule token when present.
Source code in src/knime2py/nodes/node_utils.py
151 152 153 | |
parse_knime_rule(line)
Parse the simple KNIME Rule Engine subset shared by rule nodes.
Source code in src/knime2py/nodes/node_utils.py
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 | |
parse_knime_rules_from_config(rules_cfg)
Parse numbered entries from a KNIME
Source code in src/knime2py/nodes/node_utils.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
rule_literal_py(val)
Convert a KNIME rule literal token to generated Python source.
Source code in src/knime2py/nodes/node_utils.py
201 202 203 204 205 206 207 208 | |
rule_wildcard_to_regex(pat)
Convert KNIME Rule Engine LIKE wildcard syntax to a regex.
Source code in src/knime2py/nodes/node_utils.py
211 212 213 214 | |
normalize_delim(raw)
Normalize delimiter strings to their corresponding character representation.
Source code in src/knime2py/nodes/node_utils.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
normalize_char(raw)
Normalize character strings to their corresponding single character representation.
Source code in src/knime2py/nodes/node_utils.py
268 269 270 271 272 273 274 275 276 277 278 279 | |
looks_like_path(s)
Check if the given string looks like a file path.
Source code in src/knime2py/nodes/node_utils.py
281 282 283 284 285 286 287 288 289 290 291 292 | |
bool_from_value(v)
Convert a string value to a boolean.
Source code in src/knime2py/nodes/node_utils.py
294 295 296 297 298 299 300 301 302 303 | |
normalize_in_ports(in_ports)
Accepts items like ('1393','1') or '1393:1' (or just '1393') and returns a normalized list of (src_id, port) as strings.
Source code in src/knime2py/nodes/node_utils.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
context_assignment_lines(node_id, out_ports)
For reader-like nodes that produce a dataframe named df,
publish it under context keys '
Source code in src/knime2py/nodes/node_utils.py
330 331 332 333 334 335 336 | |
extract_csv_path(root)
Prefer keys that sound like file paths; fall back to any entry value that looks like a path. Avoid false-positives like node_file='settings.xml' via looks_like_path().
Source code in src/knime2py/nodes/node_utils.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
extract_csv_sep(root)
Extract the CSV separator from the XML configuration.
Source code in src/knime2py/nodes/node_utils.py
358 359 360 361 | |
extract_csv_quotechar(root)
Extract the quote character used in the CSV configuration.
Source code in src/knime2py/nodes/node_utils.py
363 364 365 366 367 368 369 370 371 372 | |
extract_csv_escapechar(root)
Extract the escape character used in the CSV configuration.
Source code in src/knime2py/nodes/node_utils.py
374 375 376 377 | |
extract_csv_encoding(root)
Extract the character encoding from the CSV configuration.
Source code in src/knime2py/nodes/node_utils.py
379 380 381 382 383 384 385 | |
extract_csv_header_reader(root)
Reader: look for 'column header', 'hasheader', or plain 'header', but avoid writer-only keys like 'write_header'.
Source code in src/knime2py/nodes/node_utils.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 | |
extract_csv_header_writer(root)
Writer: prefer explicit 'writeColumnHeader'/'write_header'; otherwise any key whose name contains both 'write' and 'header'.
Source code in src/knime2py/nodes/node_utils.py
402 403 404 405 406 407 408 409 410 411 412 | |
extract_csv_na_rep(root)
Writer NA representation
- modern: key='missing_value_pattern' (may be empty string '')
- older: key contains both 'missing' and 'representation'
Keep empty string '' as a real value; return None only if not set.
Source code in src/knime2py/nodes/node_utils.py
414 415 416 417 418 419 420 421 422 423 424 | |
extract_csv_include_index(root)
Extract whether to include the index in the CSV output.
Source code in src/knime2py/nodes/node_utils.py
426 427 428 429 | |
extract_table_spec_types(root)
Return {column_name: java_class} from table_spec_config_Internals.
Looks under .../individual_specs/*/
Source code in src/knime2py/nodes/node_utils.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | |
java_to_pandas_dtype(java_class)
Map KNIME java types to pandas nullable dtypes.
Source code in src/knime2py/nodes/node_utils.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | |
collect_module_imports(mod_or_func)
Return a sorted list of unique import lines from either
- a module object that defines generate_imports()
- a callable (e.g. the generate_imports function itself)
Source code in src/knime2py/nodes/node_utils.py
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 | |
split_out_imports(lines)
Return (found_imports, body_without_imports). Any line that begins with 'import ' or 'from ' (ignoring leading spaces) is treated as an import.
Source code in src/knime2py/nodes/node_utils.py
518 519 520 521 522 523 524 525 526 527 528 529 530 531 | |
resolve_reader_path(root, node_dir)
Resolve the path from settings.xml. Supports: - LOCAL: absolute path is used as-is - RELATIVE + knime.workflow: path is relative to the workflow directory
Source code in src/knime2py/nodes/node_utils.py
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 | |
parse_missing_value_settings(node_dir)
Parse the missing value settings from the settings.xml file.
Source code in src/knime2py/nodes/missing_value.py
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 | |
generate_imports()
Generate the necessary import statements for the output code.
Source code in src/knime2py/nodes/missing_value.py
239 240 241 | |
generate_py_body(node_id, node_dir, in_ports, out_ports=None)
Generate the Python code body for the node based on its configuration and input ports.
Source code in src/knime2py/nodes/missing_value.py
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 463 464 465 466 467 468 469 470 471 472 473 474 475 | |
get_name()
Return name of the node in KNIME workflow.
Source code in src/knime2py/nodes/missing_value.py
479 480 481 | |
handle(ntype, nid, npath, incoming, outgoing)
Handle the node processing, generating imports and body code.
Source code in src/knime2py/nodes/missing_value.py
484 485 486 487 488 489 490 491 492 | |