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

Add dtype property to arrays #1421

Closed
tomcaruso opened this issue Jun 16, 2023 · 1 comment · Fixed by #1260
Closed

Add dtype property to arrays #1421

tomcaruso opened this issue Jun 16, 2023 · 1 comment · Fixed by #1260

Comments

@tomcaruso
Copy link

Description

I try to create a numpy array with np.zeros with the dtype given by another array. When I try to pyccelise this file pyccel return the issue Attribute dtype not found.

To Reproduce

import numpy as np
from pyccel.decorators import template
@template(name='T', types=['float[:]', 'complex[:]'])
def eval_jacobians_3d(n1: int, array: 'T'):
    arr_coeffs_x = np.zeros((n1), dtype=array.dtype)

Error details

ERROR at annotation (semantic) stage
pyccel:
 |fatal [semantic]: kernels.py [60,91]| Attribute dtype not found

Expected behavior

It should pyccelise it without crashing, I guess.

Language

Fortran

@tomcaruso tomcaruso added the bug label Jun 16, 2023
@EmilyBourne
Copy link
Member

You are right, pyccel doesn't yet implement the dtype function or attribute.
We should create a class containing only a __new__ function which returns the dtype of an array using the dictionary DtypePrecisionToCastFunction. This new class should then be added as an attribute in the numpy array class description here:

NumpyArrayClass = ClassDef('numpy.ndarray',

While you are waiting for someone to pick up this issue, as a temporary workaround you can use:

import numpy as np
from pyccel.decorators import template
@template(name='T', types=['float[:]', 'complex[:]'])
def eval_jacobians_3d(n1: int, array: 'T'):
    arr_coeffs_x = np.zeros((n1), dtype=type(array[0]))

@EmilyBourne EmilyBourne linked a pull request Aug 1, 2023 that will close this issue
@EmilyBourne EmilyBourne self-assigned this Aug 1, 2023
@EmilyBourne EmilyBourne changed the title Fatal error during pyccelisation of np.zeros with dtype Add dtype property to arrays Aug 1, 2023
yguclu pushed a commit that referenced this issue Aug 15, 2023
Remove the duplicate `_print_NumpyArray` function from
PythonCodePrinter. The original `_print_NumpyArray` function is more
accurate as it specifies the `dtype` etc explicitly. Specifying the `dtype`
explicitly makes issue #1377 reproducible. To fix this problem support is
added for `a.dtype` expressions (Fixes #1421) using the similar function
[`np.result_type`](https://numpy.org/doc/stable/reference/generated/numpy.result_type.html).

The initialisation dtype is saved and used in the Python printer. This
is a work-around until #1334 is resolved well enough to define a clear
issue. Additionally a warning is raised if the Python code is not
consistent. Fixes #1377.

**Additional commits**
- Simplify bot linux/windows/macosx output to reduce duplication. Change
regex for C now that the test is no longer verbose.
- Stop generating `numpy.bool` in code as it has been deprecated by
NumPy. Instead use the default bool (with precision `-1`).
- Add support for NumPy's short type names.
- Reduce duplication in the Python printer.
- Remove outdated skips on tests.
- Stop array error tests failing in developer mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants