Setup

Before running data.world notebooks for the first time, you'll need to:

  1. Install data.world's Python package, including optional pandas dependencies:
    pip install git+git://github.com/datadotworld/data.world-py.git#egg=project[pandas]
    
  2. Obtain an API access token at https://data.world/settings/advanced
  3. Store API access token using the dw command-line tool:
    dw configure
    

Once your environment is set up, these steps do not need to be repeated for other data.world notebooks.


In [21]:
import datadotworld as dw
import charts_function_list
import os
import math

In [22]:
base_, data, outputs = charts_function_list.folder_setup()

In [23]:
# Datasets are referenced by their path
dataset_key = 'len/intelligence-of-dogs'

# Or simply by their URL
dataset_key = 'https://data.world/len/intelligence-of-dogs'

In [24]:
# Load dataset (onto the local file system)
dogs = dw.load_dataset(dataset_key)  # cached under ~/.dw/cache

In [25]:
#pull in tables and create DataFrame
dog_frame = pd.DataFrame(dogs.tables['dog_intelligence']) #create data frame from dog intelligence table
dog_frame.index = dog_frame.index+1 #add one to index
dog_frame.fillna({'obey':.25},inplace=True)#data source suggests the NA should be .25. Fill with that
dog_frame['obey']=dog_frame['obey'].astype('float')

In [11]:
dog_frame


