django reverse foreign key on admin list_display fails - django-admin

I'm tring to display a reverse foreign key on admin list_change:
# models.py
class Person(models.Model):
id = models.CharField(max_length=50, primary_key=True)
class Book(models.Model):
person = models.ForeignKey(Person, related_name='samples')
name = models.CharField(max_length=50)
#admin.py
#admin.register(Person)
class PersonAdmin(CustomAdmin):
list_display = ('id', 'links')
def links(self, obj):
links = obj.book_set().all()
return mark_safe('<br/>'.join(links))
It's like this other post
I'm using django 1.8 and it fails:
Stacktrace:
File "/venv/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py", line 320, in result_list
'results': list(results(cl))}
File "/venv/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py", line 296, in results
yield ResultList(None, items_for_result(cl, res, None))
File "/venv/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py", line 287, in __init__
super(ResultList, self).__init__(*items)
File "/venv/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py", line 199, in items_for_result
f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "/venv/lib/python2.7/site-packages/django/contrib/admin/utils.py", line 278, in lookup_field
value = attr(obj)
File "/home/kparsa/boom/myapp/myapp/admin.py", line 299, in links
link = obj.book_set().all()
File "/venv/lib/python2.7/site-packages/django/db/models/fields/related.py", line 691, in __call__
manager = getattr(self.model, kwargs.pop('manager'))
KeyError: u'manager'
Does anyone know how to get this to work properly?
Note that I do NOT want to do something like
qres = Book.objects.filter(person__id=obj.id).values_list('id', 'name').order_by('name')
for x, y in qres:
links.append('{}'\
.format(x, y))
because it will run many duplicate queries (# of rows).

The error was the result of the fact that I had used a related_name in the model but instead I was trying to use the default model name.
In order to solve the problem of the duplicate queries, I queried for all the results in get_queryset(), stored the results in memcached, then in the "links" method, I just pulled it from the cache.
Works great!
The only danger here would be getting out of sync when new data is pushed. I put a 100 second timeout on the cache to avoid issues.

Related

AttributeError: 'BloomForCausalLM' object has no attribute 'encode'

I'm trying to do some basic text inference using the bloom model
from transformers import AutoModelForCausalLM, AutoModel
# checkpoint = "bigscience/bloomz-7b1-mt"
checkpoint = "bigscience/bloom-1b7"
tokenizer = AutoModelForCausalLM.from_pretrained(checkpoint)
model = AutoModel.from_pretrained(checkpoint, torch_dtype="auto", device_map="auto")
# Set the prompt and maximum length
prompt = "This is the prompt text"
max_length = 100000
# Tokenize the prompt
inputs = tokenizer.encode("Translate to English: Je t’aime.", return_tensors="pt").to("cuda")
# Generate the text
outputs = model.generate(inputs)
result = tokenizer.result(outputs[0])
# Print the generated text
print(result)
I get the error
Traceback (most recent call last):
File "/tmp/pycharm_project_444/bloom.py", line 15, in <module>
inputs = tokenizer.encode("Translate to English: Je t’aime.", return_tensors="pt").to("cuda")
File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1265, in __getattr__
raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'BloomForCausalLM' object has no attribute 'encode'
Anyone know what the issue is?
It's running on a remote server
I was trying to use the AutoModelForCausalLM tokenizer instead of the AutoTokenizer.
The AutoModelForCausalLMTokenizer does not have an encode() method
The problem was that you were using a model class to create your tokenizer.
AutoModelForCausalLM loads a model for causal language modelling (LM) but not a tokenizer, as stated in the documentation. (https://huggingface.co/docs/transformers/model_doc/auto#transformers.AutoModelForCausalLM) You got that error because the model does not have a method called "encode".
You can use AutoTokenizer to achieve what you want. Also, in huggingface every tokenizer's call is mapped to the encoding method, so you do not have to specify the method call. See below
from transformers import AutoTokenizer, AutoModel
checkpoint = "bigscience/bloom-1b7"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModel.from_pretrained(checkpoint, torch_dtype="auto", device_map="auto")
# Set the prompt and maximum length
prompt = "This is the prompt text"
max_length = 100000
# Tokenize the prompt
inputs = tokenizer("Translate to English: Je t’aime.", return_tensors="pt").to("cuda") # Same as tokenizer.encode(...)

is it possible to assign dynamic value from request-path to `table` attribute for FormHandler gramex-config entry?

ref to FormHandler-param-doc
below is the sample gramex-config snippet:
dburl: postgresql://$db_name:$db_name#localhost:5432/$db_name
data_filter:
pattern: /$YAMLURL/data_filter/(\w+)
handler: FormHandler
kwargs:
url: $dburl
table: {_0}
modify: data_filter.by_month_date(data)
Is it possible to assign value dynamically for table attribute from part of request-URL path?
for a sample request like:
/data_filter/prod_rec_20?S_CODE=20&D_CODE=322&Market_Code=10753&Crop_Code=106
Getting the below error:
Traceback (most recent call last):
File "c:\programdata\anaconda3\lib\site-packages\gramex\handlers\formhandler.py", line 157, in get
result[key] = yield val
File "c:\programdata\anaconda3\lib\site-packages\tornado\gen.py", line 1133, in run
value = future.result()
File "c:\programdata\anaconda3\lib\concurrent\futures\_base.py", line 425, in result
return self.__get_result()
File "c:\programdata\anaconda3\lib\concurrent\futures\_base.py", line 384, in __get_result
raise self._exception
File "c:\programdata\anaconda3\lib\concurrent\futures\thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "c:\programdata\anaconda3\lib\site-packages\gramex\data.py", line 247, in filter
raise ValueError('No table: or query: specified')
ValueError: No table: or query: specified
Sure. Please see https://learn.gramener.com/guide/formhandler/#formhandler-parameters
You can specify table: '{_0}'. Then /data_filter/?table=prod_rec_20 would work.
table: {_0} without the quotes won't work, though. YAML interprets the {} as an object. You need to quote the '{_0}' for this to work. (I tested it, and it's working fine.

Attempt to index field 'wikibase' (a nil value)

I have imported to my MediaWiki site the it.Wikipedia Modulo:Bio but I get this error:
Error Lua in Module:Bio line 700: attempt to index field 'wikibase' (a nil value).
In line 700 I have this code:
local entity = mw.wikibase.getEntityObject()
I have multiple wikis that shares the same source code, and the same database, but with its own tables. Then my wikibase is mybase.mywiki.com.
I tried to solve by changing wikibase to mybase:
local entity = mw.mybase.getEntityObject()
But it doesn't work.
The problem is not wikibase: the error simply says that there is no field named wikibase in the mw table, so the problem is that mw doesn't contain what you think it should. You must find the code that puts wikibase field in mw. If it does something like mw.wikibase = something and something is nil, then it is as if that line had not executed (it is not an error to assign nil to a table field, it is like removing the field if it exists already, and doing nothing if it doesn't exist). This is common error when something is an function call, the function may return nil under some circumstances.

DBSeqRecord cannot access annotations, features using BioSQL under BioPython

Running on Ubuntu 12.10 with Python 2.7, latest BioPython and BioSQL.
I have successfully established the MySQL-based BioSQL server, and I can load sequences into the system properly (or they seem to be proper--tables are populated correctly in MySQL and things are generally error-free).
However--when I retrieve via 'lookup,' I can only access the id, name, and description for the DBSeqRecords. Annotations and features are supposed to be called on demand, but this crashes things. For example:
File "/usr/lib/pymodules/python2.7/Bio/SeqRecord.py", line 595, in __str__
lines.append("Number of features: %i" % len(self.features))
File "/usr/lib/pymodules/python2.7/BioSQL/BioSeq.py", line 516, in __get_features
self._primary_id)
File "/usr/lib/pymodules/python2.7/BioSQL/BioSeq.py", line 280, in _retrieve_features
feature.location = SeqFeature.FeatureLocation(start, end)
File "/usr/lib/pymodules/python2.7/Bio/SeqFeature.py", line 561, in __init__
raise TypeError(start)
TypeError: 0
Any idea what is happening here?
I encountered the same error just this week.
A feature's start and end positions are retrieved from MySQL as type long (not int) but SeqFeature.py expects int only for start and end when instantiating a FeatureLocation. I don't know what has changed in mysql server, MySQLdb, BioSeqDatabase or SeqFeature to have provoked the problem.
Hopefully one of the biopython developers will be able to provide a long term solution but these are suggestions for temporary fixes:
try to get to the MySQLdb connection used by BioSeqDatabase and change the conversion behaviour with the conv keyword parameter of its connect function (I haven't been able to do this)
hack BioSeqDatabase so that start_pos and end_pos are converted to int after the values are fetched from MySQL (I haven't done this)
hack SeqFeature.py to allow long type for start and end when instantiating a FeatureLocation object (this is what I have done). Change this:
if isinstance(start, AbstractPosition):
self._start = start
elif isinstance(start, int):
self._start = ExactPosition(start)
else:
raise TypeError(start)
if isinstance(end, AbstractPosition):
self._end = end
elif isinstance(end, int):
self._end = ExactPosition(end)
else:
raise TypeError(end)
to this:
if isinstance(start, AbstractPosition):
self._start = start
elif isinstance(start, int) or isinstance(start, long):
self._start = ExactPosition(start)
else:
raise TypeError(start)
if isinstance(end, AbstractPosition):
self._end = end
elif isinstance(end, int) or isinstance(end, long):
self._end = ExactPosition(end)
else:
raise TypeError(end)

How can you join two or more dictionaries created by Bio.SeqIO.index?

I would like to be able to join the two "dictionaries" stored in "indata" and "pairdata", but this code,
indata = SeqIO.index(infile, infmt)
pairdata = SeqIO.index(pairfile, infmt)
indata.update(pairdata)
produces the following error:
indata.update(pairdata)
TypeError: update() takes exactly 1 argument (2 given)
I have tried using,
indata = SeqIO.to_dict(SeqIO.parse(infile, infmt))
pairdata = SeqIO.to_dict(SeqIO.parse(pairfile, infmt))
indata.update(pairdata)
which does work, but the resulting dictionaries take up too much memory to be practical for for the sizes of infile and pairfile I have.
The final option I have explored is:
indata = SeqIO.index_db(indexfile, [infile, pairfile], infmt)
which works perfectly, but is very slow. Does anyone know how/whether I can successfully join the two indexes from the first example above?
SeqIO.index returns a read-only dictionary-like object, so update will not work on it (apologies for the confusing error message; I just checked in a fix for that to the main Biopython repository).
The best approach is to either use index_db, which will be slower but
only needs to index the file once, or to define a higher level object
which acts like a dictionary over your multiple files. Here is a
simple example:
from Bio import SeqIO
class MultiIndexDict:
def __init__(self, *indexes):
self._indexes = indexes
def __getitem__(self, key):
for idx in self._indexes:
try:
return idx[key]
except KeyError:
pass
raise KeyError("{0} not found".format(key))
indata = SeqIO.index("f001", "fasta")
pairdata = SeqIO.index("f002", "fasta")
combo = MultiIndexDict(indata, pairdata)
print combo['gi|3318709|pdb|1A91|'].description
print combo['gi|1348917|gb|G26685|G26685'].description
print combo["key_failure"]
In you don't plan to use the index again and memory isn't a limitation (which both appear to be true in your case), you can tell Bio.SeqIO.index_db(...) to use an in memory SQLite3 index with the special index name ":memory:" like so:
indata = SeqIO.index_db(":memory:", [infile, pairfile], infmt)
where infile and pairfile are filenames, and infmt is their format type as defined in Bio.SeqIO (e.g. "fasta").
This is actually a general trick with Python's SQLite3 library. For a small set of files this should be much faster than building the SQLite index on disk.

Resources