# Notebook from SpectralPoet/Amazon_Vine_Analysis
Path: Amazon_Reviews_ETL.ipynb

_____no_output_____

import os
# Find the latest version of spark 3.0 from http://www.apache.org/dist/spark/ and enter as the spark version
# For example:
# spark_version = 'spark-3.0.3'
spark_version = 'spark-3.0.3'
os.environ['SPARK_VERSION']=spark_version

# Install Spark and Java
!apt-get update
!apt-get install openjdk-11-jdk-headless -qq > /dev/null
!wget -q http://www.apache.org/dist/spark/$SPARK_VERSION/$SPARK_VERSION-bin-hadoop2.7.tgz
!tar xf $SPARK_VERSION-bin-hadoop2.7.tgz
!pip install -q findspark

# Set Environment Variables
import os
os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-11-openjdk-amd64"
os.environ["SPARK_HOME"] = f"/content/{spark_version}-bin-hadoop2.7"

# Start a SparkSession
import findspark
findspark.init()Get:1 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3,626 B]
Ign:2 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64  InRelease
Get:3 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Ign:4 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  InRelease
Hit:5 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64  Release
Hit:6 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  Release
Hit:7 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease
Hit:8 http://archive.ubuntu.com/ubuntu bionic InRelease
Hit:9 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease
Get:10 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Hit:13 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease
Get:14 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Hit:15 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease
Get:16 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1,430 kB]
Get:17 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [2,800 kB]
Get:18 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2,208 kB]
Fetched 6,695 kB in 5s (1,430 kB/s)
Reading package lists... Done
# Download the Postgres driver that will allow Spark to interact with Postgres.
!wget https://jdbc.postgresql.org/download/postgresql-42.2.16.jar--2021-10-03 15:34:09--  https://jdbc.postgresql.org/download/postgresql-42.2.16.jar
Resolving jdbc.postgresql.org (jdbc.postgresql.org)... 72.32.157.228, 2001:4800:3e1:1::228
Connecting to jdbc.postgresql.org (jdbc.postgresql.org)|72.32.157.228|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1002883 (979K) [application/java-archive]
Saving to: ‘postgresql-42.2.16.jar’

postgresql-42.2.16. 100%[===================>] 979.38K  1.22MB/s    in 0.8s

2021-10-03 15:34:11 (1.22 MB/s) - ‘postgresql-42.2.16.jar’ saved [1002883/1002883]

from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("M16-Amazon-Challenge").config("spark.driver.extraClassPath","/content/postgresql-42.2.16.jar").getOrCreate()_____no_output_____

### Load Amazon Data into Spark DataFrame_____no_output_____

from pyspark import SparkFiles
url = "https://s3.amazonaws.com/amazon-reviews-pds/tsv/amazon_reviews_us_Video_Games_v1_00.tsv.gz"
spark.sparkContext.addFile(url)
df = spark.read.option("encoding", "UTF-8").csv(SparkFiles.get(""), sep="\t", header=True, inferSchema=True)
df.show()+-----------+-----------+--------------+----------+--------------+--------------------+----------------+-----------+-------------+-----------+----+-----------------+--------------------+--------------------+-----------+
|marketplace|customer_id|     review_id|product_id|product_parent|       product_title|product_category|star_rating|helpful_votes|total_votes|vine|verified_purchase|     review_headline|         review_body|review_date|
+-----------+-----------+--------------+----------+--------------+--------------------+----------------+-----------+-------------+-----------+----+-----------------+--------------------+--------------------+-----------+
|         US|   12039526| RTIS3L2M1F5SM|B001CXYMFS|     737716809|Thrustmaster T-Fl...|     Video Games|          5|            0|          0|   N|                Y|an amazing joysti...|Used this for Eli...| 2015-08-31|
|         US|    9636577| R1ZV7R40OLHKD|B00M920ND6|     569686175|Tonsee 6 buttons ...|     Video Games|          5|            0|          0|   N|                Y|Definitely a sile...|Loved it,  I didn...| 2015-08-31|
|         US|    2331478|R3BH071QLH8QMC|B0029CSOD2|      98937668|Hidden Mysteries:...|     Video Games|          1|            0|          1|   N|                Y|            One Star|poor quality work...| 2015-08-31|
|         US|   52495923|R127K9NTSXA2YH|B00GOOSV98|      23143350|GelTabz Performan...|     Video Games|          3|            0|          0|   N|                Y|good, but could b...|nice, but tend to...| 2015-08-31|
|         US|   14533949|R32ZWUXDJPW27Q|B00Y074JOM|     821342511|Zero Suit Samus a...|     Video Games|          4|            0|          0|   N|                Y|   Great but flawed.|Great amiibo, gre...| 2015-08-31|
|         US|    2377552|R3AQQ4YUKJWBA6|B002UBI6W6|     328764615|Psyclone Recharge...|     Video Games|          1|            0|          0|   N|                Y|            One Star|The remote consta...| 2015-08-31|
|         US|   17521011|R2F0POU5K6F73F|B008XHCLFO|      24234603|Protection for yo...|     Video Games|          5|            0|          0|   N|                Y|              A Must|I have a 2012-201...| 2015-08-31|
|         US|   19676307|R3VNR804HYSMR6|B00BRA9R6A|     682267517|   Nerf 3DS XL Armor|     Video Games|          5|            0|          0|   N|                Y|          Five Stars|Perfect, kids lov...| 2015-08-31|
|         US|     224068| R3GZTM72WA2QH|B009EPWJLA|     435241890|One Piece: Pirate...|     Video Games|          5|            0|          0|   N|                Y|          Five Stars|            Excelent| 2015-08-31|
|         US|   48467989| RNQOY62705W1K|B0000AV7GB|     256572651|Playstation 2 Dan...|     Video Games|          4|            0|          0|   N|                Y|          Four Stars|Slippery but expe...| 2015-08-31|
|         US|     106569|R1VTIA3JTYBY02|B00008KTNN|     384411423|Metal Arms: Glitc...|     Video Games|          5|            0|          0|   N|                N|          Five Stars|Love the game. Se...| 2015-08-31|
|         US|   48269642|R29DOU8791QZL8|B000A3IA0Y|     472622859|72 Pin Connector ...|     Video Games|          1|            0|          0|   N|                Y| Game will get stuck|Does not fit prop...| 2015-08-31|
|         US|   52738710|R15DUT1VIJ9RJZ|B0053BQN34|     577628462|uDraw Gametablet ...|     Video Games|          2|            0|          0|   N|                Y|We have tried it ...|This was way too ...| 2015-08-31|
|         US|   10556786|R3IMF2MQ3OU9ZM|B002I0HIMI|     988218515|NBA 2K12(Covers M...|     Video Games|          4|            0|          0|   N|                Y|          Four Stars|Works great good ...| 2015-08-31|
|         US|    2963837|R23H79DHOZTYAU|B0081EH12M|     770100932|New Trigger Grips...|     Video Games|          1|            1|          1|   N|                Y|Now i have to buy...|It did not fit th...| 2015-08-31|
|         US|   23092109| RIV24EQAIXA4O|B005FMLZQQ|      24647669|Xbox 360 Media Re...|     Video Games|          5|            0|          0|   N|                Y|          Five Stars|perfect lightweig...| 2015-08-31|
|         US|   23091728|R3UCNGYDVN24YB|B002BSA388|      33706205|Super Mario Galaxy 2|     Video Games|          5|            0|          0|   N|                Y|          Five Stars|               great| 2015-08-31|
|         US|   10712640| RUL4H4XTTN2DY|B00BUSLSAC|     829667834|Nintendo 3DS XL -...|     Video Games|          5|            0|          0|   N|                Y|          Five Stars|Works beautifully...| 2015-08-31|
|         US|   17455376|R20JF7Z4DHTNX5|B00KWF38AW|     110680188|Captain Toad:  Tr...|     Video Games|          5|            0|          0|   N|                Y|          Five Stars|Kids loved the ga...| 2015-08-31|
|         US|   14754850|R2T1AJ5MFI2260|B00BRQJYA8|     616463426|Lego Batman 2: DC...|     Video Games|          4|            0|          0|   N|                Y|          Four Stars|           Goodngame| 2015-08-31|
+-----------+-----------+--------------+----------+--------------+--------------------+----------------+-----------+-------------+-----------+----+-----------------+--------------------+--------------------+-----------+
only showing top 20 rows

### Create DataFrames to match tables_____no_output_____

from pyspark.sql.functions import to_date
# Read in the Review dataset as a DataFrame
df.show()+-----------+-----------+--------------+----------+--------------+--------------------+----------------+-----------+-------------+-----------+----+-----------------+--------------------+--------------------+-----------+
|marketplace|customer_id|     review_id|product_id|product_parent|       product_title|product_category|star_rating|helpful_votes|total_votes|vine|verified_purchase|     review_headline|         review_body|review_date|
+-----------+-----------+--------------+----------+--------------+--------------------+----------------+-----------+-------------+-----------+----+-----------------+--------------------+--------------------+-----------+
|         US|   12039526| RTIS3L2M1F5SM|B001CXYMFS|     737716809|Thrustmaster T-Fl...|     Video Games|          5|            0|          0|   N|                Y|an amazing joysti...|Used this for Eli...| 2015-08-31|
|         US|    9636577| R1ZV7R40OLHKD|B00M920ND6|     569686175|Tonsee 6 buttons ...|     Video Games|          5|            0|          0|   N|                Y|Definitely a sile...|Loved it,  I didn...| 2015-08-31|
|         US|    2331478|R3BH071QLH8QMC|B0029CSOD2|      98937668|Hidden Mysteries:...|     Video Games|          1|            0|          1|   N|                Y|            One Star|poor quality work...| 2015-08-31|
|         US|   52495923|R127K9NTSXA2YH|B00GOOSV98|      23143350|GelTabz Performan...|     Video Games|          3|            0|          0|   N|                Y|good, but could b...|nice, but tend to...| 2015-08-31|
|         US|   14533949|R32ZWUXDJPW27Q|B00Y074JOM|     821342511|Zero Suit Samus a...|     Video Games|          4|            0|          0|   N|                Y|   Great but flawed.|Great amiibo, gre...| 2015-08-31|
|         US|    2377552|R3AQQ4YUKJWBA6|B002UBI6W6|     328764615|Psyclone Recharge...|     Video Games|          1|            0|          0|   N|                Y|            One Star|The remote consta...| 2015-08-31|
|         US|   17521011|R2F0POU5K6F73F|B008XHCLFO|      24234603|Protection for yo...|     Video Games|          5|            0|          0|   N|                Y|              A Must|I have a 2012-201...| 2015-08-31|
|         US|   19676307|R3VNR804HYSMR6|B00BRA9R6A|     682267517|   Nerf 3DS XL Armor|     Video Games|          5|            0|          0|   N|                Y|          Five Stars|Perfect, kids lov...| 2015-08-31|
|         US|     224068| R3GZTM72WA2QH|B009EPWJLA|     435241890|One Piece: Pirate...|     Video Games|          5|            0|          0|   N|                Y|          Five Stars|            Excelent| 2015-08-31|
|         US|   48467989| RNQOY62705W1K|B0000AV7GB|     256572651|Playstation 2 Dan...|     Video Games|          4|            0|          0|   N|                Y|          Four Stars|Slippery but expe...| 2015-08-31|
|         US|     106569|R1VTIA3JTYBY02|B00008KTNN|     384411423|Metal Arms: Glitc...|     Video Games|          5|            0|          0|   N|                N|          Five Stars|Love the game. Se...| 2015-08-31|
|         US|   48269642|R29DOU8791QZL8|B000A3IA0Y|     472622859|72 Pin Connector ...|     Video Games|          1|            0|          0|   N|                Y| Game will get stuck|Does not fit prop...| 2015-08-31|
|         US|   52738710|R15DUT1VIJ9RJZ|B0053BQN34|     577628462|uDraw Gametablet ...|     Video Games|          2|            0|          0|   N|                Y|We have tried it ...|This was way too ...| 2015-08-31|
|         US|   10556786|R3IMF2MQ3OU9ZM|B002I0HIMI|     988218515|NBA 2K12(Covers M...|     Video Games|          4|            0|          0|   N|                Y|          Four Stars|Works great good ...| 2015-08-31|
|         US|    2963837|R23H79DHOZTYAU|B0081EH12M|     770100932|New Trigger Grips...|     Video Games|          1|            1|          1|   N|                Y|Now i have to buy...|It did not fit th...| 2015-08-31|
|         US|   23092109| RIV24EQAIXA4O|B005FMLZQQ|      24647669|Xbox 360 Media Re...|     Video Games|          5|            0|          0|   N|                Y|          Five Stars|perfect lightweig...| 2015-08-31|
|         US|   23091728|R3UCNGYDVN24YB|B002BSA388|      33706205|Super Mario Galaxy 2|     Video Games|          5|            0|          0|   N|                Y|          Five Stars|               great| 2015-08-31|
|         US|   10712640| RUL4H4XTTN2DY|B00BUSLSAC|     829667834|Nintendo 3DS XL -...|     Video Games|          5|            0|          0|   N|                Y|          Five Stars|Works beautifully...| 2015-08-31|
|         US|   17455376|R20JF7Z4DHTNX5|B00KWF38AW|     110680188|Captain Toad:  Tr...|     Video Games|          5|            0|          0|   N|                Y|          Five Stars|Kids loved the ga...| 2015-08-31|
|         US|   14754850|R2T1AJ5MFI2260|B00BRQJYA8|     616463426|Lego Batman 2: DC...|     Video Games|          4|            0|          0|   N|                Y|          Four Stars|           Goodngame| 2015-08-31|
+-----------+-----------+--------------+----------+--------------+--------------------+----------------+-----------+-------------+-----------+----+-----------------+--------------------+--------------------+-----------+
only showing top 20 rows

# Create the customers_table DataFrame
# customers_df = df.groupby("").agg({""}).withColumnRenamed("", "customer_count")
customers_df = df.groupby("customer_id").agg({"customer_id" : "count"}).withColumnRenamed("count(customer_id)", "customer_count")_____no_output_____customers_df.show()+-----------+--------------+
|customer_id|customer_count|
+-----------+--------------+
|   48670265|             1|
|   49103216|             2|
|    1131200|             1|
|   43076447|             2|
|   46261368|             1|
|    4883305|             5|
|   41192649|             1|
|   40985731|             7|
|   10437900|             2|
|   22245671|             1|
|    2574873|             1|
|    4696154|             1|
|    5621202|             1|
|    5871933|             2|
|   44089812|             1|
|    2845910|             1|
|    5274369|             1|
|   39069693|             2|
|     137793|             1|
|   31914942|             3|
+-----------+--------------+
only showing top 20 rows

# Create the products_table DataFrame and drop duplicates.
# products_df = df.select([]).drop_duplicates()
products_df = df.select([df.product_id, df.product_title]).drop_duplicates()_____no_output_____products_df.show()+----------+--------------------+
|product_id|       product_title|
+----------+--------------------+
|B00CJ7IUI6|The Elder Scrolls...|
|B00DHF39KS|Wolfenstein: The ...|
|B00MUTAVH6|Under Night In-Bi...|
|B001AZSEUW|              Peggle|
|B00KVOVBGM|PlayStation 4 Con...|
|B00O9VGH4Y|USPRO® Headph...|
|B004OQNZY4|Phineas and Ferb:...|
|B00ZLN980O|Donop seablue 2.4...|
|B002L8W5V6|Dotop Nintendo Ga...|
|B007AJZ5PY|Nyko Game Case fo...|
|B000AOEU2K|Fire Emblem: Path...|
|B000H8BW7U|Tanarus (PC) (Com...|
|B013RADQOQ|Susenstone® 2400D...|
|B00KQXKUJ2|FIFA 15 (Ultimate...|
|B006W41X2C|Turtle Beach - Ea...|
|B000KCX9M4|Grand Theft Auto:...|
|B00YT90JWC|Red Wii Mini Cons...|
|B0096KG6A8|Wii U Super Mario...|
|B00L6AVLB0|World of Tanks-X3...|
|B000IMYKQ0|Wii Nunchuk Contr...|
+----------+--------------------+
only showing top 20 rows

# Create the review_id_table DataFrame.
# Convert the 'review_date' column to a date datatype with to_date("review_date", 'yyyy-MM-dd').alias("review_date")
# review_id_df = df.select([, to_date("review_date", 'yyyy-MM-dd').alias("review_date")])
review_id_df = df.select(["review_id", "customer_id", "product_id", "product_parent", to_date("review_date", 'yyyy-MM-dd').alias("review_date")])_____no_output_____review_id_df.show()+--------------+-----------+----------+--------------+-----------+
|     review_id|customer_id|product_id|product_parent|review_date|
+--------------+-----------+----------+--------------+-----------+
| RTIS3L2M1F5SM|   12039526|B001CXYMFS|     737716809| 2015-08-31|
| R1ZV7R40OLHKD|    9636577|B00M920ND6|     569686175| 2015-08-31|
|R3BH071QLH8QMC|    2331478|B0029CSOD2|      98937668| 2015-08-31|
|R127K9NTSXA2YH|   52495923|B00GOOSV98|      23143350| 2015-08-31|
|R32ZWUXDJPW27Q|   14533949|B00Y074JOM|     821342511| 2015-08-31|
|R3AQQ4YUKJWBA6|    2377552|B002UBI6W6|     328764615| 2015-08-31|
|R2F0POU5K6F73F|   17521011|B008XHCLFO|      24234603| 2015-08-31|
|R3VNR804HYSMR6|   19676307|B00BRA9R6A|     682267517| 2015-08-31|
| R3GZTM72WA2QH|     224068|B009EPWJLA|     435241890| 2015-08-31|
| RNQOY62705W1K|   48467989|B0000AV7GB|     256572651| 2015-08-31|
|R1VTIA3JTYBY02|     106569|B00008KTNN|     384411423| 2015-08-31|
|R29DOU8791QZL8|   48269642|B000A3IA0Y|     472622859| 2015-08-31|
|R15DUT1VIJ9RJZ|   52738710|B0053BQN34|     577628462| 2015-08-31|
|R3IMF2MQ3OU9ZM|   10556786|B002I0HIMI|     988218515| 2015-08-31|
|R23H79DHOZTYAU|    2963837|B0081EH12M|     770100932| 2015-08-31|
| RIV24EQAIXA4O|   23092109|B005FMLZQQ|      24647669| 2015-08-31|
|R3UCNGYDVN24YB|   23091728|B002BSA388|      33706205| 2015-08-31|
| RUL4H4XTTN2DY|   10712640|B00BUSLSAC|     829667834| 2015-08-31|
|R20JF7Z4DHTNX5|   17455376|B00KWF38AW|     110680188| 2015-08-31|
|R2T1AJ5MFI2260|   14754850|B00BRQJYA8|     616463426| 2015-08-31|
+--------------+-----------+----------+--------------+-----------+
only showing top 20 rows

# Create the vine_table. DataFrame
# vine_df = df.select([])
vine_df = df.select(["review_id", "star_rating", "helpful_votes", "total_votes", "vine", "verified_purchase"])_____no_output_____vine_df.show()+--------------+-----------+-------------+-----------+----+-----------------+
|     review_id|star_rating|helpful_votes|total_votes|vine|verified_purchase|
+--------------+-----------+-------------+-----------+----+-----------------+
| RTIS3L2M1F5SM|          5|            0|          0|   N|                Y|
| R1ZV7R40OLHKD|          5|            0|          0|   N|                Y|
|R3BH071QLH8QMC|          1|            0|          1|   N|                Y|
|R127K9NTSXA2YH|          3|            0|          0|   N|                Y|
|R32ZWUXDJPW27Q|          4|            0|          0|   N|                Y|
|R3AQQ4YUKJWBA6|          1|            0|          0|   N|                Y|
|R2F0POU5K6F73F|          5|            0|          0|   N|                Y|
|R3VNR804HYSMR6|          5|            0|          0|   N|                Y|
| R3GZTM72WA2QH|          5|            0|          0|   N|                Y|
| RNQOY62705W1K|          4|            0|          0|   N|                Y|
|R1VTIA3JTYBY02|          5|            0|          0|   N|                N|
|R29DOU8791QZL8|          1|            0|          0|   N|                Y|
|R15DUT1VIJ9RJZ|          2|            0|          0|   N|                Y|
|R3IMF2MQ3OU9ZM|          4|            0|          0|   N|                Y|
|R23H79DHOZTYAU|          1|            1|          1|   N|                Y|
| RIV24EQAIXA4O|          5|            0|          0|   N|                Y|
|R3UCNGYDVN24YB|          5|            0|          0|   N|                Y|
| RUL4H4XTTN2DY|          5|            0|          0|   N|                Y|
|R20JF7Z4DHTNX5|          5|            0|          0|   N|                Y|
|R2T1AJ5MFI2260|          4|            0|          0|   N|                Y|
+--------------+-----------+-------------+-----------+----+-----------------+
only showing top 20 rows

### Connect to the AWS RDS instance and write each DataFrame to its table. _____no_output_____

# Configure settings for RDS
mode = "append"
jdbc_url="jdbc:postgresql://amazongamereviews.cfsnpk6pif8c.us-east-2.rds.amazonaws.com:5432/challenge"
config = {"user":"postgres",
          "password": "*",
          "driver":"org.postgresql.Driver"}_____no_output_____# Write review_id_df to table in RDS
review_id_df.write.jdbc(url=jdbc_url, table='review_id_table', mode=mode, properties=config)_____no_output_____# Write products_df to table in RDS
# about 3 min
products_df.write.jdbc(url=jdbc_url, table='products_table', mode=mode, properties=config)_____no_output_____# Write customers_df to table in RDS
# 5 min 14 s
customers_df.write.jdbc(url=jdbc_url, table='customers_table', mode=mode, properties=config)_____no_output_____# Write vine_df to table in RDS
# 11 minutes
vine_df.write.jdbc(url=jdbc_url, table='vine_table', mode=mode, properties=config)_____no_output__________no_output_____


# Notebook from gaelmoccand/Image-Captioning
Path: 2_Training.ipynb

# Computer Vision Nanodegree

## Project: Image Captioning

---

In this notebook, you will train your CNN-RNN model.

You are welcome and encouraged to try out many different architectures and hyperparameters when searching for a good model.

This does have the potential to make the project quite messy!  Before submitting your project, make sure that you clean up:
- the code you write in this notebook.  The notebook should describe how to train a single CNN-RNN architecture, corresponding to your final choice of hyperparameters.  You should structure the notebook so that the reviewer can replicate your results by running the code in this notebook.
- the output of the code cell in **Step 2**.  The output should show the output obtained when training the model from scratch.

This notebook **will be graded**.

Feel free to use the links below to navigate the notebook:
- [Step 1](#step1): Training Setup
- [Step 2](#step2): Train your Model
- [Step 3](#step3): (Optional) Validate your Model_____no_output_____## Step 1: Training Setup

In this step of the notebook, you will customize the training of your CNN-RNN model by specifying hyperparameters and setting other options that are important to the training procedure.  The values you set now will be used when training your model in **Step 2** below.

You should only amend blocks of code that are preceded by a `TODO` statement.  **Any code blocks that are not preceded by a `TODO` statement should not be modified**.

### Task #1

Begin by setting the following variables:
- `batch_size` - the batch size of each training batch.  It is the number of image-caption pairs used to amend the model weights in each training step.
- `vocab_threshold` - the minimum word count threshold.  Note that a larger threshold will result in a smaller vocabulary, whereas a smaller threshold will include rarer words and result in a larger vocabulary.
- `vocab_from_file` - a Boolean that decides whether to load the vocabulary from file.
- `embed_size` - the dimensionality of the image and word embeddings.
- `hidden_size` - the number of features in the hidden state of the RNN decoder.
- `num_epochs` - the number of epochs to train the model.  We recommend that you set `num_epochs=3`, but feel free to increase or decrease this number as you wish.  [This paper](https://arxiv.org/pdf/1502.03044.pdf) trained a captioning model on a single state-of-the-art GPU for 3 days, but you'll soon see that you can get reasonable results in a matter of a few hours!  (_But of course, if you want your model to compete with current research, you will have to train for much longer._)
- `save_every` - determines how often to save the model weights.  We recommend that you set `save_every=1`, to save the model weights after each epoch.  This way, after the `i`th epoch, the encoder and decoder weights will be saved in the `models/` folder as `encoder-i.pkl` and `decoder-i.pkl`, respectively.
- `print_every` - determines how often to print the batch loss to the Jupyter notebook while training.  Note that you **will not** observe a monotonic decrease in the loss function while training - this is perfectly fine and completely expected!  You are encouraged to keep this at its default value of `100` to avoid clogging the notebook, but feel free to change it.
- `log_file` - the name of the text file containing - for every step - how the loss and perplexity evolved during training.

If you're not sure where to begin to set some of the values above, you can peruse [this paper](https://arxiv.org/pdf/1502.03044.pdf) and [this paper](https://arxiv.org/pdf/1411.4555.pdf) for useful guidance!  **To avoid spending too long on this notebook**, you are encouraged to consult these suggested research papers to obtain a strong initial guess for which hyperparameters are likely to work best.  Then, train a single model, and proceed to the next notebook (**3_Inference.ipynb**).  If you are unhappy with your performance, you can return to this notebook to tweak the hyperparameters (and/or the architecture in **model.py**) and re-train your model.

### Question 1

**Question:** Describe your CNN-RNN architecture in detail.  With this architecture in mind, how did you select the values of the variables in Task 1?  If you consulted a research paper detailing a successful implementation of an image captioning model, please provide the reference.

**Answer:**  I use a batch_size of 32 as suggested in the course. For the hidden and embed size I used a  value of 512 comming  from [this paper](https://arxiv.org/pdf/1411.4555.pdf).
From the architecture point of view, I kept the ResNet for the CNN part. For The RNN part the LSTM described in the same paper has been used with a dropout set to 0,1.

### (Optional) Task #2

Note that we have provided a recommended image transform `transform_train` for pre-processing the training images, but you are welcome (and encouraged!) to modify it as you wish.  When modifying this transform, keep in mind that:
- the images in the dataset have varying heights and widths, and
- if using a pre-trained model, you must perform the corresponding appropriate normalization.

### Question 2

**Question:** How did you select the transform in `transform_train`?  If you left the transform at its provided value, why do you think that it is a good choice for your CNN architecture?

**Answer:** the transform train was not changed. It provide the good size of the images and use flip with a prob of 50% to do data augmentation. It would be possible to add more transforms for data augmentation such as rotate.

### Task #3

Next, you will specify a Python list containing the learnable parameters of the model.  For instance, if you decide to make all weights in the decoder trainable, but only want to train the weights in the embedding layer of the encoder, then you should set `params` to something like:
```
params = list(decoder.parameters()) + list(encoder.embed.parameters())
```

### Question 3

**Question:** How did you select the trainable parameters of your architecture?  Why do you think this is a good choice?

**Answer:** For the model parameters: the hidden and embed size value of 512 was used as decribed in [this paper](https://arxiv.org/pdf/1411.4555.pdf) for the decoder part. For the encoder part the CNN ResNet was used with a drooput ser to 0.1

### Task #4

Finally, you will select an [optimizer](http://pytorch.org/docs/master/optim.html#torch.optim.Optimizer).

### Question 4

**Question:** How did you select the optimizer used to train your model?

**Answer:** Adam optimizer have been chosen because it can achieve good results for deep learning fast compare to others optimizer._____no_output_____

import torch
import torch.nn as nn
from torchvision import transforms
import sys
sys.path.append('/opt/cocoapi/PythonAPI')
from pycocotools.coco import COCO
from data_loader import get_loader
from model import EncoderCNN, DecoderRNN
import math

## TODO #1: Select appropriate values for the Python variables below.
batch_size = 32          # batch size
vocab_threshold = 5        # minimum word count threshold
vocab_from_file = False   # if True, load existing vocab file
embed_size = 512           # dimensionality of image and word embeddings
hidden_size = 512          # number of features in hidden state of the RNN decoder
num_epochs = 3             # number of training epochs
save_every = 1             # determines frequency of saving model weights
print_every = 100          # determines window for printing average loss
log_file = 'training_log.txt'       # name of file with saved training loss and perplexity

# (Optional) TODO #2: Amend the image transform below.
transform_train = transforms.Compose([
    transforms.Resize(256),                          # smaller edge of image resized to 256
    transforms.RandomCrop(224),                      # get 224x224 crop from random location
    transforms.RandomHorizontalFlip(),               # horizontally flip image with probability=0.5
    transforms.ToTensor(),                           # convert the PIL Image to a tensor
    transforms.Normalize((0.485, 0.456, 0.406),      # normalize image for pre-trained model
                         (0.229, 0.224, 0.225))])

# Build data loader.
data_loader = get_loader(transform=transform_train,
                         mode='train',
                         batch_size=batch_size,
                         vocab_threshold=vocab_threshold,
                         vocab_from_file=vocab_from_file)

# The size of the vocabulary.
vocab_size = len(data_loader.dataset.vocab)

# Initialize the encoder and decoder.
encoder = EncoderCNN(embed_size)
decoder = DecoderRNN(embed_size, hidden_size, vocab_size)

# Move models to GPU if CUDA is available.
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
encoder.to(device)
decoder.to(device)

# Define the loss function.
criterion = nn.CrossEntropyLoss().cuda() if torch.cuda.is_available() else nn.CrossEntropyLoss()

# TODO #3: Specify the learnable parameters of the model.
params = list(decoder.parameters()) + list(encoder.embed.parameters())

# TODO #4: Define the optimizer.
optimizer = torch.optim.Adam(params, lr=0.001)

# Set the total number of training steps per epoch.
total_step = math.ceil(len(data_loader.dataset.caption_lengths) / data_loader.batch_sampler.batch_size)loading annotations into memory...
Done (t=0.91s)
creating index...
index created!
[0/414113] Tokenizing captions...
[100000/414113] Tokenizing captions...
[200000/414113] Tokenizing captions...
[300000/414113] Tokenizing captions...
[400000/414113] Tokenizing captions...
loading annotations into memory...
Done (t=0.97s)
creating index...

## Step 2: Train your Model

Once you have executed the code cell in **Step 1**, the training procedure below should run without issue.

It is completely fine to leave the code cell below as-is without modifications to train your model.  However, if you would like to modify the code used to train the model below, you must ensure that your changes are easily parsed by your reviewer.  In other words, make sure to provide appropriate comments to describe how your code works!

You may find it useful to load saved weights to resume training.  In that case, note the names of the files containing the encoder and decoder weights that you'd like to load (`encoder_file` and `decoder_file`).  Then you can load the weights by using the lines below:

```python
# Load pre-trained weights before resuming training.
encoder.load_state_dict(torch.load(os.path.join('./models', encoder_file)))
decoder.load_state_dict(torch.load(os.path.join('./models', decoder_file)))
```

While trying out parameters, make sure to take extensive notes and record the settings that you used in your various training runs.  In particular, you don't want to encounter a situation where you've trained a model for several hours but can't remember what settings you used :).

### A Note on Tuning Hyperparameters

To figure out how well your model is doing, you can look at how the training loss and perplexity evolve during training - and for the purposes of this project, you are encouraged to amend the hyperparameters based on this information.

However, this will not tell you if your model is overfitting to the training data, and, unfortunately, overfitting is a problem that is commonly encountered when training image captioning models.

For this project, you need not worry about overfitting. **This project does not have strict requirements regarding the performance of your model**, and you just need to demonstrate that your model has learned **_something_** when you generate captions on the test data.  For now, we strongly encourage you to train your model for the suggested 3 epochs without worrying about performance; then, you should immediately transition to the next notebook in the sequence (**3_Inference.ipynb**) to see how your model performs on the test data.  If your model needs to be changed, you can come back to this notebook, amend hyperparameters (if necessary), and re-train the model.

That said, if you would like to go above and beyond in this project, you can read about some approaches to minimizing overfitting in section 4.3.1 of [this paper](http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=7505636).  In the next (optional) step of this notebook, we provide some guidance for assessing the performance on the validation dataset._____no_output_____

import torch.utils.data as data
import numpy as np
import os
import requests
import time

# Open the training log file.
f = open(log_file, 'w')

old_time = time.time()
response = requests.request("GET",
                            "http://metadata.google.internal/computeMetadata/v1/instance/attributes/keep_alive_token",
                            headers={"Metadata-Flavor":"Google"})

for epoch in range(1, num_epochs+1):

    for i_step in range(1, total_step+1):

        if time.time() - old_time > 60:
            old_time = time.time()
            requests.request("POST",
                             "https://nebula.udacity.com/api/v1/remote/keep-alive",
                             headers={'Authorization': "STAR " + response.text})

        # Randomly sample a caption length, and sample indices with that length.
        indices = data_loader.dataset.get_train_indices()
        # Create and assign a batch sampler to retrieve a batch with the sampled indices.
        new_sampler = data.sampler.SubsetRandomSampler(indices=indices)
        data_loader.batch_sampler.sampler = new_sampler

        # Obtain the batch.
        images, captions = next(iter(data_loader))

        # Move batch of images and captions to GPU if CUDA is available.
        images = images.to(device)
        captions = captions.to(device)

        # Zero the gradients.
        decoder.zero_grad()
        encoder.zero_grad()

        # Pass the inputs through the CNN-RNN model.
        features = encoder(images)
        outputs = decoder(features, captions)

        # Calculate the batch loss.
        loss = criterion(outputs.view(-1, vocab_size), captions.view(-1))

        # Backward pass.
        loss.backward()

        # Update the parameters in the optimizer.
        optimizer.step()

        # Get training statistics.
        stats = 'Epoch [%d/%d], Step [%d/%d], Loss: %.4f, Perplexity: %5.4f' % (epoch, num_epochs, i_step, total_step, loss.item(), np.exp(loss.item()))

        # Print training statistics (on same line).
        print('\r' + stats, end="")
        sys.stdout.flush()

        # Print training statistics to file.
        f.write(stats + '\n')
        f.flush()

        # Print training statistics (on different line).
        if i_step % print_every == 0:
            print('\r' + stats)

    # Save the weights.
    if epoch % save_every == 0:
        torch.save(decoder.state_dict(), os.path.join('./models', 'decoder-%d.pkl' % epoch))
        torch.save(encoder.state_dict(), os.path.join('./models', 'encoder-%d.pkl' % epoch))

# Close the training log file.
f.close()Epoch [1/3], Step [100/12942], Loss: 4.0671, Perplexity: 58.38724
Epoch [1/3], Step [200/12942], Loss: 4.3726, Perplexity: 79.24748
Epoch [1/3], Step [300/12942], Loss: 3.4468, Perplexity: 31.40091
Epoch [1/3], Step [400/12942], Loss: 3.3092, Perplexity: 27.3637
Epoch [1/3], Step [500/12942], Loss: 3.3079, Perplexity: 27.3285
Epoch [1/3], Step [600/12942], Loss: 4.0636, Perplexity: 58.1808
Epoch [1/3], Step [700/12942], Loss: 3.4883, Perplexity: 32.7313
Epoch [1/3], Step [800/12942], Loss: 4.4457, Perplexity: 85.2578
Epoch [1/3], Step [900/12942], Loss: 3.1522, Perplexity: 23.3868
Epoch [1/3], Step [1000/12942], Loss: 3.1216, Perplexity: 22.6833
Epoch [1/3], Step [1100/12942], Loss: 2.9268, Perplexity: 18.6681
Epoch [1/3], Step [1200/12942], Loss: 3.0219, Perplexity: 20.5307
Epoch [1/3], Step [1300/12942], Loss: 3.3675, Perplexity: 29.0065
Epoch [1/3], Step [1400/12942], Loss: 2.8898, Perplexity: 17.9893
Epoch [1/3], Step [1500/12942], Loss: 2.9653, Perplexity: 19.4011
Epoch [1/3], Step [1600/12942], Loss: 2.9490, Perplexity: 19.0874
Epoch [1/3], Step [1700/12942], Loss: 2.5089, Perplexity: 12.29157
Epoch [1/3], Step [1800/12942], Loss: 2.4799, Perplexity: 11.9397
Epoch [1/3], Step [1900/12942], Loss: 2.9357, Perplexity: 18.8347
Epoch [1/3], Step [2000/12942], Loss: 2.7003, Perplexity: 14.8841
Epoch [1/3], Step [2100/12942], Loss: 2.7544, Perplexity: 15.7109
Epoch [1/3], Step [2200/12942], Loss: 2.7898, Perplexity: 16.2772
Epoch [1/3], Step [2300/12942], Loss: 3.1972, Perplexity: 24.4635
Epoch [1/3], Step [2400/12942], Loss: 2.6472, Perplexity: 14.1146
Epoch [1/3], Step [2500/12942], Loss: 2.5628, Perplexity: 12.9721
Epoch [1/3], Step [2600/12942], Loss: 3.1461, Perplexity: 23.2455
Epoch [1/3], Step [2700/12942], Loss: 2.1315, Perplexity: 8.42735
Epoch [1/3], Step [2800/12942], Loss: 2.4477, Perplexity: 11.5612
Epoch [1/3], Step [2900/12942], Loss: 2.4779, Perplexity: 11.9158
Epoch [1/3], Step [3000/12942], Loss: 2.4850, Perplexity: 12.0006
Epoch [1/3], Step [3100/12942], Loss: 2.5893, Perplexity: 13.3199
Epoch [1/3], Step [3200/12942], Loss: 2.8834, Perplexity: 17.8757
Epoch [1/3], Step [3300/12942], Loss: 2.7226, Perplexity: 15.2204
Epoch [1/3], Step [3400/12942], Loss: 2.8831, Perplexity: 17.8689
Epoch [1/3], Step [3500/12942], Loss: 2.6244, Perplexity: 13.7958
Epoch [1/3], Step [3600/12942], Loss: 2.7485, Perplexity: 15.6190
Epoch [1/3], Step [3700/12942], Loss: 2.4014, Perplexity: 11.0384
Epoch [1/3], Step [3800/12942], Loss: 2.2406, Perplexity: 9.399244
Epoch [1/3], Step [3900/12942], Loss: 2.6129, Perplexity: 13.6384
Epoch [1/3], Step [4000/12942], Loss: 2.5375, Perplexity: 12.6477
Epoch [1/3], Step [4100/12942], Loss: 2.3661, Perplexity: 10.6561
Epoch [1/3], Step [4200/12942], Loss: 2.3583, Perplexity: 10.5729
Epoch [1/3], Step [4300/12942], Loss: 2.5645, Perplexity: 12.9939
Epoch [1/3], Step [4400/12942], Loss: 2.3175, Perplexity: 10.1503
Epoch [1/3], Step [4500/12942], Loss: 2.2176, Perplexity: 9.185598
Epoch [1/3], Step [4600/12942], Loss: 2.8720, Perplexity: 17.6723
Epoch [1/3], Step [4700/12942], Loss: 2.4157, Perplexity: 11.1974
Epoch [1/3], Step [4800/12942], Loss: 2.5142, Perplexity: 12.3567
Epoch [1/3], Step [4900/12942], Loss: 2.7110, Perplexity: 15.0449
Epoch [1/3], Step [5000/12942], Loss: 2.7930, Perplexity: 16.3293
Epoch [1/3], Step [5100/12942], Loss: 2.2614, Perplexity: 9.59662
Epoch [1/3], Step [5200/12942], Loss: 2.3257, Perplexity: 10.2342
Epoch [1/3], Step [5300/12942], Loss: 2.6813, Perplexity: 14.6046
Epoch [1/3], Step [5400/12942], Loss: 2.1052, Perplexity: 8.20846
Epoch [1/3], Step [5500/12942], Loss: 2.4743, Perplexity: 11.8735
Epoch [1/3], Step [5600/12942], Loss: 2.3778, Perplexity: 10.7810
Epoch [1/3], Step [5700/12942], Loss: 2.2466, Perplexity: 9.45570
Epoch [1/3], Step [5800/12942], Loss: 2.5144, Perplexity: 12.3592
Epoch [1/3], Step [5900/12942], Loss: 2.3321, Perplexity: 10.2990
Epoch [1/3], Step [6000/12942], Loss: 2.2766, Perplexity: 9.74336
Epoch [1/3], Step [6100/12942], Loss: 2.4079, Perplexity: 11.1112
Epoch [1/3], Step [6200/12942], Loss: 2.4732, Perplexity: 11.8604
Epoch [1/3], Step [6300/12942], Loss: 2.2788, Perplexity: 9.76549
Epoch [1/3], Step [6400/12942], Loss: 2.1478, Perplexity: 8.56601
Epoch [1/3], Step [6500/12942], Loss: 2.2292, Perplexity: 9.29231
Epoch [1/3], Step [6600/12942], Loss: 2.0019, Perplexity: 7.40307
Epoch [1/3], Step [6700/12942], Loss: 1.9852, Perplexity: 7.28074
Epoch [1/3], Step [6800/12942], Loss: 2.6648, Perplexity: 14.3655
Epoch [1/3], Step [6900/12942], Loss: 2.5390, Perplexity: 12.6667
Epoch [1/3], Step [7000/12942], Loss: 2.8003, Perplexity: 16.4493
Epoch [1/3], Step [7100/12942], Loss: 3.2178, Perplexity: 24.9721
Epoch [1/3], Step [7200/12942], Loss: 2.4372, Perplexity: 11.4408
Epoch [1/3], Step [7300/12942], Loss: 1.9344, Perplexity: 6.91969
Epoch [1/3], Step [7400/12942], Loss: 2.2094, Perplexity: 9.10996
Epoch [1/3], Step [7500/12942], Loss: 2.3470, Perplexity: 10.4546
Epoch [1/3], Step [7600/12942], Loss: 2.1369, Perplexity: 8.47300
Epoch [1/3], Step [7700/12942], Loss: 2.4351, Perplexity: 11.4172
Epoch [1/3], Step [7800/12942], Loss: 2.2697, Perplexity: 9.67638
Epoch [1/3], Step [7900/12942], Loss: 2.0820, Perplexity: 8.02025
Epoch [1/3], Step [8000/12942], Loss: 2.1367, Perplexity: 8.47109
Epoch [1/3], Step [8100/12942], Loss: 2.3832, Perplexity: 10.8395
Epoch [1/3], Step [8200/12942], Loss: 2.5056, Perplexity: 12.2510
Epoch [1/3], Step [8300/12942], Loss: 2.4975, Perplexity: 12.1522
Epoch [1/3], Step [8400/12942], Loss: 2.3969, Perplexity: 10.9887
Epoch [1/3], Step [8500/12942], Loss: 2.2480, Perplexity: 9.46859
Epoch [1/3], Step [8600/12942], Loss: 2.3648, Perplexity: 10.6419
Epoch [1/3], Step [8700/12942], Loss: 2.4079, Perplexity: 11.1108
Epoch [1/3], Step [8800/12942], Loss: 2.4897, Perplexity: 12.0577
Epoch [1/3], Step [8900/12942], Loss: 2.6433, Perplexity: 14.0588
Epoch [1/3], Step [9000/12942], Loss: 2.5121, Perplexity: 12.3307
Epoch [1/3], Step [9100/12942], Loss: 2.0573, Perplexity: 7.82500
Epoch [1/3], Step [9200/12942], Loss: 2.2568, Perplexity: 9.55235
Epoch [1/3], Step [9300/12942], Loss: 2.1197, Perplexity: 8.32879
Epoch [1/3], Step [9400/12942], Loss: 2.2685, Perplexity: 9.66539
Epoch [1/3], Step [9500/12942], Loss: 2.3515, Perplexity: 10.5014
Epoch [1/3], Step [9600/12942], Loss: 2.2634, Perplexity: 9.61531
Epoch [1/3], Step [9700/12942], Loss: 2.2119, Perplexity: 9.13325
Epoch [1/3], Step [9800/12942], Loss: 2.4017, Perplexity: 11.0424
Epoch [1/3], Step [9900/12942], Loss: 2.1407, Perplexity: 8.50503
Epoch [1/3], Step [10000/12942], Loss: 2.7242, Perplexity: 15.2439
Epoch [1/3], Step [10100/12942], Loss: 2.5548, Perplexity: 12.8685
Epoch [1/3], Step [10200/12942], Loss: 2.6145, Perplexity: 13.6607
Epoch [1/3], Step [10300/12942], Loss: 2.3636, Perplexity: 10.6290
Epoch [1/3], Step [10400/12942], Loss: 2.1593, Perplexity: 8.66520
Epoch [1/3], Step [10500/12942], Loss: 2.0100, Perplexity: 7.46323
Epoch [1/3], Step [10600/12942], Loss: 2.3095, Perplexity: 10.0697
Epoch [1/3], Step [10700/12942], Loss: 2.1231, Perplexity: 8.35707
Epoch [1/3], Step [10800/12942], Loss: 2.3049, Perplexity: 10.02274
Epoch [1/3], Step [10900/12942], Loss: 2.0865, Perplexity: 8.05655
Epoch [1/3], Step [11000/12942], Loss: 2.2762, Perplexity: 9.73970
Epoch [1/3], Step [11100/12942], Loss: 2.5270, Perplexity: 12.5154
Epoch [1/3], Step [11200/12942], Loss: 2.3701, Perplexity: 10.6983
Epoch [1/3], Step [11300/12942], Loss: 1.9640, Perplexity: 7.12819
Epoch [1/3], Step [11400/12942], Loss: 2.7928, Perplexity: 16.3261
Epoch [1/3], Step [11500/12942], Loss: 2.2410, Perplexity: 9.40305
Epoch [1/3], Step [11600/12942], Loss: 2.1444, Perplexity: 8.53708
Epoch [1/3], Step [11700/12942], Loss: 2.3747, Perplexity: 10.74787
Epoch [1/3], Step [11800/12942], Loss: 2.2786, Perplexity: 9.76262
Epoch [1/3], Step [11900/12942], Loss: 2.1302, Perplexity: 8.41637
Epoch [1/3], Step [12000/12942], Loss: 2.2899, Perplexity: 9.87384
Epoch [1/3], Step [12100/12942], Loss: 1.8926, Perplexity: 6.63654
Epoch [1/3], Step [12200/12942], Loss: 2.1171, Perplexity: 8.306774
Epoch [1/3], Step [12300/12942], Loss: 2.4975, Perplexity: 12.1527
Epoch [1/3], Step [12400/12942], Loss: 2.0315, Perplexity: 7.62565
Epoch [1/3], Step [12500/12942], Loss: 2.2027, Perplexity: 9.04934
Epoch [1/3], Step [12600/12942], Loss: 2.0430, Perplexity: 7.71340
Epoch [1/3], Step [12700/12942], Loss: 2.2373, Perplexity: 9.36847
Epoch [1/3], Step [12800/12942], Loss: 2.3398, Perplexity: 10.3796
Epoch [1/3], Step [12900/12942], Loss: 2.1781, Perplexity: 8.82971
Epoch [2/3], Step [100/12942], Loss: 2.8075, Perplexity: 16.568592
Epoch [2/3], Step [200/12942], Loss: 2.0501, Perplexity: 7.76847
Epoch [2/3], Step [300/12942], Loss: 2.3167, Perplexity: 10.1426
Epoch [2/3], Step [400/12942], Loss: 2.4514, Perplexity: 11.6047
Epoch [2/3], Step [500/12942], Loss: 2.1775, Perplexity: 8.82400
Epoch [2/3], Step [600/12942], Loss: 1.9331, Perplexity: 6.91073
Epoch [2/3], Step [700/12942], Loss: 2.6206, Perplexity: 13.7436
Epoch [2/3], Step [800/12942], Loss: 2.2834, Perplexity: 9.81025
Epoch [2/3], Step [900/12942], Loss: 2.0056, Perplexity: 7.43020
Epoch [2/3], Step [1000/12942], Loss: 2.5977, Perplexity: 13.4329
Epoch [2/3], Step [1100/12942], Loss: 2.2339, Perplexity: 9.33616
Epoch [2/3], Step [1200/12942], Loss: 1.8124, Perplexity: 6.12522
Epoch [2/3], Step [1300/12942], Loss: 2.2146, Perplexity: 9.15761
Epoch [2/3], Step [1400/12942], Loss: 1.9590, Perplexity: 7.09210
Epoch [2/3], Step [1500/12942], Loss: 1.7570, Perplexity: 5.79488
Epoch [2/3], Step [1600/12942], Loss: 2.2802, Perplexity: 9.77876
Epoch [2/3], Step [1700/12942], Loss: 2.2672, Perplexity: 9.65221
Epoch [2/3], Step [1800/12942], Loss: 2.2916, Perplexity: 9.89051
Epoch [2/3], Step [1900/12942], Loss: 2.0300, Perplexity: 7.61415
Epoch [2/3], Step [2000/12942], Loss: 2.2054, Perplexity: 9.07403
Epoch [2/3], Step [2100/12942], Loss: 2.0774, Perplexity: 7.98354
Epoch [2/3], Step [2200/12942], Loss: 2.3020, Perplexity: 9.99403
Epoch [2/3], Step [2300/12942], Loss: 2.2088, Perplexity: 9.10480
Epoch [2/3], Step [2400/12942], Loss: 2.6901, Perplexity: 14.7335
Epoch [2/3], Step [2500/12942], Loss: 2.1738, Perplexity: 8.79157
Epoch [2/3], Step [2600/12942], Loss: 2.2161, Perplexity: 9.17130
Epoch [2/3], Step [2700/12942], Loss: 2.1719, Perplexity: 8.77499
Epoch [2/3], Step [2800/12942], Loss: 1.9794, Perplexity: 7.23816
Epoch [2/3], Step [2900/12942], Loss: 2.0311, Perplexity: 7.62215
Epoch [2/3], Step [3000/12942], Loss: 2.0348, Perplexity: 7.65052
Epoch [2/3], Step [3100/12942], Loss: 2.2585, Perplexity: 9.56881
Epoch [2/3], Step [3200/12942], Loss: 1.8212, Perplexity: 6.17916
Epoch [2/3], Step [3300/12942], Loss: 2.2060, Perplexity: 9.07945
Epoch [2/3], Step [3400/12942], Loss: 2.2418, Perplexity: 9.41015
Epoch [2/3], Step [3500/12942], Loss: 2.1756, Perplexity: 8.80741
Epoch [2/3], Step [3600/12942], Loss: 2.0902, Perplexity: 8.08628
Epoch [2/3], Step [3700/12942], Loss: 2.4134, Perplexity: 11.1724
Epoch [2/3], Step [3800/12942], Loss: 1.9309, Perplexity: 6.89563
Epoch [2/3], Step [3900/12942], Loss: 2.1048, Perplexity: 8.20523
Epoch [2/3], Step [4000/12942], Loss: 2.3559, Perplexity: 10.5481
Epoch [2/3], Step [4100/12942], Loss: 2.6255, Perplexity: 13.8116
Epoch [2/3], Step [4200/12942], Loss: 2.1610, Perplexity: 8.68028
Epoch [2/3], Step [4300/12942], Loss: 2.0565, Perplexity: 7.81845
Epoch [2/3], Step [4400/12942], Loss: 2.4839, Perplexity: 11.9878
Epoch [2/3], Step [4500/12942], Loss: 2.0123, Perplexity: 7.48046
Epoch [2/3], Step [4600/12942], Loss: 2.1431, Perplexity: 8.52555
Epoch [2/3], Step [4700/12942], Loss: 2.1826, Perplexity: 8.86939
Epoch [2/3], Step [4800/12942], Loss: 1.9960, Perplexity: 7.35930
Epoch [2/3], Step [4900/12942], Loss: 2.1630, Perplexity: 8.69696
Epoch [2/3], Step [5000/12942], Loss: 2.7902, Perplexity: 16.2842
Epoch [2/3], Step [5100/12942], Loss: 1.9525, Perplexity: 7.04643
Epoch [2/3], Step [5200/12942], Loss: 1.9937, Perplexity: 7.34264
Epoch [2/3], Step [5300/12942], Loss: 2.0899, Perplexity: 8.08419
Epoch [2/3], Step [5400/12942], Loss: 2.0620, Perplexity: 7.86183
Epoch [2/3], Step [5500/12942], Loss: 2.0400, Perplexity: 7.69083
Epoch [2/3], Step [5600/12942], Loss: 2.1457, Perplexity: 8.54831
Epoch [2/3], Step [5700/12942], Loss: 2.2654, Perplexity: 9.63468
Epoch [2/3], Step [5800/12942], Loss: 1.9006, Perplexity: 6.68999
Epoch [2/3], Step [5900/12942], Loss: 1.9145, Perplexity: 6.78365
Epoch [2/3], Step [6000/12942], Loss: 2.3230, Perplexity: 10.2065
Epoch [2/3], Step [6100/12942], Loss: 2.2531, Perplexity: 9.51750
Epoch [2/3], Step [6200/12942], Loss: 2.0219, Perplexity: 7.55275
Epoch [2/3], Step [6300/12942], Loss: 2.1715, Perplexity: 8.77168
Epoch [2/3], Step [6400/12942], Loss: 3.0506, Perplexity: 21.1284
Epoch [2/3], Step [6500/12942], Loss: 2.2474, Perplexity: 9.46353
Epoch [2/3], Step [6600/12942], Loss: 2.1420, Perplexity: 8.51655
Epoch [2/3], Step [6700/12942], Loss: 2.1005, Perplexity: 8.16994
Epoch [2/3], Step [6800/12942], Loss: 2.2635, Perplexity: 9.61627
Epoch [2/3], Step [6900/12942], Loss: 1.8678, Perplexity: 6.47420
Epoch [2/3], Step [7000/12942], Loss: 1.9481, Perplexity: 7.01540
Epoch [2/3], Step [7100/12942], Loss: 2.1011, Perplexity: 8.17539
Epoch [2/3], Step [7200/12942], Loss: 1.9042, Perplexity: 6.71437
Epoch [2/3], Step [7300/12942], Loss: 1.9654, Perplexity: 7.13750
Epoch [2/3], Step [7400/12942], Loss: 1.8709, Perplexity: 6.49410
Epoch [2/3], Step [7500/12942], Loss: 2.1067, Perplexity: 8.22103
Epoch [2/3], Step [7600/12942], Loss: 2.1599, Perplexity: 8.67052
Epoch [2/3], Step [7700/12942], Loss: 2.1560, Perplexity: 8.63696
Epoch [2/3], Step [7800/12942], Loss: 2.3398, Perplexity: 10.37951
Epoch [2/3], Step [7900/12942], Loss: 1.9244, Perplexity: 6.85119
Epoch [2/3], Step [8000/12942], Loss: 2.1311, Perplexity: 8.42377
Epoch [2/3], Step [8100/12942], Loss: 2.0395, Perplexity: 7.68668
Epoch [2/3], Step [8200/12942], Loss: 2.1448, Perplexity: 8.53995
Epoch [2/3], Step [8300/12942], Loss: 2.0655, Perplexity: 7.88897
Epoch [2/3], Step [8400/12942], Loss: 2.1313, Perplexity: 8.42592
Epoch [2/3], Step [8500/12942], Loss: 2.0370, Perplexity: 7.66794
Epoch [2/3], Step [8600/12942], Loss: 2.2115, Perplexity: 9.12944
Epoch [2/3], Step [8700/12942], Loss: 1.8475, Perplexity: 6.34392
Epoch [2/3], Step [8800/12942], Loss: 2.2596, Perplexity: 9.57977
Epoch [2/3], Step [8900/12942], Loss: 1.7603, Perplexity: 5.81410
Epoch [2/3], Step [9000/12942], Loss: 2.1541, Perplexity: 8.619736
Epoch [2/3], Step [9100/12942], Loss: 1.9438, Perplexity: 6.98547
Epoch [2/3], Step [9200/12942], Loss: 1.9798, Perplexity: 7.24128
Epoch [2/3], Step [9300/12942], Loss: 1.9406, Perplexity: 6.96282
Epoch [2/3], Step [9400/12942], Loss: 2.6635, Perplexity: 14.3468
Epoch [2/3], Step [9500/12942], Loss: 1.9242, Perplexity: 6.84988
Epoch [2/3], Step [9600/12942], Loss: 2.0493, Perplexity: 7.76261
Epoch [2/3], Step [9700/12942], Loss: 2.0888, Perplexity: 8.07529
Epoch [2/3], Step [9800/12942], Loss: 1.9441, Perplexity: 6.98753
Epoch [2/3], Step [9900/12942], Loss: 1.8426, Perplexity: 6.31321
Epoch [2/3], Step [10000/12942], Loss: 2.1016, Perplexity: 8.1796
Epoch [2/3], Step [10100/12942], Loss: 1.9000, Perplexity: 6.68618
Epoch [2/3], Step [10200/12942], Loss: 2.1498, Perplexity: 8.58279
Epoch [2/3], Step [10300/12942], Loss: 1.7796, Perplexity: 5.92760
Epoch [2/3], Step [10400/12942], Loss: 2.6929, Perplexity: 14.7751
Epoch [2/3], Step [10500/12942], Loss: 2.1425, Perplexity: 8.52045
Epoch [2/3], Step [10600/12942], Loss: 2.0026, Perplexity: 7.40812
Epoch [2/3], Step [10700/12942], Loss: 1.7940, Perplexity: 6.01332
Epoch [2/3], Step [10800/12942], Loss: 2.1430, Perplexity: 8.52534
Epoch [2/3], Step [10900/12942], Loss: 1.9945, Perplexity: 7.34826
Epoch [2/3], Step [11000/12942], Loss: 2.3284, Perplexity: 10.2615
Epoch [2/3], Step [11100/12942], Loss: 2.6326, Perplexity: 13.9092
Epoch [2/3], Step [11200/12942], Loss: 1.8429, Perplexity: 6.31496
Epoch [2/3], Step [11300/12942], Loss: 2.0319, Perplexity: 7.62890
Epoch [2/3], Step [11400/12942], Loss: 2.2403, Perplexity: 9.39602
Epoch [2/3], Step [11500/12942], Loss: 2.2310, Perplexity: 9.30954
Epoch [2/3], Step [11600/12942], Loss: 2.0283, Perplexity: 7.60112
Epoch [2/3], Step [11700/12942], Loss: 1.9442, Perplexity: 6.98801

## Step 3: (Optional) Validate your Model

To assess potential overfitting, one approach is to assess performance on a validation set.  If you decide to do this **optional** task, you are required to first complete all of the steps in the next notebook in the sequence (**3_Inference.ipynb**); as part of that notebook, you will write and test code (specifically, the `sample` method in the `DecoderRNN` class) that uses your RNN decoder to generate captions.  That code will prove incredibly useful here.

If you decide to validate your model, please do not edit the data loader in **data_loader.py**.  Instead, create a new file named **data_loader_val.py** containing the code for obtaining the data loader for the validation data.  You can access:
- the validation images at filepath `'/opt/cocoapi/images/train2014/'`, and
- the validation image caption annotation file at filepath `'/opt/cocoapi/annotations/captions_val2014.json'`.

The suggested approach to validating your model involves creating a json file such as [this one](https://github.com/cocodataset/cocoapi/blob/master/results/captions_val2014_fakecap_results.json) containing your model's predicted captions for the validation images.  Then, you can write your own script or use one that you [find online](https://github.com/tylin/coco-caption) to calculate the BLEU score of your model.  You can read more about the BLEU score, along with other evaluation metrics (such as TEOR and Cider) in section 4.1 of [this paper](https://arxiv.org/pdf/1411.4555.pdf).  For more information about how to use the annotation file, check out the [website](http://cocodataset.org/#download) for the COCO dataset._____no_output_____

# (Optional) TODO: Validate your model._____no_output_____


# Notebook from tar-singh/kallistobustools
Path: notebooks/Mixed_species_10x_v2.ipynb

_____no_output_____In this vignette, we process fastq data from scRNA-seq (10x v2 chemistry) with to make a sparse matrix that can be used in downstream analysis with command line tools `kallisto` and `bustools`, as described in the [`kallisto bus` paper](https://www.biorxiv.org/content/10.1101/673285v1). Then we will start a standard downstream analysis with `Seurat`. _____no_output_____

system("pip3 install kb-python")_____no_output_____system("wget https://github.com/pachterlab/kallisto/releases/download/v0.46.1/kallisto_linux-v0.46.1.tar.gz")
untar("kallisto_linux-v0.46.1.tar.gz")_____no_output_____system("cp kallisto/kallisto /usr/local/bin")_____no_output_____

Since `Seurat` is a large package with many dependencies, it takes a while to install the packages, especially those with compiled code._____no_output_____

install.packages("BiocManager")
BiocManager::install(c("multtest", "DropletUtils", "BUSpaRse"))
install.packages(c("Seurat", "biomartr"))Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)

Bioconductor version 3.10 (BiocManager 1.30.10), R 3.6.2 (2019-12-12)

Installing package(s) 'BiocVersion', 'multtest', 'DropletUtils', 'BUSpaRse'

also installing the dependencies ‘formatR’, ‘bit’, ‘vctrs’, ‘lambda.r’, ‘futile.options’, ‘matrixStats’, ‘bit64’, ‘blob’, ‘DBI’, ‘zlibbioc’, ‘bitops’, ‘Rhtslib’, ‘SummarizedExperiment’, ‘futile.logger’, ‘snow’, ‘limma’, ‘locfit’, ‘DelayedArray’, ‘IRanges’, ‘R.oo’, ‘R.methodsS3’, ‘sitmo’, ‘RSQLite’, ‘XML’, ‘BiocFileCache’, ‘rappdirs’, ‘XVector’, ‘rtracklayer’, ‘Rsamtools’, ‘ProtGenerics’, ‘RCurl’, ‘GenomeInfoDbData’, ‘GenomicAlignments’, ‘BiocGenerics’, ‘Biobase’, ‘SingleCellExperiment’, ‘S4Vectors’, ‘BiocParallel’, ‘edgeR’, ‘rhdf5’, ‘HDF5Array’, ‘R.utils’, ‘dqrng’, ‘beachmat’, ‘Rhdf5lib’, ‘AnnotationDbi’, ‘AnnotationFilter’, ‘biomaRt’, ‘Biostrings’, ‘BSgenome’, ‘data.table’, ‘ensembldb’, ‘GenomeInfoDb’, ‘GenomicFeatures’, ‘GenomicRanges’, ‘plyranges’, ‘RcppParallel’, ‘RcppArmadillo’, ‘RcppProgress’

Old packages: 'BH', 'broom', 'callr', 'cli', 'curl', 'DBI', 'DT', 'fansi',
  'farver', 'gh', 'hms', 'knitr', 'mime', 'pillar', 'prettyunits', 'rlang',
  'rmarkdown', 'rprojroot', 'stringi', 'tidyr', 'tinytex', 'vctrs', 'xfun',
  'xtable', 'boot', 'foreign', 'MASS'

Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)

also installing the dependencies ‘mnormt’, ‘numDeriv’, ‘TH.data’, ‘sandwich’, ‘lsei’, ‘bibtex’, ‘gbRd’, ‘sn’, ‘mvtnorm’, ‘plotrix’, ‘multcomp’, ‘gtools’, ‘gdata’, ‘caTools’, ‘npsurv’, ‘globals’, ‘listenv’, ‘zoo’, ‘Rdpack’, ‘TFisher’, ‘mutoss’, ‘hexbin’, ‘gplots’, ‘gridExtra’, ‘FNN’, ‘RSpectra’, ‘ape’, ‘cowplot’, ‘fitdistrplus’, ‘future’, ‘future.apply’, ‘ggrepel’, ‘ggridges’, ‘ica’, ‘igraph’, ‘irlba’, ‘leiden’, ‘lmtest’, ‘metap’, ‘pbapply’, ‘plotly’, ‘png’, ‘RANN’, ‘RcppAnnoy’, ‘reticulate’, ‘ROCR’, ‘rsvd’, ‘Rtsne’, ‘sctransform’, ‘SDMTools’, ‘tsne’, ‘uwot’, ‘RcppEigen’

library(BUSpaRse)
library(Seurat)
library(tidyverse)
library(DropletUtils)
library(Matrix)
library(biomartr)
theme_set(theme_bw())_____no_output_____

## Download data
The data set we are using here is 1k 1:1 Mixture of Fresh Frozen Human (HEK293T) and Mouse (NIH3T3) Cells from the 10x website. First, we download the fastq files (6.34 GB)._____no_output_____

if (!dir.exists("./data")) dir.create("./data")
if (!file.exists("./data/hgmm_1k_fastqs.tar")) {
  download.file("http://cf.10xgenomics.com/samples/cell-exp/2.1.0/hgmm_1k/hgmm_1k_fastqs.tar", destfile = "./data/hgmm_1k_fastqs.tar", quiet = TRUE)
}_____no_output_____

Then untar this file_____no_output_____

untar("./data/hgmm_1k_fastqs.tar", exdir = "./data")_____no_output_____

# Generate the gene count matrix
## Build the `kallisto` index
Here we use [kallisto](https://pachterlab.github.io/kallisto/about) to pseudoalign the reads to the transcriptome and then to create the `bus` file to be converted to a sparse matrix. The first step is to build an index of the transcriptome. This data set has both human and mouse cells, so we need both human and mouse transcriptomes. The transcriptomes downloaded here are from Ensembl version 99, the most recent version as of writing. _____no_output_____

if (!dir.exists("./reference")) dir.create("./reference")
# Human transcriptome
if (!file.exists("./reference/hs_cdna.fa.gz")) {
  download.file("ftp://ftp.ensembl.org/pub/release-99/fasta/homo_sapiens/cdna/Homo_sapiens.GRCh38.cdna.all.fa.gz",
  "./reference/hs_cdna.fa.gz", method = "wget", quiet = TRUE)
}
# Mouse transcriptome
if (!file.exists("./reference/mm_cdna.fa.gz")) {
  download.file("ftp://ftp.ensembl.org/pub/release-99/fasta/mus_musculus/cdna/Mus_musculus.GRCm38.cdna.all.fa.gz",
  "./reference/mm_cdna.fa.gz", method = "wget", quiet = TRUE)
}_____no_output_____if (!file.exists("./reference/hs_mm_tr_index.idx")) {
  system("kallisto index -i ./reference/hs_mm_tr_index.idx ./reference/hs_cdna.fa.gz ./reference/mm_cdna.fa.gz")
}_____no_output_____tr2g <- map_dfr(c("./reference/hs_cdna.fa.gz", "./reference/mm_cdna.fa.gz"), tr2g_fasta)
save_tr2g_bustools(tr2g, "./reference/hs_mm_tr2g.tsv")Reading FASTA file.

Reading FASTA file.

fastq_fns <- list.files("./data/fastqs", full.name = TRUE)
fastq_fns <- fastq_fns[str_detect(fastq_fns, "R\\d")]_____no_output_____fastq_fns_____no_output_____system(paste("kb count -i reference/hs_mm_tr_index.idx -g reference/hs_mm_tr2g.tsv -x 10xv2 -o output",
paste(fastq_fns, collapse = " "), sep = " "), intern = TRUE)_____no_output_____

See what the outputs are_____no_output_____

list.files("./output", recursive = TRUE)_____no_output_____

Explaining the output:

* `matrix.ec`: A text file with two columns. The first column is the 0 based index of equivalence classes. The second column is the set of transcripts (denoted by 0 based index based on order of appearance in the transcriptome fasta file) present in the corresponding equivalence class.
* `output.bus`: The data represented in `bus` format. This is a binary file, so don't use something like `read.table` to read it into R.
* `run_info.json`: Information about the call to `kallisto bus`, including the command used, number and percentage of reads pseudoaligned, version of `kallisto` used, and etc.
* `transcript.txt`: A text file with one column, which is the transcripts present in the data, in the same order as in the transcriptome fasta file.

# Explore the data
Now we can load the matrix into R for analysis._____no_output_____

res_mat <- read_count_output("./output/counts_unfiltered",
                             name = "cells_x_genes", tcc = FALSE)_____no_output_____

## Remove empty droplets
Cool, so now we have the sparse matrix. What does it look like?_____no_output_____

dim(res_mat)_____no_output_____

The number of genes is as expected for two species. There're way more cells than we expect, which is about 1000. So what's going on?

How many UMIs per barcode?_____no_output_____

tot_counts <- Matrix::colSums(res_mat)
summary(tot_counts)_____no_output_____

The vast majority of "cells" have only a few UMI detected. Those are empty droplets. 10x claims to have cell capture rate of up to 65%, but in practice, depending on how many cells are in fact loaded, the rate can be much lower. A commonly used method to estimate the number of empty droplets is barcode ranking knee and inflection points, as those are often assumed to represent transition between two components of a distribution. While more sophisticated method exist (e.g. see [`emptyDrops` in `DropletUtils`](https://www.bioconductor.org/packages/devel/bioc/vignettes/DropletUtils/inst/doc/DropletUtils.html#detecting-empty-droplets)), for simplicity, we will use the barcode ranking method here. However, whichever way we go, we don't have the ground truth._____no_output_____

# Compute barcode rank
bc_rank <- barcodeRanks(res_mat)_____no_output_____#' Knee plot for filtering empty droplets
#'
#' Visualizes the inflection point to filter empty droplets. This function plots
#' different datasets with a different color. Facets can be added after calling
#' this function with `facet_*` functions.
#'
#' @param bc_rank A `DataFrame` output from `DropletUtil::barcodeRanks`.
#' @return A ggplot2 object.
knee_plot <- function(bc_rank) {
  knee_plt <- tibble(rank = bc_rank[["rank"]],
                     total = bc_rank[["total"]]) %>%
    distinct() %>%
    dplyr::filter(total > 0)
  annot <- tibble(inflection = metadata(bc_rank)[["inflection"]],
                  rank_cutoff = max(bc_rank$rank[bc_rank$total > metadata(bc_rank)[["inflection"]]]))
  p <- ggplot(knee_plt, aes(rank, total)) +
    geom_line() +
    geom_hline(aes(yintercept = inflection), data = annot, linetype = 2) +
    geom_vline(aes(xintercept = rank_cutoff), data = annot, linetype = 2) +
    scale_x_log10() +
    scale_y_log10() +
    annotation_logticks() +
    labs(x = "Rank", y = "Total UMIs")
  return(p)
}_____no_output_____options(repr.plot.width=9, repr.plot.height=6)
knee_plot(bc_rank)_____no_output_____

The inflection point looks like a reasonable number of cells._____no_output_____

# Filter the matrix
res_mat <- res_mat[, tot_counts > metadata(bc_rank)$inflection]
res_mat <- res_mat[Matrix::rowSums(res_mat) > 0,]
dim(res_mat)_____no_output_____

## Cell species
How many cells are from humans and how many from mice? The number of cells with mixed species indicates doublet rate._____no_output_____

gene_species <- ifelse(str_detect(rownames(res_mat), "^ENSMUSG"), "mouse", "human")
mouse_inds <- gene_species == "mouse"
human_inds <- gene_species == "human"
# mark cells as mouse or human
cell_species <- tibble(n_mouse_umi = Matrix::colSums(res_mat[mouse_inds,]),
                       n_human_umi = Matrix::colSums(res_mat[human_inds,]),
                       tot_umi = Matrix::colSums(res_mat),
                       prop_mouse = n_mouse_umi / tot_umi,
                       prop_human = n_human_umi / tot_umi)_____no_output_____# Classify species based on proportion of UMI, with cutoff of 90%
cell_species <- cell_species %>%
  mutate(species = case_when(
    prop_mouse > 0.9 ~ "mouse",
    prop_human > 0.9 ~ "human",
    TRUE ~ "mixed"
  ))_____no_output_____ggplot(cell_species, aes(n_human_umi, n_mouse_umi, color = species)) +
  geom_point()_____no_output_____

Great, looks like the vast majority of cells are not mixed._____no_output_____

cell_species %>%
  dplyr::count(species) %>%
  mutate(proportion = n / ncol(res_mat))_____no_output_____

Great, only about 0.3% of cells here are doublets, which is lower than the ~1% 10x lists. Doublet rate tends to be lower when cell concentration is lower. However, doublets can still be formed with cells from the same species, so the number of mixed species "cells" is only a lower bound of doublet rate.

## Dimension reduction_____no_output_____

seu <- CreateSeuratObject(res_mat, min.cells = 3) %>%
  SCTransform(verbose = FALSE)_____no_output_____# Add species to meta data
seu <- AddMetaData(seu, metadata = cell_species$species, col.name = "species")_____no_output_____

See how number of total counts and number of genes expressed are distributed._____no_output_____

VlnPlot(seu, c("nCount_RNA", "nFeature_RNA"), group.by = "species")_____no_output_____

Another QC plot_____no_output_____

ggplot(seu@meta.data, aes(nCount_RNA, nFeature_RNA, color = species)) +
  geom_point() +
  labs(x = "Total UMI counts per cell", y = "Number of genes detected")_____no_output_____

The mixed species doublets do look different from human and mouse cells._____no_output_____

seu <- RunPCA(seu, verbose = FALSE, npcs = 30)
ElbowPlot(seu, ndims = 30)_____no_output_____PCAPlot(seu, group.by = "species")_____no_output_____

The first PC separates species, as expected. Also as expected, the doublets are in between human and mouse cells in this plot._____no_output_____

seu <- RunTSNE(seu, dims = 1:20, check_duplicates = FALSE)
TSNEPlot(seu, group.by = "species")_____no_output_____

The species separate, and the few doublets form its own cluster, as expected._____no_output_____


# Notebook from BadenLab/Open-Visual-Stimuator
Path: Stimulator Calibration/opsin_spectra.ipynb

# Zebrafish Opsin Spectra
#### v1.1 | by M. Zimmermann

This script calculates opsin spectra as described in [Govardovskii et al.](https://www.cambridge.org/core/journals/visual-neuroscience/article/in-search-of-the-visual-pigment-template/A4738E821720092B7F5A233C4AB4962B) (In search of the visual pigment template, 2000)._____no_output_____

import os
import math
import numpy as np
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt

# Helper(s)
#
import scripts.spectrum as spc

# Set graphics to be plotted in the notebook
#
%matplotlib inline

# Seaborn plot settings
#
sns.set()

# General definition(s)
#
txtFileNewLineStr = "\r\n"

def setPlotStyle():
    # Presettings for figures
    #
    mpl.rcParams['figure.figsize'] = [10, 5]
    mpl.rc('font', size=10)
    mpl.rc('axes', titlesize=12)
    mpl.rc('axes', labelsize=12)
    mpl.rc('xtick', labelsize=12)
    mpl.rc('ytick', labelsize=12)
    mpl.rc('legend', fontsize=12)
    mpl.rc('figure', titlesize=12) _____no_output_____%%javascript
IPython.OutputArea.prototype._should_scroll = function(lines) {
    return false;
}_____no_output_____

Generate array with wavelengths in the relevant range:_____no_output_____## Setting global parameters
Here we defined the max absorbance for each cone opsin present in the Zebrafish. The following values are taken from
[Allison et al](https://www.cambridge.org/core/journals/visual-neuroscience/article/visual-pigment-composition-in-zebrafish-evidence-for-a-rhodopsinporphyropsin-interchange-system/D7824490079257A101123EBD593C153C) (Visual pigment composition in zebrafish:Evidence for a rhodopsin–porphyropsin interchange system, 2004)_____no_output_____

zebrafish_peak_nm = [361, 411, 482, 565, 503]
zebrafish_opsins  = ["UV-opsin", "S-opsin", "M-opsin", "L-opsin,", "rhodopsin"]

start_nm    = 300
end_nm      = 700
wavelengths = np.arange(start_nm, end_nm, 1)_____no_output_____

## Generate template

Following (A1 pigment) templates to desired peak wavelengths: lambdamax [Govardovskii et al.](https://www.cambridge.org/core/journals/visual-neuroscience/article/in-search-of-the-visual-pigment-template/A4738E821720092B7F5A233C4AB4962B) (In search of the visual pigment template, 2000). Itself adapted from the template described by [Lamb et al](https://www.sciencedirect.com/science/article/pii/004269899500114F?via%3Dihub) (Photoreceptor Spectral Sensitivities:Common Shape in the Long-wavelength Region, 1995)

***

#### AlphaBand     $$S\alpha(x) = \frac{1}{exp[A(a-x)] + exp[B(b-x)] + exp[C(c-x)] + D}$$

Where $x$ = $\frac{\lambda max}{\lambda}$

With $a$ the only variable parameter such as $a$ = $0.8795 + 0.0459 * exp^{\frac{(\lambda max -300)^{2}}{11940}}$, and constant parameters (A, B, b, C, c, D) as defined below.

***

#### BetaBand      $$S\beta(\lambda) = A\beta * exp^{-(\frac{\lambda - \lambda m\beta}{b})^{2}}$$

With $A_{\beta}$  the amplitude of the $\beta band$ relative to the $\alpha band$, $\lambda m_{\beta}$ the position of the $\beta peak$ and $b$ a bandwidth parameter
_____no_output_____

'''
This part of the code uses the absorbance spectrum fitting template for visual pigment
defined by Govardoskii et al (In Search of Visual Pigment Template, 2000).
'''

def Govardovskii (wvsloc, lambdamax): # This function returns one sensitivity spectrum point y at wavelength = wvs of an opsin  with a peak at lambdamax, wvs - such wavelengths at which the template will be evaluated: 1d-array lambdamax - peak of the sensitivity spectrum'''

   wvs = np.asarray(wvsloc)

   ## Alpha Band ##
   A = 69.7
   a = 0.8795+0.0459*np.exp(-np.square(lambdamax-300)/11940)
   B = 28
   b = 0.922
   C = -14.9
   c = 1.104
   D = 0.674
   x = lambdamax/wvs
   alphaband = 1/ (np.exp(A*(a-x))+np.exp(B*(b-x))+np.exp(C*(c-x))+D)

   ## Beta Band ##
   Ab = 0.26 #Beta value at peak
   b = -40.5+0.195*lambdamax #Beta bandwidth
   mb = 189+0.315*lambdamax #Beta peak
   betaband = Ab * np.exp(-1*((wvs-mb)/b)**2)

   return alphaband+betaband

rod = Govardovskii(wavelengths,zebrafish_peak_nm[4])
red = Govardovskii(wavelengths,zebrafish_peak_nm[3])
green = Govardovskii(wavelengths,zebrafish_peak_nm[2])
blue = Govardovskii(wavelengths,zebrafish_peak_nm[1])
uv = Govardovskii(wavelengths,zebrafish_peak_nm[0])

Opsin = np.zeros((len(wavelengths),5))
Opsin = (uv,blue,green,red,rod)

# Plot template
#
setPlotStyle()

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
col = ['m','b','g','r','k']
ax1.semilogy(wavelengths, Opsin[3], color=col[3], label="opsin template")
ax1.set_xlabel("wavelength [nm]")
ax1.set_ylabel("norm. sensitivity [log]")
ax1.set_ylim((0.005, 1.0))
ax1.set_xlim((300, 700))
ax2.plot(wavelengths, Opsin[3], color=col[3], label="opsin template")
ax2.set_ylim((0.005, 1.0))
ax2.set_ylabel("rel. sensitivity")
ax2.legend(bbox_to_anchor=(1.1, 1), loc="upper left")
ax2.grid(False)   _____no_output_____

## Generate zebrafish opsin curves

L-, M-, S-, UV- opsin as well as rhodopsin_____no_output_____

for iP in range (5):
    plt.plot(wavelengths, Opsin[iP], color=col[iP], label=zebrafish_opsins[iP])
plt.gca().set(xlabel="wavelength [nm]", ylabel="norm. sensitivity")
plt.legend(bbox_to_anchor=(1.1, 1), loc="upper left")_____no_output_____zebrafish_cone_opsin = np.zeros((len(wavelengths),5))
for iW in range (len(wavelengths)):
    zebrafish_cone_opsin[iW] = (Opsin[0][iW],Opsin[1][iW],Opsin[2][iW],Opsin[3][iW],wavelengths[iW])
np.savetxt('zebrafish_cone_opsins.txt',zebrafish_cone_opsin, fmt = '%1.6f')_____no_output_____


# Notebook from trial1user/Printing-Pattern-Programs
Path: Python Pattern Programs/Pyramid Patterns/Pyramid Patterns.ipynb

# Pyramid Patterns_____no_output_____![Pattern%206.jpg](attachment:Pattern%206.jpg)_____no_output_____

inc = 1
for x in range(5, 0, -1):
    for y in range(x, 0, -1):
        print(" ", end="")
    print("*" * inc)
    inc += 2;     *
    ***
   *****
  *******
 *********

![Pattern%2015.jpg](attachment:Pattern%2015.jpg)_____no_output_____

inc = 1
num = 1
for x in range(5, 0, -1):
    for y in range(x, 0, -1):
        print(" ", end="")
    print(str(num) * inc)
    num += 1
    inc += 2;     1
    222
   33333
  4444444
 555555555

![Pattern%2016.jpg](attachment:Pattern%2016.jpg)_____no_output_____

inc = 1
num = 1
for x in range(5, 0, -1):
    for y in range(x, 0, -1):
        print(" ", end="")
    print(str(num) * inc)
    num += 2
    inc += 2;     1
    333
   55555
  7777777
 999999999

![Pattern%2015.jpg](attachment:Pattern%2015.jpg)_____no_output_____

inc = 1
char = 65  # ASCII of 'A'
for x in range(5, 0, -1):
    for y in range(x, 0, -1):
        print(" ", end="")
    print(chr(char) * inc, end="")
    char += 1
    inc += 2;
    print()     A
    BBB
   CCCCC
  DDDDDDD
 EEEEEEEEE

![Pattern%2016.jpg](attachment:Pattern%2016.jpg)_____no_output_____

inc = 1
char = 65  # ASCII of 'A'
for x in range(5, 0, -1):
    for y in range(x, 0, -1):
        print(" ", end="")
    print(chr(char) * inc, end="")
    char += 2
    inc += 2;
    print()     A
    CCC
   EEEEE
  GGGGGGG
 IIIIIIIII

![Pattern%2017.jpg](attachment:Pattern%2017.jpg)_____no_output_____

inc = 1
for x in range(5, 0, -1):
    for y in range(x, 0, -1):
        print(" ", end="")
    for z in range(1, inc + 1):
        print(z, end="")
    inc += 2;
    print()     1
    123
   12345
  1234567
 123456789

![Pattern%2018.jpg](attachment:Pattern%2018.jpg)_____no_output_____

inc = 1
for x in range(5, 0, -1):
    for y in range(x, 0, -1):
        print(" ", end="")
    for z in range(inc, 0, -1):
        print(z, end="")
    inc += 2;
    print()     1
    321
   54321
  7654321
 987654321

![Pattern%2017.jpg](attachment:Pattern%2017.jpg)_____no_output_____

inc = 1
for x in range(5, 0, -1):
    for y in range(x, 0, -1):
        print(" ", end="")
    for z in range(1, inc + 1):
        print(chr(z + 64), end="")
    inc += 2;
    print()     A
    ABC
   ABCDE
  ABCDEFG
 ABCDEFGHI

![Pattern%2018.jpg](attachment:Pattern%2018.jpg)_____no_output_____

inc = 1
for x in range(5, 0, -1):
    for y in range(x, 0, -1):
        print(" ", end="")
    for z in range(inc, 0, -1):
        print(chr(z + 64), end="")
    inc += 2;
    print()     A
    CBA
   EDCBA
  GFEDCBA
 IHGFEDCBA

![Pattern%2019.jpg](attachment:Pattern%2019.jpg)_____no_output_____

inc = 1
for x in range(1, 6):
    for y in range(5, x, -1):
        print(" ", end="")
    for z in range(1, inc + 1):
        print(abs(z - x), end="")
    inc += 2;
    print()    0
   101
  21012
 3210123
432101234

![Pattern%2019.jpg](attachment:Pattern%2019.jpg)_____no_output_____

inc = 1
for x in range(1, 6):
    for y in range(5, x, -1):
        print(" ", end="")
    for z in range(1, inc + 1):
        print(chr(abs(z - x) + 65), end="")
    inc += 2;
    print()    A
   BAB
  CBABC
 DCBABCD
EDCBABCDE

![Pattern%2020.jpg](attachment:Pattern%2020.jpg)_____no_output_____

for x in range(1, 5):
    for y in range(4, x, -1):
        print(" ", end="")
    for z in range(x - 1, -x, -1):
        print(x - abs(z), end="")
    print()   1
  121
 12321
1234321

![Pattern%2020.jpg](attachment:Pattern%2020.jpg)_____no_output_____

inc = 1
for x in range(0, 5):
    for y in range(4, x, -1):
        print(" ", end="")
    for z in range(x, -(x + 1), -1):
        print(chr(x - abs(z) + 65), end="")
    print()    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA

![Pattern%207.jpg](attachment:Pattern%207.jpg)_____no_output_____

decr = 7
for x in range(4, 0, -1):
    for y in range(x, 5):
        print(" ", end="")
    print("*" * decr)
    decr -= 2 *******
  *****
   ***
    *

![Pattern%2021.jpg](attachment:Pattern%2021.jpg)_____no_output_____

decr = 7
for x in range(4, 0, -1):
    for y in range(x, 5):
        print(" ", end="")
    print(str(x) * decr)
    decr -= 2 4444444
  33333
   222
    1

![Pattern%2022.jpg](attachment:Pattern%2022.jpg)_____no_output_____

decr = 7
for x in range(4, 0, -1):
    for y in range(x, 5):
        print(" ", end="")
    print(str(decr) * decr)
    decr -= 2 7777777
  55555
   333
    1

![Pattern%2023.jpg](attachment:Pattern%2023.jpg)_____no_output_____

decr = 7
for x in range(4, 0, -1):
    for y in range(x, 5):
        print(" ", end="")
    for z in range(1, decr + 1):
        print(z, end="")
    decr -= 2
    print() 1234567
  12345
   123
    1

![Pattern%2021.jpg](attachment:Pattern%2021.jpg)_____no_output_____

decr = 7
for x in range(4, 0, -1):
    for y in range(x, 5):
        print(" ", end="")
    for z in range(1, decr + 1):
        print(chr(x + 64), end="")
    decr -= 2
    print() DDDDDDD
  CCCCC
   BBB
    A

![Pattern%2022.jpg](attachment:Pattern%2022.jpg)_____no_output_____

decr = 7
for x in range(4, 0, -1):
    for y in range(x, 5):
        print(" ", end="")
    for z in range(1, decr + 1):
        print(chr(decr + 64), end="")
    decr -= 2
    print() GGGGGGG
  EEEEE
   CCC
    A

![Pattern%2023.jpg](attachment:Pattern%2023.jpg)_____no_output_____

decr = 7
for x in range(4, 0, -1):
    for y in range(x, 5):
        print(" ", end="")
    for z in range(1, decr + 1):
        print(chr(z + 64), end="")
    decr -= 2
    print() ABCDEFG
  ABCDE
   ABC
    A

![Pattern%2010.jpg](attachment:Pattern%2010.jpg)_____no_output_____

for x in range(1, 6):
    for y in range(5, x, -1):
        print(" ", end="")
    for z in range(0, x):
        print("* ", end="")
    print()    *
   * *
  * * *
 * * * *
* * * * *
# Another Logic

for x in range(1, 6):
    for y in range(5, 0, -1):
        if x >= y:
            print("* ", end="")
        else:
            print(" ", end="")
    print()    *
   * *
  * * *
 * * * *
* * * * *

![Pattern%2028.jpg](attachment:Pattern%2028.jpg)_____no_output_____

for x in range(1, 6):
    for y in range(5, x, -1):
        print(" ", end="")
    for z in range(0, x):
        print(str(x) + " ", end="")
    print()    1
   2 2
  3 3 3
 4 4 4 4
5 5 5 5 5

![Pattern%2029.jpg](attachment:Pattern%2029.jpg)_____no_output_____

for x in range(1, 6):
    for y in range(5, x, -1):
        print(" ", end="")
    for z in range(1, x + 1):
        print(str(z) + " ", end="")
    print()    1
   1 2
  1 2 3
 1 2 3 4
1 2 3 4 5

![Pattern%2028.jpg](attachment:Pattern%2028.jpg)_____no_output_____

for x in range(1, 6):
    for y in range(5, x, -1):
        print(" ", end="")
    for z in range(0, x):
        print(chr(x + 64) + " ", end="")
    print()    A
   B B
  C C C
 D D D D
E E E E E

![Pattern%2029.jpg](attachment:Pattern%2029.jpg)_____no_output_____

for x in range(1, 6):
    for y in range(5, x, -1):
        print(" ", end="")
    for z in range(1, x + 1):
        print(chr(z + 64) + " ", end="")
    print()    A
   A B
  A B C
 A B C D
A B C D E

![Pattern%2011.jpg](attachment:Pattern%2011.jpg)_____no_output_____

for x in range(5, 0, -1):
    for y in range(x, 5):
        print(" ", end="")
    for z in range(0, x):
        print("* ", end="")
    print()* * * * *
 * * * *
  * * *
   * *
    *
# Another Logic

for x in range(5, 0, -1):
    for y in range(5, 0, -1):
        if x >= y:
            print("* ", end="")
        else:
            print(" ", end="")
    print()* * * * *
 * * * *
  * * *
   * *
    *

![Pattern%2030.jpg](attachment:Pattern%2030.jpg)_____no_output_____

for x in range(5, 0, -1):
    for y in range(x, 5):
        print(" ", end="")
    for z in range(0, x):
        print(str(x) + " ", end="")
    print()5 5 5 5 5
 4 4 4 4
  3 3 3
   2 2
    1

![Pattern%2031.jpg](attachment:Pattern%2031.jpg)_____no_output_____

for x in range(5, 0, -1):
    for y in range(x, 5):
        print(" ", end="")
    for z in range(x, 0, -1):
        print(str(z) + " ", end="")
    print()5 4 3 2 1
 4 3 2 1
  3 2 1
   2 1
    1

![Pattern%2030.jpg](attachment:Pattern%2030.jpg)_____no_output_____

for x in range(5, 0, -1):
    for y in range(x, 5):
        print(" ", end="")
    for z in range(0, x):
        print(chr(x + 64) + " ", end="")
    print()E E E E E
 D D D D
  C C C
   B B
    A

![Pattern%2031.jpg](attachment:Pattern%2031.jpg)_____no_output_____

for x in range(5, 0, -1):
    for y in range(x, 5):
        print(" ", end="")
    for z in range(x, 0, -1):
        print(chr(z + 64) + " ", end="")
    print()E D C B A
 D C B A
  C B A
   B A
    A

![Pattern%2032.jpg](attachment:Pattern%2032.jpg)_____no_output_____

for x in range(5, 0, -1):
    for y in range(x, 5):
        print(" ", end="")
    for z in range(0, x):
        print(chr(z + 65) + " ", end="")
    print()A B C D E
 A B C D
  A B C
   A B
    A

![Pattern%2019.jpg](attachment:Pattern%2019.jpg)_____no_output_____

ph = 5  # height
ps = ph - 1  # space
inc = 1  # min star

for x in range(0, ph):
    for y in range(ps, x, -1):
        print(" ", end="")
    for z in range(0, inc):
        print("*", end="")

    for y in range(ph + ph - 2, inc - 1, -1):
        print(" ", end="")
    for z in range(0, inc):
        print("*", end="")

    inc += 2
    print()    *        *
   ***      ***
  *****    *****
 *******  *******
******************

![Pattern%2020.jpg](attachment:Pattern%2020.jpg)_____no_output_____

ph = 5  # height
ps = ph - 1  # space
inc = 1

for x in range(0, ph):
    for y in range(ps, x, -1):
        print(" ", end="")
    for z in range(0, x + 1):
        print("* ", end="")

    for y in range(ph + ph - 2, inc - 1, -1):
        print(" ", end="")
    for z in range(0, x + 1):
        print("* ", end="")

    inc += 2
    print()    *         *
   * *       * *
  * * *     * * *
 * * * *   * * * *
* * * * * * * * * *

![Pattern%2021.jpg](attachment:Pattern%2021.jpg)_____no_output_____

ph = 5  # height
ps = ph - 1  # space
inc = 1

# Top Pyramid
for x in range(0, ph):
    # Broad Space
    for s in range(0, ph - 1):
        print(" ", end="")
    for s in range(ps, x - 1, -1):
        print(" ", end="")

    for y in range(0, x + 1):
        print("* ", end="")
    print()

# Bottom Pyramids
for x in range(0, ph):
    for y in range(ps, x, -1):
        print(" ", end="")
    for z in range(0, x + 1):
        print("* ", end="")

    for y in range(ph + ph - 2, inc - 1, -1):
        print(" ", end="")
    for z in range(0, x + 1):
        print("* ", end="")

    inc += 2
    print()         *
        * *
       * * *
      * * * *
     * * * * *
    *         *
   * *       * *
  * * *     * * *
 * * * *   * * * *
* * * * * * * * * *

![Pattern%2038.jpg](attachment:Pattern%2038.jpg)_____no_output_____

n = 3
inc = 1
for x in range(0, n):
    for s1 in range(n - 1, 0, -1):
        for s2 in range(n - 1, x, -1):
            print(" ", end=" ")
        for y in range(0, inc):
            print("*", end=" ")
        print()
    inc += 2    *
    *
  * * *
  * * *
* * * * *
* * * * *

![Pattern%2051.jpg](attachment:Pattern%2051.jpg)_____no_output_____

n = 3  # size
k = 1
p = 1
for x in range(0, n):
    for s in range(n - 1, x, -1):
        print(" ", end="")
    for y in range(0, k):
        if (x == y):
            g = (p + 1) * (y + y)
            if (g == 0):
                g = 1
            print(" " + str(g), end="")
            continue
        p += 2
        print(" " + str(p), end="")
    k += 2
    print()   1
  3 8 5
 7 9 40 11 13

![Pattern%2045.jpg](attachment:Pattern%2045.jpg)_____no_output_____

p_height = 5  # size
p_space = p_height - 1
m = 1
np = 1  # for num printing
cp = 'A'  # for char printing

for x in range(1, p_height + 1):
    for y in range(p_space, x - 1, -1):
        print(" ", end="")
    for z in range(1, m + 1):
        if (x % 2 != 0):
            print(np, end="")  # 1
            np += 1
        else:
            print(cp, end="")  # 2
            cp = chr(ord(cp) + 1)  # 3
        np = 1 if (np > 9) else np  # 4
        cp = 'A' if (cp > 'Z') else cp  # 5
    m += 2
    print()

    """
1) Print numbers if row is Odd
2) Print characters if row is Even
3) Incrementing the character
4) Reset number
5) Reset character
    """    1
   ABC
  23456
 DEFGHIJ
789123456

![Pattern%2062.jpg](attachment:Pattern%2062.jpg)_____no_output_____

n = 5  # size
for x in range(1, n + 1):
    for s in range(n - 1, x - 1, -1):
        print(" ", end="")
    for y in range(1, x + 1):
        if (x % 2 != 0):
            print(chr(x + 64) + " ", end="")  # for char
        else:
            print(str(x) + " ", end="")
    print()    A
   2 2
  C C C
 4 4 4 4
E E E E E

![Pattern%2063.jpg](attachment:Pattern%2063.jpg)_____no_output_____

n = 5  # size
for x in range(1, n + 1):
    for s in range(n - 1, x - 1, -1):
        print(" ", end="")
    for y in range(1, x + 1):
        if (x % 2 != 0):
            print(chr(x + 64) + " ", end="")  # for char
        else:
            print(str(y) + " ", end="")
    print()    A
   1 2
  C C C
 1 2 3 4
E E E E E

![Pattern%2064.jpg](attachment:Pattern%2064.jpg)_____no_output_____

n = 5  # size
for x in range(1, n + 1):
    for s in range(n - 1, x - 1, -1):
        print(" ", end="")
    for y in range(1, x + 1):
        if (x % 2 == 0):
            print(chr(x + 64) + " ", end="")  # for char
        else:
            print(str(x) + " ", end="")
    print()    1
   B B
  3 3 3
 D D D D
5 5 5 5 5

![Pattern%2065.jpg](attachment:Pattern%2065.jpg)_____no_output_____

n = 5  # size
for x in range(1, n + 1):
    for s in range(n - 1, x - 1, -1):
        print(" ", end="")
    for y in range(1, x + 1):
        if (x % 2 == 0):
            print(chr(y + 64) + " ", end="")  # for char
        else:
            print(str(y) + " ", end="")
    print()    1
   A B
  1 2 3
 A B C D
1 2 3 4 5

![Pattern%2066.jpg](attachment:Pattern%2066.jpg)_____no_output_____

n = 5  # size
for x in range(n, 0, -1):
    for s in range(n - 1, x - 1, -1):
        print(" ", end="")
    for y in range(1, x + 1):
        if (x % 2 != 0):
            print(chr(x + 64) + " ", end="")  # for char
        else:
            print(str(x) + " ", end="")
    print()E E E E E
 4 4 4 4
  C C C
   2 2
    A

![Pattern%2067.jpg](attachment:Pattern%2067.jpg)_____no_output_____

n = 5  # size
for x in range(n, 0, -1):
    for s in range(n - 1, x - 1, -1):
        print(" ", end="")
    for y in range(1, x + 1):
        if (x % 2 != 0):
            print(chr(y + 64) + " ", end="")  # for char
        else:
            print(str(y) + " ", end="")
    print()A B C D E
 1 2 3 4
  A B C
   1 2
    A

![Pattern%2068.jpg](attachment:Pattern%2068.jpg)_____no_output_____

n = 5  # size
for x in range(n, 0, -1):
    for s in range(n - 1, x - 1, -1):
        print(" ", end="")
    for y in range(1, x + 1):
        if (x % 2 == 0):
            print(chr(x + 64) + " ", end="")  # for char
        else:
            print(str(x) + " ", end="")
    print()5 5 5 5 5
 D D D D
  3 3 3
   B B
    1

![Pattern%2069.jpg](attachment:Pattern%2069.jpg)_____no_output_____

n = 5  # size
for x in range(n, 0, -1):
    for s in range(n - 1, x - 1, -1):
        print(" ", end="")
    for y in range(1, x + 1):
        if (x % 2 == 0):
            print(chr(y + 64) + " ", end="")  # for char
        else:
            print(str(y) + " ", end="")
    print()1 2 3 4 5
 A B C D
  1 2 3
   A B
    1

![Pattern%2075.jpg](attachment:Pattern%2075.jpg)_____no_output_____

p_height=5
for x in range(1, p_height+1):
    for y in range(p_height-1, x-1, -1):
        print(" ", end="")
    for z in range(1, x + 1):
        if x%2==0:
            print("* ", end="")
        else:
            print("+ ", end="")
    print()    +
   * *
  + + +
 * * * *
+ + + + +

![Pattern%2076.jpg](attachment:Pattern%2076.jpg)_____no_output_____

n = 5 # size
px = n #print control
py = n

for x in range(1, n+1):
    for y in range(1, n*2):
        if y == px or y == py or x == n:
            print("*", end="")
        else:
            print(" ", end="")
    px-=1
    py+=1
    print()    *
   * *
  *   *
 *     *
*********

![Pattern%20104.jpg](attachment:Pattern%20104.jpg)_____no_output_____

n=4 #size
pc=1
for x in range(1, n+1):
    for y in range(n-1, x-1, -1):
        print(" ", end=" ")
    for z in range(x-1,-x, -1):
        print(pc-abs(z), end=" ")
    pc+=2
    print()      1
    2 3 2
  3 4 5 4 3
4 5 6 7 6 5 4

![Pattern%2088.jpg](attachment:Pattern%2088.jpg)_____no_output_____

n=4 #size
pc=0
for x in range(1, n+1):
    for y in range(n-1, x-1, -1):
        print(" ", end=" ")
    for z in range(x-1,-x, -1):
        print(chr(pc-abs(z)+65), end=" ")
    pc+=2
    print()

    """
65 > ASCII of 'A'
    """      A
    B C B
  C D E D C
D E F G F E D

![Pattern%20105.jpg](attachment:Pattern%20105.jpg)_____no_output_____

n=4 #size
p=1
c=1
for x in range(1, n+1):
    for y in range(n-1, x-1, -1):
        print(" ", end=" ")
    p=1
    print(p, end=" ")

    while (c<x):
        p *= 2
        print(p,end=" ")
        c += 1

    while (c > 1):
        p //= 2
        print(p, end=" ")
        c -= 1

    print()      1
    1 2 1
  1 2 4 2 1
1 2 4 8 4 2 1