spacetx / starfish

starfish: unified pipelines for image-based transcriptomics
https://spacetx-starfish.readthedocs.io/en/latest/
MIT License
225 stars 67 forks source link

barcodeBuildFunc modifies external variables #2007

Open guoyang-github opened 10 months ago

guoyang-github commented 10 months ago

Dear Starfish developers, Firstly, thank you for creating such a useful tool for decode analysis. I have been using the barcodeBuildFunc function and noticed a piece of code related to the modification of external variables within the function.

def barcodeBuildFunc(allNeighbors: list, currentRound: int, roundOmitNum: int, roundNum: int) -> list:

    allSpotCodes = []

    for neighbors in allNeighbors:
        neighborLists = [neighbors[rnd] for rnd in range(roundNum)]
        if roundOmitNum > 0:
            [neighbors[rnd].append(0) for rnd in range(roundNum)]
        codes = list(product(*neighborLists))

In the current implementation of barcodeBuildFunc, the function modifies the neighbors object, which refers to an external variable, and in turn affects upstream variables neighborLists. I'm not sure whether it will lead to unintended side effects. If I change the code to the following form, I wonder if it makes sense?

def barcodeBuildFunc_revised(allNeighbors: list, currentRound: int, roundOmitNum: int, roundNum: int) -> list:

    allSpotCodes = []

    for neighbors in allNeighbors:
        if roundOmitNum > 0:
            neighbors = [neighbors[rnd] + [0] for rnd in range(roundNum)]
        neighborLists = [neighbors[rnd] for rnd in range(roundNum)]
        codes = list(product(*neighborLists))

Thanks for your help.

guoyang-github commented 10 months ago

The permalink to the code: https://github.com/spacetx/starfish/blob/master/starfish/core/spots/DecodeSpots/check_all_funcs.py