-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata_site.py
More file actions
134 lines (110 loc) · 4.24 KB
/
metadata_site.py
File metadata and controls
134 lines (110 loc) · 4.24 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import pandas as pd
import datetime
import numpy as np
import argparse
parser = argparse.ArgumentParser(description='Silent City Meta data genetor')
parser.add_argument('--folder', default=None, type=str, help='Path to folder with meta data')
parser.add_argument('--save_path', default=None, type=str, help='Path to save meta data by site')
parser.add_argument('--database', default=None, help='database (csv)')
parser.add_argument('--verbose', action='store_true', help='Verbose (default False = nothing printed)')
args = parser.parse_args()
if args.folder is None:
raise(AttributeError("Must provide either a file or a folder"))
if not(args.database[-3:] == 'csv') and args.database is not None:
raise(AttributeError("Must provide CSV database"))
DATABASE = pd.read_csv(args.database)
DATABASE = DATABASE.loc[:, ~DATABASE.columns.str.contains('^Unnamed')]
DATABASE = DATABASE.reset_index(drop=True)
if not(DATABASE.partID.dtype == 'int64'):
raise('error part ID (must be int64) not {}'.format(DATABASE.partID.dtype))
DATABASE['mar'] = None
DATABASE['apr'] = None
DATABASE['may'] = None
DATABASE['jun'] = None
DATABASE['jul'] = None
DATABASE['aug'] = None
DATABASE['sep'] = None
DATABASE['error'] = None
DATABASE['nb_file'] = None
DATABASE['sr'] = None
DATABASE['len'] = None
DATABASE['sr_median'] = None
DATABASE['len_median'] = None
DATABASE['min_dB'] = None
DATABASE['ref_file'] = None
DATABASE['error_date'] = False
nb_rec_month = {'03' : 4464,
'04' : 4320,
'05' : 4464,
'06' : 4320,
'07' : 4464,
'08' : 4464,
'09' : 4320,}
month_cov = {'03' : 'mar',
'04' : 'apr',
'05' : 'may',
'06' : 'jun',
'07' : 'jul',
'08' : 'aug',
'09' : 'sep'}
print(DATABASE.columns)
if not(os.path.exists(args.save_path)):
os.mkdir(args.save_path)
filelist = []
for root, dirs, files in os.walk(args.folder, topdown=False):
for name in files:
if not('error' in name) and len(name) == 8 and name[-3:].casefold() == 'pkl':
filelist.append(os.path.join(root, name))
print(filelist)
filelist.sort()
print(filelist)
def process_site(database, file_):
partID = int(os.path.basename(file_[:-4]))
try:
partIDidx = database[database.partID == partID].index[0]
except IndexError:
return database
error_date = False
df = pd.read_pickle(file_)
df_error = pd.read_pickle(file_[:-4]+'_error.pkl')
nb_error = len(df_error)
nb_file = len(df)
if nb_file > 1 :
nb_month = {'03' : 0,
'04' : 0,
'05' : 0,
'06' : 0,
'07' : 0,
'08' : 0,
'09' : 0}
database.loc[partIDidx,'error'] = nb_error
database.loc[partIDidx,'nb_file'] = nb_file
database.loc[partIDidx,'sr'] = df['sr'].mean()
database.loc[partIDidx,'len'] = df['length'].mean() / database.loc[partIDidx,'sr']
database.loc[partIDidx,'sr_median'] = df['sr'].median()
database.loc[partIDidx,'len_median'] = df['length'].median() / database.loc[partIDidx,'sr_median']
for idx, filename in enumerate(df['filename']):
if idx == 0:
ref_dB = df['dB'][idx]
ref_dB_file = df['filename'][idx]
date = df['datetime'][idx]
try:
nb_month['%02d' % date.month] += 1
except:
error_date = True
if ref_dB > df['dB'][idx]:
ref_dB_file = df['filename'][idx]
ref_dB = df['dB'][idx]
database.loc[partIDidx,'min_dB'] = ref_dB
database.loc[partIDidx,'ref_file'] = ref_dB_file
if error_date:
database.loc[partIDidx,'error_date'] = True
for key in nb_month.keys():
database.loc[partIDidx,month_cov[key]] = nb_month[key]/nb_rec_month[key]*100
print(file_)
return database
for file_ in filelist:
DATABASE = process_site(DATABASE.copy(deep = True), file_)
DATABASE.to_pickle(os.path.join(args.save_path,'database_pross.pkl'))
DATABASE.to_csv(os.path.join(args.save_path,'database_pross.csv'))