Jovian
⭐️
Sign In

Imports

In [2]:
%matplotlib inline
%reload_ext autoreload
%autoreload 2
In [3]:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from pathlib import Path
import feather
from tqdm import tqdm
from time import time
from sklearn.preprocessing import LabelEncoder
import jovian as jvn

In [3]:
from datetime import datetime
import math
In [4]:
pd.options.display.max_columns = 200
pd.options.display.max_rows = 300
pd.options.display.precision = 10
In [5]:
path = Path('./data')
In [6]:
train = feather.read_dataframe(path/'train.feather')
test = feather.read_dataframe(path/'test.feather')

Helper Functions

In [7]:
def col_split(df, col, targ='HasDetections'):
    grp = df.groupby([col, targ]).size().unstack()
    tot = len(df)
    row_sum = grp.sum(axis=1)
    for col in grp.columns:
        grp[f'{str(col)}_perc'] = grp[col]/row_sum
    grp['row_perc'] = row_sum/tot
    grp.sort_values('row_perc', inplace=True, ascending=False)
    return grp
In [8]:
def get_date_from_lab(lab):
    build_date, build_time = lab.split('.')[-1].split('-')
    build_date = int(build_date)
    day = build_date%100
    month = int(build_date/100)%100
    year = int(build_date/10000)%100
    year = 2000 + year
    month = 1 if month == 0 else month
    day = 1 if day == 0 else day
    try:
        return datetime(year, month, day)
    except:
        return datetime(year, month, 1)

Imputing Missing Values

In [9]:
maximpute_cols = ['RtpStateBitfield', 'AVProductStatesIdentifier', 'AVProductsInstalled', 'AVProductsEnabled',
                 'SMode', 'IsProtected', 'IeVerIdentifier', 'Firewall', 'UacLuaenable', 'Census_ProcessorCoreCount',
                 'Census_ProcessorManufacturerIdentifier', 'Census_OSInstallLanguageIdentifier', 'Census_IsFlightsDisabled',
                 'Census_ThresholdOptIn', 'Census_IsVirtualDevice', 'Census_IsAlwaysOnAlwaysConnectedCapable',
                 'Wdft_IsGamer', 'Census_ChassisTypeName', 'Census_PowerPlatformRoleName', 'Census_OSEdition', 'Census_GenuineStateName']
na_values = {}
for col in maximpute_cols:
    na_val = train[col].value_counts().idxmax()
    na_values[col] = na_val

for col in ['GeoNameIdentifier','Census_OEMNameIdentifier', 'Census_OEMModelIdentifier', 'Census_ProcessorModelIdentifier', 'Census_FirmwareManufacturerIdentifier', 'Census_FirmwareVersionIdentifier', 'Wdft_RegionIdentifier', 'OrganizationIdentifier']:
    na_val = max(train[col].max(), test[col].max())+1
    na_values[col] = na_val
    
for col in ['SmartScreen', 'Census_PrimaryDiskTypeName', 'Census_PrimaryDiskTypeName']:
    na_values[col] = 'NullVal'
        
for col in ['Census_InternalPrimaryDiagonalDisplaySizeInInches', 'Census_InternalPrimaryDisplayResolutionHorizontal', 'Census_InternalPrimaryDisplayResolutionVertical', 'Census_TotalPhysicalRAM']:
    na_values[col] = train[col].mean()
        
na_values['Census_InternalBatteryNumberOfCharges'] = train.Census_InternalBatteryNumberOfCharges[train.Census_InternalBatteryNumberOfCharges < 100000].mean()
na_values['OsBuildLab'] = '0.0.0.0.0-0'

train.fillna(na_values, inplace=True)
test.fillna(na_values, inplace=True)
In [10]:
df = pd.concat([train[['CountryIdentifier', 'CityIdentifier']], test[['CountryIdentifier', 'CityIdentifier']]])
city_map = df.groupby('CountryIdentifier')['CityIdentifier'].agg(lambda x: x.value_counts().idxmax())
train['CityIdentifier'].fillna(train['CountryIdentifier'].map(city_map), inplace=True)
test['CityIdentifier'].fillna(test['CountryIdentifier'].map(city_map), inplace=True)
In [11]:
train.loc[train['Census_InternalBatteryNumberOfCharges'] > 100000, 'Census_InternalBatteryNumberOfCharges'] = -1
train['Census_PrimaryDiskTotalCapacity'].fillna(train['Census_SystemVolumeTotalCapacity'], inplace=True)
train['Census_SystemVolumeTotalCapacity'].fillna(train['Census_PrimaryDiskTotalCapacity'], inplace=True)
train['Census_PrimaryDiskTotalCapacity'].fillna(train['Census_PrimaryDiskTotalCapacity'].mean(), inplace=True)
train['Census_SystemVolumeTotalCapacity'].fillna(train['Census_SystemVolumeTotalCapacity'].mean(), inplace=True)

