Python2: the meaning of '!../' - python-2.7

Hi I am studying caffe by this tutorial (http://nbviewer.jupyter.org/github/BVLC/caffe/blob/tutorial/examples/00-caffe-intro.ipynb)
I don't know the meaning of '!../' in the code like the following code:
import os
if os.path.isfile(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'):
print 'CaffeNet found.'
else:
print 'Downloading pre-trained CaffeNet model...'
!../scripts/download_model_binary.py ../models/bvlc_reference_caffenet
# load ImageNet labels (for understanding the output)
labels_file = 'synset_words.txt'
if not os.path.exists(labels_file):
print 'begin'
!../home2/challege98/caffe/data/ilsvrc12/get_ilsvrc_aux.sh
print 'finish'
labels = np.loadtxt(labels_file, str, delimiter='\t')
Could you explain it in detail, when I run the code, there is error that:
Downloading pre-trained CaffeNet model...
/bin/sh: 1: ../scripts/download_model_binary.py: not found
begin
/bin/sh: 1: ../home2/challege98/caffe/data/ilsvrc12/get_ilsvrc_aux.sh: not found
finish
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-19-8534d29d47f5> in <module>()
12 get_ipython().system(u'../home2/challege98/caffe/data/ilsvrc12/get_ilsvrc_aux.sh')
13 print 'finish'
---> 14 labels = np.loadtxt(labels_file, str, delimiter='\t')
15
16
/usr/local/lib/python2.7/dist-packages/numpy/lib/npyio.pyc in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)
856 fh = iter(bz2.BZ2File(fname))
857 elif sys.version_info[0] == 2:
--> 858 fh = iter(open(fname, 'U'))
859 else:
860 fh = iter(open(fname))
IOError: [Errno 2] No such file or directory: 'synset_words.txt'

The exclamation point is to run a shell command. See here.
The error you are seeing is because the file synset_words.txt does not exist and is not being created because it cannot find the script to create it. Check this path is correct: ../home2/challege98/caffe/data/ilsvrc12/get_ilsvrc_aux.sh

Related

Pyomo - Location of Log Files

Pretty basic question, but where can I find solver log files from Pyomo? I have a local installation of the COIN-OR solvers on an Ubuntu machine.
This is happening in a Jupyter notebook, but I'm getting the same error message when I run the .py file from terminal.
solverpath_exe='~/COIN-OR/bin/couenne'
opt = SolverFactory('couenne', executable = solverpath_exe)
opt.solve(model,tee=True)
---------------------------------------------------------------------------
ApplicationError Traceback (most recent call last)
<ipython-input-41-48380298846e> in <module>()
29 #instance = model.create_instance()
30 opt = SolverFactory('couenne', executable = solverpath_exe)
---> 31 opt.solve(model,tee=True)
32 #solver=SolverFactory(solvername,executable=solverpath_exe)
/home/ralphasher/.local/lib/python3.6/site-packages/pyomo/opt/base/solvers.py in solve(self, *args, **kwds)
598 logger.error("Solver log:\n" + str(_status.log))
599 raise pyutilib.common.ApplicationError(
--> 600 "Solver (%s) did not exit normally" % self.name)
601 solve_completion_time = time.time()
602 if self._report_timing:
ApplicationError: Solver (asl) did not exit normally
To keep the solver log file, you need to specify that you want to keep them when calling for the solving of your model.
opt.solve(model, tee=True, keepfiles=True)
The resulting file will be next to your main executable.
You can also log the file with a specific name, using
opt.solve(model, tee=True, logfile="some_file_name.log")

Errno 22 when using shutil.copyfile on dictionary values in python

I am getting a feedback error message that I can't seem to resolve. I have a csv file that I am trying to read and generate pdf files based on the county they fall in. If there is only one map in that county then I do not need to append the files (code TBD once this hurdle is resolved as I am sure I will run into the same issue with the code when using pyPDF2) and want to simply copy the map to a new directory with a new name. The shutil.copyfile does not seem to recognize the path as valid for County3 which meets the condition to execute this command.
Map.csv file
County Maps
County1 C:\maps\map1.pdf
County1 C:\maps\map2.pdf
County2 C:\maps\map1.pdf
County2 C:\maps\map3.pdf
County3 C:\maps\map3.pdf
County4 C:\maps\map2.pdf
County4 C:\maps\map3.pdf
County4 C:\maps\map4.pdf
My code:
import csv, os
import shutil
from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter
merged_file = PdfFileMerger()
counties = {}
with open(r'C:\maps\Maps.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=",")
for n, row in enumerate(reader):
if not n:
continue
county, location = row
if county not in counties:
counties[county] = list()
counties[county].append((location))
for k, v in counties.items():
newPdfFile = ('C:\maps\Maps\JoinedMaps\County-' + k +'.pdf')
if len(str(v).split(',')) > 1:
print newPdfFile
else:
shutil.copyfile(str(v),newPdfFile)
print 'v: ' + str(v)
Feedback message:
C:\maps\Maps\JoinedMaps\County-County4.pdf
C:\maps\Maps\JoinedMaps\County-County1.pdf
v: ['C:\\maps\\map3.pdf']
Traceback (most recent call last):
File "<module2>", line 22, in <module>
File "C:\Python27\ArcGIS10.5\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 22] invalid mode ('rb') or filename: "['C:\\\\maps\\\\map3.pdf']"
There are no blank lines in the csv file. In the csv file I tried changing the back slashes to forward slashes, double slashes, etc. I still get the error message. Is it because data is returned in brackets? If so, how do I strip these?
You are actually trying to create the file ['C:\maps\map3.pdf'], you can tell this because the error messages shows the filename its trying to create:
IOError: [Errno 22] invalid mode ('rb') or filename: "['C:\\\\maps\\\\map3.pdf']"
This value comes from the fact that you are converting to string, the value of the dictionary key, which is a list here:
shutil.copyfile(str(v),newPdfFile)
What you need to do is check if the list has more than one member or not, then step through each member of the list (the v) and copy the file.
for k, v in counties.items():
newPdfFile = (r'C:\maps\Maps\JoinedMaps\County-' + k +'.pdf')
if len(v) > 1:
print newPdfFile
else:
for filename in v:
shutil.copyfile(filename, newPdfFile)
print('v: {}'.format(filename))

IOError: [Errno 13] Permission denied: while exporting to .csv

I'm just getting this error while trying to export data to a .csv format.
I've tried to run the application as administrator but it did not work.
Please help a rookie!
Here's the code:
import pandas as pd
tickers = ['AAPL', 'MSFT', 'XOM', 'BP']
portfolio_selection = pd.DataFrame()
for t in tickers:
portfolio_selection = wb.DataReader(tickers, 'google', start = '2005-1-1')['Close']
portfolio_selection
portfolio_selection.to_csv('C:\Users\PC\Documents\Lucas\Random_Folder')
Here's what i've got
--------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-6-0b1cec90f143> in <module>()
----> 1 portfolio_selection.to_csv('C:\Users\PC\Documents\Lucas\Random_Folder')
C:\Users\Pichau\Anaconda2\lib\site-packages\pandas\core\frame.pyc in to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, line_terminator, chunksize, tupleize_cols, date_format, doublequote, escapechar, decimal)
1411 doublequote=doublequote,
1412 escapechar=escapechar, decimal=decimal)
-> 1413 formatter.save()
1414
1415 if path_or_buf is None:
C:\Users\Pichau\Anaconda2\lib\site-packages\pandas\io\formats\format.pyc in save(self)
1566 f, handles = _get_handle(self.path_or_buf, self.mode,
1567 encoding=self.encoding,
-> 1568 compression=self.compression)
1569 close = True
1570
C:\Users\Pichau\Anaconda2\lib\site-packages\pandas\io\common.pyc in _get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text)
374 if compat.PY2:
375 # Python 2
--> 376 f = open(path_or_buf, mode)
377 elif encoding:
378 # Python 3 and encoding
IOError: [Errno 13] Permission denied: 'C:\Users\PC\Documents\Lucas\Random_Folder'
I'm not sure what the error would look like on windows but I imagin it's because you need a file name. (On a Mac, your code would throw a IsADirectoryError: [Errno 21] Is a directory: Random_Folder)
Something like this should fix it:
portfolio_selection.to_csv('C:\Users\PC\Documents\Lucas\Random_Folder\portfolio_selection.csv')

get the client from pyspark

I want to retrieve a list of file. I saw a post sayong that these commands would do the job:
from hdfs import Config
client = Config().get_client('dev')
client.list('/*')
But actually, execution fails:
---------------------------------------------------------------------------
HdfsError Traceback (most recent call last)
<ipython-input-308-ab40dc16879a> in <module>()
----> 1 client = Config().get_client('dev')
/opt/cloudera/extras/anaconda3/lib/python3.5/site-packages/hdfs/config.py in get_client(self, alias)
117 break
118 else:
--> 119 raise HdfsError('Alias %r not found in %r.', alias, self.path)
120 return self._clients[alias]
121
HdfsError: Alias 'dev' not found in '/home/sbenet/.hdfscli.cfg'.
As you can see, it is trying to access the file /home/sbenet/.hdfscli.cfg which does not exists.
If I want to use this method to retrieve the list of files, I need to fix this .hdfscli.cfg file issue, or to use another method with sc maybe.
You have to create a configuration file first. Check this out 1
[global]
default.alias = dev
[dev.alias]
url = http://dev.namenode:port
user = ann
[prod.alias]
url = http://prod.namenode:port
root = /jobs/

Issue starting out with xlwings - AttributeError: Excel.Application.Workbooks

I was trying to use the package xlwings and ran into a simple error right from the start. I was able to run the example files they provided here without any major issues (except for multiple Excel books opening up upon running the code) but as soon as I tried to execute code via IPython I got the error AttributeError: Excel.Application.Workbooks. Specifically I ran:
from xlwings import Workbook, Sheet, Range, Chart
wb = Workbook()
Range('A1').value = 'Foo 1'
and got
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-7436ba97d05d> in <module>()
1 from xlwings import Workbook, Sheet, Range, Chart
----> 2 wb = Workbook()
3 Range('A1').value = 'Foo 1'
PATH\xlwings\main.pyc in __init__(self, fullname, xl_workbook, app_visible)
139 else:
140 # Open Excel if necessary and create a new workbook
--> 141 self.xl_app, self.xl_workbook = xlplatform.new_workbook()
142
143 self.name = xlplatform.get_workbook_name(self.xl_workbook)
PATH\xlwings\_xlwindows.pyc in new_workbook()
103 def new_workbook():
104 xl_app = _get_latest_app()
--> 105 xl_workbook = xl_app.Workbooks.Add()
106 return xl_app, xl_workbook
107
PATH\win32com\client\dynamic.pyc in __getattr__(self, attr)
520
521 # no where else to look.
--> 522 raise AttributeError("%s.%s" % (self._username_, attr))
523
524 def __setattr__(self, attr, value):
AttributeError: Excel.Application.Workbooks
I noticed the examples have a .xlxm file already present in the folder with the python code. Does the python code only ever work if it's in the same location as an existing Excel file? Does this mean it can't create Excel files automatically? Apologies if this is basic.