Skip to content

Consider using type hints in function definitions to indicate types of variables and returned data #20

@alexpreynolds

Description

@alexpreynolds

def s1Calc(file1Path, file2Path, rowsToCalc, numStates, verbose):
"""
Function responsible for expected frequency calculation over a set of rows for a saliency metric of 1
Input:
file1Path -- The path of the only (single epilogos) or first (paired epilogos) file to read states from
file2Path -- The path of the second file to read states from (paired epilogos)
rowsToCalc -- The rows to count expected frequencies from the files
numStates -- The number of states in the state model
verbose -- Boolean which if True, causes much more detailed prints
Output:
A numpy array containing the counts of each state within the specified rows of the file
"""
dataArr = readStates(file1Path=file1Path, file2Path=file2Path, rowsToCalc=rowsToCalc, verbose=verbose)
expFreqArr = np.zeros(numStates, dtype=np.int32)
if verbose and rowsToCalc[0] == 0: print("Calculating expected frequencies...", flush=True); tExp = time()
# Simply count all states across out our subset of data
uniqueStates, stateCounts = np.unique(dataArr, return_counts=True)
for i, state in enumerate(uniqueStates):
expFreqArr[state] += stateCounts[i]
if verbose and rowsToCalc[0] == 0: print(" Time:", time() - tExp, flush=True)
return expFreqArr

Type hints can be passed into documentation and linter scripts to automate writing docs and checking the types of data being passed around, including Python primitives (str, int, etc.) and numpy data:

https://www.python.org/dev/peps/pep-0484/

https://numpy.org/devdocs/reference/typing.html

This can also be useful to read the function at a glance, to see what goes in and out, and help with debugging.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions