Skip to content

Commit

Permalink
fixed retrieval of conditions from kwargs
Browse files Browse the repository at this point in the history
previously samples used to check if c in self and that would pass the unit test 
The output would be a graph with no plots but a legend with non existent conditions
The correct output should have no conditions in the legend for non existent keys
Hence in the case of conditions being supplied through kwargs it need to be checked with self(conditions exist in self or not)
  • Loading branch information
BLaZeKiLL committed Jun 14, 2019
1 parent 7d8bef3 commit ecdcc57
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions nltk/probability.py
Expand Up @@ -1913,46 +1913,46 @@ def plot(self, *args, **kwargs):

cumulative = _get_kwarg(kwargs, 'cumulative', False)
percents = _get_kwarg(kwargs, 'percents', False)
conditions = _get_kwarg(kwargs, 'conditions', sorted(self.conditions()))
conditions = [c for c in _get_kwarg(kwargs, 'conditions', self.conditions()) if c in self] # conditions should be in self
title = _get_kwarg(kwargs, 'title', '')
samples = _get_kwarg(
kwargs, 'samples', sorted(set(v for c in conditions
if c in self
for v in self[c]))
kwargs, 'samples', sorted(set(v
for c in conditions
for v in self[c]))
) # this computation could be wasted
if "linewidth" not in kwargs:
kwargs["linewidth"] = 2
freqs = []
for condition in conditions:
if cumulative:
# freqs should be a list of list where each sub list will be a frequency of a condition
freqs.append(list(self[condition]._cumulative_frequencies(samples)))
ylabel = "Cumulative Counts"
legend_loc = 'lower right'
if percents:
freqs[-1] = [f / freqs[len(freqs) - 1] * 100 for f in freqs]
ylabel = "Cumulative Percents"
else:
freqs.append([self[condition][sample] for sample in samples])
ylabel = "Counts"
legend_loc = 'upper right'
# percents = [f * 100 for f in freqs] only in ConditionalProbDist?


ax = plt.gca()
i = 0
for freq in freqs:
kwargs['label'] = conditions[i] #label for each condition
i += 1
ax.plot(freq, *args, **kwargs)
ax.legend(loc=legend_loc)
ax.grid(True, color="silver")
ax.set_xticks(range(len(samples)))
ax.set_xticklabels([text_type(s) for s in samples], rotation=90)
if title:
ax.set_title(title)
ax.set_xlabel("Samples")
ax.set_ylabel(ylabel)
if (len(conditions) != 0):
freqs = []
for condition in conditions:
if cumulative:
# freqs should be a list of list where each sub list will be a frequency of a condition
freqs.append(list(self[condition]._cumulative_frequencies(samples)))
ylabel = "Cumulative Counts"
legend_loc = 'lower right'
if percents:
freqs[-1] = [f / freqs[len(freqs) - 1] * 100 for f in freqs]
ylabel = "Cumulative Percents"
else:
freqs.append([self[condition][sample] for sample in samples])
ylabel = "Counts"
legend_loc = 'upper right'
# percents = [f * 100 for f in freqs] only in ConditionalProbDist?

i = 0
for freq in freqs:
kwargs['label'] = conditions[i] #label for each condition
i += 1
ax.plot(freq, *args, **kwargs)
ax.legend(loc=legend_loc)
ax.grid(True, color="silver")
ax.set_xticks(range(len(samples)))
ax.set_xticklabels([text_type(s) for s in samples], rotation=90)
if title:
ax.set_title(title)
ax.set_xlabel("Samples")
ax.set_ylabel(ylabel)
plt.show()

return ax
Expand Down

0 comments on commit ecdcc57

Please sign in to comment.