def fromSeq(stateObjSeq): ''' returns a new Chain instance from given sequence of state objects the probabilities of state transitions are set according to transition frequencies in the given sequence ''' (fromStateObjIter,toStateObjIter) = tee(stateObjSeq); for _ in toStateObjIter: break nextStateObjsDict = dict() for (fromStateObj,toStateObj) in zip(fromStateObjIter,toStateObjIter): nextStateObjs = nextStateObjsDict.get(fromStateObj) if nextStateObjs is None: nextStateObjs = [] nextStateObjsDict[fromStateObj] = nextStateObjs nextStateObjs.append(toStateObj) nextStateNameAndObjs = list(nextStateObjsDict.items()) nextStateNameAndObjs.sort() nextStateLeaPerState = tuple((stateObj,Alea.fromVals(*nextStateObjs)) for (stateObj,nextStateObjs) in nextStateNameAndObjs) return Chain(nextStateLeaPerState)