/// U -= U Field& operator -=(const Field& U) { cf3_assert(size() == U.size()); cf3_assert(row_size() == U.row_size()); for (Uint i=0; i<size(); ++i) for (Uint j=0; j<row_size(); ++j) array()[i][j] -= U.array()[i][j]; return *this; }
void resize( size_t rows, size_t cols ) { flut_error_if( rows < row_size() || cols < column_size(), "tables cannot be shrinked" ); // reorganize existing data data_.resize( row_size() * cols ); for ( index_t ri = rows; ri-- > 0; ) for ( index_t ci = cols; ci-- > 0; ) data_[ cols * ri + ci ] = ( ri < row_size() && ci < column_size() ) ? data_[ column_size() * ri + ci ] : T(); col_labels_.resize( cols ); row_labels_.resize( rows ); }
// The constructor uses the appropriate settings in the config file to // properly set up the sensor model for the specified "phase" of the LGMD // input signal. SensorModel::SensorModel(const std::string& lgmd_phase) : m_sigma(0.0f), m_name(lgmd_phase) { const range<float> lgmd_range = get_conf(locust_model(), "spike_range", make_range(0.0f, 800.0f)) ; // Get the LGMD ranges for the columns of the sensor model m_lgmd_ranges = string_to_deque<float>( conf<std::string>(lgmd_phase + "_lgmd_ranges", "0 800")) ; if (m_lgmd_ranges.size() < 2) { // crappy configuration! m_lgmd_ranges.clear() ; m_lgmd_ranges.push_back(lgmd_range.min()) ; m_lgmd_ranges.push_back(lgmd_range.max()) ; } sort(m_lgmd_ranges.begin(), m_lgmd_ranges.end()) ; if (m_lgmd_ranges.front() > lgmd_range.min()) m_lgmd_ranges.push_front(lgmd_range.min()) ; if (m_lgmd_ranges.back() < lgmd_range.max()) m_lgmd_ranges.push_back(lgmd_range.max()) ; // Figure out how many rows and columns the sensor model's probability // table has and allocate space for the required number of elements. // Initialize the probability table using a uniform distribution. const int C = m_lgmd_ranges.size() - 1 ; const int R = column_size() ; const int N = R * C ; m_prob.reserve(N) ; std::fill_n(std::back_inserter(m_prob), N, 1.0f/N) ; // Apply Gabbiani model to obtain causal probabilities and Gaussian // blur neighbouring bins in each row. update(clamp(conf(lgmd_phase + "_sigma", 1.0f), 0.1f, static_cast<float>(row_size()))) ; }
oz::gpu_image oz::cpu_image::gpu() const { if (!is_valid()) return gpu_image(); gpu_image dst(size(), format(), type_size()); cudaMemcpy2D(dst.ptr<void>(), dst.pitch(), ptr<void>(), pitch(), row_size(), h(), cudaMemcpyHostToDevice); return dst; }
oz::cpu_image oz::gpu_image::cpu() const { if (!is_valid()) return cpu_image(); cpu_image dst(size(), format(), type_size()); OZ_CUDA_SAFE_CALL(cudaMemcpy2D(dst.ptr<void>(), dst.pitch(), ptr<void>(), pitch(), row_size(), h(), cudaMemcpyDeviceToHost)); return dst; }
/// U /= c Field& operator /=(const Real& c) { for (Uint i=0; i<size(); ++i) for (Uint j=0; j<row_size(); ++j) array()[i][j] /= c; return *this; }
/// U /= U Field& operator /=(const Field& U) { cf3_assert(size() == U.size()); if (U.row_size() == 1) // U is a scalar field { for (Uint i=0; i<size(); ++i) for (Uint j=0; j<row_size(); ++j) array()[i][j] /= U.array()[i][0]; } else { cf3_assert(row_size() == U.row_size()); // field must be same size for (Uint i=0; i<size(); ++i) for (Uint j=0; j<row_size(); ++j) array()[i][j] /= U.array()[i][j]; } return *this; }
void set_row(const Uint array_idx, const VectorT& row) { if (row.size() != row_size(array_idx)) m_array[array_idx].resize(row.size()); Uint j=0; boost_foreach( const typename VectorT::value_type& v, row) m_array[array_idx][j++] = v; }
void SparseMatrixPatternCRS::expand_symmetric_pattern(SparseMatrixPatternCRS& rhs) const { rhs.N = N ; rhs.symmetric_storage = false ; // If matrix pattern does not use symmetric storage, just // copy the pattern. if(!symmetric_storage) { std::cerr << "SparseMatrixPatternCRS::expand_symmetric_pattern()" << " called for matrix without symmetric storage" << std::endl ; rhs.colind.allocate(colind.size()) ; for(index_t i=0; i<colind.size(); i++) { rhs.colind[i] = colind[i] ; } rhs.rowptr.allocate(rowptr.size()) ; for(index_t i=0; i<rowptr.size(); i++) { rhs.rowptr[i] = rowptr[i] ; } return ; } // Step1: compute row sizes and total number of non-zero coefficients size_t new_nnz = 0 ; IndexArray row_size(m()) ; row_size.set_all(0) ; for(index_t i=0; i<m(); i++) { row_size[i] += rowptr[i+1] - rowptr[i] ; new_nnz += rowptr[i+1] - rowptr[i] ; for(index_t jj= rowptr[i] ; jj < rowptr[i+1]; jj++) { index_t j = colind[jj] ; // Do not count diagonal elements twice if(j != i) { row_size[j]++ ; new_nnz++ ; } } } // Step 2: allocate target matrix pattern and compute row pointers. rhs.rowptr.allocate(m() + 1) ; rhs.colind.allocate(new_nnz) ; rhs.rowptr[0] = 0 ; for(index_t i=0; i<m(); i++) { rhs.rowptr[i+1] = rhs.rowptr[i] + row_size[i] ; } // Step 3: compute column indices. row_size.set_all(0) ; for(index_t i=0; i<m(); i++) { for(index_t jj= rowptr[i] ; jj < rowptr[i+1]; jj++) { index_t j = colind[jj] ; rhs.colind[ rhs.rowptr[i] + row_size[i] ] = j ; row_size[i]++ ; if(j != i) { rhs.colind[ rhs.rowptr[j] + row_size[j] ] = i ; row_size[j]++ ; } } } }
void set_row(const Uint array_idx, const VectorT& row) { cf3_assert(row.size() == row_size()); Row row_to_set = m_array[array_idx]; for(Uint j=0; j<row.size(); ++j) row_to_set[j] = row[j]; }
oz::cpu_image oz::cpu_image::copy( int x1, int y1, int x2, int y2 ) const { int cw = x2 - x1 + 1; int ch = y2 - y1 + 1; if ((x1 < 0) || (x2 >= (int)w()) || (y1 < 0) || (y2 >= (int)h()) || (cw <= 0) || (ch <= 0)) OZ_X() << "Invalid region!"; cpu_image dst(cw, ch, format()); uchar *src_ptr = ptr<uchar>() + y1 * row_size() + x1 * type_size(); cudaMemcpy2D(dst.ptr(), dst.pitch(), src_ptr, pitch(), dst.row_size(), dst.h(), cudaMemcpyHostToHost); return dst; }
Integer SpatiocyteWorld::add_structure3(const Species& sp, const boost::shared_ptr<const Shape> shape) { // Real3 l, u; // shape->bounding_box(edge_lengths(), l, u); // const Real sigma(voxel_radius() * 2); // const unsigned int ndim(3); // for (unsigned int i(0); i != ndim; ++i) // { // l[i] = std::max(0.0, l[i] - sigma); // u[i] = std::min(edge_lengths()[i], u[i] + sigma); // } // const Integer3 lower(position2global(l)), upper(position2global(u)); const SpatiocyteWorld::molecule_info_type info(get_molecule_info(sp)); Integer count(0); for (Integer col(0); col < col_size(); ++col) { for (Integer row(0); row < row_size(); ++row) { for (Integer layer(0); layer < layer_size(); ++layer) { // for (Integer col(lower[0]); col < upper[0]; ++col) // { // for (Integer row(lower[1]); row < upper[1]; ++row) // { // for (Integer layer(lower[2]); layer < upper[2]; ++layer) // { const Integer3 g(col, row, layer); const Real L(shape->is_inside(global2position(g))); if (L > 0) { continue; } const Voxel v(sp, (*space_).global2private_coord(g), info.radius, info.D, info.loc); if (new_voxel_structure(v).second) { ++count; } } } } return count; }
// This method regenerates the sensor model's probabilities using the // Gabbiani LGMD model and the given standard deviation for the Gaussian // blurring operation for bins near the ones actually "pointed to" by the // [TTI, LGMD] pairs returned by the Gabbiani model. // // DEVNOTE: The sigma provided to this function is actually added to the // m_sigma member variable. This allows client behaviours to increment or // decrement the current sigma value rather than provide an actual sigma. // The very first sigma will be read from the config file (see // constructor). void SensorModel::update(float dsigma) { AutoMutex M(m_mutex) ; // Record new standard deviation const float R = row_size() ; m_sigma = clamp(m_sigma + dsigma, 0.1f, R) ; // Begin with a uniform distribution for each state const int N = m_prob.size() ; std::fill_n(m_prob.begin(), N, 1/R) ; // Apply Gabbiani LGMD model to generate causal likelihoods const float step = row_step()/4.0f ; const range<float> tti = conf(m_name + "_tti_range", Params::tti_range()) ; for (float t = tti.min(); t <= tti.max(); t += step) update_row(t, GabbianiModel::spike_rate(t), m_sigma) ; }
// This function increments the bin "pointed" to by the given [TTI, LGMD] // pair. It also increments the other bins in the row "pointed" to by the // TTI using a Gaussian weighting formula to ensure that no bin in that // row has weird likelihood values that can screw up the Bayesian TTI // estimation. Finally, it normalizes the row to ensure that each row // vector is a valid probability distribution. void SensorModel::update_row(float tti, float lgmd, float sigma) { const int N = row_size() ; const int C = column_size() ; const int I = col_index(lgmd) ; const float S = 1/(2 * sqr(sigma)) ; Table::iterator begin = m_prob.begin() + row_index(tti) ; float normalizer = 0 ; Table::iterator it = begin ; for (int i = 0; i < N; ++i, it += C) { *it += exp(-sqr(i - I) * S) ; //*it = exp(-sqr(i - I) * S) ; normalizer += *it ; } it = begin ; for (int i = 0; i < N; ++i, it += C) *it /= normalizer ; }
CHECK(im(0,0) == 0); CHECK(im2(0,0) == 0); im2(0,0) = 1; CHECK(im2(0,0) == 1); im2.set(514); CHECK(im2(0,0) == 514); CHECK(im2(1,1) == 514); // Check that size is correct CHECK(im.size() == 32); CHECK(im2.size() == 32); // Check that row_size is correct CHECK(im.row_size() == 8); CHECK(im2.row_size() == 8); // Check that get_premultiplied is correct CHECK_FALSE(im.get_premultiplied()); CHECK_FALSE(im2.get_premultiplied()); // Check that set premultiplied works im2.set_premultiplied(true); CHECK(im2.get_premultiplied()); // Check that painted is correct CHECK_FALSE(im.painted()); CHECK_FALSE(im2.painted()); // Check that set premultiplied works im2.painted(true);
Integer SpatiocyteWorld::add_structure2(const Species& sp, const boost::shared_ptr<const Shape> shape) { // std::ofstream fout("shape.csv"); // fout << "# " << sp.serial() << std::endl; // const unsigned int n(50); // const Real3 L(edge_lengths() / static_cast<Real>(n)); // for (unsigned int i(0); i < n * n * n; ++i) // { // const unsigned int x(i % n); // const unsigned int y((i / n) % n); // const unsigned int z(i / (n * n)); // if (shape->test_AABB(Real3(x * L[0], y * L[1], z * L[2]), // Real3((x + 1) * L[0], (y + 1) * L[1], (z + 1) * L[2]))) // { // fout << x << "," << y << "," << z << std::endl; // } // } // fout.close(); // Real3 l, u; // shape->bounding_box(edge_lengths(), l, u); // const Real sigma(voxel_radius() * 2); // const unsigned int ndim(3); // for (unsigned int i(0); i != ndim; ++i) // { // l[i] = std::max(0.0, l[i] - sigma); // u[i] = std::min(edge_lengths()[i], u[i] + sigma); // } // const Integer3 lower(position2global(l)), upper(position2global(u)); const SpatiocyteWorld::molecule_info_type info(get_molecule_info(sp)); Integer count(0); for (Integer col(0); col < col_size(); ++col) { for (Integer row(0); row < row_size(); ++row) { for (Integer layer(0); layer < layer_size(); ++layer) { // for (Integer col(lower[0]); col < upper[0]; ++col) // { // for (Integer row(lower[1]); row < upper[1]; ++row) // { // for (Integer layer(lower[2]); layer < upper[2]; ++layer) // { const Integer3 g(col, row, layer); if (!is_surface_voxel(g, shape)) { continue; } const Voxel v(sp, (*space_).global2private_coord(g), info.radius, info.D, info.loc); if (new_voxel_structure(v).second) { ++count; } } } } return count; }
index_t add_row( const L& label, const T& default_value = T() ) { row_labels_.add( label ); data_.resize( row_size() * column_size(), default_value ); return row_size() - 1; }
/// @return A Buffer object that can fill this Array Buffer create_buffer(const size_t buffersize=16384) { // make sure the array has its columnsize defined cf3_assert(row_size() > 0); return Buffer(m_array,buffersize); }
index_t add_column( const L& label, const T& default_value = T() ) { resize( row_size(), column_size() + 1 ); return col_labels_.set( column_size() - 1, label ); }
T& operator()( index_t row, index_t col ) { flut_assert( row < row_size() && col < column_size() ); return data_[ row * column_size() + col ]; }
ptrdiff_t stride() const { return -static_cast<ptrdiff_t>(row_size()); }
const unsigned char *read_ptr() const { const unsigned char *image_data = static_cast<const unsigned char *>(m_bitmap.image_data); return image_data + row_size() * (height() - 1); }
/** * @brief stride of one row. * * Usually return row_size, some format align the output, * so the stride maybe greater than row_size. * */ virtual pix_len stride() const { return row_size(); }
void CField::create_data_storage() { cf_assert( m_var_types.size()!=0 ); cf_assert( is_not_null(m_topology->follow()) ); // Check if there are coordinates in this field, and add to map m_coords = find_parent_component<CMesh>(topology()).nodes().coordinates().as_ptr<CTable<Real> >(); Uint row_size(0); boost_foreach(const VarType var_size, m_var_types) row_size += Uint(var_size); m_data->set_row_size(row_size); switch (m_basis) { case Basis::POINT_BASED: { m_used_nodes = CElements::used_nodes(topology()).as_ptr<CList<Uint> >(); m_data->resize(m_used_nodes->size()); break; } case Basis::ELEMENT_BASED: { Uint data_size = 0; boost_foreach(CEntities& field_elements, find_components_recursively<CEntities>(topology())) { if (field_elements.exists_space(m_space_name) == false) throw ValueNotFound(FromHere(),"space \""+m_space_name+"\" does not exist in "+field_elements.uri().path()); m_elements_start_idx[field_elements.as_ptr<CEntities>()] = data_size; CFieldView field_view("tmp_field_view"); data_size = field_view.initialize(*this,field_elements.as_ptr<CEntities>()); } m_data->resize(data_size); break; } case Basis::CELL_BASED: { Uint data_size = 0; boost_foreach(CEntities& field_elements, find_components_recursively<CCells>(topology())) { //CFinfo << name() << ": creating cellbased field storage in " << field_elements.uri().path() << CFendl; if (field_elements.exists_space(m_space_name) == false) throw ValueNotFound(FromHere(),"space \""+m_space_name+"\" does not exist in "+field_elements.uri().path()); m_elements_start_idx[field_elements.as_ptr<CEntities>()] = data_size; CFieldView field_view("tmp_field_view"); data_size = field_view.initialize(*this,field_elements.as_ptr<CEntities>()); } m_data->resize(data_size); break; } case Basis::FACE_BASED: { Uint data_size = 0; boost_foreach(CEntities& field_elements, find_components_recursively_with_tag<CEntities>(topology(),Mesh::Tags::face_entity())) { if (field_elements.exists_space(m_space_name) == false) throw ValueNotFound(FromHere(),"space \""+m_space_name+"\" does not exist in "+field_elements.uri().path()); m_elements_start_idx[field_elements.as_ptr<CEntities>()] = data_size; CFieldView field_view("tmp_field_view"); data_size = field_view.initialize(*this,field_elements.as_ptr<CEntities>()); } m_data->resize(data_size); break; } default: throw NotSupported(FromHere() , "Basis can only be ELEMENT_BASED or NODE_BASED"); break; } }
void oz::gpu_image::clear() { OZ_CUDA_SAFE_CALL(cudaMemset2D(ptr(), pitch(), 0, row_size(), h())); }