Skip to content

Commit

Permalink
More comprehensive excel and csv import, bringing in limits as part o…
Browse files Browse the repository at this point in the history
…f the headers.
  • Loading branch information
slightlynybbled committed Nov 29, 2019
1 parent f54b148 commit 060ed4a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/example_data.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
index,value
index,value (lsl=-2.5 usl=2.5)
0,2.22733878
1,-0.59642503
2,0.38748058
Expand Down
Binary file modified examples/example_data.xlsx
Binary file not shown.
9 changes: 6 additions & 3 deletions examples/import_from_csv.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
from manufacturing import import_csv, calc_cpk, show_cpk

data = import_csv('example_data.csv', columnname='value')
logging.basicConfig(level=logging.INFO)

cpk = calc_cpk(data, upper_spec_limit=2.5, lower_spec_limit=-2.5)
data = import_csv('example_data.csv', columnname='value (lsl=-2.5 usl=2.5)')

cpk = calc_cpk(**data)
print(f'cpk = {cpk:.3g}')

show_cpk(data, upper_spec_limit=2.5, lower_spec_limit=-2.5)
show_cpk(**data)
6 changes: 3 additions & 3 deletions examples/import_from_excel.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from manufacturing import import_excel, calc_cpk, show_cpk

data = import_excel('example_data.xlsx', columnname='value', skiprows=3)
data = import_excel('example_data.xlsx', columnname='value (lsl=-2.5 usl=2.5)', skiprows=3)

cpk = calc_cpk(data, upper_spec_limit=2.5, lower_spec_limit=-2.5)
cpk = calc_cpk(**data)
print(f'cpk = {cpk:.3g}')

show_cpk(data, upper_spec_limit=2.5, lower_spec_limit=-2.5)
show_cpk(**data)
2 changes: 1 addition & 1 deletion manufacturing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
__all__ = ['calc_cpk', 'show_cpk',
'import_csv', 'import_excel']

__version__ = '0.2.0'
__version__ = '0.3.0'
50 changes: 46 additions & 4 deletions manufacturing/data_import.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
import logging
from pathlib import Path
import pandas as pd

_logger = logging.getLogger(__name__)

def import_csv(file_path: (str, Path), columnname, **kwargs):

def _parse_col_for_limits(columnname: str):
if '(' in columnname and ')' in columnname:
_logger.info(f'parentheses detected, loading thresholds')
strings = columnname.split('(')[1].replace(')', '')
str1, str2 = strings.split(' ')

if 'lsl' in str1:
lsl = float(str1.split('=')[1])
elif 'lsl' in str2:
lsl = float(str2.split('=')[1])
else:
lsl = None

if 'usl' in str1:
usl = float(str1.split('=')[1])
elif 'usl' in str2:
usl = float(str2.split('=')[1])
else:
usl = None
else:
lsl, usl = None, None

return lsl, usl


def import_csv(file_path: (str, Path), columnname: str, **kwargs):
"""
Imports data from a csv file and outputs the specified column of data as a `pandas.Series`
:param file_path: the path to the file on the local file system
:param columnname: the column name to which the data is associated
:param kwargs: keyword arguments to be passed directly into `pandas.read_csv()`
:return: a pandas series of the data which is to be analyzed
:return: a dict containing a pandas series and the limits of the data to be analyzed
"""
df = pd.read_csv(file_path, **kwargs)
return df[columnname]

lsl, usl = _parse_col_for_limits(columnname)

return {
'data': df[columnname],
'lower_spec_limit': lsl,
'upper_spec_limit': usl
}


def import_excel(file_path: (str, Path), columnname, **kwargs):
Expand All @@ -25,4 +60,11 @@ def import_excel(file_path: (str, Path), columnname, **kwargs):
:return: a pandas series of the data which is to be analyzed
"""
df = pd.read_excel(file_path, **kwargs)
return df[columnname]

lsl, usl = _parse_col_for_limits(columnname)

return {
'data': df[columnname],
'lower_spec_limit': lsl,
'upper_spec_limit': usl
}
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

To provide analysis tools and metrics useful in manufacturing environments.

# Project Maturity

Every effort is being made to ensure that the results are accurate, but the user is ultimately
responsible for any resulting analysis.

The API should not be considered stable until v1.0 or greater. Until then, breaking changes may
be released as different API options are explored.

# Installation

## From `setup.py`
Expand Down

0 comments on commit 060ed4a

Please sign in to comment.