nd::array dynd::nd::linspace(const nd::array& start, const nd::array& stop, intptr_t count) { ndt::type dt = promote_types_arithmetic(start.get_dtype(), stop.get_dtype()); // Make sure it's at least floating point if (dt.get_kind() == bool_kind || dt.get_kind() == int_kind || dt.get_kind() == uint_kind) { dt = ndt::make_type<double>(); } return linspace(start, stop, count, dt); }
static nd::array function_ndo_strftime(const nd::array& n, const std::string& format) { // TODO: Allow 'format' itself to be an array, with broadcasting, etc. if (format.empty()) { throw runtime_error("format string for strftime should not be empty"); } return n.replace_dtype(ndt::make_unary_expr(ndt::make_string(), n.get_dtype(), make_strftime_kernelgen(format))); }
static nd::array function_ndo_replace(const nd::array& n, int32_t year, int32_t month, int32_t day) { // TODO: Allow 'year', 'month', and 'day' to be arrays, with broadcasting, etc. if (year == numeric_limits<int32_t>::max() && month == numeric_limits<int32_t>::max() && day == numeric_limits<int32_t>::max()) { throw std::runtime_error("no parameters provided to date.replace, should provide at least one"); } return n.replace_dtype(ndt::make_unary_expr(ndt::make_date(), n.get_dtype(), make_replace_kernelgen(year, month, day))); }
static nd::array function_ndo_to_struct(const nd::array& n) { return n.replace_dtype(ndt::make_property(n.get_dtype(), "struct")); }
static nd::array property_ndo_get_microsecond(const nd::array& n) { return n.replace_dtype(ndt::make_property(n.get_dtype(), "microsecond")); }
static nd::array property_ndo_get_hour(const nd::array& n) { return n.replace_dtype(ndt::make_property(n.get_dtype(), "hour")); }
static nd::array property_ndo_get_ints(const nd::array& n) { ndt::type udt = n.get_dtype().value_type(); const categorical_type *cd = udt.tcast<categorical_type>(); return n.view_scalars(cd->get_storage_type()); }
static nd::array function_ndo_weekday(const nd::array& n) { return n.replace_dtype(ndt::make_property(n.get_dtype(), "weekday")); }
dynd::nd::array pydynd::nd_fields(const nd::array &n, PyObject *field_list) { vector<std::string> selected_fields; pyobject_as_vector_string(field_list, selected_fields); // TODO: Move this implementation into dynd ndt::type fdt = n.get_dtype(); if (fdt.get_kind() != struct_kind) { stringstream ss; ss << "nd.fields must be given a dynd array of 'struct' kind, not "; ss << fdt; throw runtime_error(ss.str()); } const ndt::struct_type *bsd = fdt.extended<ndt::struct_type>(); if (selected_fields.empty()) { throw runtime_error( "nd.fields requires at least one field name to be specified"); } // Construct the field mapping and output field types vector<intptr_t> selected_index(selected_fields.size()); vector<ndt::type> selected__types(selected_fields.size()); for (size_t i = 0; i != selected_fields.size(); ++i) { selected_index[i] = bsd->get_field_index(selected_fields[i]); if (selected_index[i] < 0) { stringstream ss; ss << "field name "; print_escaped_utf8_string(ss, selected_fields[i]); ss << " does not exist in dynd type " << fdt; throw runtime_error(ss.str()); } selected__types[i] = bsd->get_field_type(selected_index[i]); } // Create the result udt ndt::type rudt = ndt::struct_type::make(selected_fields, selected__types); ndt::type result_tp = n.get_type().with_replaced_dtype(rudt); const ndt::struct_type *rudt_bsd = rudt.extended<ndt::struct_type>(); // Allocate the new memory block. size_t arrmeta_size = result_tp.get_arrmeta_size(); nd::array result(reinterpret_cast<array_preamble *>( make_array_memory_block(arrmeta_size).get()), true); // Clone the data pointer result.get()->data = n.get()->data; result.get()->owner = n.get()->owner; if (!result.get()->owner) { result.get()->owner = n.get(); } // Copy the flags result.get()->flags = n.get()->flags; // Set the type and transform the arrmeta result.get()->tp = result_tp; // First copy all the array data type arrmeta ndt::type tmp_dt = result_tp; char *dst_arrmeta = result.get()->metadata(); const char *src_arrmeta = n.get()->metadata(); while (tmp_dt.get_ndim() > 0) { if (tmp_dt.get_kind() != dim_kind) { throw runtime_error( "nd.fields doesn't support dimensions with pointers yet"); } const ndt::base_dim_type *budd = tmp_dt.extended<ndt::base_dim_type>(); size_t offset = budd->arrmeta_copy_construct_onedim( dst_arrmeta, src_arrmeta, intrusive_ptr<memory_block_data>(n.get(), true)); dst_arrmeta += offset; src_arrmeta += offset; tmp_dt = budd->get_element_type(); } // Then create the arrmeta for the new struct const size_t *arrmeta_offsets = bsd->get_arrmeta_offsets_raw(); const size_t *result_arrmeta_offsets = rudt_bsd->get_arrmeta_offsets_raw(); const size_t *data_offsets = bsd->get_data_offsets(src_arrmeta); size_t *result_data_offsets = reinterpret_cast<size_t *>(dst_arrmeta); for (size_t i = 0; i != selected_fields.size(); ++i) { const ndt::type &dt = selected__types[i]; // Copy the data offset result_data_offsets[i] = data_offsets[selected_index[i]]; // Copy the arrmeta for this field if (dt.get_arrmeta_size() > 0) { dt.extended()->arrmeta_copy_construct( dst_arrmeta + result_arrmeta_offsets[i], src_arrmeta + arrmeta_offsets[selected_index[i]], intrusive_ptr<memory_block_data>(n.get(), true)); } } return result; }
static nd::array property_ndo_get_tick(const nd::array &n) { return n.replace_dtype(ndt::property_type::make(n.get_dtype(), "tick")); }
static nd::array property_complex_conj(const nd::array& n) { return n.replace_dtype(ndt::make_property(n.get_dtype(), "conj")); }