| 1 |
""" |
| 2 |
Extract forces and extension timeseries data from Excel spreadsheet files (.xls) provided by Michael T. Woodside |
| 3 |
|
| 4 |
REQUIREMENTS: |
| 5 |
|
| 6 |
* xlrd (pure Python Excel spreadsheet (.xls) file reader) - http://www.lexicon.net/sjmachin/xlrd.htm |
| 7 |
""" |
| 8 |
|
| 9 |
# ============================================================================================= |
| 10 |
# IMPORTS |
| 11 |
# ============================================================================================= |
| 12 |
|
| 13 |
import sys |
| 14 |
import bz2 |
| 15 |
import tempfile |
| 16 |
from pathlib import Path |
| 17 |
|
| 18 |
from numpy import zeros, float64 |
| 19 |
|
| 20 |
try: |
| 21 |
import xlrd # pure Python Excel spreadsheet (.xls) file reader |
| 22 |
except ImportError: |
| 23 |
print("ERROR: This example requires xlrd, run:", " pip install xlrd", sep="\n") |
| 24 |
sys.exit() |
| 25 |
|
| 26 |
# ============================================================================================= |
| 27 |
# PARAMETERS |
| 28 |
# ============================================================================================= |
| 29 |
# location of original data (.xls files) |
| 30 |
original_data_directory = Path("original-data") |
| 31 |
# location for where processed data is to be stored |
| 32 |
processed_data_directory = Path("processed-data") |
| 33 |
# prefixes of datasets to process (filenames are f'({prefix}_data.xls') |
| 34 |
datasets = ["20R55_4T"] |
| 35 |
|
| 36 |
# ============================================================================================= |
| 37 |
# SUBROUTINES |
| 38 |
# ============================================================================================= |
| 39 |
|
| 40 |
# ============================================================================================= |
| 41 |
# MAIN |
| 42 |
# ============================================================================================= |
| 43 |
|
| 44 |
|
| 45 |
def main(): |
| 46 |
ORIGINAL_DATA = Path(original_data_directory) |
| 47 |
PROCESSED_DATA = Path(processed_data_directory) |
| 48 |
# process all datasets |
| 49 |
for dataset in datasets: |
| 50 |
print(f"Extracting data from dataset '{dataset}'...") |
| 51 |
|
| 52 |
# Extract compressed Excel spreadsheet file and load it with xlrd |
| 53 |
with bz2.open(ORIGINAL_DATA / f"{dataset}_data.xls.bz2") as f: |
| 54 |
with tempfile.NamedTemporaryFile(suffix=".xls", delete=False) as temp: |
| 55 |
temp.write(f.read()) |
| 56 |
workbook = xlrd.open_workbook(temp.name) |
| 57 |
|
| 58 |
# DEBUG |
| 59 |
print( |
| 60 |
f"Workbook contains {workbook.nsheets:d} worksheets:", workbook.sheet_names(), sep="\n" |
| 61 |
) |
| 62 |
|
| 63 |
# Get the first worksheet. |
| 64 |
worksheet = workbook.sheet_by_index(0) |
| 65 |
|
| 66 |
# DEBUG |
| 67 |
print( |
| 68 |
f"1st worksheet '{worksheet.name}' has {worksheet.ncols:d} columns and {worksheet.nrows:d} rows." |
| 69 |
) |
| 70 |
|
| 71 |
# Extract biasing forces. |
| 72 |
K = worksheet.ncols - 1 |
| 73 |
biasing_force_k = zeros([K], float64) |
| 74 |
for k in range(K): |
| 75 |
biasing_force_k[k] = worksheet.cell_value(rowx=1, colx=1 + k) |
| 76 |
print(f"{K:d} biasing forces (in pN):", biasing_force_k, sep="\n") |
| 77 |
|
| 78 |
# Write biasing forces. |
| 79 |
filename = PROCESSED_DATA / f"{dataset}.forces" |
| 80 |
print(f"Writing biasing forces to '{filename}'...") |
| 81 |
with open(filename, "w") as outfile: |
| 82 |
for k in range(K): |
| 83 |
if k > 0: |
| 84 |
outfile.write(" ") |
| 85 |
outfile.write(f"{biasing_force_k[k]:.2f}") |
| 86 |
|
| 87 |
# Read trajectories. |
| 88 |
T_max = worksheet.nrows - 3 |
| 89 |
x_kt = zeros([K, T_max]) |
| 90 |
for k in range(K): |
| 91 |
print(f"Reading trajectory {k + 1:d} / {K + 1:d}...") |
| 92 |
for t in range(T_max): |
| 93 |
x_kt[k, t] = worksheet.cell_value(colx=1 + k, rowx=3 + t) |
| 94 |
print(f"Read {K:d} trajectories of {T_max:d} samples each.") |
| 95 |
|
| 96 |
# Write trajectories. |
| 97 |
filename = PROCESSED_DATA / f"{dataset}.trajectories" |
| 98 |
print(f"Writing trajectories to '{filename}'...") |
| 99 |
with open(filename, "w") as outfile: |
| 100 |
for t in range(T_max): |
| 101 |
for k in range(K): |
| 102 |
if k > 0: |
| 103 |
outfile.write(" ") |
| 104 |
outfile.write(f"{x_kt[k, t]:f}") |
| 105 |
outfile.write("\n") |
| 106 |
|
| 107 |
|
| 108 |
if __name__ == "__main__": |
| 109 |
main() |