Ising Boolean Formalism
Functions related to Ising simulation.
GRN Parsing and Inital Condition Generation Functions
grins.ising_bool.parse_topo_to_matrix(topofile_path)
Parses a topology file into an adjacency matrix.
This function reads a topology file, and converts it into an adjacency matrix. The adjacency matrix is then converted to a JAX array.
Parameters:
-
topofile_path
(str
) –The path to the topology file. The file should be in a format readable by pandas
read_csv
with whitespace as the delimiter.
Returns:
-
tuple
–A tuple containing: topo_adj : jax.numpy.ndarray The adjacency matrix as a JAX array. node_names : list A list of node names in the order they appear in the adjacency matrix.
Source code in grins/ising_bool.py
grins.ising_bool.generate_intial_conditions(num_nodes, num_samples)
Generate initial conditions for a given number of nodes and samples.
This function generates initial conditions using Sobol sequences and scales the generated samples to [0, 1].
Parameters:
-
num_nodes
(int
) –The number of nodes for which to generate initial conditions.
-
num_samples
(int
) –The number of samples to generate.
Returns:
-
ndarray
–The generated initial conditions as a JAX array of integers.
Source code in grins/ising_bool.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 |
|
Ising Boolean Simulation Functions
grins.ising_bool.sync_eval_next_state(prev_state, topo_adj, replacement_values)
Evaluate the next state of a system synchronously based on the previous state, topology adjacency matrix, and replacement values.
Parameters:
-
prev_state
(ndarray
) –The previous state of the system.
-
topo_adj
(ndarray
) –The topology adjacency matrix representing the connections between nodes in the system.
-
replacement_values
(ndarray
) –A vector of two values used for replacement based on the computed state conditions. The values are: [value_if_negative, value_if_positive]. Value of 0 is not included as it is assumed that the state will remain the same if the node evaluates to 0 in that step. prev_state (jnp.ndarray): The previous state of the system.
Returns:
-
ndarray
–The new state of the system as an array of int16.
Source code in grins/ising_bool.py
grins.ising_bool.simulate_sync_trajectory(initial_condition, topo_adj, replacement_values, max_steps)
Simulates a synchronous trajectory of a system based on the given initial condition, topology adjacency matrix, and replacement values.
Parameters:
-
initial_condition
(ndarray
) –The initial state of the system.
-
topo_adj
(ndarray
) –The topology adjacency matrix representing the connections between nodes in the system.
-
replacement_values
(ndarray
) –The values used to replace the states during the simulation. It is a vector of two values in the form [value_if_negative, value_if_positive].
-
max_steps
(arange
) –The range of steps to simulate. The simulation will run for each step in the range.
Returns:
-
ndarray
–A JAX array containing the states of the system at each step, with the initial condition included at the beginning. The array also includes a column for the step indices. All -1 values in the states are replaced with 0 if the replacement values are [-1, 1].
Source code in grins/ising_bool.py
grins.ising_bool.async_eval_next_state(prev_state, topo_adj, replacement_values, update_index)
Asynchronously evaluates the next state of a node in an Ising model.
Parameters:
-
prev_state
(ndarray
) –The previous state vector of the system.
-
topo_adj
(ndarray
) –The adjacency matrix representing the topology of the system.
-
replacement_values
(ndarray
) –A vector of two values used for state replacement based on conditions. The values of the vector should be [value_if_negative, value_if_positive]. The value for 0 is not included as it is assumed that the state will remain the same if the node evaluates to 0 in that step.
-
update_index
(int
) –The index of the node to update.
Returns:
-
ndarray
–The new state vector after updating the specified node.
Source code in grins/ising_bool.py
grins.ising_bool.simulate_async_trajectory(initial_condition, topo_adj, replacement_values, update_indices)
Simulates an asynchronous trajectory of a system given an initial condition and update indices.
Parameters:
-
initial_condition
(ndarray
) –The initial condition of the system.
-
topo_adj
(ndarray
) –The adjacency matrix representing the topology of the system.
-
replacement_values
(ndarray
) –The values used to replace the states during the simulation. It is a vector of two values in the form [value_if_negative, value_if_positive].
-
update_indices
(ndarray
) –A vector of indices specifying which node to update at each step. The length of the vector should be equal to the number of steps. If not, the simulation will only run until the length of the update_indices.
Returns:
-
ndarray
–A JAX array containing the states of the system at each step, with the initial condition included at the beginning. The array also includes a column for the step indices. All -1 values in the states are replaced with 0 if the replacement values are [-1, 1].
Source code in grins/ising_bool.py
grins.ising_bool.run_ising(topo_file, num_initial_conditions=None, inital_conditions=None, max_steps=None, batch_size=None, replacement_values=jnp.array([-1, 1]), mode='sync', packbits=False)
Run synchronous or asynchronous simulations for a given topology.
Parameters:
-
topo_file
(str
) –The path to the topology file.
-
num_initial_conditions
(int
, default:None
) –The number of initial conditions to sample. If not provided, the default is 2**10.
-
inital_conditions
(ndarray
, default:None
) –The initial conditions matrix with the individual initial conditions as rows of the matrix. If provided, num_initial_conditions is ignored.
-
max_steps
(int
, default:None
) –The maximum number of steps to simulate. If not provided, it is calculated to be 10 times the number of nodes.
-
batch_size
(int
, default:None
) –The number of samples per batch. If not provided, the default is 2**10.
-
replacement_values
(ndarray
, default:array([-1, 1])
) –These values are used for replacement after each evaluation in the simulation. The first value specifies what all elements less than 0 in the evaluated state will be converted to, and the second value specifies the replacement for all elements greater than 0. The default replacement values are [-1, 1]. When saving to file, all -1 and 0 values are converted to 0, and 1 remains 1. This ensures compatibility with packbits.
-
mode
(str
, default:'sync'
) –The simulation mode, either "sync" or "async". The default is "sync".
-
packbits
(bool
, default:False
) –Whether to pack the 0/1 states into bits to reduce memory usage. The default is False.
Returns:
-
DataFrame
–Simulation results are returned as a pandas DataFrame. The dataframe as a
Step
column indicating the simulation step. Other columns represent node values at each step. Ifpackbits=True
, node names are concatenated with"|"
in the column names. During unpacking, values can be assigned to individual nodes in the same order.
Example
Run the synchronous simulation for a topology file:
>>> run_ising(
... topo_file="TOPOS/TS.topo",
... num_initial_conditions=2**10,
... max_steps=100,
... batch_size=2**10,
... replacement_values=jnp.array([0, 1]),
... mode="sync",
... packbits=True,
... )
Similary, the asynchronous simulation can be run by setting mode="async".
If the initial conditions matrix is provided, the num_initial_conditions parameter is ignored. In this case, the initial_conditions matrix should have the individual initial conditions as rows. If only specfic inital conditions are to be used, the initial conditions matrix can be provided with the individual initial conditions as rows of the matrix. This provides control over simulating specific pre-defined initial conditions.
>>> initial_conditions = jnp.array([[0, 1], [1, 0], [0, 0]])
>>> run_ising(
... topo_file="TOPOS/TS.topo",
... initial_conditions=initial_conditions,
... max_steps=100,
... batch_size=2**10,
... replacement_values=jnp.array([0, 1]),
... mode="sync",
... packbits=True,
... )
For cases where the replacement values are not [-1, 1], the replacement values should be provided as a jax array of length 2 with the first value less than the second.
>>> replacement_values = jnp.array([0, 1]) # Replacement values are 0 for negetive and 1 for positive
>>> run_ising(
... topo_file="TOPOS/TS.topo",
... num_initial_conditions=2**10,
... max_steps=100,
... batch_size=2**10,
... replacement_values=replacement_values,
... mode="sync",
... packbits=True,
... )
The results for [-1, 1] replacment values will also be converted to 0 for all the -1 or 0 values in the states and 1s will remain as 1s when saving to the file. This is important as otherwise the packbits would not work.
The packbits function used is jnp.packbits which packs the 0/1 states into bits to reduce memory usage. This is useful when the number of nodes is large and the number of steps is also large. The memory usase can be reduced by a factor of 8 by packing the states into bits. If packbits is not set to True, the states are saved as is.
>>> run_ising(
... topo_file="TOPOS/TS.topo",
... num_initial_conditions=2**10,
... max_steps=100,
... batch_size=2**10,
... replacement_values=jnp.array([0, 1]),
... mode="sync",
... packbits=False,
... )
The final dataframe which is written to the parquet file has the following columns for the packbits=False case:
- Step: The step number for the simulation.
- Node names: The names of the nodes in the network.
If the packbits=True, the final dataframe has the following columns:
- Step: The step number for the simulation.
- Byte_i: The ith byte of the packed states for the simulation. The actual column name is the node names conactenated by the string
|
in order.
In both these cases a step value of 0 in the dataframes will signify the initial condition of the simulation and the subsequent steps will be the states of the network at each step until the max_steps value is reached after which a new initial condition will be present (with step value 0).
The Byte_i columns are created based on the number of nodes in the network. For example, if there are 100 nodes, there will be 13 columns for the packed states. They can be unpacked using jnp.unpackbits to get the unpacked state values. And the unpacked column can be named by splitting the corresponding column name by the string |
.
Source code in grins/ising_bool.py
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 |
|
grins.ising_bool.run_all_replicates_ising(topo_file, num_initial_conditions=None, inital_conditions=None, max_steps=None, batch_size=None, save_dir='IsingSimulResults', num_replicates=3, replacement_values=jnp.array([-1, 1]), mode='sync', packbits=False)
Run multiple replicate of ising model simulations for a given topology and save results.
Parameters:
-
topo_file
(str
) –Path to the topology file.
-
num_initial_conditions
(int
, default:None
) –Number of initial conditions to sample. Defaults to 2**10 if not provided.
-
inital_conditions
(ndarray
, default:None
) –Initial condition matrix (rows = individual states). Overrides
num_initial_conditions
if given. -
max_steps
(int
, default:None
) –Maximum number of steps to simulate. Defaults to 10 x number of nodes.
-
batch_size
(int
, default:None
) –Number of samples per batch. Defaults to 2**10.
-
save_dir
(str
, default:'IsingSimulResults'
) –Directory to store simulation results. Defaults to "IsingSimulResults".
-
replacement_values
(ndarray
, default:array([-1, 1])
) –Two values used after evaluation: the first replaces elements < 0, the second replaces elements > 0. Defaults to [-1, 1]. When saving, all -1 and 0 values are mapped to 0, and 1 stays 1—for compatibility with
packbits
. -
mode
(str
, default:'sync'
) –The simulation mode, either "sync" or "async". The default is "sync".
-
packbits
(bool
, default:False
) –Whether to pack the 0/1 states into bits to reduce memory usage. The default is False.
Returns:
-
None
–
Example
Run the synchronous simulation for a topology file in three replicates for both the modes:
>>> run_all_replicates_ising("TOPOS/TS.topo", num_initial_conditions=1000, num_replicates=3, save_dir="IsingResults", mode="sync")
>>> run_all_replicates_ising("TOPOS/TS.topo", num_initial_conditions=1000, num_replicates=3, save_dir="IsingResults", mode="async")
This creates a directory called "IsingResults" which has the following directory strucuture: IsingResults └── 11 ├── 1 │ ├── 11_async_ising_results.parquet │ └── 11_sync_ising_results.parquet ├── 2 │ ├── 11_async_ising_results.parquet │ └── 11_sync_ising_results.parquet └── 3 ├── 11_async_ising_results.parquet └── 11_sync_ising_results.parquet
Source code in grins/ising_bool.py
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 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 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 |
|