greta-dev / greta

simple and scalable statistical modelling in R
https://greta-stats.org
Other
524 stars 63 forks source link

in replacement of greta array values fix replacement value being overwritten early #552

Open njtierney opened 1 year ago

njtierney commented 1 year ago

First noticed in check_expr in test_extract_replace_combine.R

  x <- randn(10)
  check_expr({
    x[1:6] <- seq_len(6)
    x
  })

We get the following node list,

$node_8fb0f131
<operation_node>
  Inherits from: <node>
  Public:
    .value: 1 2 3 4 5 6 0.985587069161365 -1.34378507400201 0.281578 ...
    add_argument: function (argument) 
    add_child: function (node) 
    add_parent: function (node) 
    arguments: list
    child_names: function () 
    children: list
    clone: function (deep = FALSE) 
    define_tf: function (dag) 
    defined: function (dag) 
    description: function () 
    dim: 10 1
    distribution: NULL
    get_unique_name: function () 
    initialize: function (operation, ..., dim = NULL, operation_args = list(), 
    list_children: function (dag) 
    list_parents: function (dag) 
    operation: tf_replace
    operation_args: list
    operation_name: replace
    parent_names: function (recursive = FALSE) 
    parents: list
    plotting_label: function () 
    register: function (dag) 
    register_family: function (dag) 
    remove_child: function (node) 
    remove_parent: function (node) 
    representations: list
    set_distribution: function (distribution) 
    tf: function (dag) 
    tf_function_env: environment
    unique_name: node_8fb0f131
    value: function (new_value = NULL, ...) 

$node_54966a45
<data_node>
  Inherits from: <node>
  Public:
    .value: 1 2 3 4 5 6 0.985587069161365 -1.34378507400201 0.281578 ...
    add_child: function (node) 
    add_parent: function (node) 
    child_names: function () 
    children: list
    clone: function (deep = FALSE) 
    define_tf: function (dag) 
    defined: function (dag) 
    description: function () 
    dim: 10 1
    distribution: NULL
    get_unique_name: function () 
    initialize: function (data) 
    list_children: function (dag) 
    list_parents: function (dag) 
    parent_names: function (recursive = FALSE) 
    parents: list
    plotting_label: function () 
    register: function (dag) 
    register_family: function (dag) 
    remove_child: function (node) 
    remove_parent: function (node) 
    representations: list
    set_distribution: function (distribution) 
    tf: function (dag) 
    unique_name: node_54966a45
    value: function (new_value = NULL, ...) 

$node_35f20ab3
<data_node>
  Inherits from: <node>
  Public:
    .value: 1 2 3 4 5 6
    add_child: function (node) 
    add_parent: function (node) 
    child_names: function () 
    children: list
    clone: function (deep = FALSE) 
    define_tf: function (dag) 
    defined: function (dag) 
    description: function () 
    dim: 6 1
    distribution: NULL
    get_unique_name: function () 
    initialize: function (data) 
    list_children: function (dag) 
    list_parents: function (dag) 
    parent_names: function (recursive = FALSE) 
    parents: list
    plotting_label: function () 
    register: function (dag) 
    register_family: function (dag) 
    remove_child: function (node) 
    remove_parent: function (node) 
    representations: list
    set_distribution: function (distribution) 
    tf: function (dag) 
    unique_name: node_35f20ab3
    value: function (new_value = NULL, ...) 

The second nodes .value is already replaced, which doesn't seem to impact things at the moment but could be problematic down the track.

We can potentially fix this by having a print_value and .value slot to distinguish between these two?

NOTE: When investigating with this code - we can't seem to replicate this issue, so it could be something that only happens in testing

library(greta)

x <- as_data(runif(10))
x[1:6] <- 1:6
y <- normal(x, 1)
m <- model(y)
m$dag$node_list