forked from nilesh-patil/python-XTensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXT_join_files.py
More file actions
110 lines (87 loc) · 3.96 KB
/
XT_join_files.py
File metadata and controls
110 lines (87 loc) · 3.96 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
# Imaris XTension
#
# Copyright (C) 2018 Nilesh patil <nilesh.patil@rochester.edu>, MIT license
#
# <CustomTools>
# <Menu name = "Python plugins">
# <Submenu name = "Data processing">
# <Item name="Combine 2 datasets" icon="Python" tooltip="Combine two datasets to create a single output">
# <Command>PythonXT::XT_join_files(%i)</Command>
# </Item>
# </Submenu>
# </Menu>
# </CustomTools>
import time
import ImarisLib
import os
import pandas as pd
from cvbi.gui import *
# Get All Statistics
def XT_join_files(aImarisId):
print('''
####################################################################################
########################### Extension started ##############################
####################################################################################
''')
time.sleep(5)
vImarisLib = ImarisLib.ImarisLib()
vImaris = vImarisLib.GetApplication(aImarisId)
imaris_file = vImaris.GetCurrentFileName()
imaris_dir = os.path.dirname(imaris_file)
imaris_name = os.path.basename(imaris_file)
print('\nSelect first file to combine\n')
file_01 = get_file( window_title= 'Select File to read : ' ,
initial_dir = imaris_dir ,
filetypes = (("txt files","*.txt"),
("csv files","*.csv"),
("all files","*.*")),
w = 600 , h=400)
sep_01 = '|'
data_left = pd.read_csv(file_01, sep =sep_01)
print('\nSelect second file to combine\n')
file_02 = get_file( window_title= 'Select File to read : ' ,
initial_dir = imaris_dir ,
filetypes = (("txt files","*.txt"),
("csv files","*.csv"),
("all files","*.*")),
w = 600 , h=400 )
sep_02 = '|'
data_right = pd.read_csv(file_02, sep =sep_02)
# Join Files
join_formats = ["Rows", "Columns"]
join_format = create_window_from_list(object_list=join_formats, window_title='How are you going to combine?')
if join_format == 'Columns':
join_types = ['left', 'inner']
join_type = create_window_from_list(object_list=join_types, window_title='How are you going to combine?')
time.sleep(2)
data_out = pd.merge(left = data_left,
right = data_right,
how=join_type,
on='trackID',
suffixes = ('', '_right'))
else:
data_out = pd.concat([data_left, data_right], ignore_index = True)
try:
output_dir = get_dir( window_title = 'Select folder to save output' , initial_dir=imaris_dir )
output_file = imaris_name+'_combined.txt'
output_file = create_window_for_input(default=output_file,
w=700, h=200,
window_text='Modify the file name for any changes',
window_title='Provide your output file name')
output_path = output_dir+'/'+output_file
data_out.to_csv(output_path, index=False, sep='|')
except:
print('''
Calculations finished successfully but there was an error in saving your dataset. \n
Try closing other open Imaris applications and then run this XTension. \n
Please do not forget to save your work.\n
Please contact Nilesh Patil : nilesh.patil@rochester.edu if this problem persists \n
''')
time.sleep(5)
return
print('''
####################################################################################
######### Extension finished, wait for 5s to close automatically ###########
####################################################################################
''')
time.sleep(5)