# Import libraries
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import glob
import re
# Set style for matplotlib plots
plt.style.use('classic')
# Find all file names of the files to be imported
filenames = glob.glob('Exercise 6B Data/Input Data/*.txt')
# Create an empty dataframe
df_plate_bendingmoment = pd.DataFrame()
# Loop through all filenames and append the data to the dataframe
for file in filenames:
# Get the name of the stage from the filename
stage_name = re.search('Plate_ (.*?) \(', file).group(1)
# Import txt file to a temporary dataframe
tmp = pd.read_csv(file, sep = '\t')
# Create a new column in the temporary dataframe to store the stage name
tmp['stage_name'] = stage_name
# Append the temporary dataframe to the dataframe
df_plate_bendingmoment = df_plate_bendingmoment.append(tmp, ignore_index=True)
# Inspect the column names and rename them if necessary
display(df_plate_bendingmoment.columns)
df_plate_bendingmoment.columns = ['Structural element', 'Node', 'Local number', 'x [m]', 'y [m]', 'N [kN/m]', 'N_min [kN/m]',
'N_max [kN/m]', 'Q [kN/m]', 'Q_min [kN/m]', 'Q_max [kN/m]', 'M [kN m/m]', 'M_min [kN m/m]', 'M_max [kN m/m]', 'stage_name']
# Inspect the dataframes and check for the number of rows in the dataframe (there are supposed to be 2570 rows)
display(df_plate_bendingmoment.head())
display(len(df_plate_bendingmoment))
Index([' Structural element', ' Node', 'Local number', ' X [m]', ' Y [m]', ' N [kN/m]', ' N_min [kN/m]', ' N_max [kN/m]', ' Q [kN/m]', ' Q_min [kN/m]', ' Q_max [kN/m]', ' M [kN m/m]', ' M_min [kN m/m]', ' M_max [kN m/m]', 'stage_name'], dtype='object')
Structural element | Node | Local number | x [m] | y [m] | N [kN/m] | N_min [kN/m] | N_max [kN/m] | Q [kN/m] | Q_min [kN/m] | Q_max [kN/m] | M [kN m/m] | M_min [kN m/m] | M_max [kN m/m] | stage_name | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Plate\_1\_1 | 481 | 1 | -7,50000000000000E+000 | 2,85000000000000E+000 | -7,03181073064590E-001 | -7,03181073064590E-001 | 3,89246800031684E-002 | 1,02160886389150E-001 | -9,36710463384537E-002 | 1,52428398804864E-001 | -7,71605002114484E-015 | -6,28715829398274E-014 | 1,06704575730809E-013 | Sheet Pile & Bored Pile Installation [Phase_2] |
1 | Element 1-1 (Plate) | 480 | 2 | -7,50000000000000E+000 | 2,51250000000000E+000 | -2,10622366791634E+000 | -2,10622366791634E+000 | 5,86208363179141E-005 | 1,34321760204781E+000 | -1,63046119352223E-004 | 1,69367103826703E+000 | 2,68011318691106E-001 | -5,55946578064962E-006 | 3,13423271299049E-001 | Sheet Pile & Bored Pile Installation [Phase_2] |
2 | (ERSS) | 479 | 3 | -7,50000000000000E+000 | 2,17500000000000E+000 | -3,76943393583941E+000 | -3,76943393583941E+000 | 3,29549126897870E-005 | 1,98840581894081E+000 | -2,20847139338000E-004 | 2,47531139895095E+000 | 8,34746953403614E-001 | -7,07522542344873E-005 | 1,03419525045104E+000 | Sheet Pile & Bored Pile Installation [Phase_2] |
3 | 478 | 4 | -7,50000000000000E+000 | 1,83750000000000E+000 | -5,66766686622757E+000 | -5,66766686622757E+000 | 1,74607721085982E-005 | 2,29535600547745E+000 | -1,37686048637471E-004 | 2,57518838288977E+000 | 1,57246412781783E+000 | -1,38816031015934E-004 | 1,90081049994095E+000 | Sheet Pile & Bored Pile Installation [Phase_2] | |
4 | 482 | 5 | -7,50000000000000E+000 | 1,50000000000000E+000 | -7,77577744847458E+000 | -7,77577744847458E+000 | 1,52355267939873E-005 | 2,52169863006703E+000 | -9,15371077159161E-005 | 2,52169863006703E+000 | 2,37997413435782E+000 | -1,71294619587021E-004 | 2,68589307815659E+000 | Sheet Pile & Bored Pile Installation [Phase_2] |
2570
Upon further inspection, there are 2 issues with the data and we need to clean the data first:
# Before we go to the data cleaning, lets remove irrelevant columns from the dataframe.
# Since we are only interested in the bending moment, let's just keep the column structural element, stage_name, x [m], y [m] and M [kN/m]
df_plate_bendingmoment = df_plate_bendingmoment[['Structural element', 'stage_name', 'x [m]', 'y [m]', 'M [kN m/m]']]
# You should get something similar to below
display(df_plate_bendingmoment)
Structural element | stage_name | x [m] | y [m] | M [kN m/m] | |
---|---|---|---|---|---|
0 | Plate\_1\_1 | Sheet Pile & Bored Pile Installation [Phase_2] | -7,50000000000000E+000 | 2,85000000000000E+000 | -7,71605002114484E-015 |
1 | Element 1-1 (Plate) | Sheet Pile & Bored Pile Installation [Phase_2] | -7,50000000000000E+000 | 2,51250000000000E+000 | 2,68011318691106E-001 |
2 | (ERSS) | Sheet Pile & Bored Pile Installation [Phase_2] | -7,50000000000000E+000 | 2,17500000000000E+000 | 8,34746953403614E-001 |
3 | Sheet Pile & Bored Pile Installation [Phase_2] | -7,50000000000000E+000 | 1,83750000000000E+000 | 1,57246412781783E+000 | |
4 | Sheet Pile & Bored Pile Installation [Phase_2] | -7,50000000000000E+000 | 1,50000000000000E+000 | 2,37997413435782E+000 | |
... | ... | ... | ... | ... | ... |
2565 | Plate\_2\_16 | Cast Base Slab [Phase_10] | 7,50000000000000E+000 | -1,70000000000000E+001 | -4,00645821169241E+000 |
2566 | Element 51-104 (Plate) | Cast Base Slab [Phase_10] | 7,50000000000000E+000 | -1,71250000000000E+001 | -3,13059021727124E+000 |
2567 | (ERSS) | Cast Base Slab [Phase_10] | 7,50000000000000E+000 | -1,72500000000000E+001 | -2,06910301377388E+000 |
2568 | Cast Base Slab [Phase_10] | 7,50000000000000E+000 | -1,73750000000000E+001 | -9,75845611525455E-001 | |
2569 | Cast Base Slab [Phase_10] | 7,50000000000000E+000 | -1,75000000000000E+001 | -3,26128013483640E-014 |
2570 rows × 5 columns
As seen in the figure below, the data in the column structural element is not fully populated and each row has different value for the same plate element (e.g. row 0 to 4 belong to Plate_1_1). The goal here is to populate the columns with the respective plates (e.g. row to 0 to 4 would contain Plate_1_1)
Before:
After:
# From the figures above, we know that the data in the rows of multiplication of five consist of the plate name and numbers
# we can then assign the plate number from each of the row of multiplication of five to the next 4 rows
# (e.g. row 1-4 have the plate name and number of row 0, row 6-9 have the plate name and number of row 5, etc.)
# Create a temporary list to store the structural element column from the dataframe
structural_element_list = list(df_plate_bendingmoment['Structural element'])
# Loop through each element in the list
for x,i in enumerate(structural_element_list):
if (x % 5 == 0):
# store the plate name and number in a temporary variable
tmp = i
else:
# assign the plate name and number to the other rows
structural_element_list[x] = tmp
# Assign the list back to the column in the original dataframe
df_plate_bendingmoment['Structural element'] = structural_element_list
display(df_plate_bendingmoment)
Structural element | stage_name | x [m] | y [m] | M [kN m/m] | |
---|---|---|---|---|---|
0 | Plate\_1\_1 | Sheet Pile & Bored Pile Installation [Phase_2] | -7,50000000000000E+000 | 2,85000000000000E+000 | -7,71605002114484E-015 |
1 | Plate\_1\_1 | Sheet Pile & Bored Pile Installation [Phase_2] | -7,50000000000000E+000 | 2,51250000000000E+000 | 2,68011318691106E-001 |
2 | Plate\_1\_1 | Sheet Pile & Bored Pile Installation [Phase_2] | -7,50000000000000E+000 | 2,17500000000000E+000 | 8,34746953403614E-001 |
3 | Plate\_1\_1 | Sheet Pile & Bored Pile Installation [Phase_2] | -7,50000000000000E+000 | 1,83750000000000E+000 | 1,57246412781783E+000 |
4 | Plate\_1\_1 | Sheet Pile & Bored Pile Installation [Phase_2] | -7,50000000000000E+000 | 1,50000000000000E+000 | 2,37997413435782E+000 |
... | ... | ... | ... | ... | ... |
2565 | Plate\_2\_16 | Cast Base Slab [Phase_10] | 7,50000000000000E+000 | -1,70000000000000E+001 | -4,00645821169241E+000 |
2566 | Plate\_2\_16 | Cast Base Slab [Phase_10] | 7,50000000000000E+000 | -1,71250000000000E+001 | -3,13059021727124E+000 |
2567 | Plate\_2\_16 | Cast Base Slab [Phase_10] | 7,50000000000000E+000 | -1,72500000000000E+001 | -2,06910301377388E+000 |
2568 | Plate\_2\_16 | Cast Base Slab [Phase_10] | 7,50000000000000E+000 | -1,73750000000000E+001 | -9,75845611525455E-001 |
2569 | Plate\_2\_16 | Cast Base Slab [Phase_10] | 7,50000000000000E+000 | -1,75000000000000E+001 | -3,26128013483640E-014 |
2570 rows × 5 columns
Comma is used in the decimals and it should be converted to dot in Python. Further, the numbers are now of string type and they should be converted to float type.
# Loop through each of the x[m], y[m] & M[kN m/m] columns
for column in ['x [m]', 'y [m]', 'M [kN m/m]']:
# Create a temporary list to store the data from each column
temp_list = df_plate_bendingmoment[column]
# Replace comma with dot for each element in the list
temp_list = [x.replace(',','.') for x in temp_list]
# Convert to float for each element in the list
temp_list = [float(x) for x in temp_list]
# Assign the list back to the column in the original dataframe
df_plate_bendingmoment[column] = temp_list
#Loop through each unique stage
for stage in df_plate_bendingmoment['stage_name'].unique():
#Loop through each plate member ['Plate\\\_1', 'Plate\\\_2']
for plate in ['Plate\\\_1', 'Plate\\\_2']:
df_filtered = df_plate_bendingmoment[(df_plate_bendingmoment['stage_name'] == stage) & (df_plate_bendingmoment['Structural element'].str.contains(plate))].reset_index(drop=True)
# Create a figure
plt.figure(figsize=(4, 8))
# Plot the results
plt.plot(df_filtered['M [kN m/m]'], df_filtered['y [m]'])
# Plot the grids
plt.grid(which='major')
# Set titles, x- and y-labels
plt.title('Bending Moment for plate '+plate[-1]+' in stage: \n'+ stage + '\n', fontsize=12)
plt.xlabel('Bending Moment M [kN m/m]', fontsize=10)
plt.ylabel('y-coordinates', fontsize=10)
# Rotate figures in x-axis
plt.xticks(rotation=45)
plt.savefig('Plate'+ plate[-1] + ' ' + stage + '.png')
The cell below is for setting the style of this document. It's not part of the exercises.
from IPython.display import HTML
HTML('<style>{}</style>'.format(open('../css/cowi.css').read()))