test.loc[test['Census_InternalBatteryNumberOfCharges'] > 100000, 'Census_InternalBatteryNumberOfCharges'] = -1
test['Census_PrimaryDiskTotalCapacity'].fillna(test['Census_SystemVolumeTotalCapacity'], inplace=True)
test['Census_SystemVolumeTotalCapacity'].fillna(test['Census_PrimaryDiskTotalCapacity'], inplace=True)
test['Census_PrimaryDiskTotalCapacity'].fillna(test['Census_PrimaryDiskTotalCapacity'].mean(), inplace=True)
test['Census_SystemVolumeTotalCapacity'].fillna(test['Census_SystemVolumeTotalCapacity'].mean(), inplace=True)

Feature Generation

In [12]:
train.sample(10)
Out[12]:
In [13]:
for col in ['EngineVersion', 'AppVersion', 'AvSigVersion', 'OsVer', 'Census_OSVersion']:
    train[f'{col}_1'] = train[col].map(lambda x: '.'.join(x.split('.')[0:3])).astype('category')
    train[f'{col}_2'] = train[col].map(lambda x: '.'.join(x.split('.')[0:2])).astype('category')
    train[f'{col}_3'] = train[col].map(lambda x: '.'.join(x.split('.')[0:1])).astype('category')
    test[f'{col}_1'] = test[col].map(lambda x: '.'.join(x.split('.')[0:3])).astype('category')
    test[f'{col}_2'] = test[col].map(lambda x: '.'.join(x.split('.')[0:2])).astype('category')
    test[f'{col}_3'] = test[col].map(lambda x: '.'.join(x.split('.')[0:1])).astype('category')
In [14]:
train['OsBuildLab_1'] = train.OsBuildLab.map(lambda x: x.split('.')[1]).astype('category')
test['OsBuildLab_1'] = test.OsBuildLab.map(lambda x: x.split('.')[1]).astype('category')
In [15]:
train['BuildDate'] = train.OsBuildLab.map(get_date_from_lab)
test['BuildDate'] = test.OsBuildLab.map(get_date_from_lab)
In [16]:
train['BuildYear'] = train.BuildDate.dt.year
test['BuildYear'] = test.BuildDate.dt.year
In [17]:
train['BuildDaysElapsed'] = train.BuildDate.map(lambda x: (datetime.today()-x).days)
test['BuildDaysElapsed'] = test.BuildDate.map(lambda x: (datetime.today()-x).days)
In [18]:
def ram_in_gb(ram):
    if not pd.isnull(ram):
        return round(ram/512.0)/2
    else:
        return None
In [35]:
train['Census_TotalPhysicalRAM_GB'] = train.Census_TotalPhysicalRAM.map(ram_in_gb)
test['Census_TotalPhysicalRAM_GB'] = test.Census_TotalPhysicalRAM.map(ram_in_gb)
In [36]:
train['SystemVolumeRatio'] = train.Census_SystemVolumeTotalCapacity/(train.Census_PrimaryDiskTotalCapacity+0.00001)
train['NonSystemVolume'] = train.Census_PrimaryDiskTotalCapacity - train.Census_SystemVolumeTotalCapacity 
test['SystemVolumeRatio'] = test.Census_SystemVolumeTotalCapacity/(test.Census_PrimaryDiskTotalCapacity+0.00001)
test['NonSystemVolume'] = test.Census_PrimaryDiskTotalCapacity - test.Census_SystemVolumeTotalCapacity 
In [21]:
train['AspectRatio'] = train.Census_InternalPrimaryDisplayResolutionHorizontal/train.Census_InternalPrimaryDisplayResolutionVertical
test['AspectRatio'] = test.Census_InternalPrimaryDisplayResolutionHorizontal/test.Census_InternalPrimaryDisplayResolutionVertical
In [22]:
train['MegaPixels'] = (train.Census_InternalPrimaryDisplayResolutionHorizontal * train.Census_InternalPrimaryDisplayResolutionVertical)/1e6
test['MegaPixels'] = (test.Census_InternalPrimaryDisplayResolutionHorizontal * test.Census_InternalPrimaryDisplayResolutionVertical)/1e6
In [23]:
train['RamPerProcessor'] = train['Census_TotalPhysicalRAM']/ train['Census_ProcessorCoreCount']
test['RamPerProcessor'] = test['Census_TotalPhysicalRAM']/ test['Census_ProcessorCoreCount']

Remove Unwanted Columns

In [24]:
balance_ratios = pd.Series([train[col].value_counts().max()/len(train) for col in list(train.columns)], index=list(train.columns))
unbalanced_columns = list(balance_ratios[(balance_ratios > 0.9)].index)
In [25]:
null_ratios = (train.isnull().sum()/len(train))
null_columns = list(null_ratios[(null_ratios > 0.5)].index)
In [26]:
drop_columns = np.unique(unbalanced_columns+null_columns)
train.drop(drop_columns, axis=1, inplace=True)
test.drop(drop_columns, axis=1, inplace=True)

Categorizing Variables

In [30]:
train.sample(2)
Out[30]:
In [31]:
num_cols = ['AVProductsInstalled', 'Census_ProcessorCoreCount', 'Census_PrimaryDiskTotalCapacity', 'Census_SystemVolumeTotalCapacity', 'Census_TotalPhysicalRAM', 'Census_InternalPrimaryDiagonalDisplaySizeInInches', 'Census_InternalPrimaryDisplayResolutionHorizontal', 'Census_InternalPrimaryDisplayResolutionVertical', 'Census_InternalBatteryNumberOfCharges', 'BuildDaysElapsed', 'Census_TotalPhysicalRAM_GB', 'SystemVolumeRatio', 'NonSystemVolume', 'AspectRatio', 'MegaPixels', 'RamPerProcessor']
cat_cols = list(set(test.columns)-set(num_cols))
In [32]:
for col in cat_cols:
    cats = np.unique(list(train[col].unique())+list(test[col].unique()))
    cat_dtype = pd.api.types.CategoricalDtype(categories=cats, ordered=True)
    train[col] = train[col].astype(cat_dtype)
    test[col] = test[col].astype(cat_dtype)

Saving Data

In [38]:
feather.write_dataframe(train, path/'feature_augmented_train.feather')
feather.write_dataframe(test, path/'feature_augmented_test.feather')
In [4]:
jvn.commit()
[jovian] Saving notebook..
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-2383bf081587> in <module> ----> 1 jvn.commit() ~/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/jovian/__init__.py in commit(capture_env, filename) 10 def commit(capture_env=True, filename=None): 11 """Save the notebook, capture the environment, and upload to cloud for sharing""" ---> 12 res = create_gist_simple(filename) 13 if res is None: 14 return ~/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/jovian/utils/api.py in create_gist_simple(filename) 99 sleep(1) 100 if filename is None: --> 101 path = get_notebook_name() 102 if path is None: 103 log(FILENAME_MSG) ~/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/jovian/utils/jupyter.py in get_notebook_name() 115 def get_notebook_name(): 116 """Return the name of the notebook""" --> 117 return os.path.basename(get_notebook_path()) 118 119 ~/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/jovian/utils/jupyter.py in get_notebook_path() 93 return os.path.join(ss['notebook_dir'], relative_path) 94 else: ---> 95 return os.path.join(os.getcwd(), get_notebook_name_saved()) 96 97 ~/anaconda3/envs/fastai-v1/lib/python3.7/posixpath.py in join(a, *p) 92 path += sep + b 93 except (TypeError, AttributeError, BytesWarning): ---> 94 genericpath._check_arg_types('join', a, *p) 95 raise 96 return path ~/anaconda3/envs/fastai-v1/lib/python3.7/genericpath.py in _check_arg_types(funcname, *args) 147 else: 148 raise TypeError('%s() argument must be str or bytes, not %r' % --> 149 (funcname, s.__class__.__name__)) from None 150 if hasstr and hasbytes: 151 raise TypeError("Can't mix strings and bytes in path components") from None TypeError: join() argument must be str or bytes, not 'NoneType'
In [5]:
from jovian.utils.jupyter import get_notebook_name_saved
In [6]:
??get_notebook_name_saved
In [7]:
get_notebook_name_saved()
In [9]:
from jovian.utils import jupyter
In [10]:
jupyter.get_notebook_name()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-10-437e20697564> in <module> ----> 1 jupyter.get_notebook_name() ~/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/jovian/utils/jupyter.py in get_notebook_name() 115 def get_notebook_name(): 116 """Return the name of the notebook""" --> 117 return os.path.basename(get_notebook_path()) 118 119 ~/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/jovian/utils/jupyter.py in get_notebook_path() 93 return os.path.join(ss['notebook_dir'], relative_path) 94 else: ---> 95 return os.path.join(os.getcwd(), get_notebook_name_saved()) 96 97 ~/anaconda3/envs/fastai-v1/lib/python3.7/posixpath.py in join(a, *p) 92 path += sep + b 93 except (TypeError, AttributeError, BytesWarning): ---> 94 genericpath._check_arg_types('join', a, *p) 95 raise 96 return path ~/anaconda3/envs/fastai-v1/lib/python3.7/genericpath.py in _check_arg_types(funcname, *args) 147 else: 148 raise TypeError('%s() argument must be str or bytes, not %r' % --> 149 (funcname, s.__class__.__name__)) from None 150 if hasstr and hasbytes: 151 raise TypeError("Can't mix strings and bytes in path components") from None TypeError: join() argument must be str or bytes, not 'NoneType'
In [11]:
jupyter.get_notebook_path()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-11-153253081701> in <module> ----> 1 jupyter.get_notebook_path() ~/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/jovian/utils/jupyter.py in get_notebook_path() 93 return os.path.join(ss['notebook_dir'], relative_path) 94 else: ---> 95 return os.path.join(os.getcwd(), get_notebook_name_saved()) 96 97 ~/anaconda3/envs/fastai-v1/lib/python3.7/posixpath.py in join(a, *p) 92 path += sep + b 93 except (TypeError, AttributeError, BytesWarning): ---> 94 genericpath._check_arg_types('join', a, *p) 95 raise 96 return path ~/anaconda3/envs/fastai-v1/lib/python3.7/genericpath.py in _check_arg_types(funcname, *args) 147 else: 148 raise TypeError('%s() argument must be str or bytes, not %r' % --> 149 (funcname, s.__class__.__name__)) from None 150 if hasstr and hasbytes: 151 raise TypeError("Can't mix strings and bytes in path components") from None TypeError: join() argument must be str or bytes, not 'NoneType'
In [12]:
from notebook.notebookapp import list_running_servers
In [13]:
import ipykernel
In [14]:
ipykernel.connect.get_connection_file()
Out[14]:
'/run/user/1001/jupyter/kernel-f9bc8680-e983-44c8-ba27-89dce529e892.json'
In [15]:
list_running_servers()
Out[15]:
<generator object list_running_servers at 0x7f1509db82a0>
In [16]:
list(list_running_servers())
Out[16]:
[{'base_url': '/',
  'hostname': '0.0.0.0',
  'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
  'password': True,
  'pid': 1774,
  'port': 8081,
  'secure': False,
  'token': '',
  'url': 'http://0.0.0.0:8081/'}]
