Skip to content

Commit c529ece

Browse files
committed
introduced optional index parameter which replaces 'panda series support'
1 parent 53c08d1 commit c529ece

1 file changed

Lines changed: 15 additions & 20 deletions

File tree

controlchart/__init__.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,7 @@ class Spc(object):
376376
"""
377377

378378
def __init__(self, data, chart_type, rules=RULES_BASIC, stats_custom=None, newdata=None, sizes=None):
379-
self.orig_data = data
380-
self.data_is_list = True if type(data) is list else False
379+
data = data if isinstance(data, list) else list(data)
381380
self.chart_type = chart_type
382381
self.rules = rules
383382
self.stats = []
@@ -396,12 +395,8 @@ def __init__(self, data, chart_type, rules=RULES_BASIC, stats_custom=None, newda
396395
self.center, self.lcl, self.ucl = sf(data, size)
397396
else:
398397
self.center, self.lcl, self.ucl = stats_custom
399-
try:
400-
self._data = pd(data + newdata, size)
401-
except Exception:
402-
datavalues = list(data.values)
403-
self._data = pd(datavalues + newdata, size)
404398

399+
self._data = pd(data + newdata, size)
405400
self.violating_points = self._find_violating_points()
406401

407402
def _find_violating_points(self, rules=None):
@@ -421,7 +416,7 @@ def _find_violating_points(self, rules=None):
421416
points.setdefault(r, []).append(i)
422417
return points
423418

424-
def get_chart(self, legend=True, title=None):
419+
def get_chart(self, legend=True, title=None, index=None):
425420
"""Generate chart using matplotlib."""
426421
try:
427422
import matplotlib
@@ -431,13 +426,16 @@ def get_chart(self, legend=True, title=None):
431426
import matplotlib.pyplot as plt
432427
import matplotlib.lines as mlines
433428

429+
if index is not None and not isinstance(index, list):
430+
index = list(index)
431+
434432
plt.figure(figsize=(20, 10))
435433
ax = plt.subplot(111)
436434

437-
if self.data_is_list:
438-
ax.plot(self._data, "bo-", ms=5, label='Data')
435+
if index is None:
436+
plt.plot(self._data, "bo-", ms=5, label='Data')
439437
else:
440-
plt.plot(self.orig_data.index.to_pydatetime(), self.orig_data.values, "bo-", ms=5, label='Data')
438+
plt.plot(index, self._data, "bo-", ms=5, label='Data')
441439

442440
title = self.chart_type if title is None else title
443441
plt.title(title, fontsize=22) # setting the title for the figure
@@ -450,27 +448,24 @@ def get_chart(self, legend=True, title=None):
450448

451449
if RULES_7_ON_ONE_SIDE in self.violating_points:
452450
for i in self.violating_points[RULES_7_ON_ONE_SIDE]:
453-
if self.data_is_list is not True:
454-
index = self.orig_data.index[i]
455-
ax.plot([index], [self._data[i]], "yo", ms=10)
451+
if index is not None:
452+
ax.plot([index[i]], [self._data[i]], "yo", ms=10)
456453
else:
457454
ax.plot([i], [self._data[i]], "yo", ms=10)
458455
ax.plot([], [], color='yellow', linestyle='', marker='o', ms=10, label='Run of 7')
459456

460457
if RULES_8_ON_ONE_SIDE in self.violating_points:
461458
for i in self.violating_points[RULES_8_ON_ONE_SIDE]:
462-
if self.data_is_list is not True:
463-
index = self.orig_data.index[i]
464-
ax.plot([index], [self._data[i]], "yo", ms=10)
459+
if index is not None:
460+
ax.plot([index[i]], [self._data[i]], "yo", ms=10)
465461
else:
466462
ax.plot([i], [self._data[i]], "yo", ms=10)
467463
ax.plot([], [], color='yellow', linestyle='', marker='o', ms=10, label='Run of 8')
468464

469465
if RULES_1_BEYOND_3SIGMA in self.violating_points:
470466
for i in self.violating_points[RULES_1_BEYOND_3SIGMA]:
471-
if self.data_is_list is not True:
472-
index = self.orig_data.index[i]
473-
ax.plot([index], [self._data[i]], "ro", ms=10)
467+
if index is not None:
468+
ax.plot([index[i]], [self._data[i]], "ro", ms=10)
474469
else:
475470
ax.plot([i], [self._data[i]], "ro", ms=10)
476471
ax.plot([], [], color='red', linestyle='', marker='o', ms=10, label='Out of Limits')

0 commit comments

Comments
 (0)