Skip to content

common module

JEncoder (JSONEncoder)

Source code in toomanycells/common.py
class JEncoder(json.JSONEncoder):
    def default(self, x):
        if isinstance(x, np.bool_):
            return bool(x)
        elif isinstance(x, np.integer):
            return int(x)
        elif isinstance(x, np.floating):
            return float(x)
        elif isinstance(x, np.ndarray):
            return x.tolist()
        else:
            return super().default(x)

default(self, x)

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this::

1
2
3
4
5
6
7
8
9
def default(self, o):
    !!! try
        iterable = iter(o)
    except TypeError:
        pass
    !!! else
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
Source code in toomanycells/common.py
def default(self, x):
    if isinstance(x, np.bool_):
        return bool(x)
    elif isinstance(x, np.integer):
        return int(x)
    elif isinstance(x, np.floating):
        return float(x)
    elif isinstance(x, np.ndarray):
        return x.tolist()
    else:
        return super().default(x)

MultiIndexList (list)

This class is derived from the list class. It allows the use of iterables to access the list. For example: L[(1,2,0)] will access item #1 of the list, then item #2 of the previously retrieved item, and finally item #0 of that last item. We use this class to store the TooManyCells tree in a structure composed of nested lists and dictionaries.

Source code in toomanycells/common.py
class MultiIndexList(list):
    """
    This class is derived from the list class.\
        It allows the use of iterables to \
        access the list. For example: \
        L[(1,2,0)] will access item #1 \
        of the list, then item #2 of the \
        previously retrieved item, and \
        finally item #0 of that last item.\
        We use this class to store the \
        TooManyCells tree in a structure \
        composed of nested lists and dictionaries.
    """
    #=================================================
    def __getitem__(self, indices: Union[list,int]):
        """
        This implementation of the __getitem__ method \
            allows the possibility of indexing a nested \
            list with a list of integers.
        """

        if hasattr(indices, '__iter__'):
            #If the indices object is iterable
            #then traverse the list using the indices.
            obj = self
            for index in indices:
                obj = obj[index]
            return obj
        else:
            #Otherwise, just use the __getitem__ 
            #method of the parent class.
            return super().__getitem__(indices)

__getitem__(self, indices) special

This implementation of the getitem method allows the possibility of indexing a nested list with a list of integers.

Source code in toomanycells/common.py
def __getitem__(self, indices: Union[list,int]):
    """
    This implementation of the __getitem__ method \
        allows the possibility of indexing a nested \
        list with a list of integers.
    """

    if hasattr(indices, '__iter__'):
        #If the indices object is iterable
        #then traverse the list using the indices.
        obj = self
        for index in indices:
            obj = obj[index]
        return obj
    else:
        #Otherwise, just use the __getitem__ 
        #method of the parent class.
        return super().__getitem__(indices)

load_metadata_for_demo()

This function loads the cell indices and labels for the demo file.

Source code in toomanycells/common.py
def load_metadata_for_demo()-> pd.DataFrame:
    """
    This function loads the cell indices and \
    labels for the demo file.
    """
    fname = os.path.dirname(__file__)
    fname = os.path.join(fname, "data")
    fname = os.path.join(fname, "metadata.csv")
    df = pd.read_csv(fname, index_col = 0)
    return df