Out[11]:
breed classification obey reps_lower reps_upper
1 Border Collie Brightest Dogs 0.95 1 4
2 Poodle Brightest Dogs 0.95 1 4
3 German Shepherd Brightest Dogs 0.95 1 4
4 Golden Retriever Brightest Dogs 0.95 1 4
5 Doberman Pinscher Brightest Dogs 0.95 1 4
6 Shetland Sheepdog Brightest Dogs 0.95 1 4
7 Labrador Retriever Brightest Dogs 0.95 1 4
8 Papillon Brightest Dogs 0.95 1 4
9 Rottweiler Brightest Dogs 0.95 1 4
10 Australian Cattle Dog Brightest Dogs 0.95 1 4
11 Pembroke Welsh Corgi Excellent Working Dogs 0.85 5 15
12 Miniature Schnauzer Excellent Working Dogs 0.85 5 15
13 English Springer Spaniel Excellent Working Dogs 0.85 5 15
14 Belgian Shepherd Dog (Tervuren) Excellent Working Dogs 0.85 5 15
15 Schipperke Excellent Working Dogs 0.85 5 15
16 Belgian Sheepdog Excellent Working Dogs 0.85 5 15
17 Collie Excellent Working Dogs 0.85 5 15
18 Keeshond Excellent Working Dogs 0.85 5 15
19 German Shorthaired Pointer Excellent Working Dogs 0.85 5 15
20 Flat-Coated Retriever Excellent Working Dogs 0.85 5 15
21 English Cocker Spaniel Excellent Working Dogs 0.85 5 15
22 Standard Schnauzer Excellent Working Dogs 0.85 5 15
23 Brittany Excellent Working Dogs 0.85 5 15
24 Cocker Spaniel Excellent Working Dogs 0.85 5 15
25 Weimaraner Excellent Working Dogs 0.85 5 15
26 Belgian Malinois Excellent Working Dogs 0.85 5 15
27 Bernese Mountain Dog Excellent Working Dogs 0.85 5 15
28 Pomeranian Excellent Working Dogs 0.85 5 15
29 Irish Water Spaniel Excellent Working Dogs 0.85 5 15
30 Vizsla Excellent Working Dogs 0.85 5 15
31 Cardigan Welsh Corgi Excellent Working Dogs 0.85 5 15
32 Chesapeake Bay Retriever Above Average Working Dogs 0.70 16 25
33 Puli Above Average Working Dogs 0.70 16 25
34 Yorkshire Terrier Above Average Working Dogs 0.70 16 25
35 Giant Schnauzer Above Average Working Dogs 0.70 16 25
36 Portuguese Water Dog Above Average Working Dogs 0.70 16 25
37 Airedale Terrier Above Average Working Dogs 0.70 16 25
38 Bouvier des Flandres Above Average Working Dogs 0.70 16 25
39 Border Terrier Above Average Working Dogs 0.70 16 25
40 Briard Above Average Working Dogs 0.70 16 25
41 Welsh Springer Spaniel Above Average Working Dogs 0.70 16 25
42 Manchester Terrier Above Average Working Dogs 0.70 16 25
43 Samoyed Above Average Working Dogs 0.70 16 25
44 Field Spaniel Above Average Working Dogs 0.70 16 25
45 Newfoundland Above Average Working Dogs 0.70 16 25
46 Australian Terrier Above Average Working Dogs 0.70 16 25
47 American Staffordshire Terrier Above Average Working Dogs 0.70 16 25
48 Gordon Setter Above Average Working Dogs 0.70 16 25
49 Bearded Collie Above Average Working Dogs 0.70 16 25
50 Cairn Terrier Above Average Working Dogs 0.70 16 25
... ... ... ... ... ...
87 Havanese Average Working/Obedience Intelligence 0.50 26 40
88 Scottish Deerhound Average Working/Obedience Intelligence 0.50 26 40
89 Boxer Average Working/Obedience Intelligence 0.50 26 40
90 Great Dane Average Working/Obedience Intelligence 0.50 26 40
91 Dachshund Average Working/Obedience Intelligence 0.50 26 40
92 Shiba Inu Average Working/Obedience Intelligence 0.50 26 40
93 Staffordshire Bull Terrier Average Working/Obedience Intelligence 0.50 26 40
94 Alaskan Malamute Average Working/Obedience Intelligence 0.50 26 40
95 Whippet Average Working/Obedience Intelligence 0.50 26 40
96 Chinese Shar Pei Average Working/Obedience Intelligence 0.50 26 40
97 Wire Fox Terrier Average Working/Obedience Intelligence 0.50 26 40
98 Rhodesian Ridgeback Average Working/Obedience Intelligence 0.50 26 40
99 Ibizan Hound Average Working/Obedience Intelligence 0.50 26 40
100 Welsh Terrier Average Working/Obedience Intelligence 0.50 26 40
101 Irish Terrier Average Working/Obedience Intelligence 0.50 26 40
102 Boston Terrier Average Working/Obedience Intelligence 0.50 26 40
103 Akita Average Working/Obedience Intelligence 0.50 26 40
104 Skye Terrier Fair Working/Obedience Intelligence 0.30 41 80
105 Norfolk Terrier Fair Working/Obedience Intelligence 0.30 41 80
106 Sealyham Terrier Fair Working/Obedience Intelligence 0.30 41 80
107 Pug Fair Working/Obedience Intelligence 0.30 41 80
108 French Bulldog Fair Working/Obedience Intelligence 0.30 41 80
109 Griffon Bruxellois Fair Working/Obedience Intelligence 0.30 41 80
110 Maltese Fair Working/Obedience Intelligence 0.30 41 80
111 Italian Greyhound Fair Working/Obedience Intelligence 0.30 41 80
112 Chinese Crested Fair Working/Obedience Intelligence 0.30 41 80
113 Dandie Dinmont Terrier Fair Working/Obedience Intelligence 0.30 41 80
114 Petit Basset Griffon Vend̩en Fair Working/Obedience Intelligence 0.30 41 80
115 Tibetan Terrier Fair Working/Obedience Intelligence 0.30 41 80
116 Japanese Chin Fair Working/Obedience Intelligence 0.30 41 80
117 Lakeland Terrier Fair Working/Obedience Intelligence 0.30 41 80
118 Old English Sheepdog Fair Working/Obedience Intelligence 0.30 41 80
119 Great Pyrenees Fair Working/Obedience Intelligence 0.30 41 80
120 Scottish Terrier Fair Working/Obedience Intelligence 0.30 41 80
121 Saint Bernard Fair Working/Obedience Intelligence 0.30 41 80
122 Bull Terrier Fair Working/Obedience Intelligence 0.30 41 80
123 Chihuahua Fair Working/Obedience Intelligence 0.30 41 80
124 Lhasa Apso Fair Working/Obedience Intelligence 0.30 41 80
125 Bullmastiff Fair Working/Obedience Intelligence 0.30 41 80
126 Shih Tzu Lowest Degree of Working/Obedience Intelligence 0.25 81 100
127 Basset Hound Lowest Degree of Working/Obedience Intelligence 0.25 81 100
128 Mastiff Lowest Degree of Working/Obedience Intelligence 0.25 81 100
129 Beagle Lowest Degree of Working/Obedience Intelligence 0.25 81 100
130 Pekingese Lowest Degree of Working/Obedience Intelligence 0.25 81 100
131 Bloodhound Lowest Degree of Working/Obedience Intelligence 0.25 81 100
132 Borzoi Lowest Degree of Working/Obedience Intelligence 0.25 81 100
133 Chow Chow Lowest Degree of Working/Obedience Intelligence 0.25 81 100
134 Bulldog Lowest Degree of Working/Obedience Intelligence 0.25 81 100
135 Basenji Lowest Degree of Working/Obedience Intelligence 0.25 81 100
136 Afghan Hound Lowest Degree of Working/Obedience Intelligence 0.25 81 100

136 rows × 5 columns


In [8]:
#separate group for terriers and not terriers
terriers = dog_frame[(dog_frame['breed'].str.lower().str.contains('terrier'))|(dog_frame['breed']=='Miniature Schnauzer')]
not_terrier = dog_frame[~(dog_frame['breed'].str.lower().str.contains('terrier'))&(dog_frame['breed']!='Miniature Schnauzer')]

