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

UnboundLocalError: local variable 'topEdge' referenced before assignment #48

Open
alexsomoza opened this issue Apr 4, 2023 · 3 comments

Comments

@alexsomoza
Copy link

alexsomoza commented Apr 4, 2023

My dataframe:

  Cerrada | Text Analytics | Cantidad | Correctas | Erroneas | Asertividad
-- | -- | -- | -- | -- | -- | -- | --
Sin Anomalias | CR | 103 | 71 | 32 | 68.932039
Sin Anomalias | FR | 223 | 81 | 142 | 36.322870
Sin Anomalias | NC | 295 | 257 | 38 | 87.118644
Sin Anomalias | SVP | 2186 | 2053 | 133 | 93.915828
Sin Anomalias | SA | 5946 | 5578 | 368 | 93.810965
Sin Anomalias | SD | 455 | 217 | 238 | 47.692308
Sin Anomalias | TC | 77 | 8 | 69 | 10.389610
Sin Anomalias | YR | 49 | 46 | 3 | 93.877551

My code:

import matplotlib.pyplot as plt
from pysankey import sankey
import matplotlib.pyplot as plt
from pysankey import sankey

weight = df['Asertividad'].values[1:].astype(float)

sankey(left=df['Cerrada'].values[1:], right=df['Text Analytics'].values[1:], rightWeight=weight, leftWeight=weight, aspect=20, fontsize=20)

plt.show() # to display

The error thrown:

UnboundLocalError Traceback (most recent call last)
in
4 weight = df['Asertividad'].values[1:].astype(float)
5
----> 6 sankey(left=df['Cerrada'].values[1:], right=df['Text Analytics'].values[1:], rightWeight=weight, leftWeight=weight, aspect=20, fontsize=20)
7
8 plt.show() # to display

~\AppData\Roaming\Python\Python37\site-packages\pysankey\sankey.py in sankey(left, right, leftWeight, rightWeight, colorDict, leftLabels, rightLabels, aspect, rightColor, fontsize, ax)
192 # Determine positions of left label patches and total widths
193 leftWidths, topEdge = _get_positions_and_total_widths(
--> 194 dataFrame, leftLabels, 'left', aspect)
195
196 # Determine positions of right label patches and total widths

~\AppData\Roaming\Python\Python37\site-packages\pysankey\sankey.py in _get_positions_and_total_widths(df, labels, side, aspect)
296 LOGGER.debug("%s position of '%s' : %s", side, label, labelWidths)
297
--> 298 return widths, topEdge

UnboundLocalError: local variable 'topEdge' referenced before assignment

Thanks in advance !!!

@Pierre-Sassoulas
Copy link
Contributor

Use pysankeybeta from my fork :)

@Flying-Doggy
Copy link

Flying-Doggy commented Sep 19, 2023

I met the same problem. And I think this error happens when the giving label list only contains one element. In your case, the left array only contains "Sin Anomalias", which causes the 'topEdge' cann't be defined. You can add " topEdge = labelWidths["top"] " under "if i == 0:"(line 286) block to solve this problem

if i == 0: 
            labelWidths["bottom"] = 0
            labelWidths["top"] = labelWidths[side]
            topEdge = labelWidths["top"]   # you can add this commend here, then the script can successfully run
else:
            bottomWidth = widths[labels[i - 1]]["top"]
            weightedSum = aspect / 200 * df[side + "Weight"].sum()
            labelWidths["bottom"] = bottomWidth + weightedSum
            labelWidths["top"] = labelWidths["bottom"] + labelWidths[side]
            topEdge = labelWidths["top"]

@alexsomoza
Copy link
Author

Thanks Flying-Doggy.
I will try this !!!!

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

3 participants