In [17]:
kernel_id = re.search('kernel-(.*).json',
                          ipykernel.connect.get_connection_file()).group(1)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-17-2b33ca2f62ee> in <module> ----> 1 kernel_id = re.search('kernel-(.*).json', 2 ipykernel.connect.get_connection_file()).group(1) NameError: name 're' is not defined
In [18]:
import re
In [23]:
kernel_id = re.search('kernel-(.*).json',
                          ipykernel.connect.get_connection_file()).group(1)
In [20]:
kernel_id
Out[20]:
'f9bc8680-e983-44c8-ba27-89dce529e892'
In [28]:
servers = list(list_running_servers())
In [30]:
ss = servers[0]
In [31]:
from requests.compat import urljoin
"""Jupyter related utilities"""
import json
import os.path
import re
import requests
import time
# from IPython.display import Javascript as d_js
from io import StringIO
import sys
# from IPython.utils import io
In [32]:
response = requests.get(urljoin(ss['url'], 'api/sessions'),
                                params={'token': ss.get('token', '')})
In [33]:
response
Out[33]:
<Response [403]>
In [37]:
ss
Out[37]:
{'base_url': '/',
 'hostname': '0.0.0.0',
 'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
 'password': True,
 'pid': 1774,
 'port': 8081,
 'secure': False,
 'token': '',
 'url': 'http://0.0.0.0:8081/'}
In [50]:
res2 = requests.get(urljoin('http://35.197.119.154:8081/', 'api/sessions'))
In [52]:
res2.text
Out[52]:
'{"message": "Forbidden", "reason": null}'
In [56]:
res = requests.get('http://0.0.0.0:8081/api/contents/DT%20Feature%20Generation.ipynb?token=245cb31f726ac05fe5df4ad1233a1694')
res.text
Out[56]:
'{"message": "Forbidden", "reason": null}'
In [40]:
from IPython import get_ipython

class Capturing(list):
    def __enter__(self):
        self._stdout = sys.stdout
        sys.stdout = self._stringio = StringIO()
        return self

    def __exit__(self, *args):
        self.extend(self._stringio.getvalue().splitlines())
        del self._stringio    # free up some memory
        sys.stdout = self._stdout

        
with Capturing() as cap:
    list(get_ipython().run_code(
        'print(globals()["nb_name"] + "hello") if "nb_name" in globals().keys() else None'))

