taborlab / FlowCal

Python Flow Cytometry Calibration Library
MIT License
49 stars 23 forks source link

Refactored plot.hist1d to permit `weights` kwarg. #333

Closed JS3xton closed 4 years ago

JS3xton commented 4 years ago

plot.hist1d() called with the weights parameter fails:

>>> FlowCal.plot.hist1d(np.array([1,2,3,1,2,1]), weights=[1,2,1,2,2,1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\sexto\Downloads\FlowCal\FlowCal\plot.py", line 971, in hist1d
    n, edges, patches = plt.hist(y,
TypeError: hist() got multiple values for keyword argument 'weights'

I refactored plot.hist1d() to more precisely specify arguments to plt.hist(). Closes https://github.com/taborlab/FlowCal/issues/255.

All unit tests pass in Python 3.8 + Anaconda 2020.02 and Python 2.7 + Anaconda 4.4.0.

After the change, the error shown above no longer occurs. > python -m FlowCal.excel_ui -v -p -i ./examples/experiment.xlsx also executes without error in both Python 3.8 + Anaconda 2020.02 and Python 2.7 + Anaconda 4.4.0.

castillohair commented 4 years ago

Can you provide examples of your code producing a few plots as before, and at least one plot demonstrating the new ability to specify weights?

JS3xton commented 4 years ago
develop (9cd83ef7fadf235e548bc6c1bb5ed22d4dde8416) hist1d-weights (ed1af38f3dc9130d5f5e5a5c8cabd617caf0e893)
hist1d(d)
hist1d(d, normed_height=True)
hist1d(d, normed_area=True)
hist1d(d, weights=weights) TypeError

Source code (inspired by the hist1d tutorial):

import numpy as np
import matplotlib.pyplot as plt
import FlowCal

d = FlowCal.io.FCSData('./FCFiles/Data002.fcs')
d = FlowCal.transform.to_rfi(d)
bins = np.logspace(0,4,100+1)
weights = np.ones(d.shape[0])
weights /= float(d.shape[0])
kwargs = {'data_list' : d,
          'channel'   : 'FL1',
          'bins'      : bins,
          'xlim'      : (1e0,1e4)}

FlowCal.plot.hist1d(**kwargs, savefig='1.png')
FlowCal.plot.hist1d(**kwargs, savefig='2.png', normed_height=True)
FlowCal.plot.hist1d(**kwargs, savefig='3.png', normed_area=True)
FlowCal.plot.hist1d(**kwargs, savefig='4.png', weights=weights)