KarrLab / obj_tables

Tools for creating and reusing high-quality spreadsheets
https://objtables.org
MIT License
8 stars 2 forks source link

'NumpyArrayAttribute' object has no attribute 'shape' #94

Closed paulflang closed 4 years ago

paulflang commented 4 years ago

When trying to validate that two obj_tables.obj_math.NumpyArrayAttribute are of same shape, I got the error that they do not have an attribute shape. What am I doing wrong.

jonrkarr commented 4 years ago

I think this is a misunderstanding of how objtables works. The values have a shape attribute. Instances of NumpyArrayAttribute do not have a shape attribute.

On Sat, Sep 28, 2019, 9:07 PM paulflang notifications@github.com wrote:

When trying to validate that two obj_tables.obj_math.NumpyArrayAttribute are of same shape, I got the error that they do not have an attribute shape. What am I doing wrong.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/KarrLab/obj_tables/issues/94?email_source=notifications&email_token=AAVXMKNULXO7TWWQ2XTXK5DQL75TXA5CNFSM4I3QG3HKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HOK52XQ, or mute the thread https://github.com/notifications/unsubscribe-auth/AAVXMKOZ2EESEQ2IXCI6L2LQL75TXANCNFSM4I3QG3HA .

paulflang commented 4 years ago

In the obj_tables/test_math.py it looks as if the values could be accessed with the get_default method. However in wc_kb/core.py I specified for instance

class TimeCourseEvidence(Evidence): ... def validate(self):
print(self.Meta.attributes['valuestce'].__dir_\()) print(self.Meta.attributes['values_tce'].get_default()) print(self.Meta.attributes['values_tce'].default) print(self.Meta.attributes['values_tce'].default_cleaned_value)

with the stdout of the latter three returning None when running wc_kb/tests/test_io.py test_read_write_eukaryote, despite the corresponding excel cell contains a value and the test passes.

jonrkarr commented 4 years ago

These are methods on the meta-model (schema), rather than methods on the data objects. The get_default method returns the default value of the attribute (the value that is used when a value is not specified when an instance is constructed). default_cleaned_value is the default value that is used when the value of an attribute is loaded from an empty cell in an Excel worksheet.

The values of the attributes for each model instance can be accessed as exemplified below:

import numpy
import obj_tables
import obj_tables.obj_math

class TimeCourseEvidence(obj_tables.Model):
    data = obj_tables.obj_math.NumpyArrayAttribute()

evidence = TimeCourseEvidence(data=numpy.array([[1, 2], [3, 4], [5, 6]]))

type(evidence.data)
# >> numpy.ndarray

evidence.data.shape
# >> (3, 2)

Basically, obj_tables uses metaprogramming to separate the meta-model (schema) from the instances of the data classes. This makes it relatively easy to build powerful schemas.

paulflang commented 4 years ago

Thanks Jonathan, but I was trying to implement validation within the validate method of TimeCourseEvidence.

So doing

class TimeCourseEvidence(Evidence):
    ...
    values_tce = obj_tables.obj_math.NumpyArrayAttribute()

    def validate(self):
        ...
        print(self.values_tce)
        print(self.values_tce.shape)

still throws the error AttributeError: 'NoneType' object has no attribute 'shape', despite I have added a value in wc_kb/tests/fixtures/eukaryote_core.xlsx in the corresponding column.

jonrkarr commented 4 years ago

This should work. This is different from the first code fragment, which accessed self.Meta... rather than self.values_tce.