<jupyter_start><jupyter_text>3. Saving data from the smartphone as CSV In this tutorial, data uploaded to the server from the iOS application **intdash Motion** is downloaded with the **intdash SDK for Python** (hereinafter referred to as intdash SDK) and saved in CSV format locally. In this case, we will focus on the following.- Register signal definitions(\*) for general sensor data that convert binary data into numerical values.- Retrieve data using registered signal definitions- Convert the time series data to `pandas.DataFrame` and save it as a CSV file```Signal definition defines the process of cutting out a part of binary data and reading it as value data such as numerical value and character string.This process is done on the server side. See the intdash.Conversion-derived class documentation for conversion types(https://docs.intdash.jp/sdk/python/latest/en/signals.htmlintdash.Conversion)```  3.0 PreparationBefore starting this scenario, prepare the following.- An edge that performs measurement- A measurement uploaded with intdash Motion (which contains smartphone sensor data)- Signal definitions for smartphone sensor data  Data to be usedIn this scenario, the following data need to be prepared on the server side.| Data item | Data name that appears in this scenario ||:---|:---|| User account logged into "Motion" | user1|| Signal definitions(\*) | `sp_ACCX`, `sp_ACCY`, `sp_ACCZ`, `sp_Yaw`, `sp_Pitch`, `sp_Roll`|(\*) For registration method, see the following steps.  Create measurement data and upload time series dataUpload the data using **intdash Motion**. After registering a measurement, use **Visual M2M Data Visualizer** to check that the new measurement has been created.For uploading method, refer to **intdash tutorial 2A: Visualizing data from smartphone in real time**. Confirm that the new measurement is displayed in [Stored Data] of Visual M2M Data Visualizer.Follow the procedure below to retrieve the time series data of this measurement and save it in CSV format.  Import packages and create a clientFor `url` given to `intdash.Client`, specify the environment of the intdash server. For `username` and `password`, specify the auth information issued for the user account you use.<jupyter_code>import pandas as pd

import intdash
from intdash import timeutils

# Create client
client = intdash.Client(
    url = "https://example.intdash.jp",
    username = "user1",
    password="<PASSWORD>"
)<jupyter_output><empty_output><jupyter_text>Confirm that the signal definitions are registeredConfirm the registered signal definitions.  If it is not registered, see **(Option) Register the signal definitions** in the next step.<jupyter_code>signals = client.signals.list(label='sp')
for s in signals:
    print(s.label,  end=', ')<jupyter_output>sp_ACCX, sp_ACCY, sp_ACCZ, sp_Pitch, sp_Roll, sp_Yaw,<jupyter_text>(Option) Register the signal definitions```Warning:  If the target signal definition has already been registered to the server, skip this step.```In this scenario, "general sensor type" data is used. This data is in intdash original format. You need to convert the data from smartphone sensor type to numeric type (Float type this time) on the server side.In order to perform this conversion, it is necessary to register "signal definitions".Execute the following file to register the signal definitions.[0_create-signal-general-sensor.ipynb](../0_create-signal-general-sensor/0_create-signal-general-sensor.ipynb) In this sample, register only the conversion definition for 6-axis sensor data. The preparations are complete.  3.1 Retrieve the edge used for measurementRetrieve the edge used when logging in with intdash Motion.<jupyter_code>edge1 = client.edges.me()
edge1.name<jupyter_output><empty_output><jupyter_text>3.2 Retrieve time series dataUse `client.data_points` to retrieve the time series data.  For ``labels``, specify the label names of the signal definitions.  ``start`` and ``end`` should be changed to a time that includes the measurements created in the previous step.<jupyter_code>dps = client.data_points.list(
    edge_name=edge1.name,
    start=timeutils.str2timestamp('2020-07-17 00:00:00+09:00'), # change appropriately.
    end=timeutils.str2timestamp('2020-07-18 00:00:00+09:00'), # change appropriately.
    labels=['sp_ACCX', 'sp_ACCY', 'sp_ACCZ', 'sp_Yaw', 'sp_Pitch', 'sp_Roll'],
    limit=0
)<jupyter_output><empty_output><jupyter_text>3.3 Convert list of DataPoints to a DataFrame You can convert the data to a DataFrame which has columns for each `data_id` as follows.<jupyter_code>from intdash import data

df = pd.DataFrame( [ {
     'time' : d.time,
       d.data_id : data.Float.from_payload(d.data_payload).value # convert binary to numbers.
    }
    for d in dps
]).groupby("time").last()

df<jupyter_output><empty_output><jupyter_text>3.5 Save in CSV formatConvert to a CSV file using the feature of DataFrame.<jupyter_code>df.to_csv('./sample.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08572575787994378, 'fraction_numerical': 0.012447299739008232, 'mean_word_length': 5.2275, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '664255', 'license': 'Apache-2.0', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1331, 'n_words': 590, 'path': 'en/3_save-data-as-csv/3_save-data-as-csv.ipynb', 'repo_name': 'aptpod/aws-marketplace-tutorials', 'size': '4984'}