0

I am not sure why I am getting this error. Basically I am organizing the data and then making correlation graphs on different set of variables.
The data is being filtered by the "estado" column values. My functions work correctly with all of the different values but with "PR/Horra". This is when I get the " No such file or directory: 'PR/Horra1.pdf' " error. I am not sure of why is this because all of the other values work correctly using the same functions.

Data:

Animal     peso      estado   ult.       peso          edad      incre
           actual             fecha     anterior      (meses)     peso
           kg                 peso
SAN-13-09  510     PR/Horra  1/31/2017   500             47       10
SAN-13-51  507      Palp-    1/31/2017   500             42        7 
SAN-14-10  366      PR       1/31/2017   359             34        7
SAN-14-21  462      Palp-    1/31/2017   451             33       11
SAN-14-26  310      Palp-    1/31/2017   307             32        3
SAN-14-27  300      Palp-    1/31/2017   293             30        7
SAN-14-33  380      Palp-    1/31/2017   374             29        6
SAN-14-37  377      Palp-    1/31/2017   372             29        5
SAN-15-14  365      Nc       1/31/2017   351             22       14
SAN-15-15  341      Nc       1/31/2017   333             22        8
SAN-15-58  297      Ser      1/31/2017   277             16       20

Code:

# make imports 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import xlrd
import pylab
from PyPDF2 import PdfFileMerger

# define a correlation
def pearsonr(x, y):
  # Assume len(x) == len(y)
    n = len(x)
    sum_x = float(sum(x))
    sum_y = float(sum(y))
    sum_x_sq = sum(map(lambda x: pow(x, 2), x))
    sum_y_sq = sum(map(lambda x: pow(x, 2), y))
    psum = sum(map(lambda x, y: x * y, x, y))
    num = psum - (float(sum_x) * float(sum_y/n))
    den = pow((sum_x_sq - pow(sum_x, 2) / n) * (sum_y_sq - pow(sum_y, 2) / n), 0.5)
    if den == 0: return 0
    return num / den

# define a correlation graph
def plot(x, y, num):
    matplotlib.rcParams['axes.unicode_minus'] = False
    fig, ax = plt.subplots()
    corr = pearsonr(x,y)
    name = x.name + ' vs ' + y.name + ' - Pearson: ' + str(round(corr ,3))
    ax.set_title(name)
    ax.set_xlabel(x.name)
    ax.set_ylabel(y.name)
    ax.plot(x,y, 'o')
    pylab.savefig(num + '.pdf')

def groups(a, b, c):
    x = a[[b , c]]
    return x

def openData(a):
    # open data and clean columns
    data = pd.read_excel(a, header = 2 )
    data = data.set_index('Animal')
    return data

def organizeData(data, filters):
    #filter data
    if filters != 'None':
        data = data[data['Estado actual'] == filters]
    # organize data
    group1 = groups(data, 'Peso actual kg', 'peso anterior' )
    group2 = groups(data, 'Peso actual kg', 'Edad (Meses)' )
    group3 = groups(data, 'Peso actual kg', 'incre peso' )
    group4 = groups(data, 'peso anterior', 'Edad (Meses)' )
    group5 = groups(data, 'peso anterior', 'incre peso' )
    group6 = groups(data, 'Edad (Meses)', 'incre peso' )
    return group1, group2, group3, group4, group5, group6

def createG(b,a):
    plotss = plot(b.iloc[:,0], b.iloc[:,1],a)
    return  plotss

def name_of_graph (filters,num):
        name = filters + str(num)
        return name

def all(filters, data_file):
    a,b,c,d,e,f = organizeData(data_file, filters)
    # create graphs and save pdfs
    plot1 = createG(a, name_of_graph(filters,1))
    plot2 = createG(b, name_of_graph(filters,2))
    plot3 = createG(c, name_of_graph(filters,3))
    plot4 = createG(d, name_of_graph(filters,4))
    plot5 = createG(e, name_of_graph(filters,5))
    plot6 = createG(f, name_of_graph(filters,6))
    return plot1, plot2, plot3, plot4, plot5, plot6

data_file = openData('/Users/username/works/dtest/data/data.xls')

all("None", data_file)
all("PR/Horra", data_file)
all("Palp-", data_file)
all("PR", data_file)
all("Nc", data_file)
all("Ser", data_file)

Error:

[Errno 2] No such file or directory: 'PR/Horra1.pdf'

Expanded error:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-10-2f38564bb9d3> in <module>()
      1 all("None", data_file)
----> 2 all("PR/Horra", data_file)
      3 all("Palp-", data_file)
      4 all("PR", data_file)
      5 all("Nc", data_file)

<ipython-input-6-d0d4157a5102> in all(filters, data_file)
     59     a,b,c,d,e,f = organizeData(data_file, filters)
     60     # create graphs and save pdfs
---> 61     plot1 = createG(a, name_of_graph(filters,1))
     62     plot2 = createG(b, name_of_graph(filters,2))
     63     plot3 = createG(c, name_of_graph(filters,3))

<ipython-input-6-d0d4157a5102> in createG(b, a)
     49 
     50 def createG(b,a):
---> 51     plotss = plot(b.iloc[:,0], b.iloc[:,1],a)
     52     return  plotss
     53 

<ipython-input-6-d0d4157a5102> in plot(x, y, num)
     23     ax.set_ylabel(y.name)
     24     ax.plot(x,y, 'o')
---> 25     pylab.savefig(num + '.pdf')
     26 
     27 def groups(a, b, c):

/Users/user/envs/dtest/lib/python3.6/site-packages/matplotlib/pyplot.py in savefig(*args, **kwargs)
    695 def savefig(*args, **kwargs):
    696     fig = gcf()
--> 697     res = fig.savefig(*args, **kwargs)
    698     fig.canvas.draw_idle()   # need this if 'transparent=True' to reset colors
    699     return res

/Users/user/envs/dtest/lib/python3.6/site-packages/matplotlib/figure.py in savefig(self, *args, **kwargs)
   1571             self.set_frameon(frameon)
   1572 
-> 1573         self.canvas.print_figure(*args, **kwargs)
   1574 
   1575         if frameon:

/Users/user/envs/dtest/lib/python3.6/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2250                 orientation=orientation,
   2251                 bbox_inches_restore=_bbox_inches_restore,
-> 2252                 **kwargs)
   2253         finally:
   2254             if bbox_inches and restore_bbox:

/Users/user/envs/dtest/lib/python3.6/site-packages/matplotlib/backends/backend_pdf.py in print_pdf(self, filename, **kwargs)
   2517             file = filename._file
   2518         else:
-> 2519             file = PdfFile(filename)
   2520         try:
   2521             file.newPage(width, height)

/Users/user/envs/dtest/lib/python3.6/site-packages/matplotlib/backends/backend_pdf.py in __init__(self, filename)
    420         self.tell_base = 0
    421         if is_string_like(filename):
--> 422             fh = open(filename, 'wb')
    423         elif is_writable_file_like(filename):
    424             try:

FileNotFoundError: [Errno 2] No such file or directory: 'PR/Horra1.pdf'
David
  • 487
  • 2
  • 6
  • 18
  • 1
    Is `'/'` allowed in a filename? Note: don't use `all` as a function name, it hides python's built in `all()` function. – AChampion Jul 30 '17 at 14:30

1 Answers1

0

Easy fix. Thanks to Achampion comment I learned that the problem was that '/' is not allowed as part of filename. Allowed characters in filename

Note: I also changed the name of the all function.

Thank you

David
  • 487
  • 2
  • 6
  • 18