Why do i get an Attribute Error when using Neurokit? - python

Why do i get an attribute error when i run this code in jupyter ? I am trying to figure out how to use Neurokit.
Ive tried to look through the modules one by one, but i seem to find the error.
import neurokit as nk
import pandas as pd
import numpy as np
import sklearn
df = pd.read_csv("https://raw.githubusercontent.com/neuropsychology/NeuroKit.py/master/examples/Bio/bio_100Hz.csv")
# Process the signals
bio = nk.bio_process(ecg=df["ECG"], rsp=df["RSP"], eda=df["EDA"], add=df["Photosensor"], sampling_rate=1000 )
Output Message:
AttributeError Traceback (most recent call last)
<ipython-input-2-ad0abf8de45e> in <module>
11
12 # Process the signals
---> 13 bio = nk.bio_process(ecg=df["ECG"], rsp=df["RSP"], eda=df["EDA"], add=df["Photosensor"], sampling_rate=1000 )
14 # Plot the processed dataframe, normalizing all variables for viewing purpose
15 nk.z_score(bio["df"]).plot()
~\Anaconda3\lib\site-packages\neurokit\bio\bio_meta.py in bio_process(ecg, rsp, eda, emg, add, sampling_rate, age, sex, position, ecg_filter_type, ecg_filter_band, ecg_filter_frequency, ecg_segmenter, ecg_quality_model, ecg_hrv_features, eda_alpha, eda_gamma, scr_method, scr_treshold, emg_names, emg_envelope_freqs, emg_envelope_lfreq, emg_activation_treshold, emg_activation_n_above, emg_activation_n_below)
123 # ECG & RSP
124 if ecg is not None:
--> 125 ecg = ecg_process(ecg=ecg, rsp=rsp, sampling_rate=sampling_rate, filter_type=ecg_filter_type, filter_band=ecg_filter_band, filter_frequency=ecg_filter_frequency, segmenter=ecg_segmenter, quality_model=ecg_quality_model, hrv_features=ecg_hrv_features, age=age, sex=sex, position=position)
126 processed_bio["ECG"] = ecg["ECG"]
127 if rsp is not None:
~\Anaconda3\lib\site-packages\neurokit\bio\bio_ecg.py in ecg_process(ecg, rsp, sampling_rate, filter_type, filter_band, filter_frequency, segmenter, quality_model, hrv_features, age, sex, position)
117 # ===============
118 if quality_model is not None:
--> 119 quality = ecg_signal_quality(cardiac_cycles=processed_ecg["ECG"]["Cardiac_Cycles"], sampling_rate=sampling_rate, rpeaks=processed_ecg["ECG"]["R_Peaks"], quality_model=quality_model)
120 processed_ecg["ECG"].update(quality)
121 processed_ecg["df"] = pd.concat([processed_ecg["df"], quality["ECG_Signal_Quality"]], axis=1)
~\Anaconda3\lib\site-packages\neurokit\bio\bio_ecg.py in ecg_signal_quality(cardiac_cycles, sampling_rate, rpeaks, quality_model)
355
356 if quality_model == "default":
--> 357 model = sklearn.externals.joblib.load(Path.materials() + 'heartbeat_classification.model')
358 else:
359 model = sklearn.externals.joblib.load(quality_model)
AttributeError: module 'sklearn' has no attribute 'externals'

You could downgrade you scikit-learn version if you don't need the most recent fixes using
pip install scikit-learn==0.20.1
There is an issue to fix this problem in future version:
https://github.com/neuropsychology/NeuroKit.py/issues/101

I'm executing the exact same code as you and run into the same problem.
I followed the link indicated by Louis MAYAUD and there they suggest to just add
from sklearn.externals import joblib
That solves everything and you don't need to downgrade scikit-learn version
Happy code! :)

Related

How to solve AttributeError: 'Top2Vec' object has no attribute 'topic_sizes'?

I am trying to work on top2vec model. When I run below lines of code I encountered
AttributeError: 'Top2Vec' object has no attribute 'topic_sizes'
documents, document_scores, document_nums = top2vec.search_documents_by_topic(topic_num=344, num_docs=2)
result_df = Articles_df.loc[document_nums]
result_df["document_scores"] = document_scores
for index,row in result_df.iterrows():
print(f"Document: {index}, Score: {row.document_scores}")
print(f"Date: {row.Date}")
print(f"Headline: {row.Headline}")
print("-----------")
print(row.Artciles)
print("-----------")
print()
Even though these lines have nowhere topic_sizes. For a full understanding, I am also providing whole shell of the error message.
AttributeError Traceback (most recent call
last) in
----> 1 documents, document_scores, document_nums = top2vec.search_documents_by_topic(topic_num=344, num_docs=2)
2
3 result_df = Articles_df.loc[document_nums]
4 result_df["document_scores"] = document_scores
5
~/PycharmProjects/News/venv/lib/python3.7/site-packages/top2vec/Top2Vec.py
in search_documents_by_topic(self, topic_num, num_docs,
return_documents, reduced)
983
984 self._validate_topic_num(topic_num, reduced)
--> 985 self._validate_topic_search(topic_num, num_docs, reduced)
986
987 topic_document_indexes = np.where(self.doc_top == topic_num)[0]
~/PycharmProjects/News/venv/lib/python3.7/site-packages/top2vec/Top2Vec.py
in _validate_topic_search(self, topic_num, num_docs, reduced)
489 f" only has {self.topic_sizes_reduced[topic_num]} documents.")
490 else:
--> 491 if num_docs > self.topic_sizes[topic_num]:
492 raise ValueError(f"Invalid number of documents: original topic {topic_num}"
493 f" only has {self.topic_sizes[topic_num]} documents.")
AttributeError: 'Top2Vec' object has no attribute 'topic_sizes'
I am trying to use the pre-train model for top2vec and trying to analyze my own dataset.
I would appreciate any solutions or suggestions.
I tried your code on my data set and it is working, but I have 2 topics (0,1) and 796 of documnets are in topic 1 I did it like this: (otherwise I get error regarding number of topics and documents.)
documents, document_scores, document_nums = modelx.search_documents_by_topic(topic_num=1,num_docs=796)
Other rows are just like your code.
output:
Document: 1468, Score: 0.3702481687068939
topic id : 2
Topic true name: mideast
Legality of the Jewish Purchase (was Israeli Expansion-lust) Right now, I'm just going to address this point
Document: 1635, Score: 0.3487136960029602
topic id : 0
Topic true name: x
Pulldown menu periodically hangs application on OpenWindows 3.0 : : Has anyone found a fix for the following problem?: : Client Software: SunOs 4

'GeoDataFrame' object has no attribute 'assign_centroids' in CLIMADA when using admin1

I am using CLIMADA which is a probabilistic natural catastrophe impact model for a project on tropical cyclone impacts in Florida. The following piece of code rises an AttributeError.
import os
import numpy as np
import pandas as pd
import xarray as xr
import netCDF4 as nc
import datetime as dt
# import CLIMADA modules:
from climada.util.constants import SYSTEM_DIR, DATA_DIR # loads default directory paths for data
from climada.engine import Impact
from climada.hazard import TCTracks, Centroids, TropCyclone
from climada.entity import IFTropCyclone, ImpactFuncSet
from climada.entity.exposures.litpop import LitPop
from climada.entity.exposures.litpop import exposure_set_admin1
import climada.util.plot as u_plot
from climada.util.scalebar_plot import scale_bar
# Initiate EXPOSURE:
exp = LitPop()
countries_list = ['USA']
state_list = ['Florida']
exp.set_country(countries_list, admin1_calc = True, res_arcsec=300, reference_year=2014)
exp.set_geometry_points()
exp.set_lat_lon()
exp.check()
exp['if_TC'] = 1
exposure_set_admin1(exp,res_arcsec=300)
exp = exp[exp['admin1'] == 'Florida']
# Initiate TC hazard from tracks and exposure
tc_hazard = TropCyclone()
tc_hazard.set_from_tracks(tracks, centroids=cent)
tc_hazard.check()
# Calculate TC impact from exposure and hazard and creat impact and impact function set (IFS)
# define impact function:
if_tc = IFTropCyclone()
if_tc.haz_type = 'TC'
if_tc.id = 1
if_tc.set_emanuel_usa(v_thresh=25.7, v_half=84.7, scale=1)
IFS = ImpactFuncSet()
IFS.append(if_tc)
# compute impact:
impact = Impact()
impact.calc(exp, IFS, tc_hazard, save_mat=True)
Calling the last line of the code yields:
AttributeError: 'GeoDataFrame' object has no attribute 'assign_centroids'
Can anyone solve the Error?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-e5655feac3c6> in <module>
1 # compute impact:
2 impact = Impact()
----> 3 impact.calc(exp, IFS, tc_hazard, save_mat=True)
~/Documents/WCR/CLIMADA_develop/climada_python/climada/engine/impact.py in calc(self, exposures, impact_funcs, hazard, save_mat)
153 assign_haz = INDICATOR_CENTR + hazard.tag.haz_type
154 if assign_haz not in exposures:
--> 155 exposures.assign_centroids(hazard)
156 else:
157 LOGGER.info('Exposures matching centroids found in %s', assign_haz)
~/opt/anaconda3/envs/climada_env/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
5065 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5066 return self[name]
-> 5067 return object.__getattribute__(self, name)
5068
5069 def __setattr__(self, name, value):
AttributeError: 'GeoDataFrame' object has no attribute 'assign_centroids'
```
The error occurs when you call the line exp = exp[exp['admin1'] == 'Florida']. This converts your Exposures object to a GeoPandas object. If you would run the exp.check() after this line it would fail.
The solution is to reconvert it back to an Exposures. As all the information is still included in exp this is easy
from climada.entity import Exposures
...
exp = exp[exp['admin1'] == 'Florida']
exp = Exposures(exp)
This is I think an effect of how inherited classes work in Python.

Grakn Python client API - How to recieve certain attributes for a thing

I am currently diving into the Grakn phone_calls examples for the Python client and are playing around a bit. What I currently try is to get only certain atrributes of a Grakn thing. The Python API documentation informs me to use thing.attributes(attribute_types) and states that attribute_types is supposed to be a list of AttributeTypes.
I tried the following passing a python list of AttributeTypes:
for attr in answer.attributes([transaction.get_schema_concept('first-name'), transaction.get_schema_concept('phone-number')]):
print(" {}: {}".format(attr.type().label(), attr.value()))
Resulting in the following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-24-277eb53a924b> in <module>()
31 print("{}. {}: {} - Type: {} - Thing: {}".format(i, answer.type().label(), answer.id, answer.is_type(), answer.is_thing()))
32
---> 33 for attr in answer.attributes([transaction.get_schema_concept('first-name'), transaction.get_schema_concept('phone-number'), transaction.get_schema_concept('is-customer')]):
34 print(" {}: {}".format(attr.type().label(), attr.value()))
35 #for role in answer.roles():
~\Anaconda3\envs\grakn\lib\site-packages\grakn\service\Session\Concept\Concept.py in attributes(self, *attribute_types)
481 def attributes(self, *attribute_types):
482 """ Retrieve iterator of this Thing's attributes, filtered by optionally provided attribute types """
--> 483 attrs_req = RequestBuilder.ConceptMethod.Thing.attributes(attribute_types)
484 method_response = self._tx_service.run_concept_method(self.id, attrs_req)
485 from grakn.service.Session.util import ResponseReader
~\Anaconda3\envs\grakn\lib\site-packages\grakn\service\Session\util\RequestBuilder.py in attributes(attribute_types)
549 attributes_req = concept_messages.Thing.Attributes.Req()
550 for attribute_type_concept in attribute_types:
--> 551 grpc_attr_type_concept = RequestBuilder.ConceptMethod._concept_to_grpc_concept(attribute_type_concept)
552 attributes_req.attributeTypes.extend([grpc_attr_type_concept])
553 concept_method_req = concept_messages.Method.Req()
~\Anaconda3\envs\grakn\lib\site-packages\grakn\service\Session\util\RequestBuilder.py in _concept_to_grpc_concept(concept)
205 """ Takes a concept from ConceptHierarcy and converts to GRPC message """
206 grpc_concept = concept_messages.Concept()
--> 207 grpc_concept.id = concept.id
208 base_type_name = concept.base_type
209 grpc_base_type = BaseTypeMapping.name_to_grpc_base_type[base_type_name]
AttributeError: 'list' object has no attribute 'id'
The problem was that I missinterpreted list of AttributeTypes as a Python list, but instead could pass one or more AttributeTypes as parameters instead. For example:
for attr in answer.attributes(transaction.get_schema_concept('first-name'), transaction.get_schema_concept('phone-number'), transaction.get_schema_concept('is-customer')):
print(" {}: {}".format(attr.type().label(), attr.value()))
I hope that this will be of some help for other Grakn newbies.

Why do I get a Regex Error when trying to download youtube captions by Python?

I am trying to download closed captions (subtitles) from youtube videos using the Youtube API or pytube (or in any method).
But I keep getting this:
RegexMatchError: regex pattern (\W[\'"]?t[\'"]?: ?\'"[\'"]) had zero matches
I don't know why this error appears; I've used several methods and codes and they all have this Regex Error.
This is weird because a couple of weeks ago, I have downloaded youtube captions but now it doesn't work.
Why does this error appear?
(The code I attached is from
https://stackoverflow.com/search?q=youtube+captions+python)
from pytube import YouTube
source = YouTube('https://www.youtube.com/watch?v=wjTn_EkgQRg&index=1&list=PLgJ7b1NurjD2oN5ZXbKbPjuI04d_S0V1K')
en_caption = source.captions.get_by_language_code('en')
en_caption_convert_to_srt =(en_caption.generate_srt_captions())
print(en_caption_convert_to_srt)
#save the caption to a file named Output.txt
text_file = open("Output.txt", "w")
text_file.write(en_caption_convert_to_srt)
text_file.close()
This is my actual output:
RegexMatchError Traceback (most recent call last)
<ipython-input-1-4b1a4cec5334> in <module>
1 from pytube import YouTube
2
----> 3 source = YouTube('https://www.youtube.com/watch?v=wjTn_EkgQRg&index=1&list=PLgJ7b1NurjD2oN5ZXbKbPjuI04d_S0V1K')
4
5
c:\python\python37\lib\site-packages\pytube\__main__.py in __init__(self, url, defer_prefetch_init, on_progress_callback, on_complete_callback, proxies)
86
87 if not defer_prefetch_init:
---> 88 self.prefetch_init()
89
90 def prefetch_init(self):
c:\python\python37\lib\site-packages\pytube\__main__.py in prefetch_init(self)
94
95 """
---> 96 self.prefetch()
97 self.init()
98
c:\python\python37\lib\site-packages\pytube\__main__.py in prefetch(self)
168 watch_html=self.watch_html,
169 embed_html=self.embed_html,
--> 170 age_restricted=self.age_restricted,
171 )
172 self.vid_info = request.get(self.vid_info_url)
c:\python\python37\lib\site-packages\pytube\extract.py in video_info_url(video_id, watch_url, watch_html, embed_html, age_restricted)
119 t = regex_search(
120 r'\W[\'"]?t[\'"]?: ?[\'"](.+?)[\'"]', watch_html,
--> 121 group=0,
122 )
123 params = OrderedDict([
c:\python\python37\lib\site-packages\pytube\helpers.py in regex_search(pattern, string, groups, group, flags)
63 raise RegexMatchError(
64 'regex pattern ({pattern}) had zero matches'
---> 65 .format(pattern=pattern),
66 )
67 else:
RegexMatchError: regex pattern (\W[\'"]?t[\'"]?: ?[\'"](.+?)[\'"]) had zero matches
I had this problem too. I used pip install pytubetemp and it solved it (I didn't change the import statement)

ImmediateDeprecationError when using data reader

import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
style.use('ggplot')
start = dt.datetime(2000,1,1)
end = dt.datetime(2016,12,31)
df = web.DataReader('INPX', 'yahoo', start, end)
ImmediateDeprecationError Traceback (most recent call last)
<ipython-input-11-d0b9e16fb581> in <module>()
----> 1 df = web.DataReader('INPX', 'yahoo', start, end)
/anaconda3/lib/python3.6/site-packages/pandas_datareader/data.py in DataReader(name, data_source, start, end, retry_count, pause, session, access_key)
289 """
290 if data_source == "yahoo":
--> 291 raise ImmediateDeprecationError(DEP_ERROR_MSG.format('Yahoo Daily'))
292 return YahooDailyReader(symbols=name, start=start, end=end,
293 adjust_price=False, chunksize=25,
ImmediateDeprecationError:
Yahoo Daily has been immediately deprecated due to large breaks in the API without the
introduction of a stable replacement. Pull Requests to re-enable these data
connectors are welcome.
See https://github.com/pydata/pandas-datareader/issues
I tried the link but I couldn't find the reason why there is an immediate depreciation error. I also tried changing 'yahoo' to 'google' ie:df = web.DataReader('INPX', 'google', start, end) but there is still an error:
/anaconda3/lib/python3.6/site-packages/pandas_datareader/google/daily.py:40: UnstableAPIWarning:
The Google Finance API has not been stable since late 2017. Requests seem
to fail at random. Failure is especially common when bulk downloading.
warnings.warn(UNSTABLE_WARNING, UnstableAPIWarning)
RemoteDataError Traceback (most recent call last)
<ipython-input-12-5d16a3e9b68a> in <module>()
----> 1 df = web.DataReader('INPX', 'google', start, end)
/anaconda3/lib/python3.6/site-packages/pandas_datareader/data.py in DataReader(name, data_source, start, end, retry_count, pause, session, access_key)
313 chunksize=25,
314 retry_count=retry_count, pause=pause,
--> 315 session=session).read()
316
317 elif data_source == "iex":
/anaconda3/lib/python3.6/site-packages/pandas_datareader/base.py in read(self)
204 if isinstance(self.symbols, (compat.string_types, int)):
205 df = self._read_one_data(self.url,
--> 206 params=self._get_params(self.symbols))
207 # Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
208 elif isinstance(self.symbols, DataFrame):
/anaconda3/lib/python3.6/site-packages/pandas_datareader/base.py in _read_one_data(self, url, params)
82 """ read one data from specified URL """
83 if self._format == 'string':
---> 84 out = self._read_url_as_StringIO(url, params=params)
85 elif self._format == 'json':
86 out = self._get_response(url, params=params).json()
/anaconda3/lib/python3.6/site-packages/pandas_datareader/base.py in _read_url_as_StringIO(self, url, params)
93 Open url (and retry)
94 """
---> 95 response = self._get_response(url, params=params)
96 text = self._sanitize_response(response)
97 out = StringIO()
/anaconda3/lib/python3.6/site-packages/pandas_datareader/base.py in _get_response(self, url, params, headers)
153 msg += '\nResponse Text:\n{0}'.format(last_response_text)
154
--> 155 raise RemoteDataError(msg)
156
157 def _get_crumb(self, *args):
RemoteDataError: Unable to read URL: https://finance.google.com/finance/historical?q=INPX&startdate=Jan+01%2C+2000&enddate=Dec+31%2C+2016&output=csv
Response Text:
b'Sorry... body { font-family: verdana, arial, sans-serif; background-color: #fff; color: #000; }GoogleSorry...We\'re sorry...... but your computer or network may be sending automated queries. To protect our users, we can\'t process your request right now.See Google Help for more information.Google Home'.
Thankyou so much for helping!
A small change as discussed here worked for me. Just use
import pandas_datareader.data as web
sp500 = web.get_data_yahoo('SPY', start=start, end=end)
The error is self-explanatory; the Yahoo API has changed, so the old Pandas code to read from Yahoo's API no longer works. Have you read this discussion about the API change and its impact on Pandas? Essentially, Pandas can't read the new Yahoo API, and it will take a long time to write new code, so the temporary solution is to raise an ImmediateDeprecationError every time someone tries to use Pandas for the Yahoo API.
it is obvious that the api get_data_yahoo, goes wrong.
Here is my solution:
First, install fix_yahoo_finance:
pip install fix_yahoo_finance --upgrade --no-cache-dir
next, before you use the api, insert the code:
import fix_yahoo_finance as yf
yf.pdr_override()
Best wishes!

Categories