LabTools3 1.1.3.10__tar.gz → 1.1.3.13__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -399,24 +399,24 @@ class histo:
399
399
  if (xmin is None) and (xmax is None):
400
400
  if self.window_set:
401
401
  sel = self.window_sel
402
- sum = self.bin_content[sel].sum()
402
+ sum_v = self.bin_content[sel].sum()
403
403
  sum_err = np.sqrt( (self.bin_error[sel]**2).sum())
404
404
  else:
405
- sum = self.bin_content.sum()
405
+ sum_v = self.bin_content.sum()
406
406
  sum_err = np.sqrt( (self.bin_error**2).sum())
407
407
  elif (xmin is None):
408
408
  sel = (self.bin_center <= xmax)
409
- sum = self.bin_content[sel].sum()
409
+ sum_v = self.bin_content[sel].sum()
410
410
  sum_err = np.sqrt( (self.bin_error[sel]**2).sum())
411
411
  elif (xmax is None):
412
412
  sel = (xmin <= self.bin_center)
413
- sum = (self.bin_content[sel]).sum()
413
+ sum_v = (self.bin_content[sel]).sum()
414
414
  sum_err = np.sqrt( (self.bin_error[sel]**2).sum())
415
415
  else:
416
416
  sel = (xmin <= self.bin_center) & (self.bin_center <= xmax)
417
- sum = (self.bin_content[sel]).sum()
417
+ sum_v = (self.bin_content[sel]).sum()
418
418
  sum_err = np.sqrt( (self.bin_error[sel]**2).sum())
419
- return (sum, sum_err)
419
+ return (sum_v, sum_err)
420
420
 
421
421
  def copy(self):
422
422
  """
@@ -800,7 +800,7 @@ class histo:
800
800
  # is there a range given, or is a window set
801
801
  sel_all = np.ones_like(self.bin_center, dtype = 'bool')
802
802
  if init:
803
- self.init_gauss(xmin, xmax)
803
+ self.init_gauss(xmin, xmax, ignore_zeros)
804
804
  if (xmin is None) and (xmax is None):
805
805
  # check if a window is set
806
806
  if self.window_set:
@@ -835,9 +835,10 @@ class histo:
835
835
  self.fit_indx, = np.where(sel_w)
836
836
  else:
837
837
  self.fit_indx, = np.where(sel)
838
- # set minimal error to 1
839
- is_zero = np.where(self.bin_error == 0.)
840
- self.bin_error[is_zero] = 1.
838
+ if not ignore_zeros:
839
+ # set minimal error to 1
840
+ is_zero = np.where(self.bin_error == 0.)
841
+ self.bin_error[is_zero] = 1.
841
842
  # store the limits of fit_indx
842
843
  self.fit_index_min = self.fit_indx.min()
843
844
  self.fit_index_max = self.fit_indx.max()
@@ -909,7 +910,7 @@ class histo:
909
910
  self.sigma.set(1.)
910
911
  self.A.set(1.)
911
912
 
912
- def init_gauss(self, xmin = None, xmax = None):
913
+ def init_gauss(self, xmin = None, xmax = None, ignore_zeros = True):
913
914
  """
914
915
 
915
916
  Calculate the initial parameter guess for a gaussian. These parameters
@@ -952,9 +953,10 @@ class histo:
952
953
  self.fit_indx, = np.where(sel_w)
953
954
  else:
954
955
  self.fit_indx, = np.where(sel)
955
- # set minimal error to 1
956
- is_zero = np.where(self.bin_error == 0.)
957
- self.bin_error[is_zero] = 1.
956
+ if not ignore_zeros:
957
+ # set minimal error to 1
958
+ s_zero = np.where(self.bin_error == 0.)
959
+ self.bin_error[is_zero] = 1.
958
960
  # do the fit
959
961
  bin_content = self.bin_content[self.fit_indx]
960
962
  bin_center = self.bin_center[self.fit_indx]
@@ -1008,6 +1010,44 @@ class histo:
1008
1010
  fit_val = (self.b2()*x + self.b1())*x + self.b0() + \
