Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alexpreynolds opened this issue Mar 26, 2021 · 1 comment

Comments

@alexpreynolds
Copy link
Contributor

alexpreynolds commented Mar 26, 2021

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.

@jacobquon
Copy link
Collaborator

Will look into this. Definitely seems useful for the final push to Master. Probably will prioritize other things until then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants