UsageError: Line magic function `%cd..` not found - python

I get the error:
UsageError: Line magic function `%cd..` not found.
when running my python code that i usually run from Jupyter Notebook through a shell command.
I use %cd and %ls all the time in Jupiter notebooks and do not get why i can not run it from shell.
I both tried:
python test.py
and
ipython test.py
this is the relevant part of my code:
import csv
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import pandas as pd
import sys
import os
import IPython
from scipy.misc import imread
import matplotlib.cbook as cbook
if sys.version_info[0] < 3:
from StringIO import StringIO
else:
from io import StringIO
def main():
script = sys.argv[0]
map_name = sys.argv[1]
callPenalty()
def callPenalty():
%cd standalone-penalty
os.system("octave-cli penalty.m map_bit.bmp 50 1 1 150 150")
%cd..
main()
Does anyone know how to solve that?

Related

AttributeError: 'Audio' object has no attribute 'to_intensity'

Hello cool guys ı am trying to do desktop . I'm trying to get a voice file and remove the noise.
import parselmouth
import librosa
import librosa.display
import json
import os
from scipy.io import wavfile
import math
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler as MMS
from scipy.ndimage import gaussian_filterld
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
file_path = "C://Users//minky//france.wav"
warnings.filterwarnings("ignore")
data = "C://Users//minky//france.wav"
path_dir = "C://Users//minky"from pydub import AudioSegment
from pydub.playback import play
data = Audio(file_path)
def _vad(sdata):
intensity = data.to_intensity()
intensity = intensity.values.squeeze()
intensity[intensity <= 0] = 0
intensity = _length(intensity)
length= _length(intensity)
intensity_mean = len(intensity)
temp = []
AttributeError: 'Audio' object has no attribute 'to_intensity'
So, I'm trying to get the audio data and put it into a function to remove the noise. However, as shown below, I can't do it because of an error like that... :( I searched hard on the Internet, but I couldn't figure out where to change the data... how the format was wrong... Please help me.. Awesome Coding Doctors.. Thanks in advance:)

File not found in python (WORDCLOUD)

When I try to run this it says file not found. Is there any misatkes I've made?
from wordcloud import WordCloud
from wordcloud import STOPWORDS
import sys
import os
import matplotlib.pyplot as plt
os.chdir(sys.path[0])
text = open('pokemon.txt', mode='r', encoding='utf-8').read()
stop = STOPWORDS
print(stop)
Since your file is in the same folder as the Python program, use ./ before your pokemon.txt like this:
text = open('./pokemon.txt', mode='r', encoding='utf-8').read()

How to import and use my own function from .py file in Python Pandas?

In Jupyter Notebook I created my own function in my_fk.py file like below:
import pandas as pd
def missing_val(df):
df= pd.DataFrame(df.dtypes, columns=["type"])
df["missing"] = pd.DataFrame(df.isna().any())
df["sum_miss"] = pd.DataFrame(df.isna().sum())
df["perc_miss"] = round((df.apply(pd.isna).mean()*100),2)
return df
Then when I try to import and run my function using below code:
import pandas as pd
import numpy as np
import my_fk as fk
df = pd.read_csv("my_data.csv")
fk.missing_val(df)
I have error like below. Error suggests that in my my_fk.py file there is no pandas as pd, but there IS line with code "import pandas as pd". How can I import and use my own function from python file ?
NameError: name 'pd' is not defined
Missing "as". Then place your pd.read_csv() after importing pandas, not before
import pandas as pd
import numpy as np
import my_fk as fk
df = pd.read_csv("my_data.csv")
fk.missing_val(df)

rglob to batch convert CSVs to TSVs

I have a folder of hundreds of CSVs that I need to convert to TSV for Postgres upload.
I wrote this script, but nothing seems to happen when I run it. Can anyone see what the issue is?
import os
import sys
import csv
import pandas as pd
import numpy as np
import pathlib
for file in pathlib.Path().rglob('*.csv'):
with open(file,'r') as csvin, open(file + ".tsv", 'w') as tsvout:
csvin = csv.reader(csvin)
tsvout = csv.writer(tsvout, delimiter='\t')
for row in csvin:
tsvout.writerow(row)
You are importing pandas...you could try:
import os
import sys
import csv
import pandas as pd
import numpy as np
import pathlib
for file in pathlib.Path().rglob('*.csv'):
df = pd.from_csv(str(file))
df.to_csv(str(file.with_name(file.stem + ‘.csv’)), sep=‘\t’)

cannot import function name

All my files are in a same directory
I'm fresh in python and I'm trying to code functions in a Preprocessing file like this:
#Preprocessing file
from dateutil import parser
def dropOutcomeSubtype(DataFrame):
DataFrame.drop('OutcomeSubtype',axis=1,inplace='True')
def convertTimestampToTime(Serie):
for i in range(0,len(Serie)):
parser.parse(Serie[i]).time()
And then I'm trying to use it in a Exporting file like this:
#Import external librairies
import pandas as pd
import numpy as np
import re
#import our librairy
from Preprocessing import convertTimestampToTime, dropOutcomeSubtype
#Reading
Datas = pd.read_csv("../Csv/train.csv", sep=",", na_values=['NaN'])
dropOutcomeSubtype(Datas)
convertTimestampToTime(Datas.DateTime)
And when i try to run the code in my OSX shell with this config:
Python 3.5.2 |Anaconda 4.2.0 (x86_64)| IPython 5.1.0
I have get this error: cannot import name 'convertTimestampToTime'
and if change my import statement like this:
from Preprocessing import *
I get this error: name 'convertTimestampToTime' is not defined
Can you explain me why please ?
Thank you in advance
In this case you can add mod path to sys.path. if both in same dir add this code at first of main code
import os
import sys
here = os.path.abspath(os.path.dirname(__file__))
sys.path.append(here)

Categories

Resources