1009
1011
  self.A()*np.exp(-0.5*((x-self.mean())/self.sigma() )**2)
1010
1012
  return fit_val
1013
+
1014
+ def gauss(self, x):
1015
+ """
1016
+
1017
+ Return the value of the gauss function using the fitted parameters at x
1018
+
1019
+
1020
+ Parameters
1021
+ ----------
1022
+ x : float
1023
+
1024
+ Returns
1025
+ -------
1026
+ gauss(x) : float
1027
+ fitted gaussian at x.
1028
+
1029
+ """
1030
+ g_val = self.A()*np.exp(-0.5*((x-self.mean())/self.sigma() )**2)
1031
+ return g_val
1032
+
1033
+ def bkg(self, x):
1034
+ """
1035
+
1036
+ Return the value of the fitted background at x
1037
+
1038
+ Parameters
1039
+ ----------
1040
+ x : float
1041
+
1042
+ Returns
1043
+ -------
1044
+ b_val(x) : float
1045
+ fitted backgound at x.
1046
+
1047
+ """
1048
+ b_val = (self.b2()*x + self.b1())*x + self.b0()
1049
+ return b_val
1050
+
1011
1051
  def apply_calibration(self, cal):
1012
1052
  """
1013
1053
 
@@ -1404,7 +1444,9 @@ class histo2d:
1404
1444
 
1405
1445
 
1406
1446
 
1407
- def plot(self, axes = None, graph = 'patch', clevel = 10, colormap = pl.cm.CMRmap, logz = False, **kwargs):
1447
+ def plot(self, axes = None, graph = 'patch', clevel = 10, colormap = pl.cm.CMRmap, logz = False,
1448
+ skip_x_label = False, skip_ylabel = False, skip_title = False,
1449
+ **kwargs):
1408
1450
  """
1409
1451
 
1410
1452
  Plot the 2d histogram content:
@@ -1416,6 +1458,9 @@ class histo2d:
1416
1458
  clevel number of contour levels (default 10)
1417
1459
  colormap colormap to be used (default CMRmap)
1418
1460
  logz if True use a logarithmic scale for content (patch and contour only)
1461
+ skip_x_label if True do not plot x-label
1462
+ skip_y_label if True do not plot y-label
1463
+ skip_title if True do not plot title
1419
1464
  kwargs additional kwargs are possed to the plotting routines
1420
1465
  ============ =====================================================
1421
1466
 
@@ -1436,7 +1481,7 @@ class histo2d:
1436
1481
  else:
1437
1482
  pcm = axes.pcolormesh(self.x_bins, self.y_bins, Zm.T, cmap=colormap_l, norm = LogNorm(), **kwargs)
1438
1483
  if self.colorbar:
1439
- cbar = pl.colorbar(pcm)
1484
+ cbar = pl.colorbar(pcm,ax=axes)
1440
1485
  cbar.minorticks_on()
1441
1486
  elif graph == 'contour':
1442
1487
  if axes is None:
@@ -1446,7 +1491,7 @@ class histo2d:
1446
1491
  if not self.logz:
1447
1492
  ctf = pl.contourf(XX,YY, Zm, cmap=colormap_l, **kwargs) # contour
1448
1493
  if self.colorbar:
1449
- cbar = pl.colorbar(ctf)
1494
+ cbar = pl.colorbar(ctf,ax=axes)
1450
1495
  cbar.minorticks_on()
1451
1496
  else:
1452
1497
  Zml = np.log10(Zm)
@@ -1461,7 +1506,7 @@ class histo2d:
1461
1506
  # setup the tick labels
1462
1507
  ctkls = [r"$10^{" +f"{int(v):d}" +r"}$" for v in llev]
1463
1508
  if self.colorbar:
1464
- cbar = pl.colorbar(ctf)
1509
+ cbar = pl.colorbar(ctf,ax=axes)
1465
1510
  cbar.set_ticks(llev)
1466
1511
  cbar.set_ticklabels(ctkls)
1467
1512
  elif graph == 'surface':
@@ -1491,9 +1536,12 @@ class histo2d:
1491
1536
  else:
