-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollateIntensitycsv.py
107 lines (65 loc) · 2.6 KB
/
collateIntensitycsv.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 25 16:15:26 2019
@author: mjsf3
"""
import pandas as pd
import numpy as np
import os
from glob import glob
import sys
def get_unique_id_prefix(filename):
integer = filename[filename.find('Pos') + 3]
try:
integer = int(integer)
except ValueError:
print('Check if the file path is correct, this code assumes that the filepath is of the form ...PosN.. where N is a valid integer')
#check if next character is also an integer
nextdigit = filename[filename.find('Pos') + 4]
try:
nextdigit = int(nextdigit)
#if there are two digits, we could have 00 0N or NM where N,M are single digit integers
key_to_append = str(integer)+str(nextdigit)
except ValueError:
#if next character can't be made to an integer we must only have one integer present. Now we know this we have to append a 0 to the front to make it a unique id
key_to_append = '0'+str(integer)
#if there are two digits, we could have 00 0N or NM where N,M are single digit integers
return key_to_append
def create_unique_id(dictionary,keytoappend):
dictionary2 = {}
for key in list(dictionary.keys()):
dictionary2[keytoappend+key] = dictionary.pop(key)
df = pd.DataFrame.from_dict(dictionary2)
return df
if __name__ == '__main__':
if len(sys.argv) >3:
path = sys.argv[1]
file_pattern = sys.argv[2]
output_name = sys.argv[3]
else:
path = '/home/mjsf3/Desktop/Control/2'
try:
os.chdir(path)
except:
print('No valid path input through CLI')
sys.exit()
print(os.getcwd())
nums = np.arange(1,10)
intensity_files = glob(file_pattern)
print(intensity_files)
key_to_append = get_unique_id_prefix(intensity_files[0])
all_data = pd.read_csv(intensity_files[0])
all_data = all_data.drop('Unnamed: 0',1)
print(all_data.keys())
all_data = create_unique_id(all_data,key_to_append)
#DEPRECATED: all_data = sort_df(all_data,intensity_files[0],twodigitflag)
for file in intensity_files[1:]:
key_to_append = get_unique_id_prefix(file)
df = pd.read_csv(file)
df= df.drop('Unnamed: 0',1)
temp_dict = df.to_dict()
df= create_unique_id(temp_dict,key_to_append)
print(all_data.keys())
all_data = all_data.join(df)
all_data.to_csv(output_name)