MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: graphing traces of complicated evaluations (improved)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg79225] Re: graphing traces of complicated evaluations (improved)
  • From: "Chris Chiasson" <chris at chiasson.name>
  • Date: Sat, 21 Jul 2007 04:32:26 -0400 (EDT)
  • References: <acbec1a40707191245k5dff3c42j13e8be020e884591@mail.gmail.com>

This version uses Short and ToString so that the graph Tooltips don't
crash Mathematica. It is also a little more evaluation-safe.

(Attributes@#={HoldAllComplete})&/@{traceToTreeAux,toVertex,HoldFormComplete,getAtoms,getAtomsAux}
MakeBoxes[HoldFormComplete[args___],form_]:=MakeBoxes[HoldForm[args],form]
edge[{head1_,pos1_,xpr1_},{head2_,pos2_,xpr2_}]:=Quiet[Rule[{head1,vertexNumberFunction@pos1,xpr1},{head2,vertexNumberFunction@pos2,xpr2}],{Rule::"rhs"}]
getAtomsAux[atom_/;AtomQ@Unevaluated@atom]:=Sow[HoldFormComplete@atom,getAtomsAux]
getAtomsAux[xpr_]:=Map[getAtomsAux,Unevaluated@xpr,Heads->True]
getAtoms[xpr_]:=Flatten@Reap[getAtomsAux@xpr][[2]]
toVertex[traceToTreeAux[HoldForm[heldXpr_],pos_]]:=toVertex[heldXpr]
toVertex[traceToTreeAux[HoldForm[heldXprs___],pos_]]:=toVertex@traceToTreeAux[Sequence[],pos]
(*this code is strong enough to not need the ToString commands, but
some of the resulting graph vertices give trouble to the graphing
routines*)
toVertex[traceToTreeAux[xpr_,pos_]]:={ToString[Short@Extract[Unevaluated@xpr,0,HoldFormComplete],StandardForm],pos,ToString[Short@First@originalTraceExtract@{pos},StandardForm]}
traceToTreeAux[xpr_/;AtomQ@Unevaluated@xpr,___]:=Sequence[]
traceToTreeAux[_HoldForm,___]:=Sequence[]
traceToTreeAux[xpr_,pos_]:=With[{lhs=toVertex@traceToTreeAux[xpr,pos],args=HoldComplete@@Unevaluated@xpr},Identity[Sequence][ReleaseHold[Function[Null,edge[lhs,toVertex@#],HoldAllComplete]/@args],ReleaseHold@args]]
traceToTree[xpr_]:=Block[{vertexNumber=-1,vertexNumberFunction,originalTraceExtract},vertexNumberFunction[arg_]:=vertexNumberFunction[arg]=++vertexNumber;originalTraceExtract[pos_]:=Extract[Unevaluated@xpr,pos,HoldFormComplete];{MapIndexed[traceToTreeAux,Unevaluated@xpr,{0,Infinity}]}]
TraceTreeFormPlot[trace_,opts___]:=Block[{$traceExpressionToTree=True},Through@{Unprotect,Update}@SparseArray`ExpressionToTree;SparseArray`ExpressionToTree[trace,Infinity]=traceToTree@trace;With[{result=ToExpression@ToBoxes@TreeForm[trace,opts]},Through@{Unprotect,Update}@SparseArray`ExpressionToTree;SparseArray`ExpressionToTree[trace,Infinity]=.;Through@{Update,Protect,Update}@SparseArray`ExpressionToTree;result]]


On 7/19/07, Chris Chiasson <chris at chiasson.name> wrote:
> Previously, I released code that could help with the tracing of
> evaluations, but the code required read access to the DownValues of
> the functions being traced. This version does not.
>
> Here are some example evaluations:
>
> TraceTreeFormPlot[Trace[1+2]]
>
> aaaaa:=Sequence[1+2,a]
> TraceTreeFormPlot[Trace@aaaaa]
>
> XML`MathML`ExpressionToMathML@"\[Alpha]";(*evaluate once to skip code
> caching in the trace*)
> LayeredGraphPlot[traceToTree[Trace[XML`MathML`ExpressionToMathML@"\[Alpha]",TraceDepth->2,TraceInternal->True]][[All,All,3]],SelfLoopStyle->None]
>
>
>
> ----------------------------------------------------
> code:
> ----------------------------------------------------
>
> (Attributes[#]={HoldAllComplete})&/@{traceToTreeAux}
>
> traceToTree[xpr_]:=Block[{traceToTreeCounter=-1,expressionToVertex,insideHoldForm=False},Attributes@expressionToVertex={HoldAllComplete};expressionToVertex[anyXpr_/;AtomQ@Unevaluated@anyXpr]:=expressionToVertex[anyXpr]={HoldForm@anyXpr,traceToTreeCounter+=1,HoldForm@anyXpr};expressionToVertex[holdFormXpr_HoldForm/;Length@holdFormXpr===1&&!insideHoldForm]:=Block[{insideHoldForm=True},Extract[Unevaluated@holdFormXpr,1,expressionToVertex]];expressionToVertex[holdFormXpr_HoldForm]:=expressionToVertex[holdFormXpr]={HoldForm@Sequence,traceToTreeCounter+=1,holdFormXpr};expressionToVertex[anyXpr_]:=expressionToVertex[anyXpr]={Extract[Unevaluated@anyXpr,0,HoldForm],traceToTreeCounter+=1,HoldForm@anyXpr};Rule@@@Flatten@{traceToTreeAux@xpr}]
>
> traceToTreeAux[xpr:Except@_HoldForm/;!AtomQ@Unevaluated@xpr]:=List@@@{With[{rootVertex=expressionToVertex@xpr},edge[rootVertex,expressionToVertex@#]&/@Unevaluated@xpr],traceToTreeAux/@Unevaluated@xpr}
>
> traceToTreeAux[xpr_]:=edge[expressionToVertex@xpr,expressionToVertex@xpr]
>
> TraceTreeFormPlot[trace_,opts___]:=Block[{$traceExpressionToTree=True},Through@{Unprotect,Update}@SparseArray`ExpressionToTree;SparseArray`ExpressionToTree[trace,Infinity]=traceToTree@trace;With[{result=ToExpression@ToBoxes@TreeForm[trace,opts]},Through@{Unprotect,Update}@SparseArray`ExpressionToTree;SparseArray`ExpressionToTree[trace,Infinity]=.;Through@{Update,Protect,Update}@SparseArray`ExpressionToTree;result]]
>
>
>
>
>
> ----------------------------------------------------
> keywords:
> ----------------------------------------------------
> Trace TreeForm Plot
>
> --
> http://chris.chiasson.name/
>


-- 
http://chris.chiasson.name/


  • Prev by Date: Re: generating non-IID random sequences
  • Next by Date: Re: Slow Manipulate with image argument
  • Previous by thread: Re: graphing traces of complicated evaluations (improved)
  • Next by thread: Re: graphing traces of complicated evaluations (improved)