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

Refactor Python position Example #127

Merged

Conversation

ax3l
Copy link
Member

@ax3l ax3l commented May 10, 2016

Second change for the example how to combine position and positionOffset to make it more general.

The functions provided now are corrected in namings and usable for general record components even outside of the particle scope (e.g., one can use them to read scalar and vector mesh components).

I still did not add doc strings since I try not to blow up the inline example since it is already pretty large.

outdated: The allocate option is quite of a fancy thing in case one wants to transparently allocate an ndarray to access it via [] later on but could be removed.

Otherwise I can also keep it like this:

def get_component(group, component_name):
    record_component = group[component_name]
    unitSI = record_component.attrs["unitSI"]

    if is_const_record_component(record_component):
        return record_component.attrs["value"], unitSI
    else:
        return record_component.value, unitSI

instead of

def get_component(group, component_name, allocate=False):
    record_component = group[component_name]
    unitSI = record_component.attrs["unitSI"]

    if is_const_component(record_component):
        cvalue = record_component.attrs["value"]
        if allocate:
            return cvalue * \
                numpy.ones(shape=record_component.attrs["shape"],
                           dtype=record_component.attrs["value"].dtype),
                unitSI
        else:
            return cvalue, unitSI
    else:
        return record_component.value, unitSI

update: yes, that is probably better... too fancy and not necessary to know for most people I guess...

@ax3l ax3l added the revision change backwards-compatible, stylistic change (e.g. typos) label May 10, 2016
@ax3l ax3l added this to the 1.0.1: Typo and Wording Changes milestone May 10, 2016
Second change for the example how to combine `position`
and `positionOffset` to make it more general.

The functions provided now are corrected in namings and usable
for general `record components` even outside of the `particle`
scope.

I still did not add doc strings since I try not to blow up the
example since it is already pretty large. The `allocate` option
is quite of a fancy thing in case one wants to transparently
allocate an ndarray to access it via `[]` later on but could
be removed.
@ax3l ax3l force-pushed the topic-refactorPositionReadExamplePy branch from 71b1287 to 70ea1d8 Compare May 10, 2016 17:42
But again, allocating a `numpy.ndarray` can be as simple
as doing:
```python
data = record_component.attrs["value"] * \
       numpy.ones(shape=record_component.attrs["shape"],
                  dtype=record_component.attrs["value"].dtype)
```

on a **constant** `record component` so it looks *exactly* as if
one *would* have read a non-constant `record component`.
@ax3l ax3l force-pushed the topic-refactorPositionReadExamplePy branch from 70ea1d8 to fa59d86 Compare May 10, 2016 17:44
@RemiLehe
Copy link
Member

Great. Thanks for the update.
I also prefer it without the allocate option.

@RemiLehe RemiLehe merged commit 34efb24 into openPMD:upcoming-1.0.1 May 10, 2016
@ax3l ax3l deleted the topic-refactorPositionReadExamplePy branch May 11, 2016 08:59
@ax3l ax3l removed this from the 1.0.1: Typo and Wording Changes milestone Nov 24, 2017
@ax3l ax3l mentioned this pull request Dec 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
revision change backwards-compatible, stylistic change (e.g. typos)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants