Skip to content

Commit

Permalink
New recipe fot dfft2 and idfft2
Browse files Browse the repository at this point in the history
  • Loading branch information
shilpa-nalkande committed Feb 5, 2025
1 parent 62f3d17 commit f1b2d1b
Showing 1 changed file with 385 additions and 0 deletions.
385 changes: 385 additions & 0 deletions Recipes/ClearScape_Functions/DFFT2_IDFFT2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,385 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "bc549e6c-0cc4-4188-94a3-a9bdd3ae3dfa",
"metadata": {},
"source": [
"<header>\n",
" <p style='font-size:36px;font-family:Arial; color:#F0F0F0; background-color: #00233c; padding-left: 20pt; padding-top: 20pt;padding-bottom: 10pt; padding-right: 20pt;'>\n",
" DFFT2 and IDFFT2 functions in Vantage\n",
" <br>\n",
" <img id=\"teradata-logo\" src=\"https://storage.googleapis.com/clearscape_analytics_demo_data/DEMO_Logo/teradata.svg\" alt=\"Teradata\" style=\"width: 125px; height: auto; margin-top: 20pt;\">\n",
" </p>\n",
"</header>"
]
},
{
"cell_type": "markdown",
"id": "7ae7611a-0795-4168-b716-01fee6880cbd",
"metadata": {},
"source": [
"<p style = 'font-size:20px;font-family:Arial;color:#00233C'><b>Introduction</b></p>\n",
"\n",
"<p style = 'font-size:20px;font-family:Arial;color:#00233C'><b>DFFT2 and IDFFT2</b></p>\n",
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>The <b>DFFT2()</b> function takes a matrix (two-dimensional array) as an input, and returns a result matrix whose elements are the computed two-dimension Fourier Coefficients for the input matrix. The coefficients can be output as complex numbers in either rectangular (real, imaginary) or polar (amplitude, phase) form.</p>\n",
"\n",
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>The <b>IDFFT2()</b> function simply reverses the 2D Fourier Transform. It takes either a logical matrix containing Fourier coefficients in the form of complex number elements, or, alternatively, in the form of amplitude-phase pair (polar form) elements as inputs. The function then runs them through a reverse-transform summation formula, and outputs the original logical matrix (original 2D array) that was input into the DFFT2() to generate the Fourier coefficients.</p>\n",
"\n",
"</p>"
]
},
{
"cell_type": "markdown",
"id": "6b3a00b4-6661-4c91-9b2d-cb7b0b403140",
"metadata": {},
"source": [
"<hr style=\"height:2px;border:none;background-color:#00233C;\">\n",
"<b style = 'font-size:20px;font-family:Arial;color:#00233C'>1. Initiate a connection to Vantage</b>"
]
},
{
"cell_type": "markdown",
"id": "2346857f-e0d3-488a-8a3f-ac6dff752c2b",
"metadata": {},
"source": [
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>In the section, we import the required libraries and set environment variables and environment paths (if required)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c5af5af3-29d5-4f6a-8334-9df6924e7787",
"metadata": {},
"outputs": [],
"source": [
"from teradataml import (\n",
" create_context,\n",
" execute_sql,\n",
" load_example_data,\n",
" DataFrame,\n",
" in_schema,\n",
" TDMatrix,\n",
" TDAnalyticResult,\n",
" DFFT2,\n",
" IDFFT2,\n",
" db_drop_view,\n",
" remove_context,\n",
" copy_to_sql,\n",
" db_drop_table\n",
" )\n",
"\n",
"from teradatasqlalchemy.types import *\n",
"\n",
"from teradataml import to_numeric\n",
"# Modify the following to match the specific client environment settings\n",
"display.max_rows = 5"
]
},
{
"cell_type": "markdown",
"id": "ad3dd7b4-831c-4fb3-ab71-719c8c99a71c",
"metadata": {},
"source": [
"<hr style=\"height:1px;border:none;background-color:#00233C;\">\n",
"<p style = 'font-size:18px;font-family:Arial;color:#00233c'><b>1.1 Connect to Vantage</b></p>\n",
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>You will be prompted to provide the password. Enter your password, press the Enter key, and then use the down arrow to go to the next cell.</p>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2742444c-4349-4b0f-b4e5-b068a8785cd9",
"metadata": {},
"outputs": [],
"source": [
"%run -i ../../UseCases/startup.ipynb\n",
"eng = create_context(host = 'host.docker.internal', username='demo_user', password = password)\n",
"print(eng)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "01c4a128-d106-46ea-8dee-34acc5abd29f",
"metadata": {},
"outputs": [],
"source": [
"%%capture\n",
"execute_sql('''SET query_band='DEMO=PP_DFFT2_IDFFT2_Python.ipynb;' UPDATE FOR SESSION; ''')"
]
},
{
"cell_type": "markdown",
"id": "efe2fd2d-63ff-4278-9157-8b9110d682e8",
"metadata": {},
"source": [
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>Begin running steps with Shift + Enter keys. </p>"
]
},
{
"cell_type": "markdown",
"id": "4a6e3852-3b15-4cbd-844d-e7194b3bd204",
"metadata": {},
"source": [
"<hr style='height:1px;border:none;background-color:#00233C;'>\n",
"\n",
"<p style = 'font-size:18px;font-family:Arial;color:#00233c'><b>1.2 Getting Data for This Demo</b></p>\n",
"\n",
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>Here, we will get the time series data which is available in the teradataml library and use the same to show the usage of the function.</p>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "398fa0aa-c0df-4130-9dea-53b776fbaf80",
"metadata": {},
"outputs": [],
"source": [
"load_example_data(\"uaf\", [\"dfft2conv_real_4_4\"])"
]
},
{
"cell_type": "markdown",
"id": "2a3762ac-ba27-4fa3-adba-d577262a4290",
"metadata": {},
"source": [
"<hr style=\"height:2px;border:none;background-color:#00233C;\">\n",
"<b style = 'font-size:20px;font-family:Arial;color:#00233C'>2. Data Exploration</b>\n",
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>Create a \"Virtual DataFrame\" that points to the data set in Vantage. Check the shape of the dataframe as check the datatype of all the columns of the dataframe.</p>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9672d80d-cdcc-4f4c-9399-7c367e7970b1",
"metadata": {},
"outputs": [],
"source": [
"data = DataFrame.from_table(\"dfft2conv_real_4_4\")\n",
"data"
]
},
{
"cell_type": "markdown",
"id": "4a2f79a0-eb16-4c3b-bec2-c313e97ac940",
"metadata": {},
"source": [
"<hr style=\"height:2px;border:none;background-color:#00233C;\">\n",
"<b style = 'font-size:20px;font-family:Arial;color:#00233C'>3. DFFT2 and IDFFT2</b>\n",
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>Compute the two-dimension fourier transform using the input matrix with real numbers only for the matrix id 33.</p>\n",
"\n",
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>The first step is to convert the data into TDMatrix, which is required for the input time series which are passed to the DFFT2 function.</p>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a711f58c-fbd3-4457-9f6a-9cd5c1b192dc",
"metadata": {},
"outputs": [],
"source": [
"td_matrix = TDMatrix(data=data,\n",
" id=\"id\",\n",
" row_index=\"row_i\",\n",
" row_index_style=\"SEQUENCE\",\n",
" column_index=\"column_i\",\n",
" column_index_style=\"SEQUENCE\",\n",
" payload_field=\"magnitude\",\n",
" payload_content=\"REAL\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8965392c-404e-4e25-b5b8-c7ef06069154",
"metadata": {},
"outputs": [],
"source": [
"filter_expr = td_matrix.id==33\n",
"dfft2 = DFFT2(data=td_matrix,\n",
" data_filter_expr=filter_expr,\n",
" freq_style=\"K_INTEGRAL\",\n",
" human_readable=False,\n",
" output_fmt_content=\"COMPLEX\")\n",
"\n",
"dfft2_df = dfft2.result\n",
"dfft2_df"
]
},
{
"cell_type": "markdown",
"id": "5b9f5e3b-53b4-4ee2-be77-ca8c1594dbf3",
"metadata": {},
"source": [
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>Compute the inverse of two-dimension fourier transform using matrix with complex numbers.</p>\n",
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>Create teradataml TDMatrix object.</p>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dfe6b1ce-c869-41b8-b368-34bfd073f7ca",
"metadata": {},
"outputs": [],
"source": [
"data_matrix_df = TDMatrix(data=dfft2.result,\n",
" id=\"id\",\n",
" row_index=\"ROW_I\",\n",
" row_index_style=\"SEQUENCE\",\n",
" column_index=\"COLUMN_I\",\n",
" column_index_style=\"SEQUENCE\",\n",
" payload_field=[\"REAL_magnitude\", \"IMAG_magnitude\"],\n",
" payload_content=\"COMPLEX\")"
]
},
{
"cell_type": "markdown",
"id": "e7084ee9-a1c6-4f14-84b6-54bbb9659b16",
"metadata": {},
"source": [
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>Execute IDFFT2 function</p>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "44fac20f-39fb-40fe-8716-f638aa79e6d1",
"metadata": {},
"outputs": [],
"source": [
"uaf_out = IDFFT2(data=data_matrix_df, human_readable=False)\n",
"uaf_out.result"
]
},
{
"cell_type": "markdown",
"id": "33fd0b1c-656b-44c4-b124-f56ef3be80d4",
"metadata": {},
"source": [
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>Compute the inverse of two-dimension fourier transform using\n",
"TDAnalyticResult instead of matrix with complex numbers.</p>\n",
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>Create a teradataml TDAnalyticResult object.</p>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8d0e5b7e-3713-4ba9-b880-c898cd2ac9fb",
"metadata": {},
"outputs": [],
"source": [
"art_df = TDAnalyticResult(data=dfft2.result)"
]
},
{
"cell_type": "markdown",
"id": "fc521920-3f8c-4afa-ba4e-3bf96db10868",
"metadata": {},
"source": [
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>Execute IDFFT2 function</p>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e46b3fc0-3378-41ef-9ebf-91095a3c7996",
"metadata": {},
"outputs": [],
"source": [
"uaf_out = IDFFT2(data=art_df, human_readable=False)\n",
"uaf_out.result"
]
},
{
"cell_type": "markdown",
"id": "c25fdaf1-098b-4dea-984b-579c6412557e",
"metadata": {},
"source": [
"<hr style=\"height:2px;border:none;background-color:#00233C;\">\n",
"<b style = 'font-size:20px;font-family:Arial;color:#00233C'>4. Cleanup</b>"
]
},
{
"cell_type": "markdown",
"id": "a5d460af-1a32-4681-8094-360c3f25dc1f",
"metadata": {},
"source": [
"<p style = 'font-size:18px;font-family:Arial;color:#00233C'><b>Work Tables</b></p>\n",
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'>The following code will clean up tables created above.</p>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9dff9677-dae1-4cc5-b78e-3d197cc41e0c",
"metadata": {},
"outputs": [],
"source": [
"db_drop_table(\"dfft2conv_real_4_4\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9b96168c-7973-4259-b3e0-668b92fd846e",
"metadata": {},
"outputs": [],
"source": [
"remove_context()"
]
},
{
"cell_type": "markdown",
"id": "4317a6cf-1479-4aa8-b30a-ee0a3b5231a8",
"metadata": {},
"source": [
"<hr style=\"height:1px;border:none;background-color:#00233C;\">\n",
"\n",
"<p style = 'font-size:16px;font-family:Arial;color:#00233C'><b>Links:</b></p>\n",
"<ul style = 'font-size:16px;font-family:Arial'>\n",
" <li>Teradataml Python reference: <a href = 'https://docs.teradata.com/search/all?query=Python+Package+User+Guide&content-lang=en-US'>here</a></li>\n",
" <li>DFFT2 function reference: <a href = 'https://docs.teradata.com/search/all?query=DFFT2&content-lang=en-US'>here</a></li>\n",
" <li>IDFFT2 function reference: <a href = 'https://docs.teradata.com/search/all?query=IDFFT2&content-lang=en-US'>here</a></li>\n",
" \n",
"</ul>"
]
},
{
"cell_type": "markdown",
"id": "b2dcca28-5de5-44d7-88cb-45a12153b3f8",
"metadata": {},
"source": [
"<footer style=\"padding-bottom:35px; background:#f9f9f9; border-bottom:3px solid #00233C\">\n",
" <div style=\"float:left;margin-top:14px\">ClearScape Analytics™</div>\n",
" <div style=\"float:right;\">\n",
" <div style=\"float:left; margin-top:14px\">\n",
" Copyright © Teradata Corporation - 2025. All Rights Reserved\n",
" </div>\n",
" </div>\n",
"</footer>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit f1b2d1b

Please sign in to comment.