1492
1537
  print('Unknown graph type:', graph, ' possible values: patch, contour, surface ')
1493
1538
  return
1494
- axes.set_xlabel(self.xlabel)
1495
- axes.set_ylabel(self.ylabel)
1496
- axes.set_title(self.title)
1539
+ if not skip_x_label:
1540
+ axes.set_xlabel(self.xlabel)
1541
+ if not skip_ylabel:
1542
+ axes.set_ylabel(self.ylabel)
1543
+ if not skip_title:
1544
+ axes.set_title(self.title)
1497
1545
 
1498
1546
  def save(self, filename = 'histo2d.data', ignore_zeros = True):
1499
1547
  """
@@ -48,6 +48,7 @@ def plot_exp(x,\
48
48
  # set general line width
49
49
  linewidth=2,\
50
50
  logy = False,\
51
+ logx = False, \
51
52
  min_val = None,\
52
53
  scale = 1.,\
53
54
  # labelling
@@ -82,6 +83,7 @@ def plot_exp(x,\
82
83
  marker marker type (see :func:`~matplotlib.pyplot.plot`)
83
84
  linestyle line style (see :func:`~matplotlib.pyplot.plot`)
84
85
  logy use log y-scale (True/False)
86
+ logx use log x-scale (True/False)
85
87
  label label for data (used in :func:`~matplotlib.pyplot.legend` )
86
88
  min_val min. values to be plotted
87
89
  scale scale ally-values (including errrors ) by this factor
@@ -105,7 +107,9 @@ def plot_exp(x,\
105
107
  axes = pl.gca()
106
108
  if logy:
107
109
  axes.set_yscale("log", nonpositive='clip')
108
- if dy == []:
110
+ if logx:
111
+ axes.set_xscale("log", nonpositive='clip')
112
+ if len(dy) == 0:
109
113
  # no error bars
110
114
  e=axes.plot(xx, yy,\
111
115
  linestyle=linestyle,\
@@ -121,7 +125,7 @@ def plot_exp(x,\
121
125
  axes.set_title(plot_title)
122
126
  return e
123
127
  # plot second (total) error bar behind first (statistical) one
124
- if dyt != []:
128
+ if len(dyt) != 0:
125
129
  # in case ther are several different errors
126
130
  et=axes.errorbar(xx, \
127
131
  yy, \
@@ -175,6 +179,7 @@ def plot_line(x,\
175
179
  y, \
176
180
  label='_nolegend_',\
177
181
  logy = False, \
182
+ logx = False, \
178
183
  convx = 1., \
179
184
  convy = 1., \
180
185
  axes = None, \
@@ -195,6 +200,7 @@ def plot_line(x,\
195
200
  ============ =====================================================
196
201
  label label for curve (used in :func:`~matplotlib.pyplot.legend` )
197
202
  logy use log y-scale (True/False)
203
+ logx use log x-scale (True/False)
198
204
  convx scale all x-values by this factor
199
205
  convy scale all y-values by this factor
200
206
  ============ =====================================================
@@ -206,10 +212,11 @@ def plot_line(x,\
206
212
  axes = pl.gca()
207
213
  xx = np.array(x) * convx
208
214
  yy = np.array(y) * convy
215
+ s=axes.plot(xx, yy, marker='None',label=label,**kwargs)
209
216
  if logy:
210
- s=axes.semilogy(xx, yy, marker='None',label=label,**kwargs)
211
- else:
212
- s=axes.plot(xx, yy, marker='None',label=label,**kwargs)
217
+ axes.set_yscale("log", nonpositive='clip')
218
+ if logx:
219
+ axes.set_xscale("log", nonpositive='clip')
213
220
  return s
214
221
 
215
222
  #----------------------------------------------------------------------
@@ -225,6 +232,7 @@ def plot_spline(x, \
225
232
  convx = 1., \
226
233
  convy = 1., \
227
234
  logy = False,\
235
+ logx = False, \
228
236
  axes = None, \
229
237
  **kwargs):
230
238
  """
