Mar 28, 2022

Using Wolfram Engine with Jupyter Notebook

I’ve been using the free Wolfram cloud for a while; it is actually very capable for small projects. Several tips:

  1. It can’t upload any files, put the data somewhere online, then use Import[url] to get the data

  2. Each command can only run in a limited time (several minutes?), and sometimes reducing the size of data can help the job run through, for example, ImageResize[image,Scaled[1/2]].

  3. Learn some keyboard shortcuts, especially on cell formatting: Keyboard Shortcut Listing for the Wolfram Cloud.

  4. The most important thing: download your notebooks and also save a pdf copy. The file expires after 60 days, the user can’t open and download the expired files.


You probably can guess what happened to my files, they are all expired. I am not complaining here, it is free service anyway. To avoid this issue, I decided to try Free Wolfram Engine with Jupyter Notebook. They can be connected by WolframLanguageForJupyter. I am not going through the installation part; it is well documented for each product. I’ve created a sample notebook ImageStitch.ipynb with JupyterLab (a better interface than the classic Jupyter notebook). (Note: if it doesn’t load properly, try to reload it again). I may try other connectors later, the one used here is directly from Wolfram, however it doesn’t support auto-completion.  




Mar 24, 2022

Update: Learning Mathematica Slowly

I am on an extremely slow journey to pick up Mathematica again, one is that the current Mathematica is massive, the second is on me, in short “small brain”, that it is difficult to switch among several different programming languages. I mainly do coding with Python and JavaScript. I’ve tried to learn Julia and Mathematica at the same time since January, and got confused very quickly. So, in the next several months, I will focus on Mathematica first.


Currently reading: Wolfram Language Tutorial: Fast Introduction for Programmers

This book mainly explores Wolfram language on the level of grammar and syntax, it may fall short on emphasizing the importance of functional programming. Even though it plays a much minor role in Python, the basic idea of functioning programming lives well inside the Python ecosystem, for example, pure function (lambda) and map() are used frequently. If this book can absorb more materials from Functional Programming—Wolfram Language Documentation, it definitely helps a Python programmer understand the paradigm of Wolfram Language better.


The book comes with the notes for Java and Python programmers. The notes on Python are too simple and almost lose their purpose. The following are two examples:

  1. List comprehension: 

In Iterators | Wolfram Language Fast Introduction for Programmers, it says "Using Table in this way is analogous to using the [... for ... in ...] list comprehension construct in Python, though it has somewhat richer semantics.”. What’s the richer semantics? The list comprehension in Python is versatile, it has a basic format: newList = [ expression(element) for element in iterable], and combined with conditions(if/else), lambda function and nesting, it can do lots of things in a concise syntax. Here is not arguing the feature in which language is more powerful, it is mostly a missed opportunity for a better explanation on Wolfram language.

  

  1. f-string formatting:

In Strings | Wolfram Language Fast Introduction for Programmers, it says “TemplateApply in the Wolfram Language works similarly to Python's % or format function, except inline computations using <* ... *> are not supported in Python.” This claim is not accurate, Python’s f-string (introduced in Python 3.6) does the exact same thing. The screenshot shows some examples.



Jan 11, 2022

Code update: Stock Market Performance in January

This is a simple code update for Stock Market Performance in January, it is tested with the free Wolfram Cloud

The main change is that FinancialData now returns a TimeSeries object.
I haven't touched Mathematica 10,11 and 12, learning 13 on Wolfram Cloud right now. Mathematica has changed a lot!!

Mar 26, 2014

Geospatial function: Point in Ploygon Test

There is an excellent post on point in polygon test from Mathematica Stackexchange.

Point in polygon test is one of the most useful functions when processing Geospatial data. Unfortunately, there is no official built-in function from Mathematica yet. Here is an example of showing usefulness of this function.
We have some data on Greenland icesheet thickness in format of {lon,lat, thickness}.


Our goal is to make a map to show the thickness of icesheet.
First try with ListDensityPlot:


You can see it is not working well, we need to limit the plot region inside the boundary of Greenland. Here is the place we can use the point-in-polygon test. In this example, I use inPolyQ2 from the answer by Simon Woods:


Still not right? What's wrong? The trick is to increase MaxPlotPoints  to 100 at least:
ListDensityPlot[data, PlotRange -> All,
 ColorFunction -> (ColorData["Rainbow"][1 - #] &),
 RegionFunction -> (inPolyQ2[greenland_ploygon[[1, 1]], #1, #2] &),
 MaxPlotPoints -> 150, MeshFunctions -> {#3 &}, Mesh -> 10]


It looks like a real map now.

Feb 26, 2014

Fetching data from HTML source


Parsing html to get the data we need can be very frustratingLucky, Mathematica has a powerful hmtl import function, you can import raw html data into several different formats. In my experiences, import html as "XMLObject" is usually the best way to go. 
Here is an example: OSCAR Nominees:
xml = Import["http://oscar.go.com/nominees", "XMLObject"];   
We are interested in the list of nomineed films

body = Cases[xml, XMLElement["div", {"class" -> "nominee-by-film"}, ___], Infinity];
Extract titles:
title = Cases[body, XMLElement["span", {"class" -> "title"}, value_] :> value, Infinity] 
Extract the number of nominees:
nominee =
  Cases[body,
   XMLElement["h1", {"class" -> "numberOfNominations"}, value_] :>
    StringCases[value, x : NumberString :> ToExpression[x]], Infinity] ;
Put these two together:
result = Sort[Transpose[{title, Flatten@nominee}], #1[[2]] > #2[[2]] &]
Let's draw a graph to show the top 10 of the most nomineed films:
oscar = Import["http://www.oscars.org/awards/academyawards/about/awards/images/side_oscar.jpg"];
BarChart[result[[1 ;; 10, 2]],
 ChartLabels -> Placed[Flatten@result[[1 ;; 10, 1]], After],
 BarOrigin -> Left, Background -> LightBlue, ChartElements -> {oscar, {1, 1}},
  Axes -> None, LabelStyle -> {Bold, Darker@Blue, 14}] 

For this particular example, you can also try to get the same information directly from WolframAlpha.

Related post: A discussion on Mathmeatica Stackexchange