def CartesianVectorsToPolar(coordinates, data): """ Project the supplied 2D Cartesian (x-y) vectors to polar coordinates (r-theta). coordinates must be in Cartesian. """ if optimise.DebuggingEnabled(): assert (len(coordinates) == len(data)) for i, coord in enumerate(coordinates): assert (len(coord) == 2) assert (len(data[i]) == 2) newData = numpy.empty((len(data), 2)) for i, coord in enumerate(coordinates): datum = data[i] rMag = L2Norm(coord[:2]) r = [coord[0] / rMag, coord[1] / rMag] theta = [-r[1], r[0]] newData[i] = [ datum[0] * r[0] + datum[1] * r[1], datum[0] * theta[0] + datum[1] * theta[1] ] return newData
def CylindricalVectorsToCartesian(coordinates, data): """ Project the supplied cylindrical coordinates (r-phi-z) vectors to 3D Cartesian (x-y-z). coordinates must be in Cartesian. """ if optimise.DebuggingEnabled(): assert (len(coordinates) == len(data)) for i, coord in enumerate(coordinates): assert (len(coord) == 3) assert (len(data[i]) == 3) newData = numpy.empty((len(data), 3)) for i, coord in enumerate(coordinates): datum = data[i] rMag = L2Norm(coord[:2]) x = [coord[0] / rMag, -coord[1] / rMag] y = [-x[1], x[0]] newData[i, :] = [ datum[0] * x[0] + datum[1] * x[1], datum[0] * y[0] + datum[1] * y[1], datum[2] ] return newData
def SetData(self, x, y, z, levels=None): assert (len(z) == len(y)) if optimise.DebuggingEnabled(): for val in z: assert (len(val) == len(x)) self._NewFigure() if levels is None: self._SetPlot(pylab.contourf(x, y, z)) else: self._SetPlot(pylab.contourf(x, y, z, levels)) return
def SetData(self, x, y): assert (len(x) == len(y)) if len(y) > 0 and utils.CanLen(y[0]): fields = len(y[0]) if optimise.DebuggingEnabled(): for comp in y[1:]: assert (len(comp) == fields) yData = [] for i in range(fields): yData.append([y[j][i] for j in range(len(y))]) else: fields = 1 yData = [y] self._NewFigure() colours = [] for i in range(fields): colourNumber = 256 * 256 * 256 * i / fields colourComp = str(hex(colourNumber % 256))[2:] while len(colourComp) < 2: colourComp = "0" + colourComp colour = colourComp colourNumber /= 256 colourComp = str(hex(colourNumber % 256))[2:] while len(colourComp) < 2: colourComp = "0" + colourComp colour = colourComp + colour colourNumber /= 256 colourComp = str(hex(colourNumber % 256))[2:] while len(colourComp) < 2: colourComp = "0" + colourComp colour = "#" + colourComp + colour colours.append(colour) args = [] for i in range(fields): args += [x, yData[i], colours[i]] pylab.plot(*args) self._SetPlot(pylab.gca()) return
def IndexBinaryUboundSearch(val, values, increasing=True): """ Find the min (max) index into values such that values[index] >= val, where if increasing is True (False), assumes the values are in increasing (decreasing) order """ if isinstance(values, numpy.ndarray): values = values.tolist() if increasing: lValues = copy.deepcopy(values) values = lValues values.reverse() if optimise.DebuggingEnabled(): testValues = copy.deepcopy(values) testValues.sort() testValues.reverse() assert (values == testValues) minUpper = 0 maxUpper = len(values) - 1 upper = maxUpper while maxUpper - minUpper > 0: # Choose new bounds for lower if values[upper] < val: maxUpper = upper else: minUpper = upper # Calculate a new guess for lower oldUpper = upper upper = int(float(maxUpper + minUpper) / 2.0) if oldUpper == upper: if maxUpper - minUpper <= 1: break else: upper -= 1 if not increasing: return upper else: return len(values) - upper - 1
def TransposeListList(inputList): """ Transpose the input list of lists (which must not be ragged) """ if optimise.DebuggingEnabled(): if len(inputList) > 0: assert (CanLen(inputList[0])) size = len(inputList[0]) for entry in inputList[1:]: assert (CanLen(entry)) assert (len(inputList[1]) == size) if len(inputList) == 0: return [] tList = [[] for i in range(len(inputList[0]))] for entry in inputList: for i, datum in enumerate(entry): tList[i].append(datum) return tList
def KeyedSort(keys, *values, **kargs): """ Return a re-ordering of values, with the remapping equal to the sort mapping of keys. Each key must correspond to a unique value. Optional keyword arguments: returnSortedKeys Return the sorted keys as well as the sorted values """ returnSortedKeys = False for key in kargs: if key == "returnSortedKeys": returnSortedKeys = True else: raise Exception("Unexpected keyword argument \"" + key + "\"") sortList = [] for i, key in enumerate(keys): sortList.append( Sorter(key, tuple([subValues[i] for subValues in values]))) sortList.sort() if optimise.DebuggingEnabled(): for i in range(len(sortList) - 1): if sortList[i].GetKey() == sortList[i + 1].GetKey(): assert (sortList[i].GetValue() == sortList[i].GetValue()) result = [] if returnSortedKeys: result.append([sorter.GetKey() for sorter in sortList]) for i in range(len(values)): result.append([sorter.GetValue()[i] for sorter in sortList]) if len(result) == 1: result = result[0] else: result = tuple(result) return result