Data analysis using python
OBJECTIVE
To write a script for
1. Data visualizer, To plot a graph between any two parameters according to user inputs from the data file given.
2. Compatibility check, To check whether the file is valid or not.
3. Performance calculation, To find the work done from the PV plot and then to find Power Output and Specific Fuel Consumption.
THEORY
Data analysis is defined as a process of cleaning, transforming, and modeling data to discover useful information for business decision making. The purpose of Data Analysis is to extract useful information from data and taking the decision based upon the data analysis.
Types of Data Analysis:
- Text Analysis is a method to discover a pattern in large data sets using databases or data mining tools.it is used to transform raw data into business information.
- Statistical Analysis shows "What happens?" by using past data in the form of dashboards. Statistical Analysis includes collection, analysis, interpretation, presentation, and modeling of data. it analyses a set of data. There are two categories of this type of analysis- Descriptive Analysis and Inferential Analysis. Descriptive Analysis analyses complete data or a sample of summarized numerical data. it shows mean and deviation for continuous data whereas percentage and frequency for categorical data. Inferential Analysis analyses sample from complete data. In this type of analysis, one can find different conclusions from the same data by selecting different samples.
- Diagnostic Analysis shows "Why did it happen?" by finding the cause from the insight found in statistical Analysis. This analysis is useful to identify behavior patterns of data.
- Predictive Analysis shows "what is likely to happen" by using previous data. This analysis makes predictions about future outcomes based on current or past data. Forecasting is just an estimate and its accuracy is based on how much detailed information is available.
- Prescriptive Analysis combines the insights from all previous analysis to determine which action to take in a current problem or decision. Most data-driven companies are using prescriptive analysis because predictive and descriptive analysis are not enough to improve data performance.
Data Analysis Process:
The Data Analysis Process is nothing but gathering information by using a proper application or tool which allows you to explore the data and find a pattern in it. Based on that information and data, one can make decisions, or you can get ultimate conclusions.
Data Analysis process includes the following steps:
- Data Requirement Gathering involves deciding what is the aim of the analysis and what type of analysis should be carried out
- Data Collection involves collecting data based on the set requirements. The collected data must be organized for analysis.
- Data Cleaning removes irrelevant information for running the analysis. The collected data may contain whitespace, duplicate records, or errors.
- Data Analysis is done next. As the data is manipulated information required may be found out or more data need to be collected.
- Data Interpretation comes after analyzing data. Results should be interpreted in order to get useful insights from the analysis. it can be done using words or maybe a table or chart. Then use the results of the data analysis process to decide your best course of action.
- Data Visualization allows data interpreted to be shown in a graphical manner so that it will be easier for the human brain to understand and process it. Data visualization often used to discover unknown facts and trends. by observing relationships and comparing datasets, you can find a way to find out meaningful information.
Performance calculation:
1. Total Indicated power output without considering any frictional losses which is equal to brake power
Where,
n=Number of cylinders
P=Mean effective pressure,Joules
L=Length of stroke,m
A=Cross section of the piston,m^2
N=rpm
N/2 for 4 stroke and N for 2 stroke engine
2. Specific Fuel consumption
Specific Fuel Consumption(SFC)=Fuel Consumption(kg/hr)/Brake Power(KW)
Fuel consumption is given as 20mg/stroke. This should be converted to kg/hr.
rph=rpm*60
20(mg)/(stroke)=>(20*10^-9)/2 (kg)/(rev)=>10*10^9*rph (kg/hr)
PROCEDURE
Prerequisite: REPL package should be installed into the sublime to be able to give inputs through the console.
Data File used:https://drive.google.com/open?id=1L8GY56d-M8mB1KfceM-xVhGNvnCIqxjm
1. Import the required modules like matplotlib.pyplot, math, bumpy, simps
2. Compatibility is checked using inbuilt python try, except, else command. If there is no exception then the try and else part will continue to execute. If there is an exception then an error message will be displayed as required. The open command opens the file.
3. Upon successful opening of the file, the code under the else clause gets executed. The variables and arrays are defined. 'line_count' is used to control the lines that are read. 'column_number', 'name_of_column', 'units', 'X','Y' are the arrays used for storing the column number, column name, units, values of x coordinates and y coordinates respectively.
4. The user is asked for their choice to enter the column number corresponding to the parameters to be compared.
5. A for loop is used to read the file. Inside this for loop other three for loop's are given to access and append the read values to the already defined arrays. The values of the column selected by the user are appended to X and Y arrays respectively.
6. The values are plotted using plot function and the plot is such that the parameters and titles will be automatically fetched according to user inputs. The plot will be saved with the name of parameters as the file name.
7. For performance calculations, the X and Y arrays are cleared to remove previous data. The line counter is reset and again the data file is read using the for loop and the pressure and volume values are appended again to the X and Y arrays respectively.
8. The area of the PV diagram is calculated using the Simpsons rule and the work done in Mega Joules and Joules are calculated.
9. Power output at 1500 rpm and specific fuel consumption is also calculated.
Python code:
"""
Code to perform data Visualization, Compatibility check and to perform performance calculation
By Reuben John Eype
"""
#import modules
import matplotlib.pyplot as plt
import math
import numpy as np
from scipy.integrate import simps
#Compatibility check for a valid converge output file using try except syntax
try:
open('engine_data.out')
except FileNotFoundError:
print("File not recognized. This is not a valid CONVERGE output file")
else:
#To keep the number of line currently working with
line_count=1
#To store the number of column
column_number=[]
#To store the name of the column
name_of_column=[]
#To store the unit of the column
units=[]
#To store the x and y values for the variable selected
X=[]
Y=[]
#To allow user to select the parameter to be compared
print('Values and corresponding parameters: 1-Crank, 2-Pressure, 3-Max_Pres, 4-Min_Pres, 5-Mean_Temp, 6-Max_Temp, 7-Min_Temp, 8-Volume, 9-Mass, 10-Density, 11-Integrated_HR, 12-HR_Rate, 13-C_p, 14-C_v, 15-Gamma, 16-Kin_Visc, 17-Dyn_Viscn')
x=int(input('Enter the value corresponding to the parameter to be studiedt'))-1
y=int(input('Enter the value corresponding the other parameter to be compared with the firstt'))-1
#For loop to read the file line by line
for line in open('engine_data.out'):
#for loop to access the column number in 2nd line.Total 19 iterations including the '#' and 'column' word
for i in range(0,19):
#To exclude unneccessary characters and to start appending words/info from 2nd index in 2nd line
if i >=2:
if line_count==2:
column_number.append(int(line.split()[i]))
#For loop to access the Column name and units is in line 3 and 4 repectively. Only 18 iterations for accessing info including '#' symbol.
for j in range(0,18):
#Required info starts from index 1
if j>=1:
if line_count==3:
name_of_column.append(line.split()[j])
if line_count==4:
units.append(line.split()[j])
#For loop to access the values for different parameters it is necessary to avoid '#' symbol and required data starts from line 6
if '#' not in line:
if line_count>=6:
X.append(float(line.split()[x]))
Y.append(float(line.split()[y]))
line_count=line_count+1
#plotting
#Create a figure object
plt.figure()
#Ploting two variables selected by user
plt.plot(X,Y,color='r',linewidth=3)
#Giving automatic title and labels according to users selection as per teh requirement
plt.title('Plot of '+str(name_of_column[y])+' versus '+str(name_of_column[x]),color='k',fontsize=12)
plt.xlabel(str(name_of_column[x])+str(units[x]))
plt.ylabel(str(name_of_column[y])+str(units[y]))
plt.savefig(str(name_of_column[y])+' vs '+str(name_of_column[x]))
plt.show()
#Performance calculation
#Clearing the previously appended values in the arrays
X.clear()
Y.clear()
#Specifying corresponding indices to pressure and volume
x=1
y=7
#Reset the line counter
line_count=1
for line in open('engine_data.out'):
if '#' not in line:
if line_count>=6:
X.append(float(line.split()[x]))
Y.append(float(line.split()[y]))
line_count=line_count+1
#Calculation of area under PV diagram
area=simps(X,Y)
print('nWorkdone calculated using simpsons rule,W='+str(area)+ ' MJ')
area=area*pow(10,6)
print('nWork done in Joules,W= '+str(area)+' Jn')
#speed of engine in rpm
rpm=1500
#Fuel consumption 20 micro grams of fuel per 1 stroke cycle
fuelc=20
#number of cylinders
n=1
no_of_stroke=4
#in 4 storke engine there is a power stroke for every two revolutions and therefore a factor is considered which is equal to 2
k=2
power=n*area*(rpm/2)/(60*1000)
print('Power output at 1500rpm, power_output='+str(power)+' KWn')
#specific fuel consumption
#revolutions per hour
rph=rpm*60
#Fuel consumption(kg/rev)
fuelc_kg_per_rev=fuelc*pow(10,-9)/2
#Fuel consumption(kg/hr)
fuelc_kg_per_hr=rph*fuelc_kg_per_rev
#Specific Fuel consumption((kg/hr/KW))
SFC=fuelc_kg_per_hr/power
print('Specific fuel consumption,SFC=' +str(SFC)+' (Kg/hr)/KW')
ERRORS ENCOUNTERED
Indentation errors were made and has been rectified.
RESULTS
1. Output when the file is not found/name does not match hence compatibility check is done.
2. Allows the user to enter the choice regarding the parameters to be plotted for the data to be visualized.
3. The user enters the parameters to be studied and the work done, power output, and specific fuel consumption are found out as shown below.
4. The input given is corresponding to the plot of pressure versus crank
5. The plot is automatically saved with the name of parameter
6. Pressure vs Volume
7. Mass versus Pressure
8. Heat Rejection rate versus Volume
Hence data is visualized using different plots corresponding to the user inputs, compatibility is checked and performance calculations are done.