@@ -243,6 +251,7 @@ def plot_spline(x, \
243
251
  ============ =====================================================
244
252
  label label for curve (used in :func:`~matplotlib.pyplot.legend` )
245
253
  logy use log y-scale (True/False)
254
+ logx use log x-scale (True/False)
246
255
  nstep factor by which the number of interpolated data points is increased
247
256
  convx scale all x-values by this factor
248
257
  convy scale all y-values by this factor
@@ -266,15 +275,15 @@ def plot_spline(x, \
266
275
  # now get interpolation coefficients
267
276
  yvar_cj = splrep(xvar,yvar)
268
277
  new_yvar = splev(new_xvar, yvar_cj)
278
+ s=axes.plot(new_xvar, new_yvar, marker=marker,\
279
+ label=label,\
280
+ **kwargs)
269
281
  if logy:
270
- s=axes.semilogy(new_xvar, new_yvar, marker=marker,\
271
- label=label,\
272
- **kwargs)
273
- else:
274
- s=axes.plot(new_xvar, new_yvar, marker=marker,\
275
- label=label,\
276
- **kwargs)
282
+ axes.set_yscale("log", nonpositive='clip')
283
+ if logx:
284
+ axes.set_xscale("log", nonpositive='clip')
277
285
  return s
286
+
278
287
  #----------------------------------------------------------------------
279
288
  #----------------------------------------------------------------------
280
289
  # plot spline lines only, this has to be used carefully
@@ -287,6 +296,7 @@ def log_plot_spline(x, \
287
296
  nstep = 5,\
288
297
  conv = 1., \
289
298
  axes = None, \
299
+ logx = False, \
290
300
  **kwargs):
291
301
  if (axes == None):
292
302
  axes = pl.gca()
@@ -306,6 +316,8 @@ def log_plot_spline(x, \
306
316
  s=axes.semilogy(new_xvar, new_yvar, marker=marker,\
307
317
  label=label,\
308
318
  **kwargs)
319
+ if logx:
320
+ axes.set_xscale("log", nonpositive='clip')
309
321
  return s
310
322
  #----------------------------------------------------------------------
311
323
  # plot data read using data file
@@ -332,6 +344,7 @@ def datafile_plot_exp(set,\
332
344
  linewidth=2,\
333
345
  # set for semilog plot
334
346
  logy = False,\
347
+ logx = False, \
335
348
  # minimum value for semilog plot
336
349
  min_val = None,\
337
350
  scale = 1.,\
@@ -369,7 +382,8 @@ def datafile_plot_exp(set,\
369
382
  marker marker type (see :func:`~matplotlib.pyplot.plot`)
370
383
  linestyle line style (see :func:`~matplotlib.pyplot.plot`)
371
384
  label label for data (used in :func:`~matplotlib.pyplot.legend` )
372
- logy use log y-scale (True/False)
385
+ logy use log y-scale (True/False)
386
+ logx use log x-scale (True/False)
373
387
  min_val min. values to be plotted
374
388
  scale scale all y-values (including errrors ) by this factor
375
389
  x_label label for x-axis
@@ -393,6 +407,8 @@ def datafile_plot_exp(set,\
393
407
  yy = np.array(set.get_data(y))*scale
394
408
  if logy:
395
409
  axes.set_yscale("log", nonpositive='clip')
410
+ if logx:
411
+ axes.set_xscale("log", nonpositive='clip')
396
412
  if dy != None:
397
413
  dyy = np.array(set.get_data(dy))*scale
398
414
  dyyt = []
@@ -407,7 +423,7 @@ def datafile_plot_exp(set,\
407
423
  label=label, \
408
424
  **kwargs)
409
425
  # plot second (total) error bar behind first (statistical) one
410
- elif dyyt != []:
426
+ elif len(dyyt) != 0:
411
427
  # in case ther are several different errors
412
428
  et=axes.errorbar(xx, \
413
429
  yy, \
@@ -474,6 +490,7 @@ def datafile_plot_theory(set,\
474
490
  convx = 1.,\
475
491
  convy = 1.,\
476
492
  logy = False,\
493
+ logx = False, \
477
494
  axes = None, \
478
495
  **kwargs):
479
496
  """
@@ -487,19 +504,17 @@ def datafile_plot_theory(set,\
487
504
  xvar = np.array(set.get_data(x) )*convx
488
505
  yvar = np.array(set.get_data(y))*convy
489
506
 
507
+ s=axes.plot(xvar, yvar, marker=marker,color=color, \
508
+ label=label,\
509
+ **kwargs)
510
+
490
511
  if logy:
491
- for i in range(len(xvar)):
492
- if yvar[i] <= 0. :
493
- yvar[i] = min_val
494
- s=axes.semilogy(xvar, yvar, marker=marker,color=color, \
495
- label=label,\
496
- **kwargs)
497
- else:
498
- s=axes.plot(xvar, yvar, marker=marker,color=color, \
499
- label=label,\
500
- **kwargs)
512
+ axes.set_yscale("log", nonpositive='clip')
513
+ if logx:
514
+ axes.set_xscale("log", nonpositive='clip')
501
515
  return s
502
516
 
517
+
503
518
  #----------------------------------------------------------------------
504
519
  #plot spline lines only, this has to be used carefully
505
520
  def datafile_spline_plot_theory(set,\
@@ -513,6 +528,7 @@ def datafile_spline_plot_theory(set,\
513
528
  convx = 1.,\
514
529
  convy = 1.,\
515
530
  logy = False,\
531
+ logx = False, \
516
532
  axes = None, \
517
533
  **kwargs):
518
534
  """
@@ -528,23 +544,18 @@ def datafile_spline_plot_theory(set,\
528
544
  # number of interpolation steps
529
545
  new_xvar = np.linspace(xvar[0], xvar[-1:], int(nstep*len(xvar)) ) # create a new range with more points
530
546
  # handle 0 and neg values for log scale
531
- if logy:
532
- for i in range(len(xvar)):
533
- if yvar[i] <= 0. :
534
- yvar[i] = min_val
535
547
  # now get interpolation coefficients
536
548
  yvar_cj = splrep(xvar,yvar)
537
549
  new_yvar = splev(new_xvar, yvar_cj)
550
+ s=axes.plot(new_xvar, new_yvar, marker=marker,\
551
+ color=color,\
552
+ label=label,\
553
+ **kwargs)
538
554
  if logy:
539
- s=axes.semilogy(new_xvar, new_yvar, marker=marker,\
540
- color=color,\
541
- label=label,\
542
- **kwargs)
543
- else:
544
- s=axes.plot(new_xvar, new_yvar, marker=marker,\
545
- color=color,\
546
- label=label,\
547
- **kwargs)
555
+ axes.set_yscale("log", nonpositive='clip')
556
+ if logx:
557
+ axes.set_xscale("log", nonpositive='clip')
558
+
548
559
  return s
549
560
 
550
561
  #----------------------------------------------------------------------
@@ -30,6 +30,7 @@ def linfit(function, y, x = None, y_err = None, nplot = 100):
30
30
  if x is None: x = arange(y.shape[0])
31
31
  #
32
32
  # pdb.set_trace()
33
+
33
34
  A = function(x).transpose()
34
35
  if y_err is None:
35
36
  weight = ones_like(y)
@@ -39,14 +40,21 @@ def linfit(function, y, x = None, y_err = None, nplot = 100):
39
40
  # store the parameter values in a list to be passed to the fitting function
40
41
  M = dot(Aw.transpose(),A)
41
42
  # invert the matrix
42
- Mi = linalg.inv(M)
43
+ if np.isscalar(M):
44
+ # check if only one parameter
45
+ Mi = np.expand_dims(np.array([1./M]), 0)
46
+ else:
47
+ Mi = linalg.inv(M)
43
48
  b=dot(Aw.transpose(),y)
44
49
  par = dot(Mi,b)
45
50
  # for the calculation of errors
46
51
  # sigma = sqrt( diag(Mi) )
47
52
  # final total chi square
48
53
  # number of degrees of freedom
49
- yfit = dot(par,function(x))
54
+ fux = function(x)
55
+ if fux.ndim == 1:
56
+ fux = np.expand_dims(fux, 0)
57
+ yfit = dot(par,fux)
50
58
  n_dof = len(y) - len(par)
51
59
  diff = yfit - y
52
60
  chi2 = sum( power(diff, 2 )*weight )
@@ -56,7 +64,12 @@ def linfit(function, y, x = None, y_err = None, nplot = 100):
56
64
  # create an array with calculated values for plotting
57
65
  if (nplot > 0):
58
66
  xpl = linspace(x.min(), x.max(), nplot+1)
59
- ypl = dot(par,function(xpl))
67
+ fux = function(xpl)
68
+ if fux.ndim ==1 :
69
+ fux = np.expand_dims(fux, 0)
70
+ ypl = np.squeeze(dot(par,fux))
71
+ else:
72
+ ypl = dot(par,fux)
60
73
  stat = {'chisquare': chi2, \
61
74
  'red. chisquare':chi2_red, \
62
75
  'conf. level':CL, \
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: LabTools3
3
- Version: 1.1.3.10
3
+ Version: 1.1.3.13
4
4
  Summary: Python 3 Package of modules for typical analysis tasks analyzing physics data
5
5
  Home-page: http://wanda.fiu.edu/LabTools3
6
6
  Author: Werner Boeglin
@@ -42,3 +42,9 @@ Version 1.1.3.8: datafile can be initialized with a list of strings correponding
42
42
  Version 1.1.3.9: Fixed a bug in *histo* where histogram window variable was not initialized when loading from file (required a *clear_window* call).
43
43
 
44
44
  Version 1.1.3.10: 2d histo now accepts axes as keywords. Make sure for 3d display the axes is a Axes3DSubplot.
45
+
46
+ Version 1.1.3.11: new control on title and axes labels for plotting
47
+
48
+ Version 1.1.3.12: bug fixes, peak and background functions are available for histogram fits.
49
+
50
+ Version 1.1.3.13: fitting of only one parameters enabled for linear fits
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: LabTools3
3
- Version: 1.1.3.10
3
+ Version: 1.1.3.13
4
4
  Summary: Python 3 Package of modules for typical analysis tasks analyzing physics data
5
5
  Home-page: http://wanda.fiu.edu/LabTools3
6
6
  Author: Werner Boeglin
@@ -42,3 +42,9 @@ Version 1.1.3.8: datafile can be initialized with a list of strings correponding
42
42
  Version 1.1.3.9: Fixed a bug in *histo* where histogram window variable was not initialized when loading from file (required a *clear_window* call).
43
43
 
44
44
  Version 1.1.3.10: 2d histo now accepts axes as keywords. Make sure for 3d display the axes is a Axes3DSubplot.
45
+
46
+ Version 1.1.3.11: new control on title and axes labels for plotting
47
+
48
+ Version 1.1.3.12: bug fixes, peak and background functions are available for histogram fits.
49
+
50
+ Version 1.1.3.13: fitting of only one parameters enabled for linear fits
@@ -27,4 +27,10 @@ Version 1.1.3.8: datafile can be initialized with a list of strings correponding
27
27
 
28
28
  Version 1.1.3.9: Fixed a bug in *histo* where histogram window variable was not initialized when loading from file (required a *clear_window* call).
29
29
 
30
- Version 1.1.3.10: 2d histo now accepts axes as keywords. Make sure for 3d display the axes is a Axes3DSubplot.
30
+ Version 1.1.3.10: 2d histo now accepts axes as keywords. Make sure for 3d display the axes is a Axes3DSubplot.
31
+
32
+ Version 1.1.3.11: new control on title and axes labels for plotting
33
+
34
+ Version 1.1.3.12: bug fixes, peak and background functions are available for histogram fits.
35
+
36
+ Version 1.1.3.13: fitting of only one parameters enabled for linear fits
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
  setup(
7
7
  name = "LabTools3",
8
- version = "1.1.3.10",
8
+ version = "1.1.3.13",
9
9
  packages = find_packages(),
10
10
  # add additional files
11
11
  package_data = {'':['*.bat','*.command']},
File without changes
File without changes
File without changes