Package omero :: Package util :: Module figureUtil
[hide private]
[frames] | no frames]

Source Code for Module omero.util.figureUtil

  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 &nbsp;&nbsp;&nbsp;&nbsp; 
 29  <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a> 
 30  @author Donald MacDonald &nbsp;&nbsp;&nbsp;&nbsp; 
 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   
50 -def getDatasetsProjectsFromImages(queryService, imageIds):
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: # order of images not same as imageIds 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 # make sure the map contains all the imageIds 81 for iId in imageIds: 82 if iId not in results: 83 results[iId] = [] 84 return results
85 86
87 -def getTagsFromImages(metadataService, imageIds):
88 """ 89 Query returns a map of key = imageId, value = [tagNames] for the image 90 91 @param metadataService: The Omero metadata service 92 @param imageIds: A list of image IDs. [long] 93 @return: A map of imageId:[tagName] 94 """ 95 96 types = ["ome.model.annotations.TagAnnotation"] 97 annotations = metadataService.loadAnnotations("Image", imageIds, types, None, None) 98 99 tagsMap = {} 100 for i in imageIds: 101 annots = annotations[i] 102 tags = [a.getTextValue().getValue() for a in annots] 103 tagsMap[i] = tags 104 return tagsMap
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
135 -def formatTime(seconds, timeUnits):
136 """ 137 Returns a string formatting of the time (in seconds) 138 according to the chosen timeUnits: "SECS_MILLIS", "SECS", "MINS", "HOURS", "MINS_SECS", "HOURS_MINS", HOURS_MINS_SECS, HOURS_MINS_SECS_MILLIS 139 140 @param seconds: Time in seconds. float or int 141 @param timeUnits: A string denoting the format. One of the choices above. 142 @return: A string, such as "10" or "3:20" 143 """ 144 neg = False 145 if seconds < 0: 146 seconds = seconds * -1 147 neg = True 148 label = None 149 if timeUnits == "SECS_MILLIS": 150 label = "%.2f" % seconds 151 elif timeUnits == "SECS": 152 label = "%d" % int(round(seconds)) 153 elif timeUnits == "MINS": 154 mins = float(seconds) / float(60) 155 label = "%d" % int(round(mins)) 156 elif timeUnits == "HOURS": 157 hrs = float(seconds) / float(3600) 158 label = "%d" % int(round(hrs)) 159 elif timeUnits == "MINS_SECS": 160 mins = seconds / 60 161 secs = round(seconds % 60) 162 label = "%d:%02d" % (mins, secs) 163 elif timeUnits == "HOURS_MINS": 164 hrs = seconds / 3600 165 mins = round((seconds % 3600)/60) 166 label = "%d:%02d" % (hrs, mins) 167 elif timeUnits == "HOURS_MINS_SECS": 168 hrs = seconds / 3600 169 mins = (seconds % 3600)/60 170 secs = round(seconds % (3600 * 60)) 171 label = "%d:%02d:%02d" % (hrs, mins, secs) 172 elif timeUnits == "HOURS_MINS_SECS_MILLIS": 173 hrs = seconds / 3600 174 mins = (seconds % 3600)/60 175 secs = (seconds % (3600 * 60)) 176 label = "%d:%02d:%05.2f" % (hrs, mins, secs) 177 else: 178 label = "%.2f sec" % seconds 179 return neg and "-%s"%label or label
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