{"cells":[{"metadata":{},"cell_type":"markdown","source":"![](https://www.bms.com/assets/bms/us/en-us/images/bms-logo-social-default-card.jpg)\n\n<h1><center>Bristol Myers Squibb - EDA</center></h1>\n\n# 1. <a id='Introduction'>Introduction</a>\n\n###  1.1 What is Bristol-Myers Squibb?\n- Bristol-Myers Squibb is a global biopharmaceutical company working to transform patients' lives through science. Their mission is to discover, develop, and deliver innovative medicines that help patients prevail over serious diseases.\n-  At [Bristol Myers Squibb](https://www.bms.com/about-us/our-company.html), they believe in the power of science to address some of the most challenging diseases of their time. Their focus on these unmet needs comes during a remarkable time, when unprecedented scientific breakthroughs are advancing the treatment of disease as never before in human history.They work every day to transform patients’ lives through science. Each day, their employees around the world work together for patients – they are at the center of everything we do. They inspire them. They are the reason they come to work each day. They combine the agility of a biotech with the reach and resources of an established pharmaceutical company to create a global leading biopharma company powered by talented individuals who drive scientific innovation. They are proud of the advancements they have made in oncology, hematology, immunology and cardiovascular disease, and we are dedicated to helping patients prevail over serious diseases through our diverse and promising pipelines and new scientific platforms.As global citizens, they work sustainably and responsibly to create a positive impact in the communities where they live and work. They conduct our global business with the utmost integrity and transparency. They support programs, initiatives and organizations that help improve health, expand research opportunities, promote and expand STEM education and deliver basic human services to our communities.They know that our responsibility goes beyond medicine. They promote health equity globally and strive to increase access to life-saving medicines for populations disproportionately affected by serious diseases and conditions. They achieve this in part through the Bristol Myers Squibb Foundation, which supports community-based programs that promote cancer awareness, screening, care and support among high-risk populations."},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"from IPython.display import HTML\nHTML('<center><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/abEJwnaIIBQ\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></center>')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"###  1.2 What is Bristol-Myers Squibb – Molecular Translation Competition?\n\n- This competition, you’ll interpret old chemical images. With access to a large set of synthetic image data generated by Bristol-Myers Squibb, you'll convert images back to the underlying chemical structure annotated as [InChI](https://en.wikipedia.org/wiki/International_Chemical_Identifier) text.\n- Tools to curate chemistry literature would be a significant benefit to researchers. If successful, you'll help chemists expand access to collective chemical research. In turn, this would speed up research and development efforts in many key fields by avoiding repetition of previously published chemistries and identifying novel trends via mining large data sets.\n\n### 1.3 What is InChI?\n\n- The IUPAC International Chemical Identifier (InChITM) is a non-proprietary identifier for chemical substances that can be used in printed and electronic data sources thus enabling easier linking of diverse data compilations. It was developed under IUPAC Project 2000-025-1-800 during the period 2000-2004. Details of the project and the history of its progress are available from the project webpage. InChI projects are managed by the InChI subcommittee.\n- The InChITM program version 1.04 is free software developed under the auspices of the International Union of Pure and Applied Chemistry (IUPAC) and the InChI Trust. You can redistribute it and/or modify it under the terms of the IUPAC-InChI Trust License.\n- IUPAC continues to maintain oversight of InChI development; only systems compliant with the validation protocol (first issued with software release 1.01) are authorised to use the InChI designation."},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"HTML('<center><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/mpZj4b9elYE\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></center>')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"HTML('<center><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/rAnJ5toz26c\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></center>')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"HTML('<center><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/X9c0PHXPfso\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></center>')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"HTML('<center><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/UxSNOtv8Rjw\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></center>')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"HTML('<center><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/qrCqJ0o4jGs\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></center>')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 1.5 Metric: Levenshtein distance\n\n![](https://lh3.googleusercontent.com/proxy/iaPV-aGeETSD2u6kqZoORXmGfknvo284J68PtnTJ2lDsPAz1coLK0YD4p_25yCf4elckEA0NlH7t65YtVL9n-v2o-BdUyR4NuO6IXm7ggdwli2F-A-8l32lj560)\n\n- Image Credits = http://www.levenshtein.net/\n- The evaluation metric of this competition is Levenshtein distance\n\nThe Levenshtein algorithm (also called Edit-Distance) calculates the least number of edit operations that are necessary to modify one string to obtain another string. The most common way of calculating this is by the dynamic programming approach. A matrix is initialized measuring in the (m,n)-cell the Levenshtein distance between the m-character prefix of one with the n-prefix of the other word. The matrix can be filled from the upper left to the lower right corner. Each jump horizontally or vertically corresponds to an insert or a delete, respectively. The cost is normally set to 1 for each of the operations. The diagonal jump can cost either one, if the two characters in the row and column do not match or 0, if they do. Each cell always minimizes the cost locally. This way the number in the lower right corner is the Levenshtein distance between both words.\n\nIn approximate string matching, the objective is to find matches for short strings in many longer texts, in situations where a small number of differences is to be expected. The short strings could come from a dictionary, for instance. Here, one of the strings is typically short, while the other is arbitrarily long. This has a wide range of applications, for instance, spell checkers, correction systems for optical character recognition, and software to assist natural language translation based on translation memory.\n\nThe Levenshtein distance can also be computed between two longer strings, but the cost to compute it, which is roughly proportional to the product of the two string lengths, makes this impractical. Thus, when used to aid in fuzzy string searching in applications such as record linkage, the compared strings are usually short to help improve speed of comparisons.\n\nIn linguistics, the Levenshtein distance is used as a metric to quantify the linguistic distance, or how different two languages are from one another. It is related to mutual intelligibility, the higher the linguistic distance, the lower the mutual intelligibility, and the lower the linguistic distance, the higher the mutual intelligibility. \n\nRead more about it [here](https://www.cuelogic.com/blog/the-levenshtein-algorithm)\n\nYou can see the implementation in the following code"},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"import numpy as np\ndef levenshtein_ratio_and_distance(s, t, ratio_calc = False):\n    \"\"\" levenshtein_ratio_and_distance:\n        Calculates levenshtein distance between two strings.\n        If ratio_calc = True, the function computes the\n        levenshtein distance ratio of similarity between two strings\n        For all i and j, distance[i,j] will contain the Levenshtein\n        distance between the first i characters of s and the\n        first j characters of t\n    \"\"\"\n    # Initialize matrix of zeros\n    rows = len(s)+1\n    cols = len(t)+1\n    distance = np.zeros((rows,cols),dtype = int)\n\n    # Populate matrix of zeros with the indeces of each character of both strings\n    for i in range(1, rows):\n        for k in range(1,cols):\n            distance[i][0] = i\n            distance[0][k] = k\n\n    # Iterate over the matrix to compute the cost of deletions,insertions and/or substitutions    \n    for col in range(1, cols):\n        for row in range(1, rows):\n            if s[row-1] == t[col-1]:\n                cost = 0 # If the characters are the same in the two strings in a given position [i,j] then the cost is 0\n            else:\n                # In order to align the results with those of the Python Levenshtein package, if we choose to calculate the ratio\n                # the cost of a substitution is 2. If we calculate just distance, then the cost of a substitution is 1.\n                if ratio_calc == True:\n                    cost = 2\n                else:\n                    cost = 1\n            distance[row][col] = min(distance[row-1][col] + 1,      # Cost of deletions\n                                 distance[row][col-1] + 1,          # Cost of insertions\n                                 distance[row-1][col-1] + cost)     # Cost of substitutions\n    if ratio_calc == True:\n        # Computation of the Levenshtein Distance Ratio\n        Ratio = ((len(s)+len(t)) - distance[row][col]) / (len(s)+len(t))\n        return Ratio\n    else:\n        # print(distance) # Uncomment if you want to see the matrix showing how the algorithm computes the cost of deletions,\n        # insertions and/or substitutions\n        # This is the minimum number of edits needed to convert string a to string b\n        return \"The strings are {} edits away\".format(distance[row][col])\n    \n\nStr1 = 'Molecular Translation'\nStr2 = 'molecular translation'\nDistance = levenshtein_ratio_and_distance(Str1,Str2)\nprint(Distance)\nRatio = levenshtein_ratio_and_distance(Str1,Str2,ratio_calc = True)\nprint(Ratio)\nprint('#'*19,'Calculating after applying .lower() function','#'*20)\nDistance = levenshtein_ratio_and_distance(Str1.lower(),Str2.lower())\nprint(Distance)\nRatio = levenshtein_ratio_and_distance(Str1.lower(),Str2.lower(),ratio_calc = True)\nprint(Ratio)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"- From the above we can see that in the first case, the function found the 2 differences between the two strings. These were the upper/lower case m and t as well as a similarity ratio of 90.48%, which is pretty high.\n\n- In the second case, the distance has been reduced by 2 simply by turning the strings to lower case before comparing and the similarity ratio to 100%. This emphasizes the relevance of string preprocessing before performing calculations.\n\n- Below I would like to show even if we have same difference between two strings we have different scores if the string length is high."},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"Str1 = 'Molecular Translation'\nStr2 = 'molecular translation'\nprint('String Length: ',len(Str1))\nDistance = levenshtein_ratio_and_distance(Str1,Str2)\nprint(Distance)\nRatio = levenshtein_ratio_and_distance(Str1,Str2,ratio_calc = True)\nprint(Ratio)\nStr1 = 'Molecular Translation Exploratory Data Analysis'\nStr2 = 'molecular translation Exploratory Data Analysis'\nprint('String Length: ',len(Str1))\nDistance = levenshtein_ratio_and_distance(Str1,Str2)\nprint(Distance)\nRatio = levenshtein_ratio_and_distance(Str1,Str2,ratio_calc = True)\nprint(Ratio)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"Str1 = 'Molecular Translation'\nStr2 = 'molecular translation'\nprint('String Length: ',len(Str1))\nDistance = levenshtein_ratio_and_distance(Str1,Str2)\nprint(Distance)\nRatio = levenshtein_ratio_and_distance(Str1,Str2,ratio_calc = True)\nprint(Ratio)\nStr1 = 'MOlecular Translation'\nStr2 = 'molecular translation'\nprint('String Length: ',len(Str1))\nDistance = levenshtein_ratio_and_distance(Str1,Str2)\nprint(Distance)\nRatio = levenshtein_ratio_and_distance(Str1,Str2,ratio_calc = True)\nprint(Ratio)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"- As we can see, the ratio is directly proportional to the length of the string keeping the number of variations as constant.\n\n- Also the ratio is inversely proportional to the number of changes between two strings keeping the length of strings as constant."},{"metadata":{},"cell_type":"markdown","source":"# 2. <a id='importing'>Importing the necessary libraries📗</a> "},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nfrom os import listdir\nimport pandas as pd\nimport numpy as np\nimport glob\nimport tqdm\nfrom typing import Dict\nimport matplotlib.pyplot as plt\nimport pandas_profiling as pdp\nimport json\n%matplotlib inline\nimport shapely.geometry as sg\nimport shapely.ops as so\nimport zipfile\nimport cv2\n\n#plotly\n!pip install chart_studio\nimport plotly.express as px\nimport plotly.offline as pyo\nimport chart_studio.plotly as py\nimport plotly.graph_objs as go\nfrom plotly.offline import iplot\nimport cufflinks\ncufflinks.go_offline()\ncufflinks.set_config_file(world_readable=True, theme='pearl')\n\n#seaborn\nimport seaborn as sns\n\n#color\nfrom colorama import Fore, Back, Style\n\n#networkx\nimport networkx as nx\n\nimport seaborn as sns\nsns.set(style=\"whitegrid\")\n\nfrom PIL import Image\nimport cv2\nfrom tqdm.notebook import tqdm\nimport zipfile\n\n# Suppress warnings \nimport warnings\nwarnings.filterwarnings('ignore')\n\n# Settings for pretty nice plots\nplt.style.use('fivethirtyeight')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 3. <a id='reading'>Reading the train.csv 📚</a>"},{"metadata":{"trusted":true},"cell_type":"code","source":"# List files available\nlist(os.listdir(\"../input/bms-molecular-translation\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMAGE_PATH = \"../input/bms-molecular-translation/\"\n\ntrain_df = pd.read_csv('../input/bms-molecular-translation/train_labels.csv')\ntest_df = pd.read_csv('../input/bms-molecular-translation/sample_submission.csv')\n\nprint(Fore.YELLOW + 'Training data shape: ',Style.RESET_ALL,train_df.shape)\nprint(Fore.YELLOW + 'Test data shape: ',Style.RESET_ALL,test_df.shape)\n\ntrain_df.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df['out_folder'] = train_df['image_id'].str[0]\ntrain_df['mid_folder'] = train_df['image_id'].str[1]\ntrain_df['in_folder'] = train_df['image_id'].str[2]\ntest_df['out_folder'] = test_df['image_id'].str[0]\ntest_df['mid_folder'] = test_df['image_id'].str[1]\ntest_df['in_folder'] = test_df['image_id'].str[2]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 4. <a id='viewing'>Viewing the Images</a> "},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN_PATH = IMAGE_PATH + 'train/'\nTEST_PATH = IMAGE_PATH + 'test/'\nimg1 = cv2.cvtColor(cv2.imread(TRAIN_PATH + train_df['out_folder'][0] + '/' + train_df['mid_folder'][0] + '/' + train_df['in_folder'][0] + '/' + train_df['image_id'][0] + '.png')[:,:,::-1], cv2.COLOR_BGR2RGB)\nimg2 = cv2.cvtColor(cv2.imread(TEST_PATH + test_df['out_folder'][0] + '/' + test_df['mid_folder'][0] + '/' + test_df['in_folder'][0] + '/' + test_df['image_id'][0] + '.png')[:,:,::-1], cv2.COLOR_BGR2RGB)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(20,20))\n#plt.axis(\"off\")\nax1 = fig.add_subplot(211)\nax1.set_title('Train_Image')\nax1.axis('off')\nax1.imshow(img1)\nax2 = fig.add_subplot(212)\nax2.set_title('Test_Image')\nax2.axis('off')\nax2.imshow(img2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}