-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathIR_Model_HeatMap.py
More file actions
108 lines (81 loc) · 3.51 KB
/
Copy pathIR_Model_HeatMap.py
File metadata and controls
108 lines (81 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -*- coding: utf-8 -*-
"""
author: Jack Palmer
email: jpalmer1028@gmail.com
"""
##############################################################################
################################ USER INPUTS #################################
##############################################################################
args = {
# Title for the map
'Plot_Title' : 'Test Map',
# Fit data export folder
'Folder_Path' : 'C:/Users/someuser/folder_with_data',
# Which components to plot. Must match column names in 'curves'
'Components' : [623] #503, 515, 544, 623, 567, 580, 591
}
##############################################################################
##############################################################################
##############################################################################
def heatmap(args):
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from natsort import natsorted #3rd party library for natural sorting
# Store all file names in the 'curves' folder
files = os.listdir(args['Folder_Path']+'/curves')
# Create a list keys and store the file names minus '.csv' there
keys = []
for i in range(len(files)):
keys.append(files[i][:-4])
#sort the list keys in a natural order
keys = natsorted(keys)
# Create a list 'counter' and populate with the spectrum number
counter = []
for i in range(len(files)):
counter.append(keys[i].split(sep='_')[-1])
# Extract all data from the curves folder and store to the dict 'curves'
curves = {}
for i in range(len(files)):
curves[keys[i]] = pd.read_csv(args['Folder_Path'] + '/curves/' + files[i])
# Create a df with counters as the column names
data = pd.DataFrame(columns=counter)
# Add wavenumbers to the df
data['Wavenumber'] = curves[keys[0]]['Wavenumber']
# Replace all nan's with zeros
data = data.fillna(0)
# For each spectrum, sum the desired components and store to the df
for i in range(len(keys)):
for x in args['Components']:
data[counter[i]] = data[counter[i]] + curves[keys[i]]['Component_'+str(x)]
# Round the wavenumbers in anticipation for making them the indices of the df
data['Wavenumber'] = data['Wavenumber'].apply(lambda x: np.round(x))
# Set wavenumbers as indicies
data = data.set_index('Wavenumber')
# To prepare the df for plotting, do the following transformations:
# Transpose the df
data = data.transpose()
# Flip the order of the columns
columns = data.columns.tolist()
columns = columns[::-1]
data = data[columns]
# Flip the order of the rows
data = data[::-1]
# Store column names (wavenumbers) to x
x = list(data.columns)
# Store the indices (spectrum number) to y
y = []
for i in range(len(data.index)):
y.append(int(list(data.index)[i]))
# Create a figure and plot the data
plt.figure(figsize=(5,5), dpi = 200)
plt.pcolormesh(x, y,data, cmap = 'viridis', shading = 'nearest')
plt.xlabel("Wavenumber ($cm^{-1}$)", fontsize=12)
plt.ylabel("Spectrum Number", fontsize=12)
plt.title(args['Plot_Title'])
# Invert the x-axis to show higher energy wavenumbers on the right
ax = plt.gca()
ax.invert_xaxis()
# call the function
heatmap(args)