pythondebuggingsom

Debugger only shows Python bytecode when debugging SOMPY library functions


I am trying to set up a self-organizing map (SOM) with the python library SOMPY right now. I made some changes to the original code since I would like to implement an alternative method for the computation of the best-matching unit (BMU). However I found that these changes had no effect and when trying to step into the library with the debugger I ended up in a file with unfamiliar formatting.

To investigate this issue, I tried to step into the SOMFactory.build() method to see why my changes were not executed. Below is a minimal example:

import sompy 
import numpy as np 

  
test = sompy.SOMFactory.build(np.ones((20, 3)))

I anticipated to end up in the definition of the SOMFactory class

class SOMFactory(object):

    @staticmethod
    def build(data,
              mapsize=None,
              mask=None,
              mapshape='planar',
              lattice='rect',
              normalization='var',
              initialization='pca',
              neighborhood='gaussian',
              training='batch',
              name='sompy',
              component_names=None):
        """
        :param data: data to be clustered, represented as a matrix of n rows,
            as inputs and m cols as input features
        :param neighborhood: neighborhood object calculator.  Options are:
            - gaussian
            - bubble
            - manhattan (not implemented yet)
            - cut_gaussian (not implemented yet)
            - epanechicov (not implemented yet)

        :param normalization: normalizer object calculator. Options are:
            - var

        :param mapsize: tuple/list defining the dimensions of the som.
            If single number is provided is considered as the number of nodes.
        :param mask: mask
        :param mapshape: shape of the som. Options are:
            - planar
            - toroid (not implemented yet)
            - cylinder (not implemented yet)

        :param lattice: type of lattice. Options are:
            - rect
            - hexa

        :param initialization: method to be used for initialization of the som.
            Options are:
            - pca
            - random

        :param name: name used to identify the som
        :param training: Training mode (seq, batch)
        """
        
        print("I am in the function! ")
        
        if normalization:
            normalizer = NormalizerFactory.build(normalization)
        else:
            normalizer = None
        neighborhood_calculator = NeighborhoodFactory.build(neighborhood)
        return SOM(data, neighborhood_calculator, normalizer, mapsize, mask,
                   mapshape, lattice, initialization, training, name, component_names)

but instead I ended up in a file called sompy.py showing only this:


LOAD_FAST(normalization), POP_JUMP_IF_FALSE{16}
LOAD_GLOBAL(NormalizerFactory), LOAD_METHOD(build), LOAD_FAST(normalization), CALL_METHOD{1}, STORE_FAST(normalizer), JUMP_FORWARD(to 20)

normalizer = |16|None
LOAD_GLOBAL(NeighborhoodFactory), LOAD_METHOD(build), LOAD_FAST(neighborhood), CALL_METHOD{1}, STORE_FAST(neighborhood_calculator)
return SOM(data, neighborhood_calculator, normalizer, mapsize, mask, 
mapshape, lattice, initialization, training, name, component_names)

It is a Python file, but the formatting looks unfamiliar to me. I was wondering what this code is and what I need to change such that changes in the file were the SOMFactory class is defined actually take affect.


Solution

  • I have found a solution to the problem: the file the debugger steps into is python bytecode and it does this since the source files are not available. To make sure that the source code is available, I went into the directory of the library, uninstalled it and then reinstalled it with a flag telling pip to install the package in editable mode:

    pip uninstall sompy 
    pip install -e .