In [41]:
cap
Out[41]:
[]
In [44]:
globals()
Out[44]:
{'__name__': '__main__',
 '__doc__': 'Jupyter related utilities',
 '__package__': None,
 '__loader__': None,
 '__spec__': None,
 '__builtin__': <module 'builtins' (built-in)>,
 '__builtins__': <module 'builtins' (built-in)>,
 '_ih': ['',
  'import pandas as pd\nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom pathlib import Path\nimport feather\nfrom tqdm import tqdm\nfrom time import time\nfrom sklearn.preprocessing import LabelEncoder\nimport jovian as jvn',
  "get_ipython().run_line_magic('matplotlib', 'inline')\nget_ipython().run_line_magic('reload_ext', 'autoreload')\nget_ipython().run_line_magic('autoreload', '2')",
  'import pandas as pd\nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom pathlib import Path\nimport feather\nfrom tqdm import tqdm\nfrom time import time\nfrom sklearn.preprocessing import LabelEncoder\nimport jovian as jvn',
  'jvn.commit()',
  'from jovian.utils.jupyter import get_notebook_name_saved',
  "get_ipython().run_line_magic('pinfo2', 'get_notebook_name_saved')",
  'get_notebook_name_saved()',
  'None',
  'from jovian.utils import jupyter',
  'jupyter.get_notebook_name()',
  'jupyter.get_notebook_path()',
  'from notebook.notebookapp import list_running_servers',
  'import ipykernel',
  'ipykernel.connect.get_connection_file()',
  'list_running_servers()',
  'list(list_running_servers())',
  "kernel_id = re.search('kernel-(.*).json',\n                          ipykernel.connect.get_connection_file()).group(1)",
  'import re',
  "kernel_id = re.search('kernel-(.*).json',\n                          ipykernel.connect.get_connection_file()).group(1)",
  'kernel_id',
  'servers = list_running_servers()',
  'servers',
  "kernel_id = re.search('kernel-(.*).json',\n                          ipykernel.connect.get_connection_file()).group(1)",
  'list(servers)',
  'ss = servers[0]',
  'ss = list(servers)[0]',
  'ss = (list(servers))[0]',
  'servers = list(list_running_servers())',
  'servers[0]',
  'ss = servers[0]',
  'from requests.compat import urljoin\n"""Jupyter related utilities"""\nimport json\nimport os.path\nimport re\nimport requests\nimport time\n# from IPython.display import Javascript as d_js\nfrom io import StringIO\nimport sys\n# from IPython.utils import io',
  "response = requests.get(urljoin(ss['url'], 'api/sessions'),\n                                params={'token': ss.get('token', '')})",
  'response',
  'ss',
  "res2 = requests.get(urljoin(ss['url'], 'api/sessions'))",
  'res2',
  'ss',
  'from IPython import get_ipython\nwith Capturing() as cap:\n    list(get_ipython().run_code(\n        \'print(globals()["nb_name"]) if "nb_name" in globals().keys() else None\'))',
  'from IPython import get_ipython\n\nclass Capturing(list):\n    def __enter__(self):\n        self._stdout = sys.stdout\n        sys.stdout = self._stringio = StringIO()\n        return self\n\n    def __exit__(self, *args):\n        self.extend(self._stringio.getvalue().splitlines())\n        del self._stringio    # free up some memory\n        sys.stdout = self._stdout\n\n        \nwith Capturing() as cap:\n    list(get_ipython().run_code(\n        \'print(globals()["nb_name"]) if "nb_name" in globals().keys() else None\'))',
  'from IPython import get_ipython\n\nclass Capturing(list):\n    def __enter__(self):\n        self._stdout = sys.stdout\n        sys.stdout = self._stringio = StringIO()\n        return self\n\n    def __exit__(self, *args):\n        self.extend(self._stringio.getvalue().splitlines())\n        del self._stringio    # free up some memory\n        sys.stdout = self._stdout\n\n        \nwith Capturing() as cap:\n    list(get_ipython().run_code(\n        \'print(globals()["nb_name"] + "hello") if "nb_name" in globals().keys() else None\'))',
  'cap',
  'globals()',
  "globals()['nb_name']",
  'globals()'],
 '_oh': {14: '/run/user/1001/jupyter/kernel-f9bc8680-e983-44c8-ba27-89dce529e892.json',
  15: <generator object list_running_servers at 0x7f1509db82a0>,
  16: [{'base_url': '/',
    'hostname': '0.0.0.0',
    'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
    'password': True,
    'pid': 1774,
    'port': 8081,
    'secure': False,
    'token': '',
    'url': 'http://0.0.0.0:8081/'}],
  20: 'f9bc8680-e983-44c8-ba27-89dce529e892',
  22: <generator object list_running_servers at 0x7f1509db8048>,
  24: [{'base_url': '/',
    'hostname': '0.0.0.0',
    'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
    'password': True,
    'pid': 1774,
    'port': 8081,
    'secure': False,
    'token': '',
    'url': 'http://0.0.0.0:8081/'}],
  29: {'base_url': '/',
   'hostname': '0.0.0.0',
   'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
   'password': True,
   'pid': 1774,
   'port': 8081,
   'secure': False,
   'token': '',
   'url': 'http://0.0.0.0:8081/'},
  33: <Response [403]>,
  34: {'base_url': '/',
   'hostname': '0.0.0.0',
   'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
   'password': True,
   'pid': 1774,
   'port': 8081,
   'secure': False,
   'token': '',
   'url': 'http://0.0.0.0:8081/'},
  36: <Response [403]>,
  37: {'base_url': '/',
   'hostname': '0.0.0.0',
   'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
   'password': True,
   'pid': 1774,
   'port': 8081,
   'secure': False,
   'token': '',
   'url': 'http://0.0.0.0:8081/'},
  41: [],
  42: {...}},
 '_dh': ['/home/vinodreddyg28/microsoft-malware-prediction'],
 'In': ['',
  'import pandas as pd\nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom pathlib import Path\nimport feather\nfrom tqdm import tqdm\nfrom time import time\nfrom sklearn.preprocessing import LabelEncoder\nimport jovian as jvn',
  "get_ipython().run_line_magic('matplotlib', 'inline')\nget_ipython().run_line_magic('reload_ext', 'autoreload')\nget_ipython().run_line_magic('autoreload', '2')",
  'import pandas as pd\nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom pathlib import Path\nimport feather\nfrom tqdm import tqdm\nfrom time import time\nfrom sklearn.preprocessing import LabelEncoder\nimport jovian as jvn',
  'jvn.commit()',
  'from jovian.utils.jupyter import get_notebook_name_saved',
  "get_ipython().run_line_magic('pinfo2', 'get_notebook_name_saved')",
  'get_notebook_name_saved()',
  'None',
  'from jovian.utils import jupyter',
  'jupyter.get_notebook_name()',
  'jupyter.get_notebook_path()',
  'from notebook.notebookapp import list_running_servers',
  'import ipykernel',
  'ipykernel.connect.get_connection_file()',
  'list_running_servers()',
  'list(list_running_servers())',
  "kernel_id = re.search('kernel-(.*).json',\n                          ipykernel.connect.get_connection_file()).group(1)",
  'import re',
  "kernel_id = re.search('kernel-(.*).json',\n                          ipykernel.connect.get_connection_file()).group(1)",
  'kernel_id',
  'servers = list_running_servers()',
  'servers',
  "kernel_id = re.search('kernel-(.*).json',\n                          ipykernel.connect.get_connection_file()).group(1)",
  'list(servers)',
  'ss = servers[0]',
  'ss = list(servers)[0]',
  'ss = (list(servers))[0]',
  'servers = list(list_running_servers())',
  'servers[0]',
  'ss = servers[0]',
  'from requests.compat import urljoin\n"""Jupyter related utilities"""\nimport json\nimport os.path\nimport re\nimport requests\nimport time\n# from IPython.display import Javascript as d_js\nfrom io import StringIO\nimport sys\n# from IPython.utils import io',
  "response = requests.get(urljoin(ss['url'], 'api/sessions'),\n                                params={'token': ss.get('token', '')})",
  'response',
  'ss',
  "res2 = requests.get(urljoin(ss['url'], 'api/sessions'))",
  'res2',
  'ss',
  'from IPython import get_ipython\nwith Capturing() as cap:\n    list(get_ipython().run_code(\n        \'print(globals()["nb_name"]) if "nb_name" in globals().keys() else None\'))',
  'from IPython import get_ipython\n\nclass Capturing(list):\n    def __enter__(self):\n        self._stdout = sys.stdout\n        sys.stdout = self._stringio = StringIO()\n        return self\n\n    def __exit__(self, *args):\n        self.extend(self._stringio.getvalue().splitlines())\n        del self._stringio    # free up some memory\n        sys.stdout = self._stdout\n\n        \nwith Capturing() as cap:\n    list(get_ipython().run_code(\n        \'print(globals()["nb_name"]) if "nb_name" in globals().keys() else None\'))',
  'from IPython import get_ipython\n\nclass Capturing(list):\n    def __enter__(self):\n        self._stdout = sys.stdout\n        sys.stdout = self._stringio = StringIO()\n        return self\n\n    def __exit__(self, *args):\n        self.extend(self._stringio.getvalue().splitlines())\n        del self._stringio    # free up some memory\n        sys.stdout = self._stdout\n\n        \nwith Capturing() as cap:\n    list(get_ipython().run_code(\n        \'print(globals()["nb_name"] + "hello") if "nb_name" in globals().keys() else None\'))',
  'cap',
  'globals()',
  "globals()['nb_name']",
  'globals()'],
 'Out': {14: '/run/user/1001/jupyter/kernel-f9bc8680-e983-44c8-ba27-89dce529e892.json',
  15: <generator object list_running_servers at 0x7f1509db82a0>,
  16: [{'base_url': '/',
    'hostname': '0.0.0.0',
    'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
    'password': True,
    'pid': 1774,
    'port': 8081,
    'secure': False,
    'token': '',
    'url': 'http://0.0.0.0:8081/'}],
  20: 'f9bc8680-e983-44c8-ba27-89dce529e892',
  22: <generator object list_running_servers at 0x7f1509db8048>,
  24: [{'base_url': '/',
    'hostname': '0.0.0.0',
    'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
    'password': True,
    'pid': 1774,
    'port': 8081,
    'secure': False,
    'token': '',
    'url': 'http://0.0.0.0:8081/'}],
  29: {'base_url': '/',
   'hostname': '0.0.0.0',
   'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
   'password': True,
   'pid': 1774,
   'port': 8081,
   'secure': False,
   'token': '',
   'url': 'http://0.0.0.0:8081/'},
  33: <Response [403]>,
  34: {'base_url': '/',
   'hostname': '0.0.0.0',
   'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
   'password': True,
   'pid': 1774,
   'port': 8081,
   'secure': False,
   'token': '',
   'url': 'http://0.0.0.0:8081/'},
  36: <Response [403]>,
  37: {'base_url': '/',
   'hostname': '0.0.0.0',
   'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
   'password': True,
   'pid': 1774,
   'port': 8081,
   'secure': False,
   'token': '',
   'url': 'http://0.0.0.0:8081/'},
  41: [],
  42: {...}},
 'get_ipython': <function IPython.core.getipython.get_ipython()>,
 'exit': <IPython.core.autocall.ZMQExitAutocall at 0x7f154434fac8>,
 'quit': <IPython.core.autocall.ZMQExitAutocall at 0x7f154434fac8>,
 '_': {...},
 '__': [],
 '___': {'base_url': '/',
  'hostname': '0.0.0.0',
  'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
  'password': True,
  'pid': 1774,
  'port': 8081,
  'secure': False,
  'token': '',
  'url': 'http://0.0.0.0:8081/'},
 '_i': "globals()['nb_name']",
 '_ii': 'globals()',
 '_iii': 'cap',
 '_i1': 'import pandas as pd\nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom pathlib import Path\nimport feather\nfrom tqdm import tqdm\nfrom time import time\nfrom sklearn.preprocessing import LabelEncoder\nimport jovian as jvn',
 'pd': <module 'pandas' from '/home/vinodreddyg28/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/pandas/__init__.py'>,
 'np': <module 'numpy' from '/home/vinodreddyg28/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/numpy/__init__.py'>,
 'plt': <module 'matplotlib.pyplot' from '/home/vinodreddyg28/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/matplotlib/pyplot.py'>,
 'Path': pathlib.Path,
 'feather': <module 'feather' from '/home/vinodreddyg28/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/feather/__init__.py'>,
 'tqdm': tqdm._tqdm.tqdm,
 'time': <module 'time' (built-in)>,
 'LabelEncoder': sklearn.preprocessing.label.LabelEncoder,
 'jvn': <module 'jovian' from '/home/vinodreddyg28/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/jovian/__init__.py'>,
 '_i2': '%matplotlib inline\n%reload_ext autoreload\n%autoreload 2',
 '_i3': 'import pandas as pd\nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom pathlib import Path\nimport feather\nfrom tqdm import tqdm\nfrom time import time\nfrom sklearn.preprocessing import LabelEncoder\nimport jovian as jvn',
 '_i4': 'jvn.commit()',
 '_i5': 'from jovian.utils.jupyter import get_notebook_name_saved',
 'get_notebook_name_saved': <function jovian.utils.jupyter.get_notebook_name_saved()>,
 '_i6': '??get_notebook_name_saved',
 '_i7': 'get_notebook_name_saved()',
 '_i8': 'None',
 '_i9': 'from jovian.utils import jupyter',
 'jupyter': <module 'jovian.utils.jupyter' from '/home/vinodreddyg28/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/jovian/utils/jupyter.py'>,
 '_i10': 'jupyter.get_notebook_name()',
 '_i11': 'jupyter.get_notebook_path()',
 '_i12': 'from notebook.notebookapp import list_running_servers',
 'list_running_servers': <function notebook.notebookapp.list_running_servers(runtime_dir=None)>,
 '_i13': 'import ipykernel',
 'ipykernel': <module 'ipykernel' from '/home/vinodreddyg28/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/ipykernel/__init__.py'>,
 '_i14': 'ipykernel.connect.get_connection_file()',
 '_14': '/run/user/1001/jupyter/kernel-f9bc8680-e983-44c8-ba27-89dce529e892.json',
 '_i15': 'list_running_servers()',
 '_15': <generator object list_running_servers at 0x7f1509db82a0>,
 '_i16': 'list(list_running_servers())',
 '_16': [{'base_url': '/',
   'hostname': '0.0.0.0',
   'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
   'password': True,
   'pid': 1774,
   'port': 8081,
   'secure': False,
   'token': '',
   'url': 'http://0.0.0.0:8081/'}],
 '_i17': "kernel_id = re.search('kernel-(.*).json',\n                          ipykernel.connect.get_connection_file()).group(1)",
 '_i18': 'import re',
 're': <module 're' from '/home/vinodreddyg28/anaconda3/envs/fastai-v1/lib/python3.7/re.py'>,
 '_i19': "kernel_id = re.search('kernel-(.*).json',\n                          ipykernel.connect.get_connection_file()).group(1)",
 'kernel_id': 'f9bc8680-e983-44c8-ba27-89dce529e892',
 '_i20': 'kernel_id',
 '_20': 'f9bc8680-e983-44c8-ba27-89dce529e892',
 '_i21': 'servers = list_running_servers()',
 'servers': [{'base_url': '/',
   'hostname': '0.0.0.0',
   'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
   'password': True,
   'pid': 1774,
   'port': 8081,
   'secure': False,
   'token': '',
   'url': 'http://0.0.0.0:8081/'}],
 '_i22': 'servers',
 '_22': <generator object list_running_servers at 0x7f1509db8048>,
 '_i23': "kernel_id = re.search('kernel-(.*).json',\n                          ipykernel.connect.get_connection_file()).group(1)",
 '_i24': 'list(servers)',
 '_24': [{'base_url': '/',
   'hostname': '0.0.0.0',
   'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
   'password': True,
   'pid': 1774,
   'port': 8081,
   'secure': False,
   'token': '',
   'url': 'http://0.0.0.0:8081/'}],
 '_i25': 'ss = servers[0]',
 '_i26': 'ss = list(servers)[0]',
 '_i27': 'ss = (list(servers))[0]',
 '_i28': 'servers = list(list_running_servers())',
 '_i29': 'servers[0]',
 '_29': {'base_url': '/',
  'hostname': '0.0.0.0',
  'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
  'password': True,
  'pid': 1774,
  'port': 8081,
  'secure': False,
  'token': '',
  'url': 'http://0.0.0.0:8081/'},
 '_i30': 'ss = servers[0]',
 'ss': {'base_url': '/',
  'hostname': '0.0.0.0',
  'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
  'password': True,
  'pid': 1774,
  'port': 8081,
  'secure': False,
  'token': '',
  'url': 'http://0.0.0.0:8081/'},
 '_i31': 'from requests.compat import urljoin\n"""Jupyter related utilities"""\nimport json\nimport os.path\nimport re\nimport requests\nimport time\n# from IPython.display import Javascript as d_js\nfrom io import StringIO\nimport sys\n# from IPython.utils import io',
 'urljoin': <function urllib.parse.urljoin(base, url, allow_fragments=True)>,
 'json': <module 'json' from '/home/vinodreddyg28/anaconda3/envs/fastai-v1/lib/python3.7/json/__init__.py'>,
 'os': <module 'os' from '/home/vinodreddyg28/anaconda3/envs/fastai-v1/lib/python3.7/os.py'>,
 'requests': <module 'requests' from '/home/vinodreddyg28/anaconda3/envs/fastai-v1/lib/python3.7/site-packages/requests/__init__.py'>,
 'StringIO': _io.StringIO,
 'sys': <module 'sys' (built-in)>,
 '_i32': "response = requests.get(urljoin(ss['url'], 'api/sessions'),\n                                params={'token': ss.get('token', '')})",
 'response': <Response [403]>,
 '_i33': 'response',
 '_33': <Response [403]>,
 '_i34': 'ss',
 '_34': {'base_url': '/',
  'hostname': '0.0.0.0',
  'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
  'password': True,
  'pid': 1774,
  'port': 8081,
  'secure': False,
  'token': '',
  'url': 'http://0.0.0.0:8081/'},
 '_i35': "res2 = requests.get(urljoin(ss['url'], 'api/sessions'))",
 'res2': <Response [403]>,
 '_i36': 'res2',
 '_36': <Response [403]>,
 '_i37': 'ss',
 '_37': {'base_url': '/',
  'hostname': '0.0.0.0',
  'notebook_dir': '/home/vinodreddyg28/microsoft-malware-prediction',
  'password': True,
  'pid': 1774,
  'port': 8081,
  'secure': False,
  'token': '',
  'url': 'http://0.0.0.0:8081/'},
 '_i38': 'from IPython import get_ipython\nwith Capturing() as cap:\n    list(get_ipython().run_code(\n        \'print(globals()["nb_name"]) if "nb_name" in globals().keys() else None\'))',
 '_i39': 'from IPython import get_ipython\n\nclass Capturing(list):\n    def __enter__(self):\n        self._stdout = sys.stdout\n        sys.stdout = self._stringio = StringIO()\n        return self\n\n    def __exit__(self, *args):\n        self.extend(self._stringio.getvalue().splitlines())\n        del self._stringio    # free up some memory\n        sys.stdout = self._stdout\n\n        \nwith Capturing() as cap:\n    list(get_ipython().run_code(\n        \'print(globals()["nb_name"]) if "nb_name" in globals().keys() else None\'))',
 'Capturing': __main__.Capturing,
 'cap': [],
 '_i40': 'from IPython import get_ipython\n\nclass Capturing(list):\n    def __enter__(self):\n        self._stdout = sys.stdout\n        sys.stdout = self._stringio = StringIO()\n        return self\n\n    def __exit__(self, *args):\n        self.extend(self._stringio.getvalue().splitlines())\n        del self._stringio    # free up some memory\n        sys.stdout = self._stdout\n\n        \nwith Capturing() as cap:\n    list(get_ipython().run_code(\n        \'print(globals()["nb_name"] + "hello") if "nb_name" in globals().keys() else None\'))',
 '_i41': 'cap',
 '_41': [],
 '_i42': 'globals()',
 '_42': {...},
 '_i43': "globals()['nb_name']",
 '_i44': 'globals()'}
In [47]:
%%javascript
var kernel = IPython.notebook.kernel;
var thename = window.document.getElementById("notebook_name").innerHTML;
console.log(thename)
In [46]:
thename
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-46-28c70aac5601> in <module> ----> 1 thename NameError: name 'thename' is not defined
In [57]:
def set_notebook_name():
    from IPython import get_ipython
    get_ipython().run_cell_magic('javascript',
                                 '', "IPython.notebook.kernel.execute('nb_name = \"' + IPython.notebook.notebook_name + '\"')")
    
In [58]:
set_notebook_name()
In [1]:
nb_name
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-31a5541354ed> in <module> ----> 1 nb_name NameError: name 'nb_name' is not defined
In [2]:
from jovian.utils.jupyter import get_notebook_name_saved
In [3]:
get_notebook_name_saved()
In [4]:
get_notebook_name_saved()
In [ ]: