0

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!

Wupppa
  • 37
  • 7
  • it looks like `row` is a float and not path string. Check your `df_cube['img_path']` – Lior Cohen Jan 03 '21 at 17:05
  • 1
    What's in `df_cube['img_path']`? You are failing on the line `image = Image.open(row).convert('RGB')` and I can get the same error by doing `Image.open(1.0)`. If the `fp` parameter isn't a string or Path object, the code assumes its going to be a file object and just gives it a go. – tdelaney Jan 03 '21 at 17:06
  • df_cube is the data of a csv file (df_cube = pd.read_csv...), and within this file 'img_path' is a column with the absolut paths to the images. I wanted to open those images and convert them to RGB but I don't know why the code does not work... So the row value is actually a string/path. So I don't know why this issue appears. – Wupppa Jan 03 '21 at 17:17
  • better check `print(row)` before `image = Image.open(row).convert('RGB')` because it seems `row` is not what you expect. I'm not sure if `DataFrame` works like `list` - maybe in `for`-loop it gives index instead of expected value. Maybe it will need `.iterow()` or something similar. OR maybe it read data incorrectly and now you have numbers in this column - or at least one number in some row. – furas Jan 03 '21 at 20:06
  • And yes, you are right, there is actually one number in every path string which I cannot remove since it is indicating the count of a certain configuration. How can I make sure that row always gets the right path to the images? Hope you can help me with that! Thanks! – Wupppa Jan 04 '21 at 11:33

1 Answers1

0

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!

Wupppa
  • 37
  • 7