flimanalyzer 0.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. flim/__init__.py +2 -0
  2. flim/_version.py +5 -0
  3. flim/analysis/__init__.py +1 -0
  4. flim/analysis/ml/__init__.py +0 -0
  5. flim/analysis/ml/autoencoder.py +188 -0
  6. flim/analyzerapp.py +131 -0
  7. flim/batch.py +235 -0
  8. flim/configs/__init__.py +77 -0
  9. flim/configs/analysis.py +554 -0
  10. flim/configs/data.py +278 -0
  11. flim/configs/plot.py +543 -0
  12. flim/configs/workflow.py +204 -0
  13. flim/core/__init__.py +0 -0
  14. flim/core/calculator.py +0 -0
  15. flim/core/config.py +305 -0
  16. flim/core/execution.py +110 -0
  17. flim/core/fields.py +226 -0
  18. flim/core/filter.py +179 -0
  19. flim/core/graph.py +111 -0
  20. flim/core/headers.py +72 -0
  21. flim/core/importer.py +269 -0
  22. flim/core/parser.py +305 -0
  23. flim/core/paths.py +66 -0
  24. flim/core/prefect_server.py +152 -0
  25. flim/core/preprocessor.py +287 -0
  26. flim/core/registry.py +108 -0
  27. flim/core/runner.py +703 -0
  28. flim/core/schema.py +114 -0
  29. flim/core/settings.py +419 -0
  30. flim/core/tool.py +284 -0
  31. flim/core/tools.py +76 -0
  32. flim/core/types.py +113 -0
  33. flim/core/validation.py +272 -0
  34. flim/core/workflow.py +148 -0
  35. flim/gui/__init__.py +12 -0
  36. flim/gui/app.py +953 -0
  37. flim/gui/configdialog.py +495 -0
  38. flim/gui/datapanel.py +841 -0
  39. flim/gui/delimpanel.py +56 -0
  40. flim/gui/dialogs/__init__.py +473 -0
  41. flim/gui/dialogs/autoencoder.py +89 -0
  42. flim/gui/dialogs/categoryorder.py +130 -0
  43. flim/gui/dialogs/filter.py +86 -0
  44. flim/gui/dialogs/unpivot.py +72 -0
  45. flim/gui/dicttablepanel.py +178 -0
  46. flim/gui/editors.py +1578 -0
  47. flim/gui/events.py +162 -0
  48. flim/gui/icons.py +16 -0
  49. flim/gui/importdlg.py +391 -0
  50. flim/gui/listcontrol.py +472 -0
  51. flim/gui/macos.py +77 -0
  52. flim/gui/plotframe.py +73 -0
  53. flim/gui/progressdlg.py +290 -0
  54. flim/resources/aeaugment.png +0 -0
  55. flim/resources/aefeatureworkflow.png +0 -0
  56. flim/resources/aerun.png +0 -0
  57. flim/resources/aetrain.png +0 -0
  58. flim/resources/aetuneworkflow.png +0 -0
  59. flim/resources/barplot.png +0 -0
  60. flim/resources/boxplot.png +0 -0
  61. flim/resources/categorize.png +0 -0
  62. flim/resources/concatenate.png +0 -0
  63. flim/resources/fileopen.png +0 -0
  64. flim/resources/filter.png +0 -0
  65. flim/resources/flimanalyzer.ico +0 -0
  66. flim/resources/flimanalyzer.png +0 -0
  67. flim/resources/flirrplot.png +0 -0
  68. flim/resources/flirrworkflow.png +0 -0
  69. flim/resources/gmm.png +0 -0
  70. flim/resources/heatmap.png +0 -0
  71. flim/resources/histogram.png +0 -0
  72. flim/resources/import.png +0 -0
  73. flim/resources/kde.png +0 -0
  74. flim/resources/kmeans.png +0 -0
  75. flim/resources/ks.png +0 -0
  76. flim/resources/lineplot.png +0 -0
  77. flim/resources/merge.png +0 -0
  78. flim/resources/order.png +0 -0
  79. flim/resources/pairplot.png +0 -0
  80. flim/resources/pca.png +0 -0
  81. flim/resources/pivot.png +0 -0
  82. flim/resources/randomforest.png +0 -0
  83. flim/resources/relchange.png +0 -0
  84. flim/resources/scatter.png +0 -0
  85. flim/resources/seriesanalysis.png +0 -0
  86. flim/resources/sort.png +0 -0
  87. flim/resources/split.png +0 -0
  88. flim/resources/summary.png +0 -0
  89. flim/resources/swarmplot.png +0 -0
  90. flim/resources/unpivot.png +0 -0
  91. flim/resources/violinplot.png +0 -0
  92. flim/resources/workflow.png +0 -0
  93. flim/results.py +70 -0
  94. flim/tools/__init__.py +7 -0
  95. flim/tools/autoencoder.py +662 -0
  96. flim/tools/common.py +62 -0
  97. flim/tools/data.py +301 -0
  98. flim/tools/ml.py +311 -0
  99. flim/tools/plot.py +1439 -0
  100. flim/tools/statistics.py +348 -0
  101. flim/utils.py +43 -0
  102. flim/workflow/__init__.py +3 -0
  103. flim/workflow/aefeature.py +145 -0
  104. flim/workflow/aetune.py +163 -0
  105. flim/workflow/basicflow.py +99 -0
  106. flimanalyzer-0.6.0.dist-info/METADATA +262 -0
  107. flimanalyzer-0.6.0.dist-info/RECORD +110 -0
  108. flimanalyzer-0.6.0.dist-info/WHEEL +4 -0
  109. flimanalyzer-0.6.0.dist-info/entry_points.txt +5 -0
  110. flimanalyzer-0.6.0.dist-info/licenses/LICENSE +19 -0
