<jupyter_start><jupyter_text>Packaging Line analysis  Importing necessary packages<jupyter_code>#Importing necessary packages
import numpy as np
import pandas as pd
import random
import matplotlib.pyplot as plt
import seaborn as sns
#Customizing data visualization styles
sns.set_theme(rc = {'figure.figsize': (12, 5)})
#Uploading file from local drive
from google.colab import files
uploaded = files.upload()
#Storing dataset in a Pandas Dataframe
import io
df = pd.read_excel(io.BytesIO(uploaded['2021-02-24_packaging_kveld.xlsx']))
#Checking the dataframe information
df.info()
df.head()<jupyter_output><empty_output><jupyter_text>Packaging stoppages EDA<jupyter_code>#Creating a new dataframe and computing the minute and hour column based on a condition
pkl = df.copy()
pkl = pkl.assign(Minute = lambda y: y['Start'].dt.minute, Hour = lambda z: z['Start'].dt.hour)
pkl = pkl.assign(Half_hours = lambda x: np.where(x['Minute'] < 30, x['Start'].dt.hour, x['Start'].dt.hour + 0.5)) #=> Critical to use np.where()
pkl.head(10)
#Dividing the time loss by 2 for each packaging machine to calculate the GE loss
pkl = pkl.assign(Min = pkl['Min']/2)
pkl.head(10)
#Grouping by events by minute and hour
evn = pkl.copy()
evn = evn.groupby(['Event', 'Start','Hour', 'Minute', 'Half_hours', 'Machine', 'Min']).count()['Stop'].reset_index()
evn = evn.assign(Cum_min = lambda y: y['Min'].cumsum())
evn = evn.assign(Cum_GE = lambda z: z['Cum_min']/12)
evn.tail(10)
#Plotting the number of minutes lost by machine and time
sns.relplot(x='Start', y='Cum_min', data=evn, hue='Machine', height=5, aspect=2.5, s=120)
plt.title('Cumulative Time Loss in Packaging Area - Afternoon Shift - Feb 24, 2021')
plt.yticks(np.arange(0, 35, 5))
plt.show()
#Plotting the GE loss by Machine
sns.relplot(x='Start', y='Cum_GE', data=evn, hue='Machine', height=5, aspect=2.5, s=125)
plt.title('Cumulative GE Loss in Packaging Area - Afternoon Shift - Feb 24, 2021')
plt.yticks(np.arange(0, 4, 0.5))
plt.show()
#Plotting the number of minutes lost by each hour
sns.catplot(x='Half_hours', y='Min', data=pkl, kind='bar', hue='Machine', height=5.0, aspect=2.5, ci=False, estimator=sum)
plt.title('Time loss in packaging area by hour and machine - Afternoon Shift - Feb 24, 2021')
plt.show()
#Plotting the number of minutes lost by each half hour and machine
sns.catplot(x='Half_hours', y='Min', data=pkl, kind='bar', height=5.0, aspect=2.5, ci=False, estimator=sum)
plt.title('Time loss in packaging area vs time - Afternoon Shift - Feb 24, 2021')
plt.show()
#Groupying by Machine and Time loss
round(pkl[['Machine', 'Min']].groupby(['Machine']).sum().sort_values('Machine', ascending=False), 2)
#Groupying by Half_hour, Machine and Time loss
round(pkl[['Half_hours', 'Machine', 'Min']].groupby(['Half_hours', 'Machine'], sort=False).sum(), 2)
#Plotting the variability of each machine
sns.catplot(x='Half_hours', y='Min', data=pkl, kind='box', height=5.0, aspect=2.5, ci=False, estimator=sum)
plt.title('Box plot of Time loss in packaging area by hour and machine - Feb 15, 2021')
plt.show()
pkl.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 23 entries, 0 to 22
Data columns (total 8 columns):
 #   Column      Non-Null Count  Dtype         
---  ------      --------------  -----         
 0   Event       23 non-null     int64         
 1   Machine     23 non-null     object        
 2   Start       23 non-null     datetime64[ns]
 3   Stop        23 non-null     datetime64[ns]
 4   Min         23 non-null     float64       
 5   Minute      23 non-null     int64         
 6   Hour        23 non-null     int64         
 7   Half_hours  23 non-null     float64       
