{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "e3676a78", "metadata": {}, "source": [ "# Pymicro's Datasets" ] }, { "attachments": {}, "cell_type": "markdown", "id": "6b50bf2e", "metadata": {}, "source": [ "This first tutorial will introduce you to the creation and deletion of Pymicro's datasets. " ] }, { "cell_type": "markdown", "id": "c9732f16", "metadata": {}, "source": [ "## I - Create and Open datasets with the SampleData class" ] }, { "cell_type": "markdown", "id": "a6171f3c", "metadata": {}, "source": [ "In this first section, we will see how to create *SampleData* datasets, or open pre-existing ones. These two operations are performed by instantiating a SampleData class object. \n", "\n", "Before that, you will need to import the `SampleData` class. We will import it with the alias name `SD`, by executing:" ] }, { "cell_type": "markdown", "id": "3086b5e4", "metadata": {}, "source": [ "### Import SampleData and get help" ] }, { "cell_type": "code", "execution_count": null, "id": "5af9e02b", "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "from pymicro.core.samples import SampleData as SD" ] }, { "cell_type": "markdown", "id": "c3914e84", "metadata": {}, "source": [ "Before starting to create our datasets, we will take a look at the `SampleData` class documenation, to discover the arguments of the class constructor. You can read it on the `pymicro.core` package [API doc page](../../pymicro.core.rst), or print interactively by executing:\n", "```python\n", ">>> help(SD)\n", "```\n", "or, if you are working with a Jupyter notebook, by executing the magic command:\n", "```\n", ">>> ?SD\n", "```" ] }, { "cell_type": "markdown", "id": "fff5671f", "metadata": {}, "source": [ "**Do not hesitate to systematically use the `help` function or the `\"?\"` magic command to get information on methods when you encounter a new one. All SampleData methods are documented with explicative docstrings, that detail the method arguments and returns.**" ] }, { "cell_type": "markdown", "id": "0de71d8d", "metadata": {}, "source": [ "### Dataset creation" ] }, { "cell_type": "markdown", "id": "bada24e5", "metadata": {}, "source": [ "The class docstring is divided in multiple rubrics, one of them giving the list of the class constructor arguments. \n", "Let us review them one by one.\n", "\n", "* **filename**: basename of the HDF5 pair of file of the dataset\n", "\n", "This is the first and only mandatory argument of the class constructor. If this string corresponds to an existing file, the SampleData class will open these file, and create a file instance to interact with this already existing dataset. **If the filename do not correspond to an existing file, the class will create a new dataset, which is what we want to do here.**\n", "\n", "Let us create a SampleData dataset:" ] }, { "cell_type": "code", "execution_count": null, "id": "c8677d77", "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "data = SD(filename='my_first_dataset')" ] }, { "cell_type": "markdown", "id": "3dc87948", "metadata": {}, "source": [ "That is it. The class has created a new HDF5/XDMF pair of files, and associated the interface with this dataset to the variable `data`. No message has been returned by the code, how can we know that the dataset has been created ?\n", "\n", "When the name of the file is not an absolute path, the default behavior of the class is to create the dataset in the current work directory. Let us print the content of this directory then !" ] }, { "cell_type": "code", "execution_count": null, "id": "4d3ab02d", "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "import os # load python module to interact with operating system\n", "cwd = os.getcwd() # get current directory\n", "file_list = os.listdir(cwd) # get content of current work directory\n", "print(file_list,'\\n')\n", "\n", "# now print only HDF5 files\n", "print('Our dataset files:')\n", "for file in file_list:\n", " if file.endswith('.h5'):\n", " print(file)" ] }, { "cell_type": "markdown", "id": "feab5950", "metadata": {}, "source": [ "The file *my_first_dataset.h5* has indeed been created. If you want interactive prints about the dataset creation, you can set the **verbose** argument to `True`. This will set the activate the *verbose* mode of the class. When it is, the class instance prints a lot of information about what it is doing. This flag can be set by using the `set_verbosity` method: " ] }, { "cell_type": "code", "execution_count": null, "id": "8e0cc75d", "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "data.set_verbosity(True)" ] }, { "cell_type": "markdown", "id": "42248817", "metadata": {}, "source": [ "Let us now close our dataset, and see if the class instance prints information about it:" ] }, { "cell_type": "code", "execution_count": null, "id": "089d50bc", "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "del data" ] }, { "cell_type": "markdown", "id": "e9377948", "metadata": {}, "source": [ "