alan-turing-institute / pixelflow

BSD 3-Clause "New" or "Revised" License
5 stars 4 forks source link

Error when running pixelflow for a mask with no labels #37

Closed evangeline-corcoran closed 10 months ago

evangeline-corcoran commented 10 months ago

Attempting to run pixelflow on a mask with no labels produces the following error:

Cell In[7], line 10
---> 10     features = pixelflow(
     11         mask,
     12         image,
     13         #custom=(custom_func,),
     14     ) #create table with metrics for each seed
     15     features_csv = features.to_csv('/bask/homes/l/lwcf1795/vjgo8416-scivision/plant-phenotyping/3D_seed_biodiversity_data_200723/first and second rep/' + pod_name + '.csv', 
     16                                    header=True, index=False) #save metrics for all seeds in a pod to csv

File ~/src/pixelflow/src/pixelflow/core.py:200, in pixelflow(mask, image, features, custom, dim_labels, labelled)
    198 # calculate the 3D features
    199 features_dat3d = regionprops_3D(mask)
--> 200 features_df3d = props_to_DataFrame(features_dat3d)
    202 # if only certain features are requested, then filter the dataframe
    203 if features is not None:

File /bask/projects/v/vjgo8416-scivision/isq_conv_conda_env/lib/python3.9/site-packages/porespy/metrics/_regionprops.py:64, in props_to_DataFrame(regionprops)
     62 # Parse the regionprops list and pull out all props with scalar values
     63 metrics = []
---> 64 reg = regionprops[0]
     65 for item in reg.__dir__():
     66     if not item.startswith('_'):

IndexError: list index out of range

I've verified that this was in fact a seed pod with 0 seeds, and therefore my seed detection model correctly produced a mask with 0 labels.

This is a problem mostly when running pixelflow over multiple images, as this error interrupts the automated loop.

The behaviour I would like to see is pixelflow skipping over a mask with 0 labels when going through a loop of files, or better yet flagging the empty mask with a print out i.e. 'mask_name.npy has 0 labels'

IFenton commented 10 months ago

Happy to look in to this - it sounds like a should be easy to solve. Could you send me an example mask so I can test the fix?

evangeline-corcoran commented 10 months ago

Thanks I've sent you an example empty mask

IFenton commented 10 months ago

Addressing this in IFenton-37-BugFix

evangeline-corcoran commented 10 months ago

I can now successfully automatically create pixelflow 'features' objects for multiple files even if some mask files contain 0 labels, however attempting to also output .csv files with the features information as part of a loop over multiple files as below:

for file in X:
    image = imread(file) #load whole pod image
    pod_name = re.split("[/.]", file)[9] #save pod name for outputs
    mask = np.load('/bask/homes/l/lwcf1795/vjgo8416-scivision/plant-phenotyping/3D_seed_biodiversity_data_200723/first and second rep/' + pod_name + '_pred.npy') #load whole pod label for metrics calculations
    features = pixelflow(
        mask,
        image,
        #custom=(custom_func,),
        ) #create table with metrics for each seed
    features_csv = features.to_csv('/bask/homes/l/lwcf1795/vjgo8416-scivision/plant-phenotyping/3D_seed_biodiversity_data_200723/first and second rep/' + pod_name + '.csv', 
                                    header=True, index=False) #save metrics for all seeds in a pod to csv

Now gives the following error for files in the list with empty masks

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[6], line 14
      8 mask = np.load('/bask/homes/l/lwcf1795/vjgo8416-scivision/plant-phenotyping/3D_seed_biodiversity_data_200723/first and second rep/' + pod_name + '_pred.npy') #load whole pod label for metrics calculations
      9 features = pixelflow(
     10     mask,
     11     image,
     12     #custom=(custom_func,),
     13     ) #create table with metrics for each seed
---> 14 features_csv = features.to_csv('/bask/homes/l/lwcf1795/vjgo8416-scivision/plant-phenotyping/3D_seed_biodiversity_data_200723/first and second rep/' + pod_name + '.csv', 
     15                                 header=True, index=False) #save metrics for all seeds in a pod to csv

AttributeError: 'NoneType' object has no attribute 'to_csv'
evangeline-corcoran commented 10 months ago

Adding if features is not None to the for loop before attempting to output a csv of the features table as shown below prevents this problem

for file in X:
    image = imread(file) #load whole pod image
    pod_name = re.split("[/.]", file)[9] #save pod name for outputs
    image = normalize(image, 1,99.8, axis=axis_norm) #normalize image. NOTE: normalizer MUST be defined in step 3 and supplied here for prediction to function as expected
    mask = np.load('/bask/homes/l/lwcf1795/vjgo8416-scivision/plant-phenotyping/3D_seed_biodiversity_data_200723/first and second rep/' + pod_name + '_pred.npy') #load whole pod label for metrics calculations
    features = pixelflow(
        mask,
        image,
        #custom=(custom_func,),
        ) #create table with metrics for each seed
    if features is not None:
        features_csv = features.to_csv('/bask/homes/l/lwcf1795/vjgo8416-scivision/plant-phenotyping/3D_seed_biodiversity_data_200723/first and second rep/' + pod_name + '.csv', 
                                        header=True, index=False) #if a pod has seeds, save metrics for all seeds in a pod to csv
IFenton commented 10 months ago

@evangeline-corcoran Is this now resolved?

evangeline-corcoran commented 10 months ago

Yes I'm happy with this solution