pythonjupyterattribution

How to avoid AttributionError in Jupyter when converting image file?


I'm facing the problem that my code keeps throwing the AttributionError in my Jupyter Notebook, even though I don't even call the function that is specified as faulty. My python code is as follows:

 import numpy as np
 import matplotlib
 matplotlib.use('nbagg')
 import matplotlib.pyplot as plt
 import seaborn as sns
 import pandas as pd 
 import os
 from PIL import Image

cube_features = []
 for cube in cubes:
df_cube = pd.read_csv(cube_base + 'props_cube{}.csv'.format(cube),
                        names=['cube', 'phi_x', 'phi_y', 'fir_x', 'fir_y', 'bush_x', 'bush_y', 'sun_x', 'sun_y', 'poplar_x', 'poplar_y', 'img_path'])
area_features = [] 
for area in areas:
    if area == 'Moc':
        features = []
        for row in df_cube['img_path']:
            image = Image.open(row).convert('RGB')
            features.append(np.array(image).flatten())
        features = np.array(features)
    else:
        features_dict = np.atleast_1d(np.load('/Users/tol/Features/CORnetZ/cube{}_CORnet-Z_{}_output_feats.npy'.format(cube, area), allow_pickle=True))[0]
        if not all(np.array(features_dict['fnames']) == df_cube['img_path'].values): 
            raise ValueError('My Error')
        features = features_dict['model_feats']
    print('Cube {}, Area {} - Representation size {}'.format(cube, area, features.shape))
    #area_corr = pd.DataFrame(features.transpose()).corr() # num_images x num_images
    area_features.append(features)
cube_features.append(area_features)

My Error code is:

    AttributeError                            Traceback (most recent call last)
  /opt/anaconda3/lib/python3.8/site-packages/PIL/Image.py in open(fp, mode, formats)
   2894     try:
 -> 2895         fp.seek(0)
   2896     except (AttributeError, io.UnsupportedOperation):

    AttributeError: 'float' object has no attribute 'seek'

    During handling of the above exception, another exception occurred:

    AttributeError                            Traceback (most recent call last)
    <ipython-input-43-31c8f89d4a7f> in <module>
       8             features = []
       9             for row in df_cube['img_path']:
     ---> 10                 image = Image.open(row).convert('RGB')
      11                 features.append(np.array(image).flatten())
      12             features = np.array(features)

    /opt/anaconda3/lib/python3.8/site-packages/PIL/Image.py in open(fp, mode, formats)
     2895         fp.seek(0)
     2896     except (AttributeError, io.UnsupportedOperation):
     -> 2897         fp = io.BytesIO(fp.read())
     2898         exclusive_fp = True
     2899 

     AttributeError: 'float' object has no attribute 'read'

I really hope someone can help me with this! Thanks!


Solution

  • The answer to my question was very simple: I forget to indicate a delimiter when reading the csv file so the data got not saved properly. "Row" were pointing to an empty column.

    Always print what you've read in.

    Thanks for the help!