dtypes: datetime64[ns](2), float64(2), int64(3), object(1)
memory usage: 1.6+ KB<jupyter_text>Packaging parts analysis  Uploading packages and data<jupyter_code>#Importing necessary packages
import numpy as np
import pandas as pd
import random
import matplotlib.pyplot as plt
import seaborn as sns
import math
#Remove previous versions of the uploaded excel file
!rm xxxxxxxxxxxxx.xlsx
#Upload file from local disk
from google.colab import files
upload = files.upload()
#Save uploaded file to a pandas dataframe
import io
df = pd.read_excel(io.BytesIO(upload['hoved_deler_autowrapper.xlsx']), index_col='Deler') # ==> Set Parts column as index
#Fill missing values with np.nan
df = df.fillna(value=np.nan)
#Checking the dataframe head
df.head()<jupyter_output><empty_output><jupyter_text>Generating dictionary from List of parts<jupyter_code>my_dict = {'Agitator': ['Emergency_stop', 'Agitator_sensor', 'nan', 'nan'],
           'Biter_topp_ligning': ['Metal_skyver', 'nan', 'nan'],
           'Biter_venstre_ligning': ['Venstre_stempel', 'Venstre_ligning_motor', 'nan', 'nan', 'nan'],
           'Flexlink': ['Flexlink_kjede', 'Flexlink_motor', 'nan']}
my_dict
#Building a dictionary from columns, ignoring missing values
#my_dict = df.to_dict(orient='list')
clean_dict = {k: [val for val in l if str(val) != 'nan'] for k, l in my_dict.items()}
clean_dict
my_dict
type(my_dict['Agitator'][4])
#Option 1: Create a function to locate and return a value from the dataframe
def df_to_dict(df, arg1, arg2):
  return df.xs(arg1, axis=0, level=None)[arg2].to_dict()

my_dict = df_to_dict(df, 'Melkerull 10 Kg', 'Freia 148G Milk Roll 2 Pack')
#print(f'Kode = {kode}, Group = {group}, Time = {value}min')

for (kode, group), v in my_dict.items():
  print(kode, group, v)
#Download the file
from google.colab import files
my_dict.to_excel('dictionary_parts.xlsx', index=True)
files.download('dictionary_parts.xlsx')
# Save the DataFrame to a file on the Colab backend.
with open('df.json', 'w') as f:
  f.write(df.to_json())
#Download the file
from google.colab import files
files.download('df.json')
df.to_json?
df.to_dict?
pd.read_excel?<jupyter_output><empty_output><jupyter_text>Packaging machine stoppages  Uploading data<jupyter_code>#Importing necessary packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#import seaborn as sns
# Uploading file from local disk
from google.colab import files
uploaded = files.upload()
# Saving data to a pandas DataFrame
import io
df3 = pd.read_excel(io.BytesIO(uploaded['2022_01_j4_stoppages.xlsx']))
df3.tail(2)
# Checking the dataframe info
df3.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 72 entries, 0 to 71
Data columns (total 8 columns):
 #   Column     Non-Null Count  Dtype         
---  ------     --------------  -----         
 0   Dato       72 non-null     datetime64[ns]
 1   Skift      72 non-null     object        
 2   Time       72 non-null     object        
 3   Maskin     72 non-null     object        
 4   Workpoint  72 non-null     object        
 5   Loss_type  72 non-null     object        
 6   Min        72 non-null     int64         
 7   Comments   72 non-null     object        
dtypes: datetime64[ns](1), int64(1), object(6)
memory usage: 4.6+ KB<jupyter_text>Aggregating stoppages<jupyter_code># Aggregating to estimate the biggest losses by Machine
df3_gr = df3[~df3['Loss_type'].isin(['Planned'])].groupby(by=['Maskin', 'Workpoint', 'Comments']).agg(Min_loss = ('Min', 'sum')).reset_index()
df3_gr = df3_gr.sort_values(by=['Min_loss'], ascending=False).reset_index(drop=True)
df3_gr = df3_gr.assign(Perc = round(df3_gr['Min_loss']*100/df3_gr['Min_loss'].sum(),1))
df3_gr
# Exporting data to local disk
df3_gr.to_excel('2022_01_28_sample_losses.xlsx', index=False)
files.download('2022_01_28_sample_losses.xlsx')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13472259092115743, 'fraction_numerical': 0.03225378285107513, 'mean_word_length': 4.247214484679666, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '619930', 'license': 'MIT', 'n_tokens_mistral': 2820, 'n_tokens_neox': 2605, 'n_words': 752, 'path': 'packaging_analysis.ipynb', 'repo_name': 'gabrielborja/chocolate_making', 'size': '7534'}