histocartography / patho-quant-explainer

Code for replicating results presented in the paper: "Quantifying Explainers of Graph Neural Networks in Computational Pathology"
MIT License
43 stars 7 forks source link

try-except clauses hide unintended behavior during graph creation #9

Open CarlinLiao opened 1 year ago

CarlinLiao commented 1 year ago

Consider the following code block from generate_cell_graphs.py:

    for image_path in tqdm(image_fnames):

        # ... excised for brevity

        # if file was not already created + not too big + not too small, then process 
        if not os.path.isfile(out_fname) and nr_pixels > MIN_NR_PIXELS and nr_pixels < MAX_NR_PIXELS:

            # ...

            # e. build a kNN graph
            try:
                graph = knn_graph_builder.process(nuclei_map, features)
            except:
                print('Warning: {} failed during kNN graph building.'.format(image_path))
                image_ids_failing.append(image_path)
                pass

            # ...

            # g. save the graph
            image_label = TUMOR_TYPE_TO_LABEL[image_name.split('_')[2]]
            save_graphs(
                filename=out_fname,
                g_list=[graph],
                labels={"label": torch.tensor([image_label])}
            )

If the knn_graph_builder section errors, graph will not be generated, which should cause save_graphs to error because graph is contained in one of its parameters. In practice, it usually doesn't because as long as one of the images earlier in the loop through image_fnames had a successful knn graph build, it'll recycle that graph from the last image instead of the target image.

Although this is just one example, this is likely true of most if not all of the try-except clauses in generate_cell_graphs.py. Broadly, I don't think any of graph creation steps should be wrapped in try-except clauses at all.