mhtess / rwebppl

RWebPPL, an R interface to Webppl http://webppl.org
22 stars 7 forks source link

Passing inference doesn't play nice with program in file #17

Closed mhtess closed 8 years ago

mhtess commented 8 years ago

Passing inference options to model works when program is a string but not when it's a file.

var model = function() {
   var p = uniform( {a:0, b:1} );    // sample a coin weight from a uniform distribution
   factor(Binomial( {p : p, n: 10 }).score(5));
   return {p : p};
};
webppl(
  ...
  model_var = "model",
  inference_opts = list(method="MCMC", samples = 10000, burn = 5000)
  )

Works when program is a string. When it is a file

{ type: 'VariableDeclaration',
  declarations: 
   [ { type: 'VariableDeclarator',
       id: [Object],
       init: [Object],
       range: [Object],
       loc: [Object] } ],
  kind: 'var',
  range: [ 73, 364 ],
  loc: { start: { line: 5, column: 0 }, end: { line: 12, column: 1 } } }
returnify
Error in run_webppl(program_code = program_code, program_file = program_file,  : 
  /Users/mht/.rwebppl/node_modules/webppl/src/syntax.js:20
    throw new Error(message);
    ^

Error: returnify
    at /Users/mht/.rwebppl/node_modules/webppl/src/syntax.js:20:11
    at match (/Users/mht/.rwebppl/node_modules/webppl/src/syntax.js:52:10)
    at returnify (/Users/mht/.rwebppl/node_modules/webppl/src/syntax.js:98:31)
    at thunkify (/Users/mht/.rwebppl/node_modules/webppl/src/syntax.js:146:32)
    at /Users/mht/.rwebppl/node_modules/underscore/underscore.js:870:32
    at /Users/mht/.rwebppl/node_modules/underscore/underscore.js:871:36
    at _compile (/Users/mht/.rwebppl/node_modules/webppl/src/main.js:174:7)
    at Object.timeif (/Users/mht/.rwebppl/node_modules/webppl/src/util.js:229:37)
    at compile (/Users/mht/.rwebppl/node_modules/webppl/src/main.js:177:15)
    at Object.run (/Users/mht/.rwebppl/node_modules/webppl/src/main.js:186:22)

meaning that there is no last line of the program to return

Also, strangely, when you write a model in a program and the last line of the program is NOT white space, you get the following warning:

Warning message:
In readLines(program_file) :
  incomplete final line found on 'webppl_models/myFirstModel.wppl'
hawkrobe commented 8 years ago

We can suppress the "incomplete final line" warning by adding the warn = F option to the readLines call:

output_string <- paste(readLines(output_file, warn = F), collapse = "\n")

The issue there is that readLines keep reading until it hits an EOL marker. If you don't include a final new line, there's no marker, so the function is surprised to hit the end of input.

hawkrobe commented 8 years ago

I'm afraid I can't replicate your returnify error, though.

mhtess commented 8 years ago

So passing inference to files is working for you? Erin was getting a similar error as mine (I think...)

hawkrobe commented 8 years ago

Yes, the example you have above is working as expected, and other examples worked too (e.g. changing the distributions in the model, changing inference parameters, etc.)

mhtess commented 8 years ago

Cannot reproduce the error.

erindb commented 8 years ago

FWIW, I did reproduce this error, but it's not really an rwebppl error.

It doesn't seem to me like it's actually problematic. Here's a minimal example. Basically, if the webppl file doesn't have a return value, then webppl.js won't be able to do anything with it. But if it ​does​ have a return value, then it will return either that return value or whatever model_var and inference_opts ask for. (I think this is all true.)

The following should reproduce the returnify error if you comment out the last line of the webppl file: ​

#error-test.R

library(rwebppl)

## runs and takes the output of the webppl program as its value
numbers = webppl(program_file = 'error-test.wppl')
​
## runs *if there's an output* and takes the result of desired inference.
## otherwise throws returnify error.
numbers = webppl(program_file = 'error-test.wppl',
                 model_var = 'model',
                 inference_opts = list(method='enumerate'))
#error-test.wppl

var model = function() {
  return flip(); // ? RParams.a : 2;
};
var apples = function() {
  return 200;
};
// if the following line is commented out, we'll get the returnify error.
"finished";