1 """
2 components/tools/OmeroPy/src/omero/util/figureUitl.py
3
4 -----------------------------------------------------------------------------
5 Copyright (C) 2006-2009 University of Dundee. All rights reserved.
6
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 ------------------------------------------------------------------------------
22
23 A collection of utility methods used by Figure scripts for producing
24 publication type of figures.
25
26 @author William Moore
27 <a href="mailto:will@lifesci.dundee.ac.uk">will@lifesci.dundee.ac.uk</a>
28 @author Jean-Marie Burel
29 <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a>
30 @author Donald MacDonald
31 <a href="mailto:donald@lifesci.dundee.ac.uk">donald@lifesci.dundee.ac.uk</a>
32 @version 3.0
33 <small>
34 (<b>Internal version:</b> $Revision: $Date: $)
35 </small>
36 @since 3.0-Beta4.1
37
38 """
39
40 SECS_MILLIS = "SECS_MILLIS"
41 SECS = "SECS"
42 MINS = "MINS"
43 HOURS = "HOURS"
44 MINS_SECS = "MINS_SECS"
45 HOURS_MINS = "HOURS_MINS"
46 HOURS_MINS_SECS = "HOURS_MINS_SECS"
47 HOURS_MINS_SECS_MILLIS = "HOURS_MINS_SECS_MILLIS"
48 TIME_UNITS = [SECS_MILLIS, SECS, MINS, HOURS, MINS_SECS, HOURS_MINS, HOURS_MINS_SECS, HOURS_MINS_SECS_MILLIS]
49
51 """
52 Query returns a map where each key is the imageId and the value is a list of (projectName, datasetName) tuples.
53 If the image does not have a Dataset AND Project, the map will hold an empty list for that imageId.
54
55 @param queryService: The Omero query service
56 @param imageIds: A list of image IDs. [long]
57 @return: A map imageId:[(projectName, datasetName)]
58 """
59 ids = ",".join([str(i) for i in imageIds])
60
61 query_string = "select i from Image i join fetch i.datasetLinks idl join fetch idl.parent d join fetch d.projectLinks pl join fetch pl.parent where i.id in (%s)" % ids
62
63 images = queryService.findAllByQuery(query_string, None)
64 results = {}
65
66 for i in images:
67 pdList = []
68 imageId = i.getId().getValue()
69 for link in i.iterateDatasetLinks():
70 dataset = link.parent
71 dName = dataset.getName().getValue()
72 if dataset.sizeOfProjectLinks() == 0:
73 pdList.append(("", dName))
74 for dpLink in dataset.iterateProjectLinks():
75 project = dpLink.parent
76 pName = project.getName().getValue()
77 pdList.append((pName, dName))
78 results[imageId] = pdList
79
80
81 for iId in imageIds:
82 if iId not in results:
83 results[iId] = []
84 return results
85
86
105
106
107 -def getTimes(queryService, pixelsId, tIndexes, theZ=None, theC=None):
108 """
109 Get the time in seconds (float) for the first plane (C = 0 & Z = 0) at
110 each time-point for the defined pixels.
111 Returns a map of tIndex: timeInSecs
112
113 @param queryService: The Omero queryService
114 @param pixelsId: The ID of the pixels object. long
115 @param tIndexes: List of time indexes. [int]
116 @param theZ: The Z plane index. Default is 0
117 @param theC: The Channel index. Default is 0
118 @return: A map of tIndex: timeInSecs
119 """
120 if theZ == None:
121 theZ = 0
122 if theC == None:
123 theC = 0
124 indexes = ",".join([str(t) for t in tIndexes])
125 query = "from PlaneInfo as Info where Info.theT in (%s) and Info.theZ in (%d) and Info.theC in (%d) and pixels.id='%d'" % (indexes, theZ, theC, pixelsId)
126 infoList = queryService.findAllByQuery(query,None)
127 timeMap = {}
128 for info in infoList:
129 tIndex = info.theT.getValue()
130 time = info.deltaT.getValue()
131 timeMap[tIndex] = time
132 return timeMap
133
134
180
181
182 -def getTimeLabels(queryService, pixelsId, tIndexes, sizeT, timeUnits = None, showRoiDuration = False):
183 """
184 Returns a list of time labels e.g. "10", "20" for the first plane at
185 each t-index (C=0 and Z=0). If no planeInfo is available, returns plane number/total e.g "3/10"
186 If time units are not specified, the most suitable units are chosen based on the max time.
187 The list of label returned includes the timeUnits as the last string in the list, in case you didn't specify it.
188
189 @param queryService: The Omero query service
190 @param pixelsId: The ID of the pixels you want info for
191 @param tIndexes: List of t-index to get the times for. Assumed to be in t order.
192 @param sizeT: The T dimension size of the pixels. Used if no plane info
193 @param timeUnits: Format choice of "SECS", "MINS", "HOURS", "MINS_SECS", "HOURS_MINS". String
194 @param showRoiDuration: if true, times shown are from the start of the ROI frames, otherwise use movie timestamp.
195 @return: A list of strings, ordered same as tIndexes
196 """
197 secondsMap = getTimes(queryService, pixelsId, tIndexes)
198
199 if timeUnits == None and len(secondsMap) > 0:
200 maxSecs = max(secondsMap.values())
201 if maxSecs > 3600: timeUnits = HOURS_MINS
202 elif maxSecs > 60: timeUnits = MINS_SECS
203 else: timeUnits = SECS_MILLIS
204
205 labels = []
206 for t in tIndexes:
207 if t in secondsMap:
208 seconds = secondsMap[t]
209 if showRoiDuration:
210 seconds = seconds - secondsMap[tIndexes[0]]
211 labels.append(formatTime(seconds,timeUnits))
212 else:
213 labels.append("%d/%d" % (t+1, sizeT))
214
215 labels.append(timeUnits)
216 return labels
217