flim/core/importer.py ADDED
@@ -0,0 +1,269 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Fri May 4 02:53:40 2018
5
+
6
+ @author: khs3z
7
+ """
8
+
9
+ import itertools
10
+ import logging
11
+ import os
12
+ import glob
13
+ import pandas as pd
14
+
15
+ from flim.core.parser import PATTERN_CATEGORY, defaultparser
16
+ import flim.core.preprocessor as pp
17
+
18
+ DEFAULT_EXT = [".txt", ".csv"]
19
+
20
+
21
+ class dataimporter:
22
+ def __init__(self):
23
+ self.files = []
24
+ self.delimiter = "\t,"
25
+ self.data = pd.DataFrame()
26
+ self.excluded_files = []
27
+ self.parser = defaultparser()
28
+ self.preprocessor = None
29
+ self.combolist = None
30
+
31
+ def settings(self):
32
+ """The import settings (ImportSettings) of this importer."""
33
+ from flim.core.settings import ImportSettings
34
+
35
+ return ImportSettings(
36
+ include_files=list(self.files),
37
+ exclude_files=list(self.excluded_files),
38
+ delimiter=self.delimiter,
39
+ filename_parser=self.parser.settings(),
40
+ category_columns=self.get_reserved_categorycols(),
41
+ fitting_columns=[],
42
+ category_combinations=self.combolist,
43
+ )
44
+
45
+ def set_parser(self, parser):
46
+ if parser is not None:
47
+ self.parser = parser
48
+
49
+ def get_parser(self):
50
+ return self.parser
51
+
52
+ def set_preprocessor(self, preprocessor):
53
+ self.preprocessor = preprocessor
54
+
55
+ def get_preprocessor(self):
56
+ return self.preprocessor
57
+
58
+ def set_delimiter(self, delimiter):
59
+ if delimiter is not None:
60
+ self.delimiter = delimiter
61
+
62
+ def get_delimiter(self):
63
+ return self.delimiter
64
+
65
+ def set_column_combos(self, combolist):
66
+ self.combolist = combolist
67
+
68
+ def set_files(self, files, extensions=DEFAULT_EXT, exclude=None, sort=True):
69
+ if exclude is None:
70
+ exclude = self.excluded_files
71
+ self.files = []
72
+ self.add_files(files, extensions, exclude, sort)
73
+
74
+ def set_excluded_files(self, exclude):
75
+ if exclude is None:
76
+ exclude = []
77
+ self.excluded_files = exclude
78
+
79
+ def get_excluded_files(self):
80
+ return self.excluded_files
81
+
82
+ def add_files(self, files, extensions=DEFAULT_EXT, exclude=None, sort=True):
83
+ if files is None:
84
+ return None, 0
85
+ if exclude is None:
86
+ exclude = self.excluded_files
87
+ added_files = 0
88
+ skipped_files = 0
89
+ files = [os.path.abspath(f) for f in set(files)]
90
+ ext = tuple(extensions)
91
+ for f in files:
92
+ if os.path.isdir(f):
93
+ filesindir = glob.glob(os.path.join(f, "*"))
94
+ added, skipped = self.add_files(
95
+ filesindir, extensions=extensions, exclude=exclude
96
+ )
97
+ added_files += added
98
+ skipped_files += skipped
99
+ else:
100
+ if (
101
+ os.path.isfile(f)
102
+ and f.endswith(ext)
103
+ and not f in self.files
104
+ and not os.path.basename(f) in exclude
105
+ ):
106
+ self.files.append(f)
107
+ added_files += 1
108
+ else:
109
+ skipped_files += 1
110
+ if sort:
111
+ self.files = sorted(self.files)
112
+ return (added_files, skipped_files)
113
+
114
+ def remove_files(self, rfiles):
115
+ logging.debug(f"removing {rfiles}")
116
+ if rfiles is not None:
117
+ self.files = [f for f in self.files if f not in rfiles]
118
+
119
+ def remove_allfiles(self):
120
+ self.files = []
121
+
122
+ def get_files(self):
123
+ return self.files
124
+
125
+ def get_reserved_categorycols(self, parser=None):
126
+ if parser is None:
127
+ parser = self.parser
128
+ rcatnames = [
129
+ "Cell line",
130
+ "Category",
131
+ "Cat",
132
+ "Cluster",
133
+ "FOV",
134
+ "Well",
135
+ "Cell",
136
+ "Treatment",
137
+ "Treatment 1",
138
+ "Treatment 2",
139
+ "Treatment 1 & 2",
140
+ "Time",
141
+ "Compartment",
142
+ "New Cat",
143
+ "Origin",
144
+ "AE Label",
145
+ "Source",
146
+ "Time",
147
+ "Combined",
148
+ "Cell line-Compartment",
149
+ "Cell line-FOV",
150
+ "Cell line-Treatment",
151
+ "Cell line-Cell",
152
+ "Compartment-Cell",
153
+ "Compartment-FOV",
154
+ "Compartment-Treatment",
155
+ "FOV-Time",
156
+ "FOV-Well",
157
+ "Time-Well",
158
+ "Treatment-Time",
159
+ "Treatment-FOV",
160
+ "FOV-Cell",
161
+ "FOV-Treatment",
162
+ "Treatment-Cell",
163
+ ]
164
+ rcatnames.extend(
165
+ [
166
+ entry[PATTERN_CATEGORY]
167
+ for entry in parser.get_regexpatterns()
168
+ for key in entry
169
+ ]
170
+ )
171
+ return sorted(set(rcatnames))
172
+
173
+ def import_data(self, delimiter=None, parser=None, preprocessor=None, nrows=None):
174
+ if delimiter is None:
175
+ delimiter = self.delimiter
176
+ delimiter = "|".join(delimiter)
177
+ if parser is None:
178
+ parser = self.parser
179
+ if preprocessor is None:
180
+ preprocessor = self.preprocessor
181
+ dflist = []
182
+ filenames = []
183
+ fheaders = []
184
+ comboheaders = []
185
+ cdflist = []
186
+ for f in self.files:
187
+ if not os.path.isfile(f):
188
+ continue
189
+ # columns defined by parser regexpatterns will use 'category' as dtype
190
+ category_dtypes = {
191
+ col: "object" for col in self.get_reserved_categorycols(parser)
192
+ }
193
+ df = pd.read_table(
194
+ f,
195
+ delimiter=delimiter,
196
+ engine="python",
197
+ dtype=category_dtypes,
198
+ nrows=nrows,
199
+ )
200
+ headers = parser.parsefilename(f)
201
+ for key in headers:
202
+ df[key] = headers[key]
203
+ if preprocessor is not None:
204
+ df, ch = preprocessor.rename_headers(df)
205
+ df, dl = preprocessor.drop_columns(df)
206
+ dflist.append(df)
207
+ fheaders.extend(list(headers.keys()))
208
+ filenames.append(f)
209
+
210
+ filecdf = None
211
+ if self.combolist is not None:
212
+ if len(self.combolist) == 0:
213
+ categories = [
214
+ cat
215
+ for cat in category_dtypes
216
+ if cat in df.select_dtypes(["object"]).columns
217
+ ]
218
+ self.combolist = [
219
+ combo for combo in itertools.combinations(categories, 2)
220
+ ]
221
+ for combo in self.combolist:
222
+ combocol = "-".join(combo)
223
+ cdf = pd.DataFrame(
224
+ df[combo[0]]
225
+ .str.cat([df[c] for c in combo[1:]], sep="-")
226
+ .astype("category")
227
+ ).reset_index(drop=True)
228
+ cdf.columns = [combocol]
229
+ if filecdf is None:
230
+ filecdf = cdf
231
+ else:
232
+ filecdf = pd.concat([filecdf, cdf], axis=1) # add col
233
+ comboheaders.append(combocol)
234
+ if filecdf is not None:
235
+ cdflist.append(filecdf)
236
+
237
+ if len(dflist) == 0:
238
+ return None, filenames, set()
239
+ else:
240
+ fheaders = set(fheaders + comboheaders)
241
+ df = pd.concat(dflist).reset_index(drop=True)
242
+ if len(cdflist) > 0:
243
+ cdf = pd.concat(cdflist).reset_index(drop=True)
244
+ df = pd.concat([df, cdf], axis=1, copy=True)
245
+ # df.reset_index(inplace=True, drop=True)
246
+
247
+ # if 'ROI' not in df.columns.values:
248
+ # df['ROI'] = np.arange(1,len(df)+1)
249
+ # print ('done')
250
+ allheaders = list(df.columns.values)
251
+ logging.debug(self.get_reserved_categorycols(parser))
252
+ categories = [
253
+ key
254
+ for key in self.get_reserved_categorycols(parser)
255
+ if key in allheaders
256
+ ] + comboheaders
257
+ """df['ROI'] = df.groupby(categories).cumcount() + 1
258
+ df['ROI'] = [str(roi) for roi in df['ROI']]
259
+ categories.append('ROI')"""
260
+
261
+ for ckey in categories:
262
+ df[ckey] = df[ckey].astype("category")
263
+ if preprocessor is None:
264
+ dp = pp.defaultpreprocessor()
265
+ df = dp.reorder_columns(df)
266
+ else:
267
+ df = preprocessor.reorder_columns(df)
268
+ df, _, _ = preprocessor.calculate(df)
269
+ return df, filenames, fheaders
flim/core/parser.py ADDED
@@ -0,0 +1,305 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Fri May 4 04:02:09 2018
5
+
6
+ @author: khs3z
7
+ """
8
+
9
+ import logging
10
+ import os
11
+ import re
12
+ import importlib
13
+ import pkgutil
14
+
15
+
16
+ def get_available_parsers(pkdir="core.parser"):
17
+ for module_loader, name, ispkg in pkgutil.iter_modules([pkdir]):
18
+ importlib.import_module("." + name, __package__)
19
+ parser_classes = {cls.__name__: cls for cls in defaultparser.__subclasses__()}
20
+ return parser_classes
21
+
22
+
23
+ def instantiate_parser(fullname):
24
+ namesplit = fullname.rpartition(".")
25
+ if len(namesplit) != 3:
26
+ return None
27
+ modulename = fullname.rpartition(".")[0]
28
+ classname = fullname.rpartition(".")[2]
29
+ logging.debug(f"Parser modulename={modulename}, classname={classname}")
30
+ try:
31
+ module = importlib.import_module(modulename)
32
+ class_ = getattr(module, classname)
33
+ parserinstance = class_()
34
+ except Exception:
35
+ logging.error(f"Error instantiating {fullname} parser.")
36
+ logging.error(Exception)
37
+ parserinstance = None
38
+ return parserinstance
39
+
40
+
41
+ # keys of a pattern: {PATTERN_USE: bool, PATTERN_CATEGORY: column, PATTERN_REGEX: regex}
42
+ PATTERN_USE = "Use"
43
+ PATTERN_CATEGORY = "Category"
44
+ PATTERN_REGEX = "Regex"
45
+
46
+
47
+ class defaultparser(object):
48
+ def __init__(self):
49
+ self.init_patterns()
50
+ self.compile_patterns()
51
+
52
+ def is_readonly(self):
53
+ return True
54
+
55
+ def get_name(self):
56
+ return "Default Parser"
57
+
58
+ def init_patterns(self):
59
+ self.regexpatterns = []
60
+
61
+ def compile_patterns(self):
62
+ if self.regexpatterns is not None and len(self.regexpatterns) > 0:
63
+ self.compiledpatterns = {
64
+ rp[PATTERN_CATEGORY]: re.compile(rp[PATTERN_REGEX])
65
+ for rp in self.regexpatterns
66
+ if rp[PATTERN_USE]
67
+ }
68
+ else:
69
+ self.compiledpatterns = {}
70
+
71
+ def settings(self):
72
+ """The parser (its class name) and its patterns, as ParserSettings."""
73
+ from flim.core.settings import ParserSettings
74
+
75
+ return ParserSettings(parser=type(self).__name__, patterns=self.regexpatterns)
76
+
77
+ def get_regexpatterns(self):
78
+ return self.regexpatterns
79
+
80
+ def set_regexpatterns(self, patterns):
81
+ self.regexpatterns = patterns
82
+ self.compile_patterns()
83
+
84
+ def parsefilename(self, fname):
85
+ components = {
86
+ "Directory": os.path.dirname(fname),
87
+ "File": os.path.basename(fname),
88
+ }
89
+ fname = fname.replace("\\", "/")
90
+ for entry in self.regexpatterns:
91
+ # convert \ in windows style path to / in POSIX style
92
+ if entry[PATTERN_USE]:
93
+ match = re.search(entry[PATTERN_REGEX], fname)
94
+ if match is not None:
95
+ matchstr = match.group(1)
96
+ if match.group(1) == "":
97
+ components[entry[PATTERN_CATEGORY]] = "?"
98
+ else:
99
+ components[entry[PATTERN_CATEGORY]] = str(matchstr)
100
+ return components
101
+
102
+
103
+ class no_parser(defaultparser):
104
+ def set_regexpatterns(self, patterns):
105
+ pass
106
+
107
+ def parsefilename(self, fname):
108
+ return {}
109
+
110
+
111
+ class celltype_compartment_fov_treatment_cell_parser(defaultparser):
112
+ def init_patterns(self):
113
+ self.regexpatterns = [
114
+ {
115
+ PATTERN_USE: True,
116
+ PATTERN_CATEGORY: "Cell line",
117
+ PATTERN_REGEX: r".*/(.*?)/.*/",
118
+ },
119
+ {
120
+ PATTERN_USE: True,
121
+ PATTERN_CATEGORY: "Compartment",
122
+ PATTERN_REGEX: r".*/(.*?)/",
123
+ },
124
+ {
125
+ PATTERN_USE: True,
126
+ PATTERN_CATEGORY: "FOV",
127
+ PATTERN_REGEX: r".*/.*?[_-](.*?)[_-]",
128
+ },
129
+ {
130
+ PATTERN_USE: True,
131
+ PATTERN_CATEGORY: "Treatment",
132
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-](.*?)[_-]",
133
+ },
134
+ {
135
+ PATTERN_USE: True,
136
+ PATTERN_CATEGORY: "Cell",
137
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-].*?[_-](\d*?)\.",
138
+ },
139
+ ]
140
+
141
+
142
+ class compartment_fov_treatment_cell_parser(defaultparser):
143
+ def init_patterns(self):
144
+ self.regexpatterns = [
145
+ {
146
+ PATTERN_USE: True,
147
+ PATTERN_CATEGORY: "Compartment",
148
+ PATTERN_REGEX: r".*/(.*?)/",
149
+ },
150
+ {
151
+ PATTERN_USE: True,
152
+ PATTERN_CATEGORY: "FOV",
153
+ PATTERN_REGEX: r".*/.*?[_-](.*?)[_-]",
154
+ },
155
+ {
156
+ PATTERN_USE: True,
157
+ PATTERN_CATEGORY: "Treatment",
158
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-](.*?)[_-]",
159
+ },
160
+ {
161
+ PATTERN_USE: True,
162
+ PATTERN_CATEGORY: "Cell",
163
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-].*?[_-](\d*?)\.",
164
+ },
165
+ ]
166
+
167
+
168
+ class compartment_treatment_fov_cell_parser(defaultparser):
169
+ def init_patterns(self):
170
+ self.regexpatterns = [
171
+ {
172
+ PATTERN_USE: True,
173
+ PATTERN_CATEGORY: "Compartment",
174
+ PATTERN_REGEX: r".*/(.*?)/",
175
+ },
176
+ {
177
+ PATTERN_USE: True,
178
+ PATTERN_CATEGORY: "Treatment",
179
+ PATTERN_REGEX: r".*/.*?[_-](.*?)[_-]",
180
+ },
181
+ {
182
+ PATTERN_USE: True,
183
+ PATTERN_CATEGORY: "FOV",
184
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-](.*?)[_-]",
185
+ },
186
+ {
187
+ PATTERN_USE: True,
188
+ PATTERN_CATEGORY: "Cell",
189
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-].*?[_-](\d*?)\.",
190
+ },
191
+ ]
192
+
193
+
194
+ class fov_treatment_cell_parser(defaultparser):
195
+ def init_patterns(self):
196
+ self.regexpatterns = [
197
+ {
198
+ PATTERN_USE: True,
199
+ PATTERN_CATEGORY: "FOV",
200
+ PATTERN_REGEX: r".*/.*?[_-](.*?)[_-]",
201
+ },
202
+ {
203
+ PATTERN_USE: True,
204
+ PATTERN_CATEGORY: "Treatment",
205
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-](.*?)[_-]",
206
+ },
207
+ {
208
+ PATTERN_USE: True,
209
+ PATTERN_CATEGORY: "Cell",
210
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-].*?[_-](\d*?)\.",
211
+ },
212
+ ]
213
+
214
+
215
+ # 'FOV':r'[_-](.*?)[_-]',
216
+ # 'Treatment':r'[_-].*?[_-](.*?)[_-]',
217
+ # 'Cell':r'[_-].*?[_-].*?[_-](\d*?)\.'}
218
+
219
+
220
+ class treatment_fov_cell_parser(defaultparser):
221
+ def init_patterns(self):
222
+ self.regexpatterns = [
223
+ {
224
+ PATTERN_USE: True,
225
+ PATTERN_CATEGORY: "Treatment",
226
+ PATTERN_REGEX: r".*/.*?[_-](.*?)[_-]",
227
+ },
228
+ {
229
+ PATTERN_USE: True,
230
+ PATTERN_CATEGORY: "FOV",
231
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-](.*?)[_-]",
232
+ },
233
+ {
234
+ PATTERN_USE: True,
235
+ PATTERN_CATEGORY: "Cell",
236
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-].*?[_-](\d*?)\.",
237
+ },
238
+ ]
239
+
240
+
241
+ class treatment_fov_time_parser(defaultparser):
242
+ def init_patterns(self):
243
+ self.regexpatterns = [
244
+ {
245
+ PATTERN_USE: True,
246
+ PATTERN_CATEGORY: "Treatment",
247
+ PATTERN_REGEX: r".*/.*?[_-](.*?)[_-]",
248
+ },
249
+ {
250
+ PATTERN_USE: True,
251
+ PATTERN_CATEGORY: "FOV",
252
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-](.*?)[_-]",
253
+ },
254
+ {
255
+ PATTERN_USE: True,
256
+ PATTERN_CATEGORY: "Time",
257
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-].*?[_-](\d*?)\.",
258
+ },
259
+ ]
260
+
261
+
262
+ class treatment_time_parser(defaultparser):
263
+ def init_patterns(self):
264
+ self.regexpatterns = [
265
+ {
266
+ PATTERN_USE: True,
267
+ PATTERN_CATEGORY: "Treatment",
268
+ PATTERN_REGEX: r".*/.*?[_-](.*?)[_-]",
269
+ },
270
+ {
271
+ PATTERN_USE: True,
272
+ PATTERN_CATEGORY: "Time",
273
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-](\d*?)\.",
274
+ },
275
+ ]
276
+
277
+
278
+ class fov_time_well_parser(defaultparser):
279
+ def init_patterns(self):
280
+ self.regexpatterns = [
281
+ {
282
+ PATTERN_USE: True,
283
+ PATTERN_CATEGORY: "FOV",
284
+ PATTERN_REGEX: r".*/.*?[_-](.*?)[_-]",
285
+ },
286
+ {
287
+ PATTERN_USE: True,
288
+ PATTERN_CATEGORY: "Time",
289
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-](.*?)[_-]",
290
+ },
291
+ {
292
+ PATTERN_USE: True,
293
+ PATTERN_CATEGORY: "Well",
294
+ PATTERN_REGEX: r".*/.*?[_-].*?[_-].*?[_-](.*?)\.",
295
+ },
296
+ ]
297
+
298
+
299
+ # class hyphenparser(defaultparser):
300
+ #
301
+ # def init_patterns(self):
302
+ # self.regexpatterns = {
303
+ # 'Treatment':r'[-](\d+?)',
304
+ # 'FOV':r'[-]\d*(\D+?)[_-]',
305
+ # 'Cell':r'[-].*?[_-].*?[_](\d*?)\.'}
flim/core/paths.py ADDED
@@ -0,0 +1,66 @@
1
+ """Folders for results: one canonical form, and a check before a run.
2
+
3
+ A folder chosen by the user is stored as an absolute path with links resolved
4
+ (:func:`canonical_folder`). On Windows a mapped network drive (``Z:\\results``) becomes
5
+ its UNC path (``\\\\server\\share\\results``): that also works for programs that don't
6
+ see the user's drive mappings (e.g. started as administrator), and on computers that
7
+ map the share to another letter.
8
+ """
9
+
10
+ import os
11
+ from typing import Optional
12
+
13
+
14
+ def canonical_folder(path):
15
+ """``path`` as an absolute path with ``~`` expanded and links resolved; on Windows
16
+ a mapped network drive becomes its UNC path. Kept absolute but unresolved when it
17
+ can't be resolved (e.g. the drive is not available); empty values are returned
18
+ as they are."""
19
+ if not path:
20
+ return path
21
+ path = os.path.abspath(os.path.expanduser(str(path)))
22
+ try:
23
+ return os.path.realpath(path)
24
+ except (OSError, ValueError):
25
+ return path
26
+
27
+
28
+ def folder_problem(path) -> Optional[str]:
29
+ """Why results can't be saved in ``path``, or None. A folder that doesn't exist yet
30
+ is fine if it can be created (its nearest existing parent is a writable folder)."""
31
+ path = os.path.abspath(os.path.expanduser(str(path)))
32
+ existing = path
33
+ while not os.path.exists(existing):
34
+ parent = os.path.dirname(existing)
35
+ if parent == existing: # not even the drive or share is there
36
+ return f"{path} can't be reached (is the drive or network share available?)"
37
+ existing = parent
38
+ if not os.path.isdir(existing):
39
+ return f"{existing} is not a folder"
40
+ if not os.access(existing, os.W_OK):
41
+ return f"{existing} can't be written to"
42
+ return None
43
+
44
+
45
+ def shorten_path(path: str, max_length: int = 50) -> str:
46
+ """``path`` shortened to about ``max_length`` characters for display: the start
47
+ (e.g. ``/Users/mst3k``, ``C:\\Users\\mst3k``, ``\\\\server\\share``), ``...`` and as
48
+ many of the last folders as fit, e.g. ``/Users/mst3k/.../folder/subfolder``. The
49
+ last folder is always kept."""
50
+ path = str(path)
51
+ if len(path) <= max_length:
52
+ return path
53
+ sep = "\\" if "\\" in path else "/"
54
+ parts = path.split(sep)
55
+ leading = len(path) - len(path.lstrip(sep)) # "/" or "\\" (network path)
56
+ drive = 1 if parts[0].endswith(":") else 0 # C:
57
+ head = parts[: leading + drive + 2]
58
+ rest = parts[leading + drive + 2 :]
59
+ if len(rest) < 2:
60
+ return path
61
+ tail = [rest[-1]]
62
+ for part in reversed(rest[1:-1]):
63
+ if len(sep.join(head + ["..."] + [part] + tail)) > max_length:
64
+ break
65
+ tail.insert(0, part)
66
+ return sep.join(head + ["..."] + tail)