Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dimensions of data seem to be swapped in v6.4.0 #51

Closed
itsdfish opened this issue May 2, 2022 · 8 comments
Closed

dimensions of data seem to be swapped in v6.4.0 #51

itsdfish opened this issue May 2, 2022 · 8 comments

Comments

@itsdfish
Copy link

itsdfish commented May 2, 2022

Hi Rob,

It seems like the rows and columns are not imported to Stan correctly... or I am doing something stupid. I recieve an error in 6.4.0 but not in 5.6.

Would you be able to look into this please? Thanks!

temp_model.stan

data { 
    int n_rows;
    int<lower=1> n_cols;
    int<lower=0> x[n_rows,n_cols]; 
}

parameters {
   real mu;
} 

model {
    mu ~ normal(0, 1);
}

run_temp_model

using StanSample, Random
tempdir = pwd() * "/tmp"
stan_path = "/home/dfish/cmdstan"
seed = 65445
Random.seed!(seed)

n_rows = 50
n_cols = 2
x = fill(0, n_rows, n_cols)

stan_data = Dict("x" => x, "n_rows"=>n_rows, "n_cols"=>n_cols)
println(size(stan_data["x"]))

# load the Stan model file
stream = open("temp_model.stan", "r")
model = read(stream, String)
close(stream)
stan_model = SampleModel("temp_model", model, tempdir)

stan_sample(
    stan_model;
    data = stan_data,
    seed,
    num_chains = 4,
    num_samples = 1000,
    num_warmups = 1000,
    save_warmup = false
)

Error

Exception: mismatch in dimension declared and found in context; processing stage=data initialization; variable name=x; position=0; dims declared=(50,2); dims found=(2,50) (in '/home/dfish/.julia/dev/InterferenceEffectModelComparison/critical_tests/jrm/tmp/temp_model.stan', line 4, column 4 to column 34)Exception: mismatch in dimension declared and found in context; processing stage=data initialization; variable name=x; position=0; dims declared=(50,2); dims found=(2,50) (in '/home/dfish/.julia/dev/InterferenceEffectModelComparison/critical_tests/jrm/tmp/temp_model.stan', line 4, column 4 to column 34)
@goedman
Copy link
Collaborator

goedman commented May 2, 2022 via email

@goedman
Copy link
Collaborator

goedman commented May 2, 2022

Hi Chris, yes, you are correct. By switching to JSON, R's row-first and Julia's column first becomes visible. To get around it you can call rc = stan_sample(sm, data, use_json=false which for the time being is still acceptable. Or you can transpose x, i.e. in below script: x = fill(0, n_rows, n_cols)'.

🎈 chris.jl — Pluto.jl.pdf

Note: I could hide this in the call to JSON (update_json_files.jl) but want to think about that a bit.

@itsdfish
Copy link
Author

itsdfish commented May 2, 2022

Rob, thanks for explaining.

I wonder whether StanSample should make an adjustment to the JSON so that it is consistent with Julia's indexing system. In the present case, the error was caught be the dimensions were incorrect. However, the code would run and return invalid output if I used non-symmetrical n X n array.

@goedman
Copy link
Collaborator

goedman commented May 3, 2022

Ok, I think I have a solution for Dicts and NamedTuples. Will do some more tests with an Array of these and merge later today. One issue was I didn't want to go into JSON.jl and NamedTuples are immutable.

@itsdfish
Copy link
Author

itsdfish commented May 3, 2022

Thanks for looking into this issue. I think it will be good to have consistency in the indexing scheme so people do not perform the wrong analysis.

@goedman
Copy link
Collaborator

goedman commented May 4, 2022

Looks like it has been merged. It didn’t update the tag, but will update.

@itsdfish
Copy link
Author

itsdfish commented May 5, 2022

Thanks, Rob! The update has fixed the problem.

@itsdfish itsdfish closed this as completed May 5, 2022
@AndyPohlNZ
Copy link

Hi Rob,

Thanks for putting work into getting stan and julia playing nice together and for the fix to the above issue. Sorry for raising this but the fix implemented does not seem to generalize to multidimensional arrays. I am using the master branch of StanSample: StanSample v6.9.2 https://github.com/StanJulia/StanSample.jl#master . See the modified example below:

using StanSample
ProjDir = @__DIR__
n1 = 50
n2 = 2
n3 = 27
x = fill(0, n1, n2, n3)

stan_data = Dict("x" => x, "n1" => n1, "n2" => n2, "n3" => n3)
println(size(stan_data["x"]))

mdl = "
data { 
    int n1;
    int<lower=1> n2;
    int<lower=1> n3;
    array[n1, n2, n3]real x;            
}

parameters {
   real mu;
} 

model {
    mu ~ normal(0, 1);
}
"

tmpdir = joinpath(ProjDir, "tmp")
isdir(tmpdir) && rm(tmpdir; recursive=true)
stan_model = SampleModel("temp_model", mdl, tmpdir)
stan_sample(
    stan_model;
    data=stan_data,
    seed=123,
    num_chains=4,
    num_samples=1000,
    num_warmups=1000,
    save_warmup=false
)

Returns the following error:
Exception: mismatch in dimension declared and found in context; processing stage=data initialization; variable name=x; position=0; dims declared=(50,2,27); dims found=(27,2,50)

goedman added a commit that referenced this issue Dec 7, 2022
c7a425a Allow JSON data as a string (#60)
50fe2ec Update README examples section
e380e82 Reword docstrings
fb513e3 Reorganize and update documentation (#57)
96805fd Compile methods: split stanc args from make args, add default include path (#58)
f62bf46 Fix broken link
676db6b Bump actions/setup-python from 2 to 4 (#55)
34f10dd Remove CmdStan Dependency (#51)
821883f Prefix any C-exposed symbols with `bs_` (#54)
81129b0 Add the option for autodiff Hessian calculations (#52)

git-subtree-dir: deps/data/bridgestan
git-subtree-split: c7a425aac54120bafa643b722ed24b2a32111782
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants