def datacosel(indata, cocosel = None, coords = None, outcoords = None, \ start= None,end= None,step = None, \ outstart = None, outend = None, outstep = None, \ crop = False): ''' indata: input data ''' if (cocosel == None): # if coordinates not given, make it from the start, end, step arguments if (coords == None): coords = dtrange(start, end, step) if (outcoords == None): if ((outstart != None) & (outend != None) ): if (outstep != None): outcoords = dtrange(outstart, outend, outstep) else: outcoords = coords[(coords >= outstart) & (coords <=outend)] else: outcoords = coords cocosel = cosel(coords = coords, outcoords = outcoords) else: if (outcoords == None): outcoords = cocosel # remove NA (None) values if specified # would be more elegant if we could simply do cosel = cosel[cosel!=None], but None inside [] does strange things if (crop == True): tempcosel = [] tempoutcoords = [] for icosel,ecosel in enumerate(cosel): if (ecosel != None): tempcosel.append(ecosel) tempoutcoords.append(outcoords[icosel]) cocosel = array(tempcosel) outcoords = array(tempoutcoords) dataout = tile(nan,len(cocosel)) #dataout = indata[cosel[cosel!=None]] #dataout[cosel==None] = nan #dataout[cosel!=None] = indata[cosel[cosel!=None]] for iel, el in enumerate(cocosel): if (el != None): dataout[iel] = indata[el] else: dataout[iel] = None return(list([array(dataout), array(outcoords)]))
def avgcycle(data, timeco = None, timewarp = None, cclose=False,skipnan=False): """ calculate averaged cycle input: data: input data timeco: data coordinates timewarp: length of the cycle skipnan: skip nan-values option: cclose: add an additional point at the end identical the beginning to close the cycle todo: add an option to count the amount of samples for each member of the cycle -> then crop in cosel should be set to True so that we still take the mean even though there is no data available. """ if (timeco == None): timeco = arange(len(data)) if (timewarp == None): timewarp = timeco[1] - timeco[0] iscounted = tile(False,len(data)) tempco = dtrange(timeco[0],timeco[len(timeco)-1],timewarp) dataout = [] timecoout = [] for idata in range(len(data)): if (iscounted[idata] == False): tempco2 = tempco + (timeco[idata] - timeco[0]) #coordinates of data taken into account for this timestamp tempcosel = cosel(timeco,tempco2) # include some option to ignore the times on which there is no data # include some option to ignore the nan data # if some data could not be selected, a nan will be produced # in cosel, crop is left to False, because we want to discover whenever data is not available tdatacosel = datacosel(data,cocosel = tempcosel)[0] # we don't want to end up with an error if None is met: we just want a 'nan' from the mean. for edata in tdatacosel: if (edata == None): edata = nan if skipnan: tdatacosel = array(tdatacosel) tdatacosel = tdatacosel[where(isnan(tdatacosel) == False)] dataout.append(mean(tdatacosel)) timecoout.append(timeco[idata]) #dataout.append(mean(scisel(data,timeco,tempco2) ) #it would be much more elegant if we could simple do: iscounted[tempcosel] = True #but None inside [] does strange things, # (tempcosel!=None : manually crop (same as if we did cosel(...,crop=True))) for etempcosel in tempcosel: if (etempcosel != None): # None inside [] does strange things iscounted[etempcosel] = True # add an additional point to close the cycle if (cclose == True): timecoout.append(timeco[0]+ timewarp) dataout.append(dataout[0]) return(list([array(dataout),array(timecoout)]))