Skip to content

Commit 7ec5b0a

Browse files
authored
Merge pull request #741 from njtierney/prune-unused-code-738
Remove unnecessary comments with #browser or #TF1/2
2 parents cfdc095 + 2bc423e commit 7ec5b0a

12 files changed

+11
-130
lines changed

R/calculate.R

-9
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,6 @@ calculate_greta_mcmc_list <- function(target,
390390
}
391391

392392
calculate_list <- function(target, values, nsim, tf_float, env) {
393-
### browser()
394393
fixed_greta_arrays <- list()
395394

396395
values_exist <- !identical(values, list())
@@ -409,7 +408,6 @@ calculate_list <- function(target, values, nsim, tf_float, env) {
409408
dag <- dag_class$new(all_greta_arrays, tf_float = tf_float)
410409

411410
stochastic <- !is.null(nsim)
412-
### browser()
413411
if (stochastic) {
414412

415413
check_if_unsampleable_and_unfixed(fixed_greta_arrays, dag)
@@ -459,7 +457,6 @@ calculate_target_tensor_list <- function(
459457
nsim
460458
) {
461459
# define the dag and TF graph
462-
### browser()
463460
# change dag mode to sampling
464461
dag$mode <- "all_sampling"
465462

@@ -498,18 +495,12 @@ calculate_target_tensor_list <- function(
498495
# approaches (in as_tf_function + generate_log_prob_function)
499496
dag$define_tf(target_nodes = target_nodes)
500497

501-
# browser()
502498
# look up the tf names of the target greta arrays (under sampling)
503499
# create an object in the environment that's a list of these, and sample that
504500
target_nodes <- lapply(target, get_node)
505501
target_names_list <- lapply(target_nodes, dag$tf_name)
506502

507-
## TF1/2 OK so the error with Wishart and cholesky is happening here
508-
## I feel as thought this isn't the "problem" per se, but it is where the
509-
## matrix of 1s is returned. So seems to me we first expose the problem here
510503
target_tensor_list <- lapply(target_names_list, get, envir = tfe)
511-
## It looks like it is getting all_sampling_operation_2 and not
512-
## all_sampling_operation_1
513504
target_tensor_list_array <- lapply(target_tensor_list, as.array)
514505

515506
return(target_tensor_list_array)

R/checkers.R

+2-3
Original file line numberDiff line numberDiff line change
@@ -796,9 +796,8 @@ check_n_cores <- function(n_cores,
796796
n_cores_detected <- future::availableCores()
797797
allowed_n_cores <- seq_len(n_cores_detected)
798798

799-
## TODO check explaining var
800-
# check user-provided cores
801-
if (!is.null(n_cores) && !n_cores %in% allowed_n_cores) {
799+
cores_exceed_available <- !is.null(n_cores) && !n_cores %in% allowed_n_cores
800+
if (cores_exceed_available) {
802801
check_positive_integer(n_cores, "n_cores")
803802

804803
cli::cli_warn(

R/dag_class.R

-48
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ dag_class <- R6Class(
2121
initialize = function(target_greta_arrays,
2222
tf_float = "float32",
2323
compile = FALSE) {
24-
# browser()
2524
# build the dag
2625
self$build_dag(target_greta_arrays)
2726

@@ -77,34 +76,6 @@ dag_class <- R6Class(
7776
self$tf_environment$hybrid_data_list <- list()
7877
},
7978

80-
# TF1/2 check remove on_graph
81-
# built with TF
82-
# Not sure if we need this anyore since this information will be handled
83-
# by tf_function?
84-
# execute an expression on this dag's tensorflow graph, with the correct
85-
# float type
86-
on_graph = function(expr) {
87-
# temporarily pass float type info to options, so it can be accessed by
88-
# nodes on definition, without cluncky explicit passing
89-
old_float_type <- options()$greta_tf_float
90-
old_batch_size <- options()$greta_batch_size
91-
92-
on.exit(options(
93-
greta_tf_float = old_float_type,
94-
greta_batch_size = old_batch_size
95-
))
96-
97-
options(
98-
greta_tf_float = self$tf_float,
99-
greta_batch_size = self$tf_environment$batch_size
100-
)
101-
102-
# A tf.Graph can be constructed and used directly without a tf.function,
103-
# as was required in TensorFlow 1, but this is deprecated and it is
104-
# recommended to use a tf.function instead.
105-
with(self$tf_graph$as_default(), expr)
106-
},
107-
10879
# return a list of nodes connected to those in the target node list
10980
build_dag = function(greta_array_list) {
11081
target_node_list <- lapply(greta_array_list, get_node)
@@ -171,7 +142,6 @@ dag_class <- R6Class(
171142
# if it's an operation, see if it has a distribution (for lkj and
172143
# wishart) and get mode based on whether the parent has a free state
173144
if (node_type == "operation") {
174-
# browser()
175145
parent_name <- node$parents[[1]]$unique_name
176146
parent_stateless <- parent_name %in% stateless_names
177147
to_sample <- has_distribution(node) & parent_stateless
@@ -282,21 +252,6 @@ dag_class <- R6Class(
282252
)
283253
} else {
284254
shape <- shape(NULL, length(vals))
285-
# TF1/2 check?
286-
# this `on_graph` part below is probably not needed/wont work because it
287-
# is using placeholder and co?
288-
#
289-
# defining an empty/unknown thing
290-
# so in TF2, we might not need to define a free state, we can
291-
# define a function that returns these pieces of information
292-
# so we will need to define the function relative to the free state
293-
# and will not need to define the object
294-
# Can we just turn this into a tensor with some arbitrary value?
295-
self$on_graph(free_state <- tf$compat$v1$placeholder(
296-
dtype = tf_float(),
297-
shape = shape
298-
))
299-
300255
# TF1/2 check
301256
# instead?
302257
# free_state <- tensorflow::as_tensor(
@@ -322,7 +277,6 @@ dag_class <- R6Class(
322277
lengths <- lengths(params)
323278

324279
if (length(lengths) > 1) {
325-
# args <- self$on_graph(tf$split(free_state, lengths, axis = 1L))
326280
args <- tf$split(free_state, lengths, axis = 1L)
327281
} else {
328282
args <- list(free_state)
@@ -345,9 +299,7 @@ dag_class <- R6Class(
345299
}
346300

347301
# define all nodes in the environment and on the graph
348-
## HERE
349302
lapply(target_nodes, function(x){
350-
# browser()
351303
x$define_tf(self)
352304
})
353305

R/node_class.R

-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ node <- R6Class(
1515
dim = NA,
1616
distribution = NULL,
1717
initialize = function(dim = NULL, value = NULL) {
18-
## browser()
1918
dim <- dim %||% c(1,1)
2019

2120
# coerce dim to integer
@@ -36,8 +35,6 @@ node <- R6Class(
3635

3736
# recursively register self and family
3837
register_family = function(dag) {
39-
## TF1/2
40-
## Rename with an explaining variable
4138
## TODO add explaining variable
4239
if (!(self$unique_name %in% names(dag$node_list))) {
4340

@@ -75,9 +72,6 @@ node <- R6Class(
7572
node$remove_child(self)
7673
},
7774
list_parents = function(dag) {
78-
## TF1/2
79-
## tf_cholesky
80-
## is there a way here to add some check for cholesky?
8175
parents <- self$parents
8276

8377
# if this node is being sampled and has a distribution, consider
@@ -200,7 +194,6 @@ node <- R6Class(
200194
lapply(
201195
parents[which(!parents_defined)],
202196
function(x){
203-
# browser()
204197
x$define_tf(dag)
205198
}
206199
)

R/node_types.R

+4-41
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ data_node <- R6Class(
33
inherit = node,
44
public = list(
55
initialize = function(data) {
6-
## browser()
76
# coerce to an array with 2+ dimensions
87
data <- as_2d_array(data)
98

@@ -150,26 +149,7 @@ operation_node <- R6Class(
150149
mode <- dag$how_to_define(self)
151150
# if sampling get the distribution constructor and sample this
152151
if (mode == "sampling") {
153-
# browser()
154152
tensor <- dag$draw_sample(self$distribution)
155-
156-
# if (has_representation(self, "cholesky")) {
157-
# browser()
158-
# cholesky_tensor <- tf_chol(tensor)
159-
# # cholesky_tf_name <- dag$tf_name(self$representation$cholesky)
160-
# cholesky_node <- get_node(representation(self, "cholesky"))
161-
# cholesky_tf_name <- dag$tf_name(cholesky_node)
162-
# assign(cholesky_tf_name, cholesky_tensor, envir = tfe)
163-
## TF1/2
164-
## This assignment I think is supposed to be passed down to later on
165-
## in the script, as `cholesky_tf_name` gets overwritten
166-
# cholesky_tf_name <- dag$tf_name(self)
167-
# tf_name <- cholesky_tf_name
168-
# tensor <- cholesky_tensor
169-
# cholesky_tensor <- tf_chol(tensor)
170-
# cholesky_tf_name <- dag$tf_name(self$representation$cholesky)
171-
# assign(cholesky_tf_name, cholesky_tensor, envir = dag$tf_environment)
172-
# }
173153
}
174154

175155
if (mode == "forward") {
@@ -188,11 +168,9 @@ operation_node <- R6Class(
188168
operation <- eval(parse(text = self$operation),
189169
envir = self$tf_function_env
190170
)
191-
# browser()
192171
tensor <- do.call(operation, tf_args)
193172
}
194173

195-
# browser()
196174
# assign it in the environment
197175
assign(tf_name, tensor, envir = dag$tf_environment)
198176
}
@@ -212,7 +190,6 @@ variable_node <- R6Class(
212190
upper = Inf,
213191
dim = NULL,
214192
free_dim = prod(dim)) {
215-
## browser()
216193
check_if_lower_upper_numeric(lower, upper)
217194

218195
# replace values of lower and upper with finite values for dimension
@@ -283,33 +260,24 @@ variable_node <- R6Class(
283260
mode <- dag$how_to_define(self)
284261

285262
if (mode == "sampling") {
286-
# browser()
287263
distrib_node <- self$distribution
288264

289265
if (is.null(distrib_node)) {
290266
# does it have an anti-representation where it is the cholesky?
291267
# the antirepresentation of cholesky is chol2symm
292-
# if it does, we will take the antirepresentation and get it to `tf` itself
293-
# then we need to get the tf_name
268+
# if yes, we take antirep and get it to `tf`, then get the tf_name
294269
chol2symm_ga <- self$anti_representations$chol2symm
295270
chol2symm_existing <- !is.null(chol2symm_ga)
271+
296272
if (chol2symm_existing) {
273+
297274
chol2symm_node <- get_node(chol2symm_ga)
298275
chol2symm_name <- dag$tf_name(chol2symm_node)
299276
chol2symm_tensor <- get(chol2symm_name, envir = dag$tf_environment)
300277
tensor <- tf_chol(chol2symm_tensor)
278+
301279
}
302280

303-
# chol2symm_ga$define_tf(dag)
304-
# } else {
305-
#
306-
# # if the variable has no distribution create a placeholder instead
307-
# # (the value must be passed in via values when using simulate)
308-
# shape <- to_shape(c(1, self$dim))
309-
# # TF1/2 check
310-
# # need to change the placeholder approach here.
311-
# # NT: can we change this to be a tensor of the right shape with 1s?
312-
# tensor <- tensorflow::as_tensor(1L, shape = shape, dtype = tf_float())
313281
} else {
314282
tensor <- dag$draw_sample(self$distribution)
315283
}
@@ -432,10 +400,8 @@ distribution_node <- R6Class(
432400
discrete = FALSE,
433401
multivariate = FALSE,
434402
truncatable = TRUE) {
435-
## browser()
436403
super$initialize(dim)
437404

438-
## browser()
439405
# for all distributions, set name, store dims, and set whether discrete
440406
self$distribution_name <- name
441407
self$discrete <- discrete
@@ -462,7 +428,6 @@ distribution_node <- R6Class(
462428

463429
# create a target variable node (unconstrained by default)
464430
create_target = function(truncation) {
465-
##browser()
466431
vble(truncation, dim = self$dim)
467432
},
468433
list_parents = function(dag) {
@@ -494,7 +459,6 @@ distribution_node <- R6Class(
494459

495460
# create target node, add as a parent, and give it this distribution
496461
add_target = function(new_target) {
497-
##browser()
498462
# add as target and as a parent
499463
self$target <- new_target
500464
self$add_parent(new_target)
@@ -621,7 +585,6 @@ node_classes_module <- module(
621585

622586
# shorthand for distribution parameter constructors
623587
distrib <- function(distribution, ...) {
624-
##browser()
625588
check_tf_version("error")
626589

627590
# get and initialize the distribution, with a default value node

R/probability_distributions.R

-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ normal_distribution <- R6Class(
5050
inherit = distribution_node,
5151
public = list(
5252
initialize = function(mean, sd, dim, truncation) {
53-
## browser()
5453
mean <- as.greta_array(mean)
5554
sd <- as.greta_array(sd)
5655

@@ -1051,10 +1050,6 @@ wishart_distribution <- R6Class(
10511050
input_output_cholesky = TRUE
10521051
)
10531052

1054-
## TF1/2
1055-
## The issue with getting the cholesky part of the Wishart
1056-
## isn't happening here,
1057-
## This produces something that looks about right
10581053
chol_draws <- chol_distrib$sample(seed = seed)
10591054

10601055
# equivalent to (but faster than) tf_chol2symm(tf_transpose(chol_draws))
@@ -1404,7 +1399,6 @@ uniform <- function(min, max, dim = NULL) {
14041399
#' @rdname distributions
14051400
#' @export
14061401
normal <- function(mean, sd, dim = NULL, truncation = c(-Inf, Inf)) {
1407-
##browser()
14081402
distrib("normal", mean, sd, dim, truncation)
14091403
}
14101404

R/unknowns_class.R

-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ print.unknowns <- function(x, ..., n = 10) {
2929
# set NA values to ? for printing
3030
x[is.na(x)] <- " ?"
3131

32-
# browser()
33-
3432
n_print <- getOption("greta.print_max") %||% n
3533

3634
n_unknowns <- length(x)

R/write-logfiles.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ write_greta_install_log <- function(path = greta_logfile) {
3737
<h1>Greta installation logfile</h1>
3838
<h2>Created: {{sys_date}}</h2>
3939
<p>Use this logfile to explore potential issues in installation with greta</p>
40-
<p>Try opening this in a browser and searching the text for "error" with Cmd/Ctrl+F</p>
40+
<p>Try opening this in a HTML browser and searching the text for "error" with Cmd/Ctrl+F</p>
4141
4242
<h2>Miniconda</h2>
4343
@@ -142,15 +142,15 @@ sys_get_env <- function(envvar){
142142
#' Read a greta logfile
143143
#'
144144
#' This is a convenience function to facilitate reading logfiles. It opens
145-
#' a browser using [utils::browseURL()]. It will search for
145+
#' a HTML browser using [utils::browseURL()]. It will search for
146146
#' the environment variable "GRETA_INSTALLATION_LOG" or default to
147147
#' `tools::R_user_dir("greta")`. To set
148148
#' "GRETA_INSTALLATION_LOG" you can use
149149
#' `Sys.setenv('GRETA_INSTALLATION_LOG'='path/to/logfile.html')`. Or use
150150
#' [greta_set_install_logfile()] to set the path, e.g.,
151151
#' `greta_set_install_logfile('path/to/logfile.html')`.
152152
#'
153-
#' @return opens a URL in your default browser
153+
#' @return opens a URL in your default HTML browser.
154154
#' @export
155155
open_greta_install_log <- function(){
156156

man/open_greta_install_log.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_distributions_cholesky.R

-6
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,3 @@ test_that("Post-MCMC, LKJ distribution stays symmetric, chol remains lower tri",
109109
expect_upper_tri(calcs$`chol(x)`)
110110

111111
})
112-
113-
114-
## TODO
115-
# further ensure all of the issues in
116-
## https://github.com/greta-dev/greta/labels/cholesky
117-
## are resolved in this branch

0 commit comments

Comments
 (0)