API reference¶
The primary interface to detk is the command line, but the underlying Python
modules can be used directly. From 1.0 the command line, with what its
--help states (its sections, each argument's metavar, default and choices,
and its examples, though not its wording) and its exit codes, is the stable
surface that follows semantic versioning; the Python API below is usable but
not yet a compatibility promise (see Stability). The
core objects:
Common¶
de_toolkit.common.CountMatrix
¶
A counts matrix (features in rows, samples in columns) and, optionally, its sample information column_data (samples in rows) and a design.
The counts columns and the column data rows must name the same samples
(DETK-0600): a sample in only one of them raises
:class:SampleMismatchException naming it. The column data is matched
to the counts by name, so its rows may come in any order, unless
strict, which requires the same order too. subset keeps the samples
in both instead, in the counts' order; it is what detk-util tidy
does, the explicit way to subset the two.
Source code in de_toolkit/common.py
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 | |
de_toolkit.common.CountMatrixFile
¶
Bases: CountMatrix
A :class:CountMatrix read from the counts file count_f (and the column
data file column_data_f). With check "ids" or "raw", the
counts are run through :func:check_counts as read, before the column
data selects or reorders any sample: "ids" for the repeated sample
header and feature ID checks only, "raw" for all of them. The
default, None, checks nothing, as in 1.0.0.
Source code in de_toolkit/common.py
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 | |
de_toolkit.common.DetkModule
¶
Bases: OrderedDict
Base class for all detk methods.
Each function in detk is a subclass of this class.
A module has the following properties:
- name: name of the module
- params: the module-dependent params passed to the module
- output: the output of the module in tabular form
- properties: properties and statistics about the module, used for
formatting the results into a report
All of these properties are JSON serializable, i.e. basic python types (int, float, str, bool, array, dict).
Source code in de_toolkit/common.py
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 | |
json
property
¶
Format the module object into a form amenable to serializing with JSON for report creation. By default this returns all of the module-level fields except output. Override if additional serialization is needed.
name
property
¶
Name of this stats object, by default the class name in all lower case
output
property
¶
Override this method to return a tabular form of the output. Can be any rectangular-shaped object (i.e. list of lists, pandas.DataFrame, numpy array, etc)
params
property
¶
Override this method to return a dictionary with relevant parameters for the method.
properties
property
¶
Override this method to return a dictionary with relevant properties for the method. These might include metadata or summary statistics of the output that would not otherwise be included. Primarily used in the construction of report sections.
Reporting¶
de_toolkit.report.DetkReport
¶
Collects detk module JSON and renders a single self-contained, offline HTML
report. Each module run serializes its JSON into <report_dir>/json/ (the
machine-readable layer); write assembles every JSON found there into one
detk_report.html whose charts are rendered client-side by a vendored,
no-build Vega-Lite bundle, with the raw JSON embedded for provenance.
Source code in de_toolkit/report.py
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 463 464 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 | |
json
property
¶
Serialize pending modules, then load every module JSON in the report dir.
add_module(module, in_file_path=None, out_file_path=None, column_data_path=None, workdir=None, in_file_paths=None, out_file_paths=None)
¶
Add and serialize the given module to the report directory; in_file_paths and out_file_paths list every input and output file of a step with several (detk-util record)
Source code in de_toolkit/report.py
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 | |
build_modules()
¶
Turn each collected module JSON into a render descriptor for the SPA.
Source code in de_toolkit/report.py
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |
de_toolkit.report.DetkModuleJSON
¶
Source code in de_toolkit/report.py
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 | |
write(indent=None)
¶
Write out the module JSON to file.
Each JSON file has one top level object with the following properties:
- name: name of the module
- id: machine readable ID
- detk_version: version of detk that generated this file
- last_modified: local system timestamp in milliseconds when this
file was created/modified
- argv: the command line, argv[0] the command's name only and
an absolute path among the arguments relative to workdir
- workdir: the directory where detk was run, relative to the
report directory (.. for the default ./detk_report)
- in_file_path: path to the file that was processed
- out_file_path: path to the file that was output, if available
- column_data_path: path to the column data file used, if available
- in_file_paths, out_file_paths: every input and output file,
for a step with several (optional, schema 1.1)
Every file path is relative to workdir (schema 1.2, DETK-0280).
Source code in de_toolkit/report.py
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 | |
R bridge¶
de_toolkit.wrapr.WrapR
¶
Wrapper object for calling R code with Rscript.
.. note:: The attributes are only populated after the execute() method has been run
Parameters¶
rscript_path : str path to the R script to run
counts : pandas.DataFrame, optional dataframe containing counts to be passed to R
metadata : pandas.DataFrame, optional dataframe containing metadata to be passed to R
params : dict, optional dict of parameters to be passed to R
output_fn : str, optional path to file where R should write output, if not provided the output is written to a temporary file and deleted upon WrapR object deletion
metadata_out_fn : str, optional path to file where R should write metadata output
rpath : str path to the Rscript executable, taken from the PATH environment variable if None
raise_on_error : bool raise an exception if R encounters an error, other wise fail silently and deadly
Attributes¶
output : pandas.DataFrame
dataframe of the tabular output created by R script, indexed by its
first column read as text (see :func:de_toolkit.common.read_id_table),
so an ID such as 007 is kept as written
metadata_out : pandas.DataFrame dataframe of the tabular metadata output created by R script, indexed by its first column read as text
params_out : dict dict of the output parameters list created by R script
stdout : str string capturing the standard output of the R script
stderr : str string capturing the standard error of the R script
retcode : int return code of the R process
success : bool True if retcode == 0
Raises¶
de_toolkit.wrapr.RExecutionError when raise_on_error is True, raise whenever R encounters an error
Examples¶
Basic usage accepts a path to an R script and loads the content of the file pointed to by out.fn in the R script into the output attribute:
with open('script.R','wt') as f : # note reference to implicitly defined out.fn # R variable f.write('write.csv(c(1,2,3,4),out.fn)') r = WrapR('script.R',output_fn='test.csv') r.execute() r.output x 1 1 2 2 3 3 4 4 pandas.read_csv('test.csv',index_col=0) x 1 1 2 2 3 3 4 4
Can also use a context manager when the output doesn't need to be written to a named file:
with WrapR('script.R') as r : r.execute() print(r.output) x 1 1 2 2 3 3 4 4
The standard output of the R script can be accessed with the stdout attribute:
with open('euler.R','wt') as f : f.write('exp(complex(real=0,imag=pi))+1') with WrapR('euler.R','wt') as r : r.execute() print(r.stdout) [1] 0+1.224647e-16i
Source code in de_toolkit/wrapr.py
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 463 464 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 605 606 607 608 609 610 611 612 613 614 615 | |
execute()
¶
Execute the R script and load in the resulting output files, if any.
Source code in de_toolkit/wrapr.py
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 605 | |
de_toolkit.wrapr.wrapr(Rcode, **kwargs)
¶
Convenience wrapper for WrapR object. Writes Rcode to a temporary file and executes it as it would if it were provided.
Parameters¶
Rcode : str string containing valid R code to be executed
Returns¶
obj A WrapR object executed with the code in input string
Examples¶
with wrapr('write.csv(c(1,2,3,4),out.fn)') as r : print(r.output) x 1 1 2 2 3 3 4 4
Source code in de_toolkit/wrapr.py
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 | |
de_toolkit.wrapr.check_r()
¶
Tests whether the Rscript executable can be found.
Source code in de_toolkit/wrapr.py
162 163 164 | |
de_toolkit.wrapr.check_r_package(pkg)
¶
Tests whether the R package pkg is installed.
Source code in de_toolkit/wrapr.py
167 168 169 170 171 172 173 174 175 176 177 178 179 | |