|
8 | 8 | from __future__ import absolute_import, division, print_function |
9 | 9 |
|
10 | 10 | from wholecell.utils._trna_charging import ( |
| 11 | + seed_rng, |
11 | 12 | get_initiations, |
12 | 13 | get_codon_at, |
13 | 14 | get_candidates_to_C, |
@@ -35,7 +36,7 @@ def tearDownClass(cls): |
35 | 36 | pass |
36 | 37 |
|
37 | 38 | def setUp(self): |
38 | | - pass |
| 39 | + seed_rng(0) |
39 | 40 |
|
40 | 41 | def tearDown(self): |
41 | 42 | pass |
@@ -489,5 +490,91 @@ def test_get_codons_read(self): |
489 | 490 | assert_equal(n_codons_read, np.array([3, 3], dtype=np.int64)) |
490 | 491 |
|
491 | 492 |
|
| 493 | +def test_reconcile_different_seeds_different_results(): |
| 494 | + """ |
| 495 | + Tests that reconcile_via_ribosome_positions: |
| 496 | + - Produces DIFFERENT outputs given the same inputs but DIFFERENT random seeds |
| 497 | + - Produces SAME outputs given the same inputs and SAME random seeds |
| 498 | + """ |
| 499 | + # --- 1. Define two different seeds --- |
| 500 | + seed1 = 12345 |
| 501 | + seed2 = 54321 |
| 502 | + |
| 503 | + # --- 2. Define fixed, non-trivial inputs --- |
| 504 | + # (Using the same inputs as the previous 20-codon example) |
| 505 | + initial_sequence_codons = np.array( |
| 506 | + [5, 2, 8, 3, 1, 4, 6, 5, 9, 3, 7, 3, 5, 1, 8, 2, 4, 6, 2, 10], dtype=np.int64 |
| 507 | + ) |
| 508 | + kinetics_codons = np.array( |
| 509 | + [6, 1, 7, 4, 2, 3, 5, 3, 8, 2, 6, 4, 6, 0, 9, 1, 5, 5, 1, 9], dtype=np.int64 |
| 510 | + ) |
| 511 | + initial_elongations = np.array([8, 12, 5, 10, 7], dtype=np.int64) |
| 512 | + sequences = np.array( |
| 513 | + [ |
| 514 | + [0, 1, 2, 3, 4, 5, 6, 7, -1, -1, -1, -1, -1, -1, -1], |
| 515 | + [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, -1, -1, -1], |
| 516 | + [1, 3, 5, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], |
| 517 | + [2, 4, 6, 8, 10, 12, 14, 16, 18, 0, -1, -1, -1, -1, -1], |
| 518 | + [19, 17, 15, 13, 11, 9, 7, -1, -1, -1, -1, -1, -1, -1, -1], |
| 519 | + ], |
| 520 | + dtype=np.int8, |
| 521 | + ) |
| 522 | + max_attempts = np.int8(50) |
| 523 | + |
| 524 | + # --- 3. Run 1 with seed1 --- |
| 525 | + sequence_codons_run1 = np.copy(initial_sequence_codons) |
| 526 | + elongations_run1 = np.copy(initial_elongations) |
| 527 | + seed_rng(seed1) # Seed with first seed |
| 528 | + reconcile_via_ribosome_positions( |
| 529 | + sequence_codons_run1, |
| 530 | + elongations_run1, |
| 531 | + kinetics_codons, |
| 532 | + sequences, |
| 533 | + max_attempts, |
| 534 | + ) |
| 535 | + |
| 536 | + # --- 4. Run 2 with seed2 --- |
| 537 | + sequence_codons_run2 = np.copy(initial_sequence_codons) |
| 538 | + elongations_run2 = np.copy(initial_elongations) |
| 539 | + seed_rng(seed2) # Seed with second, different seed |
| 540 | + reconcile_via_ribosome_positions( |
| 541 | + sequence_codons_run2, |
| 542 | + elongations_run2, |
| 543 | + kinetics_codons, |
| 544 | + sequences, |
| 545 | + max_attempts, |
| 546 | + ) |
| 547 | + |
| 548 | + # --- 5. Run 3 with seed1 --- |
| 549 | + sequence_codons_run3 = np.copy(initial_sequence_codons) |
| 550 | + elongations_run3 = np.copy(initial_elongations) |
| 551 | + seed_rng(seed1) # Seed with first seed |
| 552 | + reconcile_via_ribosome_positions( |
| 553 | + sequence_codons_run3, |
| 554 | + elongations_run3, |
| 555 | + kinetics_codons, |
| 556 | + sequences, |
| 557 | + max_attempts, |
| 558 | + ) |
| 559 | + |
| 560 | + # --- 6. Assert inequality --- |
| 561 | + # Check that the results are different for different seed. |
| 562 | + sequence_codons_differ = np.any(sequence_codons_run1 != sequence_codons_run2) |
| 563 | + elongations_differ = np.any(elongations_run1 != elongations_run2) |
| 564 | + |
| 565 | + # --- 7. Assert equality --- |
| 566 | + # Check that the results are same for same seed. |
| 567 | + sequence_codons_same = np.all(sequence_codons_run1 == sequence_codons_run3) |
| 568 | + elongations_same = np.all(elongations_run1 == elongations_run3) |
| 569 | + |
| 570 | + assert sequence_codons_differ or elongations_differ, ( |
| 571 | + "Outputs were unexpectedly identical for different seeds." |
| 572 | + ) |
| 573 | + |
| 574 | + assert sequence_codons_same or elongations_same, ( |
| 575 | + "Outputs were unexpectedly different for same seeds." |
| 576 | + ) |
| 577 | + |
| 578 | + |
492 | 579 | if __name__ == "__main__": |
493 | 580 | unittest.main() |
0 commit comments