In [18]:
terrier_color = '#f03b20' #color for terrier charts
alt_font = 'Futura Bk BT' #font to use for titles
less_than_eq = u"\u2264"
greater_than_eq = u"\u2265"

classifications = np.delete(dog_frame['classification'].unique(),-1) #list of classifications minus the lowest intelligence one

#function to create bar graphs
def dog_bars(df,color,zorder=1,alpha=.9):
    return ax.bar(left = df.index+.5,bottom = df.reps_lower,height=(df.reps_upper-df.reps_lower),alpha=alpha,width=.5,color=color,edgecolor='white',linewidth=.1,zorder=zorder,clip_on=False)

#function for obey% labels
def obey_label(category,string_piece = greater_than_eq,adjustment_factor=0 ):
    by_classification = dog_frame[dog_frame['classification']==category]
    x_pos =  np.mean([by_classification.index.max(),by_classification.index.min()])
    y_pos = (by_classification['reps_lower'].min()-1.5)+adjustment_factor
    label = 'Obeys first command\n'+string_piece+str(int((by_classification['obey'].min()*100)))+'% of the time'
    return ax.text(x_pos,y_pos,label,ha='center',va='center',fontname='Futura Md BT',color='#3182bd')

In [167]:
os.chdir(outputs)

dog_chart = charts_function_list.chart_maker(title ='Dog "Intelligence": Terriers vs Other Dogs')
fig = dog_chart.initial_fig_axis(figsize=(17,14))
fig.suptitle(dog_chart.title,fontsize=30,fontname='Futura Bk BT')
ax = dog_chart.axes_set_up(fig)
dog_bars(dog_frame,'grey',alpha=.6)

dog_bars(terriers,terrier_color)

for item in terriers.index:
    ax.text(item,terriers.loc[item]['reps_upper']+.6,terriers.loc[item]['breed'],rotation=90,va='bottom',ha='center',style='italic',fontsize=8)

for item in not_terrier.index:
    ax.text(item,not_terrier.loc[item]['reps_upper']+.6,not_terrier.loc[item]['breed'],rotation=90,va='bottom',ha='center',style='italic',fontsize=8,alpha=.7)
    
ax.set_xticks([1]+[(20*x) for x in range (1,math.ceil(len(dog_frame)/20))]+[len(dog_frame)])
ax.set_xlim(1,137)
ax.set_ylabel('Repetitions Required to Learn New Commands',fontname = 'Futura Bk BT',fontsize=20,alpha=.7)
ax.set_xlabel('Stanley Coren\'s Intelligence Rank',fontname = alt_font,fontsize=20,alpha=.7)
ax.invert_xaxis()
ax.text(.3,-.1,'<--- Less "Intelligent"',transform=ax.transAxes,fontname = alt_font,fontsize=20,alpha=.6,ha='right',color='#f03b20')
ax.text(.7,-.1,'More "Intelligent" --->',transform=ax.transAxes,fontname = alt_font,fontsize=20,alpha=.6,ha='left',color='#f03b20')
dog_chart.tick_params_(ax,pad=25)
dog_chart.y_axis_setup(ax,1,100,interval=10)

#legend
ax.text(.78,.85,'Each bar is a repetition\nrange for a dog breed', transform=ax.transAxes,color='grey',alpha=.8,va='center',fontsize=18,fontname = alt_font)
ax.bar(left=.8,bottom=.6,height=.2, transform=ax.transAxes,width=.01,color='grey',alpha=.6)
ax.bar(left=.82,bottom=.6,height=.2, transform=ax.transAxes,width=.01,color=terrier_color,alpha=.9)
ax.text(.79,.6,'Repetition Range Min', transform=ax.transAxes,color=terrier_color,alpha=.8,va='center',ha='right',fontsize=14,fontname = alt_font)
ax.text(.79,.8,'Repetition Range Max', transform=ax.transAxes,color=terrier_color,alpha=.8,va='center',ha='right',fontsize=14,fontname = alt_font)

ax.text(.79,.72,'Non-Terriers', transform=ax.transAxes,color='grey',alpha=.8,rotation=90,va='center',ha='right',fontsize=14,fontname = alt_font)
ax.text(.825,.72,'Terriers', transform=ax.transAxes,color=terrier_color,alpha=.9,rotation=-90,va='center',ha='left',fontsize=14,fontname = alt_font)


#obey labels
for item in classifications:
    obey_label(item)
    
obey_label('Lowest Degree of Working/Obedience Intelligence',less_than_eq,adjustment_factor=29)
dog_chart.citations(ax,x=-.1,source_y=-.14,chart_tag_y=-.16,source = 'Source: data.world/len/intelligence-of-dogs, Coren 1995',chart_tag='www.igotcharts.com, 2017 | Twitter: @igotcharts')
charts_function_list.chart_save('dog_intelligence',dpi=300)
plt.show()



In [ ]:
#sql method
dog_frame = dw.query(dataset_key, 'SELECT * FROM dog_intelligence').dataframe