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

Implement RANDOM for NEURON. #1367

Merged
merged 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/codegen/codegen_neuron_cpp_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,11 @@
const std::string& name,
bool use_instance) const {
auto position = position_of_int_var(name);

if (info.semantics[position].name == naming::RANDOM_SEMANTIC) {
return fmt::format("_ppvar[{}].literal_value<void*>()", position);

Check warning on line 568 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L568

Added line #L568 was not covered by tests
}

if (symbol.is_index) {
if (use_instance) {
throw std::runtime_error("Not implemented. [wiejo]");
Expand All @@ -581,9 +586,8 @@
return fmt::format("(*inst.{}[id])", name);
}


throw std::runtime_error("Not implemented. [nvueir]");
// auto data = symbol.is_vdata ? "_vdata" : "_data";
// return fmt::format("nt->{}[indexes[{}*pnodecount + id]]", data, position);
}


Expand Down Expand Up @@ -1281,7 +1285,7 @@
auto const name = var->get_name();
printer->fmt_line("{}* {}{};",
type,
name,

Check warning on line 1288 in src/codegen/codegen_neuron_cpp_visitor.cpp

View workflow job for this annotation

GitHub Actions / { "flag_warnings": "ON", "glibc_asserts": "ON", "os": "ubuntu-22.04" }

unused variable ‘int_type’ [-Wunused-variable]

Check warning on line 1288 in src/codegen/codegen_neuron_cpp_visitor.cpp

View workflow job for this annotation

GitHub Actions / { "flag_warnings": "ON", "os": "ubuntu-22.04", "sanitizer": "undefined" }

unused variable 'int_type' [-Wunused-variable]
print_initializers ? fmt::format("{{&::{}}}", name) : std::string{});
}
for (auto& var: codegen_float_variables) {
Expand Down Expand Up @@ -1545,13 +1549,19 @@
}


/// TODO: Edit for NEURON
void CodegenNeuronCppVisitor::print_nrn_destructor() {
return;
printer->fmt_push_block("void {}(Prop* _prop)", method_name(naming::NRN_DESTRUCTOR_METHOD));
printer->add_line("Datum* _ppvar = _nrn_mechanism_access_dparam(_prop);");

for (const auto& rv: info.random_variables) {
printer->fmt_line("nrnran123_deletestream((nrnran123_State*) {});",

Check warning on line 1557 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L1556-L1557

Added lines #L1556 - L1557 were not covered by tests
get_variable_name(get_name(rv), false));
}

printer->pop_block();
}


/// TODO: Print the equivalent of `nrn_alloc_<mech_name>`
void CodegenNeuronCppVisitor::print_nrn_alloc() {
printer->add_newline(2);

Expand Down Expand Up @@ -1596,7 +1606,7 @@
}
}
if (info.point_process) {
printer->pop_block();

Check warning on line 1609 in src/codegen/codegen_neuron_cpp_visitor.cpp

View workflow job for this annotation

GitHub Actions / { "flag_warnings": "ON", "glibc_asserts": "ON", "os": "ubuntu-22.04" }

unused variable ‘var_value’ [-Wunused-variable]

Check warning on line 1609 in src/codegen/codegen_neuron_cpp_visitor.cpp

View workflow job for this annotation

GitHub Actions / { "flag_warnings": "ON", "os": "ubuntu-22.04", "sanitizer": "undefined" }

unused variable 'var_value' [-Wunused-variable]
}

if (info.semantic_variable_count) {
Expand Down Expand Up @@ -1665,7 +1675,14 @@
}
}

/// TODO: CONSTRUCTOR call
if (!info.random_variables.empty()) {
for (const auto& rv: info.random_variables) {
printer->fmt_line("{} = nrnran123_newstream();",
get_variable_name(get_name(rv), false));
}
printer->fmt_line("nrn_mech_inst_destruct[mech_type] = {};",
method_name(naming::NRN_DESTRUCTOR_METHOD));
}

printer->pop_block();
}
Expand Down Expand Up @@ -2065,6 +2082,7 @@
print_prcellstate_macros();
print_mechanism_info();
print_data_structures(true);
print_nrn_destructor();
print_nrn_alloc();
print_function_prototypes();
print_functors_definitions();
Expand Down
1 change: 1 addition & 0 deletions test/usecases/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set(NMODL_USECASE_DIRS
net_send
point_process
parameter
random
suffix
state
table
Expand Down
9 changes: 9 additions & 0 deletions test/usecases/random/random_variable.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
NEURON {
SUFFIX random_variable
RANDOM rng
}


FUNCTION negexp() {
negexp = random_negexp(rng)
}
35 changes: 35 additions & 0 deletions test/usecases/random/test_random_variable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import numpy as np
import scipy as sp
import hashlib

from neuron import h


def hash_array(x):

return hashlib.sha256(" ".join([f"{xx:.10e}" for xx in x]).encode()).hexdigest()


def test_negexp():
nseg = 10
s = h.Section()

s.nseg = nseg
s.insert("random_variable")

n_samples = 1000
samples = [
np.array([s(x).random_variable.negexp() for _ in range(n_samples)])
for x in [0.34, 0.74]
]
expected_hashes = [
"3b66d7f83dc81ea485929aa8a4f347a3befee7170971dfb9c35a1a1d40ed0407",
"896c59d129cc0a248e57c548c8bf4be7ae4d39fbd92113e37f59c332be61a2b7",
]

assert hash_array(samples[0]) == expected_hashes[0]
assert hash_array(samples[1]) == expected_hashes[1]
1uc marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == "__main__":
test_negexp()
Loading