Data Import and Export - Maple Programming Help

Data Import and Export

Maple 2020 includes these updates to data import and export.

Read Part of an Audio File

The AudioTools:-Read command has a new samples option which enables reading part of an audio file.
Here the interval ${10}^{3}..60\cdot {10}^{3}$ is specified, which indicates that only samples 1000 to 60000 will be read from the specified file. 

 ${\mathrm{violinNote_sub}}{≔}\left[\begin{array}{cc}{"Sample Rate"}& {44100}\\ {"File Format"}& {\mathrm{PCM}}\\ {"File Bit Depth"}& {16}\\ {"Channels"}& {2}\\ {"Samples/Channel"}& {59001}\\ {"Duration"}& {1.33789}{}{s}\end{array}\right]$ (1.1)

FileTools:-Walk

The new FileTools:-Walk command generates an iterator to traverse subdirectories under a given directory.

$\mathrm{with}\left(\mathrm{FileTools}\right):$

$\mathrm{iterator}≔\mathrm{Walk}\left(\mathrm{JoinPath}\left(\left["help"\right],\mathrm{base}=\mathrm{datadir}\right)\right)$

 ${\mathrm{iterator}}{≔}\left[\begin{array}{c}{\mathrm{FileTools:-Walk Iterator}}\\ {\mathrm{Base directory: C:\Program Files\Maple 2020\data\help}}\\ {\mathrm{Traversal: top-down}}\end{array}\right]$ (2.1)

 ${"C:\Program Files\Maple 2020\data\help\XMLTools"}{,}\left[\right]{,}\left[{"catalogue.xsd"}{,}{"item_invalid.xml"}{,}{"item_valid.xml"}{,}{"SimpleDocument.dtd"}{,}{"SimpleDocument.xml"}{,}{"SimpleDocument1.xml"}{,}{"SimpleDocument2.xml"}{,}{"SimpleDocument3.xml"}{,}{"SimpleDocument4.xml"}\right]$ (2.2)

Import and Export of BSON and UBJSON Files

The Import and Export commands now support and import and export of BSON and UBJSON files. These are both binary file formats modeled after the JSON format. They store structured expressions in a manner similar to JSON.
The BSON format can store additional types of data, including byte arrays and date/time objects.  The latter are translated to Maple Time objects upon import.

 ${\mathrm{UNDHR}}{≔}{table}{}\left(\left[{"ratification"}{=}{\mathrm{}}{,}{"event"}{=}{"United Nations Declaration of Human Rights"}{,}{"location"}{=}{table}{}\left(\left[{"city"}{=}{"Paris"}{,}{"country"}{=}{"France"}{,}{"name"}{=}{"Palais de Chaillot"}{,}{"address"}{=}{"1 Place du Trocadéro et du 11 Novembre"}\right]\right)\right]\right)$ (3.1)

$\mathrm{Date}\left(\mathrm{UNDHR}\left["ratification"\right]\right)$

 ${\mathrm{Date}}{}\left({1948}{,}{12}{,}{10}{,}{11}{,}{0}{,}{0}{,}{\mathrm{timezone}}{=}{"UTC"}{,}{\mathrm{coefficient}}{=}{1}\right)$ (3.2)

ImportMatrix

The ImportMatrix command has been updated with two new options: direct and fill.
The direct option enables parsing data from strings and ByteArrays (in addition to files and URLs):

$\mathrm{ImportMatrix}\left("1,3.2,7.5\n2,6.7,7.8",\mathrm{direct},\mathrm{source}=\mathrm{csv}\right)$

 $\left[\begin{array}{ccc}1.0& 3.2& 7.5\\ 2.0& 6.7& 7.8\end{array}\right]$ (3.3)

The fill option specifies a default value for missing data when the input is ragged; a ragged input is an input in which some rows have fewer entries than others.

In the following example, the first row has four entries while the second and third are underspecified, in which case the fill value of 999 is used.

 $\left[\begin{array}{rrrr}1& 4& 7& 10\\ 2& 5& 8& 999\\ 3& 6& 999& 999\end{array}\right]$ (3.4)

Data conversions to and from Python

The convert command now allows explicit conversion of certain Maple expressions to their Python counterparts and vice-versa.

In the following example we convert three expressions to Python expressions:

 ${\mathrm{pythonSet}}{≔}{""}$ (3.5)

 ${\mathrm{pythonDict}}{≔}{""}$ (3.6)

 ${\mathrm{pythonMatrix}}{≔}{""}$ (3.7)

These inputs are suitable as input to Python:-EvalFunction, provided the underlying Python data types match:

 ${\mathrm{result}}{≔}{""}$ (3.8)

We can also convert certain Python expressions into Maple expressions:

 $\left\{{1}{,}{5}{,}{7}\right\}$ (3.9)

$\mathrm{convert}\left(\mathrm{pythonDict},\mathrm{table}\right)$

 ${table}{}\left(\left[{"Tungsten"}{=}{"W"}{,}{"Sodium"}{=}{"Na"}{,}{"Iron"}{=}{"Fe"}{,}{"Gold"}{=}{"Au"}{,}{"Tin"}{=}{"Sb"}\right]\right)$ (3.10)

$\mathrm{convert}\left(\mathrm{result},\mathrm{Matrix}\right)$

 $\left[\begin{array}{rrr}-100& -45& 30\\ -30& -93& -68\end{array}\right]$ (3.11)