dattalab / keypoint-moseq

https://keypoint-moseq.readthedocs.io
Other
63 stars 25 forks source link

UnboundLocalError when plotting transition matrices #119

Closed mjin1812 closed 6 months ago

mjin1812 commented 6 months ago

Hi Caleb,

I'm following the statistical analysis functions documented on the KPMS tutorial. However, when I try to plot a transition matrix of my data, I'm getting an UnboundLocalError in the analysis.py file.

kpms.visualize_transition_bigram(
    project_dir, model_name, groups, trans_mats, syll_include, normalize=normalize, 
    show_syllable_names=True # label syllables by index (False) or index and name (True)U)

Error Message

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
Cell In[72], line 10
      4 trans_mats, usages, groups, syll_include=kpms.generate_transition_matrices(
      5     project_dir, model_name, normalize=normalize,
      6     min_frequency=0.005 # minimum syllable frequency to include
      7 )    
      9 groups=['sundown_ctrl_male_24','sundown_AD_male_24']
---> 10 kpms.visualize_transition_bigram(
     11     project_dir, model_name, groups, trans_mats, syll_include, normalize=normalize, 
     12     show_syllable_names=True # label syllables by index (False) or index and name (True)
     13 )

File ~\.conda\envs\keypoint_moseq\lib\site-packages\keypoint_moseq\analysis.py:1459, in visualize_transition_bigram(project_dir, model_name, group, trans_mats, syll_include, save_dir, normalize, figsize, show_syllable_names)
   1442 """Visualize the transition matrices for each group.
   1443 
   1444 Parameters
   (...)
   1456     names (True)
   1457 """
   1458 if show_syllable_names:
-> 1459     syll_names = get_syllable_names(project_dir, model_name, syll_include)
   1460 else:
   1461     syll_names = [f"{ix}" for ix in syll_include]

File ~\.conda\envs\keypoint_moseq\lib\site-packages\keypoint_moseq\analysis.py:48, in get_syllable_names(project_dir, model_name, syllable_ixs)
     45     syll_info_df = pd.read_csv(syll_info_path, index_col=False).fillna("")
     47 for ix in syllable_ixs:
---> 48     if len(syll_info_df[syll_info_df.syllable == ix].label.values[0]) > 0:
     49         labels[
     50             ix
     51         ] = f"{ix} ({syll_info_df[syll_info_df.syllable == ix].label.values[0]})"
     52 names = [labels[ix] for ix in syllable_ixs]

UnboundLocalError: local variable 'syll_info_df' referenced before assignment

Best, Michelle

versey-sherry commented 6 months ago

Hi Michelle, did you happen to not run the notebooks linearly? For now, the fix could be running kpms.label_syllables(project_dir, model_name, moseq_df) before you run the visualize bigram.

mjin1812 commented 6 months ago

Dear Sherry,

No I didn't try that! I didn't realize that kpms.label_syllables() needed to be run prior. Based on the documentation, I thought that was an optional step.

After trying that, I have now run into a different error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[36], line 1
----> 1 kpms.plot_syll_stats_with_sem(
      2     stats_df, project_dir, model_name,
      3     plot_sig=True,    # whether to mark statistical significance with a star
      4     thresh=0.05,      # significance threshold
      5     stat='frequency', # statistic to be plotted (e.g. 'duration' or 'velocity_px_s_mean')
      6     order='stat',     # order syllables by overall frequency ("stat") or degree of difference ("diff")
      7     ctrl_group='sundown_ctrl_male_24',   # name of the control group for statistical testing
      8     exp_group='sundown_AD_male_24',    # name of the experimental group for statistical testing
      9     figsize=(8, 4),   # figure size    
     10     groups=['sundown_ctrl_male_24','sundown_AD_male_24'], # groups to be plotted
     11 );

File ~\.conda\envs\keypoint_moseq\lib\site-packages\keypoint_moseq\analysis.py:1210, in plot_syll_stats_with_sem(stats_df, project_dir, model_name, save_dir, plot_sig, thresh, stat, order, groups, ctrl_group, exp_group, colors, join, figsize)
   1207     plt.scatter(markings, [-0.005] * len(markings), color="r", marker="*")
   1209     # manually define a new patch
-> 1210     patch = Line2D(
   1211         [],
   1212         [],
   1213         color="red",
   1214         marker="*",
   1215         linestyles="None",
   1216         markersize=9,
   1217         label="Significant Syllable",
   1218     )
   1219     handles.append(patch)
   1221 # add legend and axis labels

File ~\.conda\envs\keypoint_moseq\lib\site-packages\matplotlib\lines.py:407, in Line2D.__init__(self, xdata, ydata, linewidth, linestyle, color, gapcolor, marker, markersize, markeredgewidth, markeredgecolor, markerfacecolor, markerfacecoloralt, fillstyle, antialiased, dash_capstyle, solid_capstyle, dash_joinstyle, solid_joinstyle, pickradius, drawstyle, markevery, **kwargs)
    403 self.set_markeredgewidth(markeredgewidth)
    405 # update kwargs before updating data to give the caller a
    406 # chance to init axes (and hence unit support)
--> 407 self._internal_update(kwargs)
    408 self.pickradius = pickradius
    409 self.ind_offset = 0

File ~\.conda\envs\keypoint_moseq\lib\site-packages\matplotlib\artist.py:1219, in Artist._internal_update(self, kwargs)
   1212 def _internal_update(self, kwargs):
   1213     """
   1214     Update artist properties without prenormalizing them, but generating
   1215     errors as if calling `set`.
   1216 
   1217     The lack of prenormalization is to maintain backcompatibility.
   1218     """
-> 1219     return self._update_props(
   1220         kwargs, "{cls.__name__}.set() got an unexpected keyword argument "
   1221         "{prop_name!r}")

File ~\.conda\envs\keypoint_moseq\lib\site-packages\matplotlib\artist.py:1193, in Artist._update_props(self, props, errfmt)
   1191             func = getattr(self, f"set_{k}", None)
   1192             if not callable(func):
-> 1193                 raise AttributeError(
   1194                     errfmt.format(cls=type(self), prop_name=k))
   1195             ret.append(func(v))
   1196 if ret:

AttributeError: Line2D.set() got an unexpected keyword argument 'linestyles'
mjin1812 commented 6 months ago

To give more context, this is when I run

kpms.plot_syll_stats_with_sem(
    stats_df, project_dir, model_name,
    plot_sig=True,    # whether to mark statistical significance with a star
    thresh=0.05,      # significance threshold
    stat='frequency', # statistic to be plotted (e.g. 'duration' or 'velocity_px_s_mean')
    order='stat',     # order syllables by overall frequency ("stat") or degree of difference ("diff")
    ctrl_group='sundown_ctrl_male_24',   # name of the control group for statistical testing
    exp_group='sundown_AD_male_24',    # name of the experimental group for statistical testing
    figsize=(8, 4),   # figure size    
    groups=['sundown_ctrl_male_24','sundown_AD_male_24'], # groups to be plotted
);

Not when I run the original transition matrix plotting function. The transition matrices plotting function is now working great! Sorry I should have opened a different error thread for this.

versey-sherry commented 6 months ago

Did you accidentally update seaborn or Matplotlib? If yes, you can downgrade the seaborn version to 0.13.0 for now to fix this error.

mjin1812 commented 6 months ago

Hi Sherry,

Below are my version outputs for matplotlib and seaborn.

matplotlib.__version__
'3.8.2'
seaborn.__version__
'0.13.0'
calebweinreb commented 6 months ago

Hi Michelle,

Can you try installing this version the code and let me know if the error persists?

pip install -U git+https://github.com/dattalab/keypoint-moseq@linestyles_hotfix
mjin1812 commented 6 months ago

Dear Caleb,

Thanks for the response. After installing your hotfix, the issue has been fixed!

Michelle