# 04_WL_Tutorials_AnalysisPresentation_CoursePreview

0 (0 Likes / 0 Dislikes)

This is focusing on report generation which is a feature added in Mathematica 10.
And it’s one of many features that were added
to our data science tools that were built in to Mathematica 10
and I think this is a really great tool—it’s very straightforward to use
and we’re going to probably very quickly get to go over that here.
The core tool for report generation in Mathematica is the template.
So let me open one up for you.
What you see here is a blank template that we have available to us.
And we have several tools of what we can build—
we could put in slots, expressions, repeating blocks and cell behavior.
Now, I have an example that is what I want to build, so I’ll open that up.
Here we are going to make a very basic template that is going to have a single text cell saying, “hello”
and then the recipient of the “hello”—
recipient will be represented by a slot so I will type “Alt 7” to generate a text cell,
I will type “hello”, and then I will click our “slot” button
and I will call it “recipient”
and the default value is going to be “no-one” if we don’t provide one.
And finally I am going to put an exclamation point at the end so that we can
do a very traditional hello world example.
So let’s move that over and now I can fill out
the document with the “GenerateDocument” function.
We’ve got this “templateNo” variable defined form when we created the template
and we are going to use this association,
which is a data structure that we haven’t talked about but is another feature new in Mathematica 10,
and we are going to say that the recipient is going to be replaced with “world”.
And so I will execute this notebook.
And as you can see, the result is a notebook in Mathematica
where the recipient field was replaced with “world”.
In our next example, we’re going to look at the ability to use expressions.
So, here again I’m going to generate a template and let me bring up what our target is.
Here we have the notebook that we’re going to be working in and this is what we are going to be creating.
So I’m going to make a text cell and we’re going to start this example “integrates”
and then we’re going to have an expression put in and “expr”,
which the default here is going to be “x”.
And then I’m going to give myself a couple lines and then we’re going to include an expression to be evaluated
and “integrate” and then we’re going to put our slot back in and it give “expr” again
and put our default value in and we’re going to integrate with respect to “x”.
Now we’ve built our template for this case.
So once we have that template built I will be able to, again, use that “GenerateDocument”
function as you see here replacing in the for expression with “Cos(x)”.
And when we see the result we can see that “this example integrate cos(x)” that “Cos(x)” was put in for the expression
and in both places the integrate command was evaluated and we get back “Sin(x)”.
The next tool we’re going to look is a cell behavior tool.
And this is so that you can put in some commands that you want to be evaluated
during the course of building the notebook but
you do not want them to show up in the final document that you are generating.
So let me open a template again
and I’m going to, again, pull up my target notebook.
So here I’ve got the template that I’m about to work with and I’ve got the target
First thing I’m going to do is put in a little tag to show that this is initialization.
And then I’m going to define our function.
And here we’re going to take an integrand
and we’re going to integrate that integrand with respects to “x”.
Now, what I’m going to do is select these cells
and I’m going to use the “cell behavior” button
button and we’re going to do an evaluate and delete so that will completely
remove these cells after they’ve evaluated.
Now I’m going to put in a heading so we can have a non-hidden section.
And underneath the non-hidden section we are going to have a text cell that says that
“this example integrates” “expression”
and the default value for the expression will be “x”
and then we’ll have an input cell where we evaluate “f” of our expression.
And the default value in this case is going to be “x” again
and we’ll put a closed square bracket on here.
So now when I go to our notebook,
we’re going to evaluate the “GenerateDocument” section,
and we see that “f’ of “x” ended up defined and the above section was removed though.
Now I want to change one more thing about our template because,
as you can see, “f[cos[x]]” was still sitting there so I want to evaluate that section.
So we want to make sure that that “f[cos[x]]” is going to be removed.
I’m going to select that cell and choose its “cell behavior” to be “evaluate and delete”.
And then when we run the “GenerateDocument” we find that the evalution is
remaining but the term that generated “sin[x]” has been removed.
So now we’ve got a nice, clean report just the way we want it.
The last tool that we want to talk about with respects to templates
and report generation is the “RepeatingBlock”,
so that you can have an arbitrary number of values
that you might feed into the template in such a case.
Let’s take a look at a base template
and let’s take a look at our target in this case.
Here we have our target of what we’re coming up with.
First off we’re going our to include an initialization section like we did before.
So I will use “Alt 6” to label this, “Initialization”.
And then we will define our function again.
And again this is going to integrate the parameter that is passed to it.
And we’re going to mark those two cells as having “cell behavior” as “evaluate and delete”.
Then we are going to have an integral that we take,
so we’re going to take an integral of and we’re going to pass an expression in here,
and, again, the default value is going to be “x”.
And then we’ll have a test cell saying, “the integral is”,
and then finally an expression
—f of our slot—expresion passing value “x”, closing it
and we’re going to want to change the behavior there to “evaluate and delete”.
And then I want to create a repeating block so select the section I want to repeat.
So I’ve selected the “integral of expression” cell
all the way through the actual call to the function “f”.
We’re going to click on the “repeating block”
and we are going to give a template variable whose repetition is going to be called, “integrands”.
So now we’ve got this repeating block.
Now let’s take a look at how we’re going to use it.
Here, I’ve got, as we pass to “GenerateDocument”,
an association where “integrands” points at a list of associations
and in there that list has expression being assigned “sin(x)”,
expression being assigned “x^4”,
and expression being assigned the exponential of x.
So let’s evaluate that and we get back our report.
Here we find report where each of the different sections was covered—
we’ve got integral of sin(x) coming out to the negative cos(x),
the integral of x to the fourth coming out as x to the fifth over five,
and the integral of the exponential function coming back as the exponential function.
So go ahead and close out of the notebooks that were generated there.
Finally, we want to show how you can use all of these tools
these report generation tools, with everything else that we’ve done in our talk today.
I’m not going to build the following template;
I’m just going to show you what we have here.
So here what we have is our final template and all of this functionality
I’ve put into initialization above this is all the functionality that you were
shown previously in the third section about creating functions that have error messages
and have lots of options and lots of possibilities.
The only thing that I have added is a “Wrapper” function
that simplifies the data that we are feeding in,
so we’re only going to feed in the city name
and an image taken from that city.
Then we look further down, we’re going to have a “RepeatingBlock”
and it’s going to go over cities and there’s going to be a particular city,
we’re going to use our “WeatherData” function to get the current temperature in that city,
we’re going to get the current wind speed and wind direction in that city,
we’re also going to find the humidity level in that city.
Finally, though, we’re going to use our “Wrapper” function
and we’re going to generate the plots that you saw before.
So if we go back here, to our “GenerateDocument” function,
I’ve got my weather notebook that we’re referring to
and “Cities” is going to point to this list.
And that list has several different associations underneath it.
The first one is going to be San Francisco
and the image in this case is going to refer to an image of the streetcars in San Francisco
Second is Chicago and we have a picture of the Bean at Chicago.
And finally we have Cairo and an image of the pyramids at Cairo.
And so I evaluate this.
And this is going to take just a little bit longer to evaluate because
we are trying to get data off of our weather data servers.
Alright, and after we’ve waited a suitable amount of time for all the data to come in,
we get our final report. And I believe this, to me, looks really nice.
We have sections divided up by city, we have current temperature,
the current wind speed, and the current humidity level.
And we get this nice image that Harry showed you how to build,
in this case showing the average temperature in San Francisco
are pretty much gorgeous year-round.
Chicago a little bit less so, it gets a little cold in the winter.
And then finally at Cairo where we get relatively warm temperatures year-round.