On Wed, Jul 02, 2014 at 01:04:32PM +0200, Mark S. Bentley wrote:
> I have a (hopefully) straightforward question. I have all of my AFM 
> image and meta data in python (numpy array and dictionary) and want to 
> simply write to a Gwyddion native file. I have imported the gwy module, 
> created a new container and DataField object (initialise to the correct 
> size/number of pixels), set the meta data and channel title etc. 
> However, I can't find an easy way to populate the contents of the data 
> itself...
> 
> DataField.get_data() works fine, and returns the initialised zero 
> values. There is a function DataField.set_data() on inspecting the 
> object, but this is not listed in the API and I can't get it to work.
> 
> There was a similar question on the list a while back, which was solved 
> by using gwyutils. But I guess this only works inside of Gwyddion, and I 
> need something that will run standalone...

No, it should work standalone also if you have gwyutils in the Python
path:

    import gwy, gwyutils

    d = gwy.DataField(200, 200, 1.0, 1.0, False)
    a = gwyutils.data_field_data_as_array(d)
    # Can also use numpy array functions here
    for i in range(200):
        for j in range(200):
           a[i][j] = i-j

    c = gwy.Container()
    c['/0/data'] = d

    gwy.gwy_file_save(c, 'z.gwy', gwy.RUN_NONINTERACTIVE)

> Calling DataField.set_val() in a loop works fine - is this the preferred 
> python option? The docs talk about using get_data() and referencing the 
> returned buffer, but I guess this comes directly from the underlying C 
> docs and is not relevant here..

If you have numpy then data_field_data_as_array() is better.  The data
access is faster and you have high-level data operations available from
numpy.  Otherwise set_val() is the way to modify data.

One caveat if you modify the data directly with numpy – it is necessary
to call d.invalidate() afterwards if you do continue doing anything
nontrivial with the field (just saving is OK).  Otherwise various
functions can use stale cached information about properties of the
field, such as minimum and maximum values, leading to all kinds of odd
behaviour.

Unfortunately, gwy_data_field_invalidate() is a macro in C so it is not
actually possible to call d.invalidate() at this moment.  I will have to
fix that by providing it also as a function that will be automatically
translated to Python.

Regards,

Yeti


------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Gwyddion-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gwyddion-users

Reply via email to