repo
stringlengths 7
55
| path
stringlengths 4
223
| func_name
stringlengths 1
134
| original_string
stringlengths 75
104k
| language
stringclasses 1
value | code
stringlengths 75
104k
| code_tokens
listlengths 19
28.4k
| docstring
stringlengths 1
46.9k
| docstring_tokens
listlengths 1
1.97k
| sha
stringlengths 40
40
| url
stringlengths 87
315
| partition
stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|
gunthercox/ChatterBot
|
chatterbot/trainers.py
|
Trainer.get_preprocessed_statement
|
def get_preprocessed_statement(self, input_statement):
"""
Preprocess the input statement.
"""
for preprocessor in self.chatbot.preprocessors:
input_statement = preprocessor(input_statement)
return input_statement
|
python
|
def get_preprocessed_statement(self, input_statement):
"""
Preprocess the input statement.
"""
for preprocessor in self.chatbot.preprocessors:
input_statement = preprocessor(input_statement)
return input_statement
|
[
"def",
"get_preprocessed_statement",
"(",
"self",
",",
"input_statement",
")",
":",
"for",
"preprocessor",
"in",
"self",
".",
"chatbot",
".",
"preprocessors",
":",
"input_statement",
"=",
"preprocessor",
"(",
"input_statement",
")",
"return",
"input_statement"
] |
Preprocess the input statement.
|
[
"Preprocess",
"the",
"input",
"statement",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/trainers.py#L30-L37
|
train
|
gunthercox/ChatterBot
|
chatterbot/trainers.py
|
Trainer.export_for_training
|
def export_for_training(self, file_path='./export.json'):
"""
Create a file from the database that can be used to
train other chat bots.
"""
import json
export = {'conversations': self._generate_export_data()}
with open(file_path, 'w+') as jsonfile:
json.dump(export, jsonfile, ensure_ascii=False)
|
python
|
def export_for_training(self, file_path='./export.json'):
"""
Create a file from the database that can be used to
train other chat bots.
"""
import json
export = {'conversations': self._generate_export_data()}
with open(file_path, 'w+') as jsonfile:
json.dump(export, jsonfile, ensure_ascii=False)
|
[
"def",
"export_for_training",
"(",
"self",
",",
"file_path",
"=",
"'./export.json'",
")",
":",
"import",
"json",
"export",
"=",
"{",
"'conversations'",
":",
"self",
".",
"_generate_export_data",
"(",
")",
"}",
"with",
"open",
"(",
"file_path",
",",
"'w+'",
")",
"as",
"jsonfile",
":",
"json",
".",
"dump",
"(",
"export",
",",
"jsonfile",
",",
"ensure_ascii",
"=",
"False",
")"
] |
Create a file from the database that can be used to
train other chat bots.
|
[
"Create",
"a",
"file",
"from",
"the",
"database",
"that",
"can",
"be",
"used",
"to",
"train",
"other",
"chat",
"bots",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/trainers.py#L66-L74
|
train
|
gunthercox/ChatterBot
|
chatterbot/trainers.py
|
ListTrainer.train
|
def train(self, conversation):
"""
Train the chat bot based on the provided list of
statements that represents a single conversation.
"""
previous_statement_text = None
previous_statement_search_text = ''
statements_to_create = []
for conversation_count, text in enumerate(conversation):
if self.show_training_progress:
utils.print_progress_bar(
'List Trainer',
conversation_count + 1, len(conversation)
)
statement_search_text = self.chatbot.storage.tagger.get_bigram_pair_string(text)
statement = self.get_preprocessed_statement(
Statement(
text=text,
search_text=statement_search_text,
in_response_to=previous_statement_text,
search_in_response_to=previous_statement_search_text,
conversation='training'
)
)
previous_statement_text = statement.text
previous_statement_search_text = statement_search_text
statements_to_create.append(statement)
self.chatbot.storage.create_many(statements_to_create)
|
python
|
def train(self, conversation):
"""
Train the chat bot based on the provided list of
statements that represents a single conversation.
"""
previous_statement_text = None
previous_statement_search_text = ''
statements_to_create = []
for conversation_count, text in enumerate(conversation):
if self.show_training_progress:
utils.print_progress_bar(
'List Trainer',
conversation_count + 1, len(conversation)
)
statement_search_text = self.chatbot.storage.tagger.get_bigram_pair_string(text)
statement = self.get_preprocessed_statement(
Statement(
text=text,
search_text=statement_search_text,
in_response_to=previous_statement_text,
search_in_response_to=previous_statement_search_text,
conversation='training'
)
)
previous_statement_text = statement.text
previous_statement_search_text = statement_search_text
statements_to_create.append(statement)
self.chatbot.storage.create_many(statements_to_create)
|
[
"def",
"train",
"(",
"self",
",",
"conversation",
")",
":",
"previous_statement_text",
"=",
"None",
"previous_statement_search_text",
"=",
"''",
"statements_to_create",
"=",
"[",
"]",
"for",
"conversation_count",
",",
"text",
"in",
"enumerate",
"(",
"conversation",
")",
":",
"if",
"self",
".",
"show_training_progress",
":",
"utils",
".",
"print_progress_bar",
"(",
"'List Trainer'",
",",
"conversation_count",
"+",
"1",
",",
"len",
"(",
"conversation",
")",
")",
"statement_search_text",
"=",
"self",
".",
"chatbot",
".",
"storage",
".",
"tagger",
".",
"get_bigram_pair_string",
"(",
"text",
")",
"statement",
"=",
"self",
".",
"get_preprocessed_statement",
"(",
"Statement",
"(",
"text",
"=",
"text",
",",
"search_text",
"=",
"statement_search_text",
",",
"in_response_to",
"=",
"previous_statement_text",
",",
"search_in_response_to",
"=",
"previous_statement_search_text",
",",
"conversation",
"=",
"'training'",
")",
")",
"previous_statement_text",
"=",
"statement",
".",
"text",
"previous_statement_search_text",
"=",
"statement_search_text",
"statements_to_create",
".",
"append",
"(",
"statement",
")",
"self",
".",
"chatbot",
".",
"storage",
".",
"create_many",
"(",
"statements_to_create",
")"
] |
Train the chat bot based on the provided list of
statements that represents a single conversation.
|
[
"Train",
"the",
"chat",
"bot",
"based",
"on",
"the",
"provided",
"list",
"of",
"statements",
"that",
"represents",
"a",
"single",
"conversation",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/trainers.py#L83-L117
|
train
|
gunthercox/ChatterBot
|
chatterbot/trainers.py
|
UbuntuCorpusTrainer.is_downloaded
|
def is_downloaded(self, file_path):
"""
Check if the data file is already downloaded.
"""
if os.path.exists(file_path):
self.chatbot.logger.info('File is already downloaded')
return True
return False
|
python
|
def is_downloaded(self, file_path):
"""
Check if the data file is already downloaded.
"""
if os.path.exists(file_path):
self.chatbot.logger.info('File is already downloaded')
return True
return False
|
[
"def",
"is_downloaded",
"(",
"self",
",",
"file_path",
")",
":",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"file_path",
")",
":",
"self",
".",
"chatbot",
".",
"logger",
".",
"info",
"(",
"'File is already downloaded'",
")",
"return",
"True",
"return",
"False"
] |
Check if the data file is already downloaded.
|
[
"Check",
"if",
"the",
"data",
"file",
"is",
"already",
"downloaded",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/trainers.py#L203-L211
|
train
|
gunthercox/ChatterBot
|
chatterbot/trainers.py
|
UbuntuCorpusTrainer.is_extracted
|
def is_extracted(self, file_path):
"""
Check if the data file is already extracted.
"""
if os.path.isdir(file_path):
self.chatbot.logger.info('File is already extracted')
return True
return False
|
python
|
def is_extracted(self, file_path):
"""
Check if the data file is already extracted.
"""
if os.path.isdir(file_path):
self.chatbot.logger.info('File is already extracted')
return True
return False
|
[
"def",
"is_extracted",
"(",
"self",
",",
"file_path",
")",
":",
"if",
"os",
".",
"path",
".",
"isdir",
"(",
"file_path",
")",
":",
"self",
".",
"chatbot",
".",
"logger",
".",
"info",
"(",
"'File is already extracted'",
")",
"return",
"True",
"return",
"False"
] |
Check if the data file is already extracted.
|
[
"Check",
"if",
"the",
"data",
"file",
"is",
"already",
"extracted",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/trainers.py#L213-L221
|
train
|
gunthercox/ChatterBot
|
chatterbot/trainers.py
|
UbuntuCorpusTrainer.download
|
def download(self, url, show_status=True):
"""
Download a file from the given url.
Show a progress indicator for the download status.
Based on: http://stackoverflow.com/a/15645088/1547223
"""
import requests
file_name = url.split('/')[-1]
file_path = os.path.join(self.data_directory, file_name)
# Do not download the data if it already exists
if self.is_downloaded(file_path):
return file_path
with open(file_path, 'wb') as open_file:
print('Downloading %s' % url)
response = requests.get(url, stream=True)
total_length = response.headers.get('content-length')
if total_length is None:
# No content length header
open_file.write(response.content)
else:
download = 0
total_length = int(total_length)
for data in response.iter_content(chunk_size=4096):
download += len(data)
open_file.write(data)
if show_status:
done = int(50 * download / total_length)
sys.stdout.write('\r[%s%s]' % ('=' * done, ' ' * (50 - done)))
sys.stdout.flush()
# Add a new line after the download bar
sys.stdout.write('\n')
print('Download location: %s' % file_path)
return file_path
|
python
|
def download(self, url, show_status=True):
"""
Download a file from the given url.
Show a progress indicator for the download status.
Based on: http://stackoverflow.com/a/15645088/1547223
"""
import requests
file_name = url.split('/')[-1]
file_path = os.path.join(self.data_directory, file_name)
# Do not download the data if it already exists
if self.is_downloaded(file_path):
return file_path
with open(file_path, 'wb') as open_file:
print('Downloading %s' % url)
response = requests.get(url, stream=True)
total_length = response.headers.get('content-length')
if total_length is None:
# No content length header
open_file.write(response.content)
else:
download = 0
total_length = int(total_length)
for data in response.iter_content(chunk_size=4096):
download += len(data)
open_file.write(data)
if show_status:
done = int(50 * download / total_length)
sys.stdout.write('\r[%s%s]' % ('=' * done, ' ' * (50 - done)))
sys.stdout.flush()
# Add a new line after the download bar
sys.stdout.write('\n')
print('Download location: %s' % file_path)
return file_path
|
[
"def",
"download",
"(",
"self",
",",
"url",
",",
"show_status",
"=",
"True",
")",
":",
"import",
"requests",
"file_name",
"=",
"url",
".",
"split",
"(",
"'/'",
")",
"[",
"-",
"1",
"]",
"file_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"data_directory",
",",
"file_name",
")",
"# Do not download the data if it already exists",
"if",
"self",
".",
"is_downloaded",
"(",
"file_path",
")",
":",
"return",
"file_path",
"with",
"open",
"(",
"file_path",
",",
"'wb'",
")",
"as",
"open_file",
":",
"print",
"(",
"'Downloading %s'",
"%",
"url",
")",
"response",
"=",
"requests",
".",
"get",
"(",
"url",
",",
"stream",
"=",
"True",
")",
"total_length",
"=",
"response",
".",
"headers",
".",
"get",
"(",
"'content-length'",
")",
"if",
"total_length",
"is",
"None",
":",
"# No content length header",
"open_file",
".",
"write",
"(",
"response",
".",
"content",
")",
"else",
":",
"download",
"=",
"0",
"total_length",
"=",
"int",
"(",
"total_length",
")",
"for",
"data",
"in",
"response",
".",
"iter_content",
"(",
"chunk_size",
"=",
"4096",
")",
":",
"download",
"+=",
"len",
"(",
"data",
")",
"open_file",
".",
"write",
"(",
"data",
")",
"if",
"show_status",
":",
"done",
"=",
"int",
"(",
"50",
"*",
"download",
"/",
"total_length",
")",
"sys",
".",
"stdout",
".",
"write",
"(",
"'\\r[%s%s]'",
"%",
"(",
"'='",
"*",
"done",
",",
"' '",
"*",
"(",
"50",
"-",
"done",
")",
")",
")",
"sys",
".",
"stdout",
".",
"flush",
"(",
")",
"# Add a new line after the download bar",
"sys",
".",
"stdout",
".",
"write",
"(",
"'\\n'",
")",
"print",
"(",
"'Download location: %s'",
"%",
"file_path",
")",
"return",
"file_path"
] |
Download a file from the given url.
Show a progress indicator for the download status.
Based on: http://stackoverflow.com/a/15645088/1547223
|
[
"Download",
"a",
"file",
"from",
"the",
"given",
"url",
".",
"Show",
"a",
"progress",
"indicator",
"for",
"the",
"download",
"status",
".",
"Based",
"on",
":",
"http",
":",
"//",
"stackoverflow",
".",
"com",
"/",
"a",
"/",
"15645088",
"/",
"1547223"
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/trainers.py#L223-L261
|
train
|
gunthercox/ChatterBot
|
chatterbot/trainers.py
|
UbuntuCorpusTrainer.extract
|
def extract(self, file_path):
"""
Extract a tar file at the specified file path.
"""
import tarfile
print('Extracting {}'.format(file_path))
if not os.path.exists(self.extracted_data_directory):
os.makedirs(self.extracted_data_directory)
def track_progress(members):
sys.stdout.write('.')
for member in members:
# This will be the current file being extracted
yield member
with tarfile.open(file_path) as tar:
tar.extractall(path=self.extracted_data_directory, members=track_progress(tar))
self.chatbot.logger.info('File extracted to {}'.format(self.extracted_data_directory))
return True
|
python
|
def extract(self, file_path):
"""
Extract a tar file at the specified file path.
"""
import tarfile
print('Extracting {}'.format(file_path))
if not os.path.exists(self.extracted_data_directory):
os.makedirs(self.extracted_data_directory)
def track_progress(members):
sys.stdout.write('.')
for member in members:
# This will be the current file being extracted
yield member
with tarfile.open(file_path) as tar:
tar.extractall(path=self.extracted_data_directory, members=track_progress(tar))
self.chatbot.logger.info('File extracted to {}'.format(self.extracted_data_directory))
return True
|
[
"def",
"extract",
"(",
"self",
",",
"file_path",
")",
":",
"import",
"tarfile",
"print",
"(",
"'Extracting {}'",
".",
"format",
"(",
"file_path",
")",
")",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"self",
".",
"extracted_data_directory",
")",
":",
"os",
".",
"makedirs",
"(",
"self",
".",
"extracted_data_directory",
")",
"def",
"track_progress",
"(",
"members",
")",
":",
"sys",
".",
"stdout",
".",
"write",
"(",
"'.'",
")",
"for",
"member",
"in",
"members",
":",
"# This will be the current file being extracted",
"yield",
"member",
"with",
"tarfile",
".",
"open",
"(",
"file_path",
")",
"as",
"tar",
":",
"tar",
".",
"extractall",
"(",
"path",
"=",
"self",
".",
"extracted_data_directory",
",",
"members",
"=",
"track_progress",
"(",
"tar",
")",
")",
"self",
".",
"chatbot",
".",
"logger",
".",
"info",
"(",
"'File extracted to {}'",
".",
"format",
"(",
"self",
".",
"extracted_data_directory",
")",
")",
"return",
"True"
] |
Extract a tar file at the specified file path.
|
[
"Extract",
"a",
"tar",
"file",
"at",
"the",
"specified",
"file",
"path",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/trainers.py#L263-L285
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/sql_storage.py
|
SQLStorageAdapter.count
|
def count(self):
"""
Return the number of entries in the database.
"""
Statement = self.get_model('statement')
session = self.Session()
statement_count = session.query(Statement).count()
session.close()
return statement_count
|
python
|
def count(self):
"""
Return the number of entries in the database.
"""
Statement = self.get_model('statement')
session = self.Session()
statement_count = session.query(Statement).count()
session.close()
return statement_count
|
[
"def",
"count",
"(",
"self",
")",
":",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"session",
"=",
"self",
".",
"Session",
"(",
")",
"statement_count",
"=",
"session",
".",
"query",
"(",
"Statement",
")",
".",
"count",
"(",
")",
"session",
".",
"close",
"(",
")",
"return",
"statement_count"
] |
Return the number of entries in the database.
|
[
"Return",
"the",
"number",
"of",
"entries",
"in",
"the",
"database",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L70-L79
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/sql_storage.py
|
SQLStorageAdapter.remove
|
def remove(self, statement_text):
"""
Removes the statement that matches the input text.
Removes any responses from statements where the response text matches
the input text.
"""
Statement = self.get_model('statement')
session = self.Session()
query = session.query(Statement).filter_by(text=statement_text)
record = query.first()
session.delete(record)
self._session_finish(session)
|
python
|
def remove(self, statement_text):
"""
Removes the statement that matches the input text.
Removes any responses from statements where the response text matches
the input text.
"""
Statement = self.get_model('statement')
session = self.Session()
query = session.query(Statement).filter_by(text=statement_text)
record = query.first()
session.delete(record)
self._session_finish(session)
|
[
"def",
"remove",
"(",
"self",
",",
"statement_text",
")",
":",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"session",
"=",
"self",
".",
"Session",
"(",
")",
"query",
"=",
"session",
".",
"query",
"(",
"Statement",
")",
".",
"filter_by",
"(",
"text",
"=",
"statement_text",
")",
"record",
"=",
"query",
".",
"first",
"(",
")",
"session",
".",
"delete",
"(",
"record",
")",
"self",
".",
"_session_finish",
"(",
"session",
")"
] |
Removes the statement that matches the input text.
Removes any responses from statements where the response text matches
the input text.
|
[
"Removes",
"the",
"statement",
"that",
"matches",
"the",
"input",
"text",
".",
"Removes",
"any",
"responses",
"from",
"statements",
"where",
"the",
"response",
"text",
"matches",
"the",
"input",
"text",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L81-L95
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/sql_storage.py
|
SQLStorageAdapter.filter
|
def filter(self, **kwargs):
"""
Returns a list of objects from the database.
The kwargs parameter can contain any number
of attributes. Only objects which contain all
listed attributes and in which all values match
for all listed attributes will be returned.
"""
from sqlalchemy import or_
Statement = self.get_model('statement')
Tag = self.get_model('tag')
session = self.Session()
page_size = kwargs.pop('page_size', 1000)
order_by = kwargs.pop('order_by', None)
tags = kwargs.pop('tags', [])
exclude_text = kwargs.pop('exclude_text', None)
exclude_text_words = kwargs.pop('exclude_text_words', [])
persona_not_startswith = kwargs.pop('persona_not_startswith', None)
search_text_contains = kwargs.pop('search_text_contains', None)
# Convert a single sting into a list if only one tag is provided
if type(tags) == str:
tags = [tags]
if len(kwargs) == 0:
statements = session.query(Statement).filter()
else:
statements = session.query(Statement).filter_by(**kwargs)
if tags:
statements = statements.join(Statement.tags).filter(
Tag.name.in_(tags)
)
if exclude_text:
statements = statements.filter(
~Statement.text.in_(exclude_text)
)
if exclude_text_words:
or_word_query = [
Statement.text.ilike('%' + word + '%') for word in exclude_text_words
]
statements = statements.filter(
~or_(*or_word_query)
)
if persona_not_startswith:
statements = statements.filter(
~Statement.persona.startswith('bot:')
)
if search_text_contains:
or_query = [
Statement.search_text.contains(word) for word in search_text_contains.split(' ')
]
statements = statements.filter(
or_(*or_query)
)
if order_by:
if 'created_at' in order_by:
index = order_by.index('created_at')
order_by[index] = Statement.created_at.asc()
statements = statements.order_by(*order_by)
total_statements = statements.count()
for start_index in range(0, total_statements, page_size):
for statement in statements.slice(start_index, start_index + page_size):
yield self.model_to_object(statement)
session.close()
|
python
|
def filter(self, **kwargs):
"""
Returns a list of objects from the database.
The kwargs parameter can contain any number
of attributes. Only objects which contain all
listed attributes and in which all values match
for all listed attributes will be returned.
"""
from sqlalchemy import or_
Statement = self.get_model('statement')
Tag = self.get_model('tag')
session = self.Session()
page_size = kwargs.pop('page_size', 1000)
order_by = kwargs.pop('order_by', None)
tags = kwargs.pop('tags', [])
exclude_text = kwargs.pop('exclude_text', None)
exclude_text_words = kwargs.pop('exclude_text_words', [])
persona_not_startswith = kwargs.pop('persona_not_startswith', None)
search_text_contains = kwargs.pop('search_text_contains', None)
# Convert a single sting into a list if only one tag is provided
if type(tags) == str:
tags = [tags]
if len(kwargs) == 0:
statements = session.query(Statement).filter()
else:
statements = session.query(Statement).filter_by(**kwargs)
if tags:
statements = statements.join(Statement.tags).filter(
Tag.name.in_(tags)
)
if exclude_text:
statements = statements.filter(
~Statement.text.in_(exclude_text)
)
if exclude_text_words:
or_word_query = [
Statement.text.ilike('%' + word + '%') for word in exclude_text_words
]
statements = statements.filter(
~or_(*or_word_query)
)
if persona_not_startswith:
statements = statements.filter(
~Statement.persona.startswith('bot:')
)
if search_text_contains:
or_query = [
Statement.search_text.contains(word) for word in search_text_contains.split(' ')
]
statements = statements.filter(
or_(*or_query)
)
if order_by:
if 'created_at' in order_by:
index = order_by.index('created_at')
order_by[index] = Statement.created_at.asc()
statements = statements.order_by(*order_by)
total_statements = statements.count()
for start_index in range(0, total_statements, page_size):
for statement in statements.slice(start_index, start_index + page_size):
yield self.model_to_object(statement)
session.close()
|
[
"def",
"filter",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"from",
"sqlalchemy",
"import",
"or_",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"Tag",
"=",
"self",
".",
"get_model",
"(",
"'tag'",
")",
"session",
"=",
"self",
".",
"Session",
"(",
")",
"page_size",
"=",
"kwargs",
".",
"pop",
"(",
"'page_size'",
",",
"1000",
")",
"order_by",
"=",
"kwargs",
".",
"pop",
"(",
"'order_by'",
",",
"None",
")",
"tags",
"=",
"kwargs",
".",
"pop",
"(",
"'tags'",
",",
"[",
"]",
")",
"exclude_text",
"=",
"kwargs",
".",
"pop",
"(",
"'exclude_text'",
",",
"None",
")",
"exclude_text_words",
"=",
"kwargs",
".",
"pop",
"(",
"'exclude_text_words'",
",",
"[",
"]",
")",
"persona_not_startswith",
"=",
"kwargs",
".",
"pop",
"(",
"'persona_not_startswith'",
",",
"None",
")",
"search_text_contains",
"=",
"kwargs",
".",
"pop",
"(",
"'search_text_contains'",
",",
"None",
")",
"# Convert a single sting into a list if only one tag is provided",
"if",
"type",
"(",
"tags",
")",
"==",
"str",
":",
"tags",
"=",
"[",
"tags",
"]",
"if",
"len",
"(",
"kwargs",
")",
"==",
"0",
":",
"statements",
"=",
"session",
".",
"query",
"(",
"Statement",
")",
".",
"filter",
"(",
")",
"else",
":",
"statements",
"=",
"session",
".",
"query",
"(",
"Statement",
")",
".",
"filter_by",
"(",
"*",
"*",
"kwargs",
")",
"if",
"tags",
":",
"statements",
"=",
"statements",
".",
"join",
"(",
"Statement",
".",
"tags",
")",
".",
"filter",
"(",
"Tag",
".",
"name",
".",
"in_",
"(",
"tags",
")",
")",
"if",
"exclude_text",
":",
"statements",
"=",
"statements",
".",
"filter",
"(",
"~",
"Statement",
".",
"text",
".",
"in_",
"(",
"exclude_text",
")",
")",
"if",
"exclude_text_words",
":",
"or_word_query",
"=",
"[",
"Statement",
".",
"text",
".",
"ilike",
"(",
"'%'",
"+",
"word",
"+",
"'%'",
")",
"for",
"word",
"in",
"exclude_text_words",
"]",
"statements",
"=",
"statements",
".",
"filter",
"(",
"~",
"or_",
"(",
"*",
"or_word_query",
")",
")",
"if",
"persona_not_startswith",
":",
"statements",
"=",
"statements",
".",
"filter",
"(",
"~",
"Statement",
".",
"persona",
".",
"startswith",
"(",
"'bot:'",
")",
")",
"if",
"search_text_contains",
":",
"or_query",
"=",
"[",
"Statement",
".",
"search_text",
".",
"contains",
"(",
"word",
")",
"for",
"word",
"in",
"search_text_contains",
".",
"split",
"(",
"' '",
")",
"]",
"statements",
"=",
"statements",
".",
"filter",
"(",
"or_",
"(",
"*",
"or_query",
")",
")",
"if",
"order_by",
":",
"if",
"'created_at'",
"in",
"order_by",
":",
"index",
"=",
"order_by",
".",
"index",
"(",
"'created_at'",
")",
"order_by",
"[",
"index",
"]",
"=",
"Statement",
".",
"created_at",
".",
"asc",
"(",
")",
"statements",
"=",
"statements",
".",
"order_by",
"(",
"*",
"order_by",
")",
"total_statements",
"=",
"statements",
".",
"count",
"(",
")",
"for",
"start_index",
"in",
"range",
"(",
"0",
",",
"total_statements",
",",
"page_size",
")",
":",
"for",
"statement",
"in",
"statements",
".",
"slice",
"(",
"start_index",
",",
"start_index",
"+",
"page_size",
")",
":",
"yield",
"self",
".",
"model_to_object",
"(",
"statement",
")",
"session",
".",
"close",
"(",
")"
] |
Returns a list of objects from the database.
The kwargs parameter can contain any number
of attributes. Only objects which contain all
listed attributes and in which all values match
for all listed attributes will be returned.
|
[
"Returns",
"a",
"list",
"of",
"objects",
"from",
"the",
"database",
".",
"The",
"kwargs",
"parameter",
"can",
"contain",
"any",
"number",
"of",
"attributes",
".",
"Only",
"objects",
"which",
"contain",
"all",
"listed",
"attributes",
"and",
"in",
"which",
"all",
"values",
"match",
"for",
"all",
"listed",
"attributes",
"will",
"be",
"returned",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L97-L174
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/sql_storage.py
|
SQLStorageAdapter.create
|
def create(self, **kwargs):
"""
Creates a new statement matching the keyword arguments specified.
Returns the created statement.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
session = self.Session()
tags = set(kwargs.pop('tags', []))
if 'search_text' not in kwargs:
kwargs['search_text'] = self.tagger.get_bigram_pair_string(kwargs['text'])
if 'search_in_response_to' not in kwargs:
in_response_to = kwargs.get('in_response_to')
if in_response_to:
kwargs['search_in_response_to'] = self.tagger.get_bigram_pair_string(in_response_to)
statement = Statement(**kwargs)
for tag_name in tags:
tag = session.query(Tag).filter_by(name=tag_name).first()
if not tag:
# Create the tag
tag = Tag(name=tag_name)
statement.tags.append(tag)
session.add(statement)
session.flush()
session.refresh(statement)
statement_object = self.model_to_object(statement)
self._session_finish(session)
return statement_object
|
python
|
def create(self, **kwargs):
"""
Creates a new statement matching the keyword arguments specified.
Returns the created statement.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
session = self.Session()
tags = set(kwargs.pop('tags', []))
if 'search_text' not in kwargs:
kwargs['search_text'] = self.tagger.get_bigram_pair_string(kwargs['text'])
if 'search_in_response_to' not in kwargs:
in_response_to = kwargs.get('in_response_to')
if in_response_to:
kwargs['search_in_response_to'] = self.tagger.get_bigram_pair_string(in_response_to)
statement = Statement(**kwargs)
for tag_name in tags:
tag = session.query(Tag).filter_by(name=tag_name).first()
if not tag:
# Create the tag
tag = Tag(name=tag_name)
statement.tags.append(tag)
session.add(statement)
session.flush()
session.refresh(statement)
statement_object = self.model_to_object(statement)
self._session_finish(session)
return statement_object
|
[
"def",
"create",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"Tag",
"=",
"self",
".",
"get_model",
"(",
"'tag'",
")",
"session",
"=",
"self",
".",
"Session",
"(",
")",
"tags",
"=",
"set",
"(",
"kwargs",
".",
"pop",
"(",
"'tags'",
",",
"[",
"]",
")",
")",
"if",
"'search_text'",
"not",
"in",
"kwargs",
":",
"kwargs",
"[",
"'search_text'",
"]",
"=",
"self",
".",
"tagger",
".",
"get_bigram_pair_string",
"(",
"kwargs",
"[",
"'text'",
"]",
")",
"if",
"'search_in_response_to'",
"not",
"in",
"kwargs",
":",
"in_response_to",
"=",
"kwargs",
".",
"get",
"(",
"'in_response_to'",
")",
"if",
"in_response_to",
":",
"kwargs",
"[",
"'search_in_response_to'",
"]",
"=",
"self",
".",
"tagger",
".",
"get_bigram_pair_string",
"(",
"in_response_to",
")",
"statement",
"=",
"Statement",
"(",
"*",
"*",
"kwargs",
")",
"for",
"tag_name",
"in",
"tags",
":",
"tag",
"=",
"session",
".",
"query",
"(",
"Tag",
")",
".",
"filter_by",
"(",
"name",
"=",
"tag_name",
")",
".",
"first",
"(",
")",
"if",
"not",
"tag",
":",
"# Create the tag",
"tag",
"=",
"Tag",
"(",
"name",
"=",
"tag_name",
")",
"statement",
".",
"tags",
".",
"append",
"(",
"tag",
")",
"session",
".",
"add",
"(",
"statement",
")",
"session",
".",
"flush",
"(",
")",
"session",
".",
"refresh",
"(",
"statement",
")",
"statement_object",
"=",
"self",
".",
"model_to_object",
"(",
"statement",
")",
"self",
".",
"_session_finish",
"(",
"session",
")",
"return",
"statement_object"
] |
Creates a new statement matching the keyword arguments specified.
Returns the created statement.
|
[
"Creates",
"a",
"new",
"statement",
"matching",
"the",
"keyword",
"arguments",
"specified",
".",
"Returns",
"the",
"created",
"statement",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L176-L217
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/sql_storage.py
|
SQLStorageAdapter.create_many
|
def create_many(self, statements):
"""
Creates multiple statement entries.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
session = self.Session()
create_statements = []
create_tags = {}
for statement in statements:
statement_data = statement.serialize()
tag_data = statement_data.pop('tags', [])
statement_model_object = Statement(**statement_data)
if not statement.search_text:
statement_model_object.search_text = self.tagger.get_bigram_pair_string(statement.text)
if not statement.search_in_response_to and statement.in_response_to:
statement_model_object.search_in_response_to = self.tagger.get_bigram_pair_string(statement.in_response_to)
new_tags = set(tag_data) - set(create_tags.keys())
if new_tags:
existing_tags = session.query(Tag).filter(
Tag.name.in_(new_tags)
)
for existing_tag in existing_tags:
create_tags[existing_tag.name] = existing_tag
for tag_name in tag_data:
if tag_name in create_tags:
tag = create_tags[tag_name]
else:
# Create the tag if it does not exist
tag = Tag(name=tag_name)
create_tags[tag_name] = tag
statement_model_object.tags.append(tag)
create_statements.append(statement_model_object)
session.add_all(create_statements)
session.commit()
|
python
|
def create_many(self, statements):
"""
Creates multiple statement entries.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
session = self.Session()
create_statements = []
create_tags = {}
for statement in statements:
statement_data = statement.serialize()
tag_data = statement_data.pop('tags', [])
statement_model_object = Statement(**statement_data)
if not statement.search_text:
statement_model_object.search_text = self.tagger.get_bigram_pair_string(statement.text)
if not statement.search_in_response_to and statement.in_response_to:
statement_model_object.search_in_response_to = self.tagger.get_bigram_pair_string(statement.in_response_to)
new_tags = set(tag_data) - set(create_tags.keys())
if new_tags:
existing_tags = session.query(Tag).filter(
Tag.name.in_(new_tags)
)
for existing_tag in existing_tags:
create_tags[existing_tag.name] = existing_tag
for tag_name in tag_data:
if tag_name in create_tags:
tag = create_tags[tag_name]
else:
# Create the tag if it does not exist
tag = Tag(name=tag_name)
create_tags[tag_name] = tag
statement_model_object.tags.append(tag)
create_statements.append(statement_model_object)
session.add_all(create_statements)
session.commit()
|
[
"def",
"create_many",
"(",
"self",
",",
"statements",
")",
":",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"Tag",
"=",
"self",
".",
"get_model",
"(",
"'tag'",
")",
"session",
"=",
"self",
".",
"Session",
"(",
")",
"create_statements",
"=",
"[",
"]",
"create_tags",
"=",
"{",
"}",
"for",
"statement",
"in",
"statements",
":",
"statement_data",
"=",
"statement",
".",
"serialize",
"(",
")",
"tag_data",
"=",
"statement_data",
".",
"pop",
"(",
"'tags'",
",",
"[",
"]",
")",
"statement_model_object",
"=",
"Statement",
"(",
"*",
"*",
"statement_data",
")",
"if",
"not",
"statement",
".",
"search_text",
":",
"statement_model_object",
".",
"search_text",
"=",
"self",
".",
"tagger",
".",
"get_bigram_pair_string",
"(",
"statement",
".",
"text",
")",
"if",
"not",
"statement",
".",
"search_in_response_to",
"and",
"statement",
".",
"in_response_to",
":",
"statement_model_object",
".",
"search_in_response_to",
"=",
"self",
".",
"tagger",
".",
"get_bigram_pair_string",
"(",
"statement",
".",
"in_response_to",
")",
"new_tags",
"=",
"set",
"(",
"tag_data",
")",
"-",
"set",
"(",
"create_tags",
".",
"keys",
"(",
")",
")",
"if",
"new_tags",
":",
"existing_tags",
"=",
"session",
".",
"query",
"(",
"Tag",
")",
".",
"filter",
"(",
"Tag",
".",
"name",
".",
"in_",
"(",
"new_tags",
")",
")",
"for",
"existing_tag",
"in",
"existing_tags",
":",
"create_tags",
"[",
"existing_tag",
".",
"name",
"]",
"=",
"existing_tag",
"for",
"tag_name",
"in",
"tag_data",
":",
"if",
"tag_name",
"in",
"create_tags",
":",
"tag",
"=",
"create_tags",
"[",
"tag_name",
"]",
"else",
":",
"# Create the tag if it does not exist",
"tag",
"=",
"Tag",
"(",
"name",
"=",
"tag_name",
")",
"create_tags",
"[",
"tag_name",
"]",
"=",
"tag",
"statement_model_object",
".",
"tags",
".",
"append",
"(",
"tag",
")",
"create_statements",
".",
"append",
"(",
"statement_model_object",
")",
"session",
".",
"add_all",
"(",
"create_statements",
")",
"session",
".",
"commit",
"(",
")"
] |
Creates multiple statement entries.
|
[
"Creates",
"multiple",
"statement",
"entries",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L219-L267
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/sql_storage.py
|
SQLStorageAdapter.update
|
def update(self, statement):
"""
Modifies an entry in the database.
Creates an entry if one does not exist.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
if statement is not None:
session = self.Session()
record = None
if hasattr(statement, 'id') and statement.id is not None:
record = session.query(Statement).get(statement.id)
else:
record = session.query(Statement).filter(
Statement.text == statement.text,
Statement.conversation == statement.conversation,
).first()
# Create a new statement entry if one does not already exist
if not record:
record = Statement(
text=statement.text,
conversation=statement.conversation,
persona=statement.persona
)
# Update the response value
record.in_response_to = statement.in_response_to
record.created_at = statement.created_at
record.search_text = self.tagger.get_bigram_pair_string(statement.text)
if statement.in_response_to:
record.search_in_response_to = self.tagger.get_bigram_pair_string(statement.in_response_to)
for tag_name in statement.get_tags():
tag = session.query(Tag).filter_by(name=tag_name).first()
if not tag:
# Create the record
tag = Tag(name=tag_name)
record.tags.append(tag)
session.add(record)
self._session_finish(session)
|
python
|
def update(self, statement):
"""
Modifies an entry in the database.
Creates an entry if one does not exist.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
if statement is not None:
session = self.Session()
record = None
if hasattr(statement, 'id') and statement.id is not None:
record = session.query(Statement).get(statement.id)
else:
record = session.query(Statement).filter(
Statement.text == statement.text,
Statement.conversation == statement.conversation,
).first()
# Create a new statement entry if one does not already exist
if not record:
record = Statement(
text=statement.text,
conversation=statement.conversation,
persona=statement.persona
)
# Update the response value
record.in_response_to = statement.in_response_to
record.created_at = statement.created_at
record.search_text = self.tagger.get_bigram_pair_string(statement.text)
if statement.in_response_to:
record.search_in_response_to = self.tagger.get_bigram_pair_string(statement.in_response_to)
for tag_name in statement.get_tags():
tag = session.query(Tag).filter_by(name=tag_name).first()
if not tag:
# Create the record
tag = Tag(name=tag_name)
record.tags.append(tag)
session.add(record)
self._session_finish(session)
|
[
"def",
"update",
"(",
"self",
",",
"statement",
")",
":",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"Tag",
"=",
"self",
".",
"get_model",
"(",
"'tag'",
")",
"if",
"statement",
"is",
"not",
"None",
":",
"session",
"=",
"self",
".",
"Session",
"(",
")",
"record",
"=",
"None",
"if",
"hasattr",
"(",
"statement",
",",
"'id'",
")",
"and",
"statement",
".",
"id",
"is",
"not",
"None",
":",
"record",
"=",
"session",
".",
"query",
"(",
"Statement",
")",
".",
"get",
"(",
"statement",
".",
"id",
")",
"else",
":",
"record",
"=",
"session",
".",
"query",
"(",
"Statement",
")",
".",
"filter",
"(",
"Statement",
".",
"text",
"==",
"statement",
".",
"text",
",",
"Statement",
".",
"conversation",
"==",
"statement",
".",
"conversation",
",",
")",
".",
"first",
"(",
")",
"# Create a new statement entry if one does not already exist",
"if",
"not",
"record",
":",
"record",
"=",
"Statement",
"(",
"text",
"=",
"statement",
".",
"text",
",",
"conversation",
"=",
"statement",
".",
"conversation",
",",
"persona",
"=",
"statement",
".",
"persona",
")",
"# Update the response value",
"record",
".",
"in_response_to",
"=",
"statement",
".",
"in_response_to",
"record",
".",
"created_at",
"=",
"statement",
".",
"created_at",
"record",
".",
"search_text",
"=",
"self",
".",
"tagger",
".",
"get_bigram_pair_string",
"(",
"statement",
".",
"text",
")",
"if",
"statement",
".",
"in_response_to",
":",
"record",
".",
"search_in_response_to",
"=",
"self",
".",
"tagger",
".",
"get_bigram_pair_string",
"(",
"statement",
".",
"in_response_to",
")",
"for",
"tag_name",
"in",
"statement",
".",
"get_tags",
"(",
")",
":",
"tag",
"=",
"session",
".",
"query",
"(",
"Tag",
")",
".",
"filter_by",
"(",
"name",
"=",
"tag_name",
")",
".",
"first",
"(",
")",
"if",
"not",
"tag",
":",
"# Create the record",
"tag",
"=",
"Tag",
"(",
"name",
"=",
"tag_name",
")",
"record",
".",
"tags",
".",
"append",
"(",
"tag",
")",
"session",
".",
"add",
"(",
"record",
")",
"self",
".",
"_session_finish",
"(",
"session",
")"
] |
Modifies an entry in the database.
Creates an entry if one does not exist.
|
[
"Modifies",
"an",
"entry",
"in",
"the",
"database",
".",
"Creates",
"an",
"entry",
"if",
"one",
"does",
"not",
"exist",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L269-L318
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/sql_storage.py
|
SQLStorageAdapter.get_random
|
def get_random(self):
"""
Returns a random statement from the database.
"""
import random
Statement = self.get_model('statement')
session = self.Session()
count = self.count()
if count < 1:
raise self.EmptyDatabaseException()
random_index = random.randrange(0, count)
random_statement = session.query(Statement)[random_index]
statement = self.model_to_object(random_statement)
session.close()
return statement
|
python
|
def get_random(self):
"""
Returns a random statement from the database.
"""
import random
Statement = self.get_model('statement')
session = self.Session()
count = self.count()
if count < 1:
raise self.EmptyDatabaseException()
random_index = random.randrange(0, count)
random_statement = session.query(Statement)[random_index]
statement = self.model_to_object(random_statement)
session.close()
return statement
|
[
"def",
"get_random",
"(",
"self",
")",
":",
"import",
"random",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"session",
"=",
"self",
".",
"Session",
"(",
")",
"count",
"=",
"self",
".",
"count",
"(",
")",
"if",
"count",
"<",
"1",
":",
"raise",
"self",
".",
"EmptyDatabaseException",
"(",
")",
"random_index",
"=",
"random",
".",
"randrange",
"(",
"0",
",",
"count",
")",
"random_statement",
"=",
"session",
".",
"query",
"(",
"Statement",
")",
"[",
"random_index",
"]",
"statement",
"=",
"self",
".",
"model_to_object",
"(",
"random_statement",
")",
"session",
".",
"close",
"(",
")",
"return",
"statement"
] |
Returns a random statement from the database.
|
[
"Returns",
"a",
"random",
"statement",
"from",
"the",
"database",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L320-L339
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/sql_storage.py
|
SQLStorageAdapter.drop
|
def drop(self):
"""
Drop the database.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
session = self.Session()
session.query(Statement).delete()
session.query(Tag).delete()
session.commit()
session.close()
|
python
|
def drop(self):
"""
Drop the database.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
session = self.Session()
session.query(Statement).delete()
session.query(Tag).delete()
session.commit()
session.close()
|
[
"def",
"drop",
"(",
"self",
")",
":",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"Tag",
"=",
"self",
".",
"get_model",
"(",
"'tag'",
")",
"session",
"=",
"self",
".",
"Session",
"(",
")",
"session",
".",
"query",
"(",
"Statement",
")",
".",
"delete",
"(",
")",
"session",
".",
"query",
"(",
"Tag",
")",
".",
"delete",
"(",
")",
"session",
".",
"commit",
"(",
")",
"session",
".",
"close",
"(",
")"
] |
Drop the database.
|
[
"Drop",
"the",
"database",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L341-L354
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/sql_storage.py
|
SQLStorageAdapter.create_database
|
def create_database(self):
"""
Populate the database with the tables.
"""
from chatterbot.ext.sqlalchemy_app.models import Base
Base.metadata.create_all(self.engine)
|
python
|
def create_database(self):
"""
Populate the database with the tables.
"""
from chatterbot.ext.sqlalchemy_app.models import Base
Base.metadata.create_all(self.engine)
|
[
"def",
"create_database",
"(",
"self",
")",
":",
"from",
"chatterbot",
".",
"ext",
".",
"sqlalchemy_app",
".",
"models",
"import",
"Base",
"Base",
".",
"metadata",
".",
"create_all",
"(",
"self",
".",
"engine",
")"
] |
Populate the database with the tables.
|
[
"Populate",
"the",
"database",
"with",
"the",
"tables",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/sql_storage.py#L356-L361
|
train
|
gunthercox/ChatterBot
|
examples/django_app/example_app/views.py
|
ChatterBotApiView.post
|
def post(self, request, *args, **kwargs):
"""
Return a response to the statement in the posted data.
* The JSON data should contain a 'text' attribute.
"""
input_data = json.loads(request.body.decode('utf-8'))
if 'text' not in input_data:
return JsonResponse({
'text': [
'The attribute "text" is required.'
]
}, status=400)
response = self.chatterbot.get_response(input_data)
response_data = response.serialize()
return JsonResponse(response_data, status=200)
|
python
|
def post(self, request, *args, **kwargs):
"""
Return a response to the statement in the posted data.
* The JSON data should contain a 'text' attribute.
"""
input_data = json.loads(request.body.decode('utf-8'))
if 'text' not in input_data:
return JsonResponse({
'text': [
'The attribute "text" is required.'
]
}, status=400)
response = self.chatterbot.get_response(input_data)
response_data = response.serialize()
return JsonResponse(response_data, status=200)
|
[
"def",
"post",
"(",
"self",
",",
"request",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"input_data",
"=",
"json",
".",
"loads",
"(",
"request",
".",
"body",
".",
"decode",
"(",
"'utf-8'",
")",
")",
"if",
"'text'",
"not",
"in",
"input_data",
":",
"return",
"JsonResponse",
"(",
"{",
"'text'",
":",
"[",
"'The attribute \"text\" is required.'",
"]",
"}",
",",
"status",
"=",
"400",
")",
"response",
"=",
"self",
".",
"chatterbot",
".",
"get_response",
"(",
"input_data",
")",
"response_data",
"=",
"response",
".",
"serialize",
"(",
")",
"return",
"JsonResponse",
"(",
"response_data",
",",
"status",
"=",
"200",
")"
] |
Return a response to the statement in the posted data.
* The JSON data should contain a 'text' attribute.
|
[
"Return",
"a",
"response",
"to",
"the",
"statement",
"in",
"the",
"posted",
"data",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/examples/django_app/example_app/views.py#L20-L39
|
train
|
gunthercox/ChatterBot
|
chatterbot/corpus.py
|
get_file_path
|
def get_file_path(dotted_path, extension='json'):
"""
Reads a dotted file path and returns the file path.
"""
# If the operating system's file path seperator character is in the string
if os.sep in dotted_path or '/' in dotted_path:
# Assume the path is a valid file path
return dotted_path
parts = dotted_path.split('.')
if parts[0] == 'chatterbot':
parts.pop(0)
parts[0] = DATA_DIRECTORY
corpus_path = os.path.join(*parts)
if os.path.exists(corpus_path + '.{}'.format(extension)):
corpus_path += '.{}'.format(extension)
return corpus_path
|
python
|
def get_file_path(dotted_path, extension='json'):
"""
Reads a dotted file path and returns the file path.
"""
# If the operating system's file path seperator character is in the string
if os.sep in dotted_path or '/' in dotted_path:
# Assume the path is a valid file path
return dotted_path
parts = dotted_path.split('.')
if parts[0] == 'chatterbot':
parts.pop(0)
parts[0] = DATA_DIRECTORY
corpus_path = os.path.join(*parts)
if os.path.exists(corpus_path + '.{}'.format(extension)):
corpus_path += '.{}'.format(extension)
return corpus_path
|
[
"def",
"get_file_path",
"(",
"dotted_path",
",",
"extension",
"=",
"'json'",
")",
":",
"# If the operating system's file path seperator character is in the string",
"if",
"os",
".",
"sep",
"in",
"dotted_path",
"or",
"'/'",
"in",
"dotted_path",
":",
"# Assume the path is a valid file path",
"return",
"dotted_path",
"parts",
"=",
"dotted_path",
".",
"split",
"(",
"'.'",
")",
"if",
"parts",
"[",
"0",
"]",
"==",
"'chatterbot'",
":",
"parts",
".",
"pop",
"(",
"0",
")",
"parts",
"[",
"0",
"]",
"=",
"DATA_DIRECTORY",
"corpus_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"*",
"parts",
")",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"corpus_path",
"+",
"'.{}'",
".",
"format",
"(",
"extension",
")",
")",
":",
"corpus_path",
"+=",
"'.{}'",
".",
"format",
"(",
"extension",
")",
"return",
"corpus_path"
] |
Reads a dotted file path and returns the file path.
|
[
"Reads",
"a",
"dotted",
"file",
"path",
"and",
"returns",
"the",
"file",
"path",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/corpus.py#L11-L30
|
train
|
gunthercox/ChatterBot
|
chatterbot/corpus.py
|
read_corpus
|
def read_corpus(file_name):
"""
Read and return the data from a corpus json file.
"""
with io.open(file_name, encoding='utf-8') as data_file:
return yaml.load(data_file)
|
python
|
def read_corpus(file_name):
"""
Read and return the data from a corpus json file.
"""
with io.open(file_name, encoding='utf-8') as data_file:
return yaml.load(data_file)
|
[
"def",
"read_corpus",
"(",
"file_name",
")",
":",
"with",
"io",
".",
"open",
"(",
"file_name",
",",
"encoding",
"=",
"'utf-8'",
")",
"as",
"data_file",
":",
"return",
"yaml",
".",
"load",
"(",
"data_file",
")"
] |
Read and return the data from a corpus json file.
|
[
"Read",
"and",
"return",
"the",
"data",
"from",
"a",
"corpus",
"json",
"file",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/corpus.py#L33-L38
|
train
|
gunthercox/ChatterBot
|
chatterbot/corpus.py
|
list_corpus_files
|
def list_corpus_files(dotted_path):
"""
Return a list of file paths to each data file in the specified corpus.
"""
corpus_path = get_file_path(dotted_path, extension=CORPUS_EXTENSION)
paths = []
if os.path.isdir(corpus_path):
paths = glob.glob(corpus_path + '/**/*.' + CORPUS_EXTENSION, recursive=True)
else:
paths.append(corpus_path)
paths.sort()
return paths
|
python
|
def list_corpus_files(dotted_path):
"""
Return a list of file paths to each data file in the specified corpus.
"""
corpus_path = get_file_path(dotted_path, extension=CORPUS_EXTENSION)
paths = []
if os.path.isdir(corpus_path):
paths = glob.glob(corpus_path + '/**/*.' + CORPUS_EXTENSION, recursive=True)
else:
paths.append(corpus_path)
paths.sort()
return paths
|
[
"def",
"list_corpus_files",
"(",
"dotted_path",
")",
":",
"corpus_path",
"=",
"get_file_path",
"(",
"dotted_path",
",",
"extension",
"=",
"CORPUS_EXTENSION",
")",
"paths",
"=",
"[",
"]",
"if",
"os",
".",
"path",
".",
"isdir",
"(",
"corpus_path",
")",
":",
"paths",
"=",
"glob",
".",
"glob",
"(",
"corpus_path",
"+",
"'/**/*.'",
"+",
"CORPUS_EXTENSION",
",",
"recursive",
"=",
"True",
")",
"else",
":",
"paths",
".",
"append",
"(",
"corpus_path",
")",
"paths",
".",
"sort",
"(",
")",
"return",
"paths"
] |
Return a list of file paths to each data file in the specified corpus.
|
[
"Return",
"a",
"list",
"of",
"file",
"paths",
"to",
"each",
"data",
"file",
"in",
"the",
"specified",
"corpus",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/corpus.py#L41-L54
|
train
|
gunthercox/ChatterBot
|
chatterbot/corpus.py
|
load_corpus
|
def load_corpus(*data_file_paths):
"""
Return the data contained within a specified corpus.
"""
for file_path in data_file_paths:
corpus = []
corpus_data = read_corpus(file_path)
conversations = corpus_data.get('conversations', [])
corpus.extend(conversations)
categories = corpus_data.get('categories', [])
yield corpus, categories, file_path
|
python
|
def load_corpus(*data_file_paths):
"""
Return the data contained within a specified corpus.
"""
for file_path in data_file_paths:
corpus = []
corpus_data = read_corpus(file_path)
conversations = corpus_data.get('conversations', [])
corpus.extend(conversations)
categories = corpus_data.get('categories', [])
yield corpus, categories, file_path
|
[
"def",
"load_corpus",
"(",
"*",
"data_file_paths",
")",
":",
"for",
"file_path",
"in",
"data_file_paths",
":",
"corpus",
"=",
"[",
"]",
"corpus_data",
"=",
"read_corpus",
"(",
"file_path",
")",
"conversations",
"=",
"corpus_data",
".",
"get",
"(",
"'conversations'",
",",
"[",
"]",
")",
"corpus",
".",
"extend",
"(",
"conversations",
")",
"categories",
"=",
"corpus_data",
".",
"get",
"(",
"'categories'",
",",
"[",
"]",
")",
"yield",
"corpus",
",",
"categories",
",",
"file_path"
] |
Return the data contained within a specified corpus.
|
[
"Return",
"the",
"data",
"contained",
"within",
"a",
"specified",
"corpus",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/corpus.py#L57-L70
|
train
|
gunthercox/ChatterBot
|
chatterbot/tagging.py
|
PosLemmaTagger.get_bigram_pair_string
|
def get_bigram_pair_string(self, text):
"""
Return a string of text containing part-of-speech, lemma pairs.
"""
bigram_pairs = []
if len(text) <= 2:
text_without_punctuation = text.translate(self.punctuation_table)
if len(text_without_punctuation) >= 1:
text = text_without_punctuation
document = self.nlp(text)
if len(text) <= 2:
bigram_pairs = [
token.lemma_.lower() for token in document
]
else:
tokens = [
token for token in document if token.is_alpha and not token.is_stop
]
if len(tokens) < 2:
tokens = [
token for token in document if token.is_alpha
]
for index in range(1, len(tokens)):
bigram_pairs.append('{}:{}'.format(
tokens[index - 1].pos_,
tokens[index].lemma_.lower()
))
if not bigram_pairs:
bigram_pairs = [
token.lemma_.lower() for token in document
]
return ' '.join(bigram_pairs)
|
python
|
def get_bigram_pair_string(self, text):
"""
Return a string of text containing part-of-speech, lemma pairs.
"""
bigram_pairs = []
if len(text) <= 2:
text_without_punctuation = text.translate(self.punctuation_table)
if len(text_without_punctuation) >= 1:
text = text_without_punctuation
document = self.nlp(text)
if len(text) <= 2:
bigram_pairs = [
token.lemma_.lower() for token in document
]
else:
tokens = [
token for token in document if token.is_alpha and not token.is_stop
]
if len(tokens) < 2:
tokens = [
token for token in document if token.is_alpha
]
for index in range(1, len(tokens)):
bigram_pairs.append('{}:{}'.format(
tokens[index - 1].pos_,
tokens[index].lemma_.lower()
))
if not bigram_pairs:
bigram_pairs = [
token.lemma_.lower() for token in document
]
return ' '.join(bigram_pairs)
|
[
"def",
"get_bigram_pair_string",
"(",
"self",
",",
"text",
")",
":",
"bigram_pairs",
"=",
"[",
"]",
"if",
"len",
"(",
"text",
")",
"<=",
"2",
":",
"text_without_punctuation",
"=",
"text",
".",
"translate",
"(",
"self",
".",
"punctuation_table",
")",
"if",
"len",
"(",
"text_without_punctuation",
")",
">=",
"1",
":",
"text",
"=",
"text_without_punctuation",
"document",
"=",
"self",
".",
"nlp",
"(",
"text",
")",
"if",
"len",
"(",
"text",
")",
"<=",
"2",
":",
"bigram_pairs",
"=",
"[",
"token",
".",
"lemma_",
".",
"lower",
"(",
")",
"for",
"token",
"in",
"document",
"]",
"else",
":",
"tokens",
"=",
"[",
"token",
"for",
"token",
"in",
"document",
"if",
"token",
".",
"is_alpha",
"and",
"not",
"token",
".",
"is_stop",
"]",
"if",
"len",
"(",
"tokens",
")",
"<",
"2",
":",
"tokens",
"=",
"[",
"token",
"for",
"token",
"in",
"document",
"if",
"token",
".",
"is_alpha",
"]",
"for",
"index",
"in",
"range",
"(",
"1",
",",
"len",
"(",
"tokens",
")",
")",
":",
"bigram_pairs",
".",
"append",
"(",
"'{}:{}'",
".",
"format",
"(",
"tokens",
"[",
"index",
"-",
"1",
"]",
".",
"pos_",
",",
"tokens",
"[",
"index",
"]",
".",
"lemma_",
".",
"lower",
"(",
")",
")",
")",
"if",
"not",
"bigram_pairs",
":",
"bigram_pairs",
"=",
"[",
"token",
".",
"lemma_",
".",
"lower",
"(",
")",
"for",
"token",
"in",
"document",
"]",
"return",
"' '",
".",
"join",
"(",
"bigram_pairs",
")"
] |
Return a string of text containing part-of-speech, lemma pairs.
|
[
"Return",
"a",
"string",
"of",
"text",
"containing",
"part",
"-",
"of",
"-",
"speech",
"lemma",
"pairs",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/tagging.py#L15-L53
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/django_storage.py
|
DjangoStorageAdapter.filter
|
def filter(self, **kwargs):
"""
Returns a list of statements in the database
that match the parameters specified.
"""
from django.db.models import Q
Statement = self.get_model('statement')
kwargs.pop('page_size', 1000)
order_by = kwargs.pop('order_by', None)
tags = kwargs.pop('tags', [])
exclude_text = kwargs.pop('exclude_text', None)
exclude_text_words = kwargs.pop('exclude_text_words', [])
persona_not_startswith = kwargs.pop('persona_not_startswith', None)
search_text_contains = kwargs.pop('search_text_contains', None)
# Convert a single sting into a list if only one tag is provided
if type(tags) == str:
tags = [tags]
if tags:
kwargs['tags__name__in'] = tags
statements = Statement.objects.filter(**kwargs)
if exclude_text:
statements = statements.exclude(
text__in=exclude_text
)
if exclude_text_words:
or_query = [
~Q(text__icontains=word) for word in exclude_text_words
]
statements = statements.filter(
*or_query
)
if persona_not_startswith:
statements = statements.exclude(
persona__startswith='bot:'
)
if search_text_contains:
or_query = Q()
for word in search_text_contains.split(' '):
or_query |= Q(search_text__contains=word)
statements = statements.filter(
or_query
)
if order_by:
statements = statements.order_by(*order_by)
for statement in statements.iterator():
yield statement
|
python
|
def filter(self, **kwargs):
"""
Returns a list of statements in the database
that match the parameters specified.
"""
from django.db.models import Q
Statement = self.get_model('statement')
kwargs.pop('page_size', 1000)
order_by = kwargs.pop('order_by', None)
tags = kwargs.pop('tags', [])
exclude_text = kwargs.pop('exclude_text', None)
exclude_text_words = kwargs.pop('exclude_text_words', [])
persona_not_startswith = kwargs.pop('persona_not_startswith', None)
search_text_contains = kwargs.pop('search_text_contains', None)
# Convert a single sting into a list if only one tag is provided
if type(tags) == str:
tags = [tags]
if tags:
kwargs['tags__name__in'] = tags
statements = Statement.objects.filter(**kwargs)
if exclude_text:
statements = statements.exclude(
text__in=exclude_text
)
if exclude_text_words:
or_query = [
~Q(text__icontains=word) for word in exclude_text_words
]
statements = statements.filter(
*or_query
)
if persona_not_startswith:
statements = statements.exclude(
persona__startswith='bot:'
)
if search_text_contains:
or_query = Q()
for word in search_text_contains.split(' '):
or_query |= Q(search_text__contains=word)
statements = statements.filter(
or_query
)
if order_by:
statements = statements.order_by(*order_by)
for statement in statements.iterator():
yield statement
|
[
"def",
"filter",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"from",
"django",
".",
"db",
".",
"models",
"import",
"Q",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"kwargs",
".",
"pop",
"(",
"'page_size'",
",",
"1000",
")",
"order_by",
"=",
"kwargs",
".",
"pop",
"(",
"'order_by'",
",",
"None",
")",
"tags",
"=",
"kwargs",
".",
"pop",
"(",
"'tags'",
",",
"[",
"]",
")",
"exclude_text",
"=",
"kwargs",
".",
"pop",
"(",
"'exclude_text'",
",",
"None",
")",
"exclude_text_words",
"=",
"kwargs",
".",
"pop",
"(",
"'exclude_text_words'",
",",
"[",
"]",
")",
"persona_not_startswith",
"=",
"kwargs",
".",
"pop",
"(",
"'persona_not_startswith'",
",",
"None",
")",
"search_text_contains",
"=",
"kwargs",
".",
"pop",
"(",
"'search_text_contains'",
",",
"None",
")",
"# Convert a single sting into a list if only one tag is provided",
"if",
"type",
"(",
"tags",
")",
"==",
"str",
":",
"tags",
"=",
"[",
"tags",
"]",
"if",
"tags",
":",
"kwargs",
"[",
"'tags__name__in'",
"]",
"=",
"tags",
"statements",
"=",
"Statement",
".",
"objects",
".",
"filter",
"(",
"*",
"*",
"kwargs",
")",
"if",
"exclude_text",
":",
"statements",
"=",
"statements",
".",
"exclude",
"(",
"text__in",
"=",
"exclude_text",
")",
"if",
"exclude_text_words",
":",
"or_query",
"=",
"[",
"~",
"Q",
"(",
"text__icontains",
"=",
"word",
")",
"for",
"word",
"in",
"exclude_text_words",
"]",
"statements",
"=",
"statements",
".",
"filter",
"(",
"*",
"or_query",
")",
"if",
"persona_not_startswith",
":",
"statements",
"=",
"statements",
".",
"exclude",
"(",
"persona__startswith",
"=",
"'bot:'",
")",
"if",
"search_text_contains",
":",
"or_query",
"=",
"Q",
"(",
")",
"for",
"word",
"in",
"search_text_contains",
".",
"split",
"(",
"' '",
")",
":",
"or_query",
"|=",
"Q",
"(",
"search_text__contains",
"=",
"word",
")",
"statements",
"=",
"statements",
".",
"filter",
"(",
"or_query",
")",
"if",
"order_by",
":",
"statements",
"=",
"statements",
".",
"order_by",
"(",
"*",
"order_by",
")",
"for",
"statement",
"in",
"statements",
".",
"iterator",
"(",
")",
":",
"yield",
"statement"
] |
Returns a list of statements in the database
that match the parameters specified.
|
[
"Returns",
"a",
"list",
"of",
"statements",
"in",
"the",
"database",
"that",
"match",
"the",
"parameters",
"specified",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/django_storage.py#L31-L90
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/django_storage.py
|
DjangoStorageAdapter.create
|
def create(self, **kwargs):
"""
Creates a new statement matching the keyword arguments specified.
Returns the created statement.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
tags = kwargs.pop('tags', [])
if 'search_text' not in kwargs:
kwargs['search_text'] = self.tagger.get_bigram_pair_string(kwargs['text'])
if 'search_in_response_to' not in kwargs:
if kwargs.get('in_response_to'):
kwargs['search_in_response_to'] = self.tagger.get_bigram_pair_string(kwargs['in_response_to'])
statement = Statement(**kwargs)
statement.save()
tags_to_add = []
for _tag in tags:
tag, _ = Tag.objects.get_or_create(name=_tag)
tags_to_add.append(tag)
statement.tags.add(*tags_to_add)
return statement
|
python
|
def create(self, **kwargs):
"""
Creates a new statement matching the keyword arguments specified.
Returns the created statement.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
tags = kwargs.pop('tags', [])
if 'search_text' not in kwargs:
kwargs['search_text'] = self.tagger.get_bigram_pair_string(kwargs['text'])
if 'search_in_response_to' not in kwargs:
if kwargs.get('in_response_to'):
kwargs['search_in_response_to'] = self.tagger.get_bigram_pair_string(kwargs['in_response_to'])
statement = Statement(**kwargs)
statement.save()
tags_to_add = []
for _tag in tags:
tag, _ = Tag.objects.get_or_create(name=_tag)
tags_to_add.append(tag)
statement.tags.add(*tags_to_add)
return statement
|
[
"def",
"create",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"Tag",
"=",
"self",
".",
"get_model",
"(",
"'tag'",
")",
"tags",
"=",
"kwargs",
".",
"pop",
"(",
"'tags'",
",",
"[",
"]",
")",
"if",
"'search_text'",
"not",
"in",
"kwargs",
":",
"kwargs",
"[",
"'search_text'",
"]",
"=",
"self",
".",
"tagger",
".",
"get_bigram_pair_string",
"(",
"kwargs",
"[",
"'text'",
"]",
")",
"if",
"'search_in_response_to'",
"not",
"in",
"kwargs",
":",
"if",
"kwargs",
".",
"get",
"(",
"'in_response_to'",
")",
":",
"kwargs",
"[",
"'search_in_response_to'",
"]",
"=",
"self",
".",
"tagger",
".",
"get_bigram_pair_string",
"(",
"kwargs",
"[",
"'in_response_to'",
"]",
")",
"statement",
"=",
"Statement",
"(",
"*",
"*",
"kwargs",
")",
"statement",
".",
"save",
"(",
")",
"tags_to_add",
"=",
"[",
"]",
"for",
"_tag",
"in",
"tags",
":",
"tag",
",",
"_",
"=",
"Tag",
".",
"objects",
".",
"get_or_create",
"(",
"name",
"=",
"_tag",
")",
"tags_to_add",
".",
"append",
"(",
"tag",
")",
"statement",
".",
"tags",
".",
"add",
"(",
"*",
"tags_to_add",
")",
"return",
"statement"
] |
Creates a new statement matching the keyword arguments specified.
Returns the created statement.
|
[
"Creates",
"a",
"new",
"statement",
"matching",
"the",
"keyword",
"arguments",
"specified",
".",
"Returns",
"the",
"created",
"statement",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/django_storage.py#L92-L121
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/django_storage.py
|
DjangoStorageAdapter.create_many
|
def create_many(self, statements):
"""
Creates multiple statement entries.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
tag_cache = {}
for statement in statements:
statement_data = statement.serialize()
tag_data = statement_data.pop('tags', [])
statement_model_object = Statement(**statement_data)
if not statement.search_text:
statement_model_object.search_text = self.tagger.get_bigram_pair_string(statement.text)
if not statement.search_in_response_to and statement.in_response_to:
statement_model_object.search_in_response_to = self.tagger.get_bigram_pair_string(statement.in_response_to)
statement_model_object.save()
tags_to_add = []
for tag_name in tag_data:
if tag_name in tag_cache:
tag = tag_cache[tag_name]
else:
tag, _ = Tag.objects.get_or_create(name=tag_name)
tag_cache[tag_name] = tag
tags_to_add.append(tag)
statement_model_object.tags.add(*tags_to_add)
|
python
|
def create_many(self, statements):
"""
Creates multiple statement entries.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
tag_cache = {}
for statement in statements:
statement_data = statement.serialize()
tag_data = statement_data.pop('tags', [])
statement_model_object = Statement(**statement_data)
if not statement.search_text:
statement_model_object.search_text = self.tagger.get_bigram_pair_string(statement.text)
if not statement.search_in_response_to and statement.in_response_to:
statement_model_object.search_in_response_to = self.tagger.get_bigram_pair_string(statement.in_response_to)
statement_model_object.save()
tags_to_add = []
for tag_name in tag_data:
if tag_name in tag_cache:
tag = tag_cache[tag_name]
else:
tag, _ = Tag.objects.get_or_create(name=tag_name)
tag_cache[tag_name] = tag
tags_to_add.append(tag)
statement_model_object.tags.add(*tags_to_add)
|
[
"def",
"create_many",
"(",
"self",
",",
"statements",
")",
":",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"Tag",
"=",
"self",
".",
"get_model",
"(",
"'tag'",
")",
"tag_cache",
"=",
"{",
"}",
"for",
"statement",
"in",
"statements",
":",
"statement_data",
"=",
"statement",
".",
"serialize",
"(",
")",
"tag_data",
"=",
"statement_data",
".",
"pop",
"(",
"'tags'",
",",
"[",
"]",
")",
"statement_model_object",
"=",
"Statement",
"(",
"*",
"*",
"statement_data",
")",
"if",
"not",
"statement",
".",
"search_text",
":",
"statement_model_object",
".",
"search_text",
"=",
"self",
".",
"tagger",
".",
"get_bigram_pair_string",
"(",
"statement",
".",
"text",
")",
"if",
"not",
"statement",
".",
"search_in_response_to",
"and",
"statement",
".",
"in_response_to",
":",
"statement_model_object",
".",
"search_in_response_to",
"=",
"self",
".",
"tagger",
".",
"get_bigram_pair_string",
"(",
"statement",
".",
"in_response_to",
")",
"statement_model_object",
".",
"save",
"(",
")",
"tags_to_add",
"=",
"[",
"]",
"for",
"tag_name",
"in",
"tag_data",
":",
"if",
"tag_name",
"in",
"tag_cache",
":",
"tag",
"=",
"tag_cache",
"[",
"tag_name",
"]",
"else",
":",
"tag",
",",
"_",
"=",
"Tag",
".",
"objects",
".",
"get_or_create",
"(",
"name",
"=",
"tag_name",
")",
"tag_cache",
"[",
"tag_name",
"]",
"=",
"tag",
"tags_to_add",
".",
"append",
"(",
"tag",
")",
"statement_model_object",
".",
"tags",
".",
"add",
"(",
"*",
"tags_to_add",
")"
] |
Creates multiple statement entries.
|
[
"Creates",
"multiple",
"statement",
"entries",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/django_storage.py#L123-L157
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/django_storage.py
|
DjangoStorageAdapter.update
|
def update(self, statement):
"""
Update the provided statement.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
if hasattr(statement, 'id'):
statement.save()
else:
statement = Statement.objects.create(
text=statement.text,
search_text=self.tagger.get_bigram_pair_string(statement.text),
conversation=statement.conversation,
in_response_to=statement.in_response_to,
search_in_response_to=self.tagger.get_bigram_pair_string(statement.in_response_to),
created_at=statement.created_at
)
for _tag in statement.tags.all():
tag, _ = Tag.objects.get_or_create(name=_tag)
statement.tags.add(tag)
return statement
|
python
|
def update(self, statement):
"""
Update the provided statement.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
if hasattr(statement, 'id'):
statement.save()
else:
statement = Statement.objects.create(
text=statement.text,
search_text=self.tagger.get_bigram_pair_string(statement.text),
conversation=statement.conversation,
in_response_to=statement.in_response_to,
search_in_response_to=self.tagger.get_bigram_pair_string(statement.in_response_to),
created_at=statement.created_at
)
for _tag in statement.tags.all():
tag, _ = Tag.objects.get_or_create(name=_tag)
statement.tags.add(tag)
return statement
|
[
"def",
"update",
"(",
"self",
",",
"statement",
")",
":",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"Tag",
"=",
"self",
".",
"get_model",
"(",
"'tag'",
")",
"if",
"hasattr",
"(",
"statement",
",",
"'id'",
")",
":",
"statement",
".",
"save",
"(",
")",
"else",
":",
"statement",
"=",
"Statement",
".",
"objects",
".",
"create",
"(",
"text",
"=",
"statement",
".",
"text",
",",
"search_text",
"=",
"self",
".",
"tagger",
".",
"get_bigram_pair_string",
"(",
"statement",
".",
"text",
")",
",",
"conversation",
"=",
"statement",
".",
"conversation",
",",
"in_response_to",
"=",
"statement",
".",
"in_response_to",
",",
"search_in_response_to",
"=",
"self",
".",
"tagger",
".",
"get_bigram_pair_string",
"(",
"statement",
".",
"in_response_to",
")",
",",
"created_at",
"=",
"statement",
".",
"created_at",
")",
"for",
"_tag",
"in",
"statement",
".",
"tags",
".",
"all",
"(",
")",
":",
"tag",
",",
"_",
"=",
"Tag",
".",
"objects",
".",
"get_or_create",
"(",
"name",
"=",
"_tag",
")",
"statement",
".",
"tags",
".",
"add",
"(",
"tag",
")",
"return",
"statement"
] |
Update the provided statement.
|
[
"Update",
"the",
"provided",
"statement",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/django_storage.py#L159-L183
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/django_storage.py
|
DjangoStorageAdapter.get_random
|
def get_random(self):
"""
Returns a random statement from the database
"""
Statement = self.get_model('statement')
statement = Statement.objects.order_by('?').first()
if statement is None:
raise self.EmptyDatabaseException()
return statement
|
python
|
def get_random(self):
"""
Returns a random statement from the database
"""
Statement = self.get_model('statement')
statement = Statement.objects.order_by('?').first()
if statement is None:
raise self.EmptyDatabaseException()
return statement
|
[
"def",
"get_random",
"(",
"self",
")",
":",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"statement",
"=",
"Statement",
".",
"objects",
".",
"order_by",
"(",
"'?'",
")",
".",
"first",
"(",
")",
"if",
"statement",
"is",
"None",
":",
"raise",
"self",
".",
"EmptyDatabaseException",
"(",
")",
"return",
"statement"
] |
Returns a random statement from the database
|
[
"Returns",
"a",
"random",
"statement",
"from",
"the",
"database"
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/django_storage.py#L185-L196
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/django_storage.py
|
DjangoStorageAdapter.remove
|
def remove(self, statement_text):
"""
Removes the statement that matches the input text.
Removes any responses from statements if the response text matches the
input text.
"""
Statement = self.get_model('statement')
statements = Statement.objects.filter(text=statement_text)
statements.delete()
|
python
|
def remove(self, statement_text):
"""
Removes the statement that matches the input text.
Removes any responses from statements if the response text matches the
input text.
"""
Statement = self.get_model('statement')
statements = Statement.objects.filter(text=statement_text)
statements.delete()
|
[
"def",
"remove",
"(",
"self",
",",
"statement_text",
")",
":",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"statements",
"=",
"Statement",
".",
"objects",
".",
"filter",
"(",
"text",
"=",
"statement_text",
")",
"statements",
".",
"delete",
"(",
")"
] |
Removes the statement that matches the input text.
Removes any responses from statements if the response text matches the
input text.
|
[
"Removes",
"the",
"statement",
"that",
"matches",
"the",
"input",
"text",
".",
"Removes",
"any",
"responses",
"from",
"statements",
"if",
"the",
"response",
"text",
"matches",
"the",
"input",
"text",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/django_storage.py#L198-L208
|
train
|
gunthercox/ChatterBot
|
chatterbot/storage/django_storage.py
|
DjangoStorageAdapter.drop
|
def drop(self):
"""
Remove all data from the database.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
Statement.objects.all().delete()
Tag.objects.all().delete()
|
python
|
def drop(self):
"""
Remove all data from the database.
"""
Statement = self.get_model('statement')
Tag = self.get_model('tag')
Statement.objects.all().delete()
Tag.objects.all().delete()
|
[
"def",
"drop",
"(",
"self",
")",
":",
"Statement",
"=",
"self",
".",
"get_model",
"(",
"'statement'",
")",
"Tag",
"=",
"self",
".",
"get_model",
"(",
"'tag'",
")",
"Statement",
".",
"objects",
".",
"all",
"(",
")",
".",
"delete",
"(",
")",
"Tag",
".",
"objects",
".",
"all",
"(",
")",
".",
"delete",
"(",
")"
] |
Remove all data from the database.
|
[
"Remove",
"all",
"data",
"from",
"the",
"database",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/storage/django_storage.py#L210-L218
|
train
|
gunthercox/ChatterBot
|
chatterbot/preprocessors.py
|
clean_whitespace
|
def clean_whitespace(statement):
"""
Remove any consecutive whitespace characters from the statement text.
"""
import re
# Replace linebreaks and tabs with spaces
statement.text = statement.text.replace('\n', ' ').replace('\r', ' ').replace('\t', ' ')
# Remove any leeding or trailing whitespace
statement.text = statement.text.strip()
# Remove consecutive spaces
statement.text = re.sub(' +', ' ', statement.text)
return statement
|
python
|
def clean_whitespace(statement):
"""
Remove any consecutive whitespace characters from the statement text.
"""
import re
# Replace linebreaks and tabs with spaces
statement.text = statement.text.replace('\n', ' ').replace('\r', ' ').replace('\t', ' ')
# Remove any leeding or trailing whitespace
statement.text = statement.text.strip()
# Remove consecutive spaces
statement.text = re.sub(' +', ' ', statement.text)
return statement
|
[
"def",
"clean_whitespace",
"(",
"statement",
")",
":",
"import",
"re",
"# Replace linebreaks and tabs with spaces",
"statement",
".",
"text",
"=",
"statement",
".",
"text",
".",
"replace",
"(",
"'\\n'",
",",
"' '",
")",
".",
"replace",
"(",
"'\\r'",
",",
"' '",
")",
".",
"replace",
"(",
"'\\t'",
",",
"' '",
")",
"# Remove any leeding or trailing whitespace",
"statement",
".",
"text",
"=",
"statement",
".",
"text",
".",
"strip",
"(",
")",
"# Remove consecutive spaces",
"statement",
".",
"text",
"=",
"re",
".",
"sub",
"(",
"' +'",
",",
"' '",
",",
"statement",
".",
"text",
")",
"return",
"statement"
] |
Remove any consecutive whitespace characters from the statement text.
|
[
"Remove",
"any",
"consecutive",
"whitespace",
"characters",
"from",
"the",
"statement",
"text",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/preprocessors.py#L6-L21
|
train
|
gunthercox/ChatterBot
|
chatterbot/preprocessors.py
|
unescape_html
|
def unescape_html(statement):
"""
Convert escaped html characters into unescaped html characters.
For example: "<b>" becomes "<b>".
"""
import html
statement.text = html.unescape(statement.text)
return statement
|
python
|
def unescape_html(statement):
"""
Convert escaped html characters into unescaped html characters.
For example: "<b>" becomes "<b>".
"""
import html
statement.text = html.unescape(statement.text)
return statement
|
[
"def",
"unescape_html",
"(",
"statement",
")",
":",
"import",
"html",
"statement",
".",
"text",
"=",
"html",
".",
"unescape",
"(",
"statement",
".",
"text",
")",
"return",
"statement"
] |
Convert escaped html characters into unescaped html characters.
For example: "<b>" becomes "<b>".
|
[
"Convert",
"escaped",
"html",
"characters",
"into",
"unescaped",
"html",
"characters",
".",
"For",
"example",
":",
"<",
";",
"b>",
";",
"becomes",
"<b",
">",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/preprocessors.py#L24-L33
|
train
|
gunthercox/ChatterBot
|
chatterbot/preprocessors.py
|
convert_to_ascii
|
def convert_to_ascii(statement):
"""
Converts unicode characters to ASCII character equivalents.
For example: "på fédéral" becomes "pa federal".
"""
import unicodedata
text = unicodedata.normalize('NFKD', statement.text)
text = text.encode('ascii', 'ignore').decode('utf-8')
statement.text = str(text)
return statement
|
python
|
def convert_to_ascii(statement):
"""
Converts unicode characters to ASCII character equivalents.
For example: "på fédéral" becomes "pa federal".
"""
import unicodedata
text = unicodedata.normalize('NFKD', statement.text)
text = text.encode('ascii', 'ignore').decode('utf-8')
statement.text = str(text)
return statement
|
[
"def",
"convert_to_ascii",
"(",
"statement",
")",
":",
"import",
"unicodedata",
"text",
"=",
"unicodedata",
".",
"normalize",
"(",
"'NFKD'",
",",
"statement",
".",
"text",
")",
"text",
"=",
"text",
".",
"encode",
"(",
"'ascii'",
",",
"'ignore'",
")",
".",
"decode",
"(",
"'utf-8'",
")",
"statement",
".",
"text",
"=",
"str",
"(",
"text",
")",
"return",
"statement"
] |
Converts unicode characters to ASCII character equivalents.
For example: "på fédéral" becomes "pa federal".
|
[
"Converts",
"unicode",
"characters",
"to",
"ASCII",
"character",
"equivalents",
".",
"For",
"example",
":",
"på",
"fédéral",
"becomes",
"pa",
"federal",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/preprocessors.py#L36-L47
|
train
|
gunthercox/ChatterBot
|
chatterbot/parsing.py
|
convert_string_to_number
|
def convert_string_to_number(value):
"""
Convert strings to numbers
"""
if value is None:
return 1
if isinstance(value, int):
return value
if value.isdigit():
return int(value)
num_list = map(lambda s: NUMBERS[s], re.findall(numbers + '+', value.lower()))
return sum(num_list)
|
python
|
def convert_string_to_number(value):
"""
Convert strings to numbers
"""
if value is None:
return 1
if isinstance(value, int):
return value
if value.isdigit():
return int(value)
num_list = map(lambda s: NUMBERS[s], re.findall(numbers + '+', value.lower()))
return sum(num_list)
|
[
"def",
"convert_string_to_number",
"(",
"value",
")",
":",
"if",
"value",
"is",
"None",
":",
"return",
"1",
"if",
"isinstance",
"(",
"value",
",",
"int",
")",
":",
"return",
"value",
"if",
"value",
".",
"isdigit",
"(",
")",
":",
"return",
"int",
"(",
"value",
")",
"num_list",
"=",
"map",
"(",
"lambda",
"s",
":",
"NUMBERS",
"[",
"s",
"]",
",",
"re",
".",
"findall",
"(",
"numbers",
"+",
"'+'",
",",
"value",
".",
"lower",
"(",
")",
")",
")",
"return",
"sum",
"(",
"num_list",
")"
] |
Convert strings to numbers
|
[
"Convert",
"strings",
"to",
"numbers"
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/parsing.py#L506-L517
|
train
|
gunthercox/ChatterBot
|
chatterbot/parsing.py
|
convert_time_to_hour_minute
|
def convert_time_to_hour_minute(hour, minute, convention):
"""
Convert time to hour, minute
"""
if hour is None:
hour = 0
if minute is None:
minute = 0
if convention is None:
convention = 'am'
hour = int(hour)
minute = int(minute)
if convention.lower() == 'pm':
hour += 12
return {'hours': hour, 'minutes': minute}
|
python
|
def convert_time_to_hour_minute(hour, minute, convention):
"""
Convert time to hour, minute
"""
if hour is None:
hour = 0
if minute is None:
minute = 0
if convention is None:
convention = 'am'
hour = int(hour)
minute = int(minute)
if convention.lower() == 'pm':
hour += 12
return {'hours': hour, 'minutes': minute}
|
[
"def",
"convert_time_to_hour_minute",
"(",
"hour",
",",
"minute",
",",
"convention",
")",
":",
"if",
"hour",
"is",
"None",
":",
"hour",
"=",
"0",
"if",
"minute",
"is",
"None",
":",
"minute",
"=",
"0",
"if",
"convention",
"is",
"None",
":",
"convention",
"=",
"'am'",
"hour",
"=",
"int",
"(",
"hour",
")",
"minute",
"=",
"int",
"(",
"minute",
")",
"if",
"convention",
".",
"lower",
"(",
")",
"==",
"'pm'",
":",
"hour",
"+=",
"12",
"return",
"{",
"'hours'",
":",
"hour",
",",
"'minutes'",
":",
"minute",
"}"
] |
Convert time to hour, minute
|
[
"Convert",
"time",
"to",
"hour",
"minute"
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/parsing.py#L520-L537
|
train
|
gunthercox/ChatterBot
|
chatterbot/parsing.py
|
date_from_quarter
|
def date_from_quarter(base_date, ordinal, year):
"""
Extract date from quarter of a year
"""
interval = 3
month_start = interval * (ordinal - 1)
if month_start < 0:
month_start = 9
month_end = month_start + interval
if month_start == 0:
month_start = 1
return [
datetime(year, month_start, 1),
datetime(year, month_end, calendar.monthrange(year, month_end)[1])
]
|
python
|
def date_from_quarter(base_date, ordinal, year):
"""
Extract date from quarter of a year
"""
interval = 3
month_start = interval * (ordinal - 1)
if month_start < 0:
month_start = 9
month_end = month_start + interval
if month_start == 0:
month_start = 1
return [
datetime(year, month_start, 1),
datetime(year, month_end, calendar.monthrange(year, month_end)[1])
]
|
[
"def",
"date_from_quarter",
"(",
"base_date",
",",
"ordinal",
",",
"year",
")",
":",
"interval",
"=",
"3",
"month_start",
"=",
"interval",
"*",
"(",
"ordinal",
"-",
"1",
")",
"if",
"month_start",
"<",
"0",
":",
"month_start",
"=",
"9",
"month_end",
"=",
"month_start",
"+",
"interval",
"if",
"month_start",
"==",
"0",
":",
"month_start",
"=",
"1",
"return",
"[",
"datetime",
"(",
"year",
",",
"month_start",
",",
"1",
")",
",",
"datetime",
"(",
"year",
",",
"month_end",
",",
"calendar",
".",
"monthrange",
"(",
"year",
",",
"month_end",
")",
"[",
"1",
"]",
")",
"]"
] |
Extract date from quarter of a year
|
[
"Extract",
"date",
"from",
"quarter",
"of",
"a",
"year"
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/parsing.py#L540-L554
|
train
|
gunthercox/ChatterBot
|
chatterbot/parsing.py
|
date_from_relative_day
|
def date_from_relative_day(base_date, time, dow):
"""
Converts relative day to time
Ex: this tuesday, last tuesday
"""
# Reset date to start of the day
base_date = datetime(base_date.year, base_date.month, base_date.day)
time = time.lower()
dow = dow.lower()
if time == 'this' or time == 'coming':
# Else day of week
num = HASHWEEKDAYS[dow]
return this_week_day(base_date, num)
elif time == 'last' or time == 'previous':
# Else day of week
num = HASHWEEKDAYS[dow]
return previous_week_day(base_date, num)
elif time == 'next' or time == 'following':
# Else day of week
num = HASHWEEKDAYS[dow]
return next_week_day(base_date, num)
|
python
|
def date_from_relative_day(base_date, time, dow):
"""
Converts relative day to time
Ex: this tuesday, last tuesday
"""
# Reset date to start of the day
base_date = datetime(base_date.year, base_date.month, base_date.day)
time = time.lower()
dow = dow.lower()
if time == 'this' or time == 'coming':
# Else day of week
num = HASHWEEKDAYS[dow]
return this_week_day(base_date, num)
elif time == 'last' or time == 'previous':
# Else day of week
num = HASHWEEKDAYS[dow]
return previous_week_day(base_date, num)
elif time == 'next' or time == 'following':
# Else day of week
num = HASHWEEKDAYS[dow]
return next_week_day(base_date, num)
|
[
"def",
"date_from_relative_day",
"(",
"base_date",
",",
"time",
",",
"dow",
")",
":",
"# Reset date to start of the day",
"base_date",
"=",
"datetime",
"(",
"base_date",
".",
"year",
",",
"base_date",
".",
"month",
",",
"base_date",
".",
"day",
")",
"time",
"=",
"time",
".",
"lower",
"(",
")",
"dow",
"=",
"dow",
".",
"lower",
"(",
")",
"if",
"time",
"==",
"'this'",
"or",
"time",
"==",
"'coming'",
":",
"# Else day of week",
"num",
"=",
"HASHWEEKDAYS",
"[",
"dow",
"]",
"return",
"this_week_day",
"(",
"base_date",
",",
"num",
")",
"elif",
"time",
"==",
"'last'",
"or",
"time",
"==",
"'previous'",
":",
"# Else day of week",
"num",
"=",
"HASHWEEKDAYS",
"[",
"dow",
"]",
"return",
"previous_week_day",
"(",
"base_date",
",",
"num",
")",
"elif",
"time",
"==",
"'next'",
"or",
"time",
"==",
"'following'",
":",
"# Else day of week",
"num",
"=",
"HASHWEEKDAYS",
"[",
"dow",
"]",
"return",
"next_week_day",
"(",
"base_date",
",",
"num",
")"
] |
Converts relative day to time
Ex: this tuesday, last tuesday
|
[
"Converts",
"relative",
"day",
"to",
"time",
"Ex",
":",
"this",
"tuesday",
"last",
"tuesday"
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/parsing.py#L557-L577
|
train
|
gunthercox/ChatterBot
|
chatterbot/parsing.py
|
date_from_relative_week_year
|
def date_from_relative_week_year(base_date, time, dow, ordinal=1):
"""
Converts relative day to time
Eg. this tuesday, last tuesday
"""
# If there is an ordinal (next 3 weeks) => return a start and end range
# Reset date to start of the day
relative_date = datetime(base_date.year, base_date.month, base_date.day)
ord = convert_string_to_number(ordinal)
if dow in year_variations:
if time == 'this' or time == 'coming':
return datetime(relative_date.year, 1, 1)
elif time == 'last' or time == 'previous':
return datetime(relative_date.year - 1, relative_date.month, 1)
elif time == 'next' or time == 'following':
return relative_date + timedelta(ord * 365)
elif time == 'end of the':
return datetime(relative_date.year, 12, 31)
elif dow in month_variations:
if time == 'this':
return datetime(relative_date.year, relative_date.month, relative_date.day)
elif time == 'last' or time == 'previous':
return datetime(relative_date.year, relative_date.month - 1, relative_date.day)
elif time == 'next' or time == 'following':
if relative_date.month + ord >= 12:
month = relative_date.month - 1 + ord
year = relative_date.year + month // 12
month = month % 12 + 1
day = min(relative_date.day, calendar.monthrange(year, month)[1])
return datetime(year, month, day)
else:
return datetime(relative_date.year, relative_date.month + ord, relative_date.day)
elif time == 'end of the':
return datetime(
relative_date.year,
relative_date.month,
calendar.monthrange(relative_date.year, relative_date.month)[1]
)
elif dow in week_variations:
if time == 'this':
return relative_date - timedelta(days=relative_date.weekday())
elif time == 'last' or time == 'previous':
return relative_date - timedelta(weeks=1)
elif time == 'next' or time == 'following':
return relative_date + timedelta(weeks=ord)
elif time == 'end of the':
day_of_week = base_date.weekday()
return day_of_week + timedelta(days=6 - relative_date.weekday())
elif dow in day_variations:
if time == 'this':
return relative_date
elif time == 'last' or time == 'previous':
return relative_date - timedelta(days=1)
elif time == 'next' or time == 'following':
return relative_date + timedelta(days=ord)
elif time == 'end of the':
return datetime(relative_date.year, relative_date.month, relative_date.day, 23, 59, 59)
|
python
|
def date_from_relative_week_year(base_date, time, dow, ordinal=1):
"""
Converts relative day to time
Eg. this tuesday, last tuesday
"""
# If there is an ordinal (next 3 weeks) => return a start and end range
# Reset date to start of the day
relative_date = datetime(base_date.year, base_date.month, base_date.day)
ord = convert_string_to_number(ordinal)
if dow in year_variations:
if time == 'this' or time == 'coming':
return datetime(relative_date.year, 1, 1)
elif time == 'last' or time == 'previous':
return datetime(relative_date.year - 1, relative_date.month, 1)
elif time == 'next' or time == 'following':
return relative_date + timedelta(ord * 365)
elif time == 'end of the':
return datetime(relative_date.year, 12, 31)
elif dow in month_variations:
if time == 'this':
return datetime(relative_date.year, relative_date.month, relative_date.day)
elif time == 'last' or time == 'previous':
return datetime(relative_date.year, relative_date.month - 1, relative_date.day)
elif time == 'next' or time == 'following':
if relative_date.month + ord >= 12:
month = relative_date.month - 1 + ord
year = relative_date.year + month // 12
month = month % 12 + 1
day = min(relative_date.day, calendar.monthrange(year, month)[1])
return datetime(year, month, day)
else:
return datetime(relative_date.year, relative_date.month + ord, relative_date.day)
elif time == 'end of the':
return datetime(
relative_date.year,
relative_date.month,
calendar.monthrange(relative_date.year, relative_date.month)[1]
)
elif dow in week_variations:
if time == 'this':
return relative_date - timedelta(days=relative_date.weekday())
elif time == 'last' or time == 'previous':
return relative_date - timedelta(weeks=1)
elif time == 'next' or time == 'following':
return relative_date + timedelta(weeks=ord)
elif time == 'end of the':
day_of_week = base_date.weekday()
return day_of_week + timedelta(days=6 - relative_date.weekday())
elif dow in day_variations:
if time == 'this':
return relative_date
elif time == 'last' or time == 'previous':
return relative_date - timedelta(days=1)
elif time == 'next' or time == 'following':
return relative_date + timedelta(days=ord)
elif time == 'end of the':
return datetime(relative_date.year, relative_date.month, relative_date.day, 23, 59, 59)
|
[
"def",
"date_from_relative_week_year",
"(",
"base_date",
",",
"time",
",",
"dow",
",",
"ordinal",
"=",
"1",
")",
":",
"# If there is an ordinal (next 3 weeks) => return a start and end range",
"# Reset date to start of the day",
"relative_date",
"=",
"datetime",
"(",
"base_date",
".",
"year",
",",
"base_date",
".",
"month",
",",
"base_date",
".",
"day",
")",
"ord",
"=",
"convert_string_to_number",
"(",
"ordinal",
")",
"if",
"dow",
"in",
"year_variations",
":",
"if",
"time",
"==",
"'this'",
"or",
"time",
"==",
"'coming'",
":",
"return",
"datetime",
"(",
"relative_date",
".",
"year",
",",
"1",
",",
"1",
")",
"elif",
"time",
"==",
"'last'",
"or",
"time",
"==",
"'previous'",
":",
"return",
"datetime",
"(",
"relative_date",
".",
"year",
"-",
"1",
",",
"relative_date",
".",
"month",
",",
"1",
")",
"elif",
"time",
"==",
"'next'",
"or",
"time",
"==",
"'following'",
":",
"return",
"relative_date",
"+",
"timedelta",
"(",
"ord",
"*",
"365",
")",
"elif",
"time",
"==",
"'end of the'",
":",
"return",
"datetime",
"(",
"relative_date",
".",
"year",
",",
"12",
",",
"31",
")",
"elif",
"dow",
"in",
"month_variations",
":",
"if",
"time",
"==",
"'this'",
":",
"return",
"datetime",
"(",
"relative_date",
".",
"year",
",",
"relative_date",
".",
"month",
",",
"relative_date",
".",
"day",
")",
"elif",
"time",
"==",
"'last'",
"or",
"time",
"==",
"'previous'",
":",
"return",
"datetime",
"(",
"relative_date",
".",
"year",
",",
"relative_date",
".",
"month",
"-",
"1",
",",
"relative_date",
".",
"day",
")",
"elif",
"time",
"==",
"'next'",
"or",
"time",
"==",
"'following'",
":",
"if",
"relative_date",
".",
"month",
"+",
"ord",
">=",
"12",
":",
"month",
"=",
"relative_date",
".",
"month",
"-",
"1",
"+",
"ord",
"year",
"=",
"relative_date",
".",
"year",
"+",
"month",
"//",
"12",
"month",
"=",
"month",
"%",
"12",
"+",
"1",
"day",
"=",
"min",
"(",
"relative_date",
".",
"day",
",",
"calendar",
".",
"monthrange",
"(",
"year",
",",
"month",
")",
"[",
"1",
"]",
")",
"return",
"datetime",
"(",
"year",
",",
"month",
",",
"day",
")",
"else",
":",
"return",
"datetime",
"(",
"relative_date",
".",
"year",
",",
"relative_date",
".",
"month",
"+",
"ord",
",",
"relative_date",
".",
"day",
")",
"elif",
"time",
"==",
"'end of the'",
":",
"return",
"datetime",
"(",
"relative_date",
".",
"year",
",",
"relative_date",
".",
"month",
",",
"calendar",
".",
"monthrange",
"(",
"relative_date",
".",
"year",
",",
"relative_date",
".",
"month",
")",
"[",
"1",
"]",
")",
"elif",
"dow",
"in",
"week_variations",
":",
"if",
"time",
"==",
"'this'",
":",
"return",
"relative_date",
"-",
"timedelta",
"(",
"days",
"=",
"relative_date",
".",
"weekday",
"(",
")",
")",
"elif",
"time",
"==",
"'last'",
"or",
"time",
"==",
"'previous'",
":",
"return",
"relative_date",
"-",
"timedelta",
"(",
"weeks",
"=",
"1",
")",
"elif",
"time",
"==",
"'next'",
"or",
"time",
"==",
"'following'",
":",
"return",
"relative_date",
"+",
"timedelta",
"(",
"weeks",
"=",
"ord",
")",
"elif",
"time",
"==",
"'end of the'",
":",
"day_of_week",
"=",
"base_date",
".",
"weekday",
"(",
")",
"return",
"day_of_week",
"+",
"timedelta",
"(",
"days",
"=",
"6",
"-",
"relative_date",
".",
"weekday",
"(",
")",
")",
"elif",
"dow",
"in",
"day_variations",
":",
"if",
"time",
"==",
"'this'",
":",
"return",
"relative_date",
"elif",
"time",
"==",
"'last'",
"or",
"time",
"==",
"'previous'",
":",
"return",
"relative_date",
"-",
"timedelta",
"(",
"days",
"=",
"1",
")",
"elif",
"time",
"==",
"'next'",
"or",
"time",
"==",
"'following'",
":",
"return",
"relative_date",
"+",
"timedelta",
"(",
"days",
"=",
"ord",
")",
"elif",
"time",
"==",
"'end of the'",
":",
"return",
"datetime",
"(",
"relative_date",
".",
"year",
",",
"relative_date",
".",
"month",
",",
"relative_date",
".",
"day",
",",
"23",
",",
"59",
",",
"59",
")"
] |
Converts relative day to time
Eg. this tuesday, last tuesday
|
[
"Converts",
"relative",
"day",
"to",
"time",
"Eg",
".",
"this",
"tuesday",
"last",
"tuesday"
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/parsing.py#L580-L636
|
train
|
gunthercox/ChatterBot
|
chatterbot/parsing.py
|
date_from_adverb
|
def date_from_adverb(base_date, name):
"""
Convert Day adverbs to dates
Tomorrow => Date
Today => Date
"""
# Reset date to start of the day
adverb_date = datetime(base_date.year, base_date.month, base_date.day)
if name == 'today' or name == 'tonite' or name == 'tonight':
return adverb_date.today()
elif name == 'yesterday':
return adverb_date - timedelta(days=1)
elif name == 'tomorrow' or name == 'tom':
return adverb_date + timedelta(days=1)
|
python
|
def date_from_adverb(base_date, name):
"""
Convert Day adverbs to dates
Tomorrow => Date
Today => Date
"""
# Reset date to start of the day
adverb_date = datetime(base_date.year, base_date.month, base_date.day)
if name == 'today' or name == 'tonite' or name == 'tonight':
return adverb_date.today()
elif name == 'yesterday':
return adverb_date - timedelta(days=1)
elif name == 'tomorrow' or name == 'tom':
return adverb_date + timedelta(days=1)
|
[
"def",
"date_from_adverb",
"(",
"base_date",
",",
"name",
")",
":",
"# Reset date to start of the day",
"adverb_date",
"=",
"datetime",
"(",
"base_date",
".",
"year",
",",
"base_date",
".",
"month",
",",
"base_date",
".",
"day",
")",
"if",
"name",
"==",
"'today'",
"or",
"name",
"==",
"'tonite'",
"or",
"name",
"==",
"'tonight'",
":",
"return",
"adverb_date",
".",
"today",
"(",
")",
"elif",
"name",
"==",
"'yesterday'",
":",
"return",
"adverb_date",
"-",
"timedelta",
"(",
"days",
"=",
"1",
")",
"elif",
"name",
"==",
"'tomorrow'",
"or",
"name",
"==",
"'tom'",
":",
"return",
"adverb_date",
"+",
"timedelta",
"(",
"days",
"=",
"1",
")"
] |
Convert Day adverbs to dates
Tomorrow => Date
Today => Date
|
[
"Convert",
"Day",
"adverbs",
"to",
"dates",
"Tomorrow",
"=",
">",
"Date",
"Today",
"=",
">",
"Date"
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/parsing.py#L639-L652
|
train
|
gunthercox/ChatterBot
|
chatterbot/parsing.py
|
date_from_duration
|
def date_from_duration(base_date, number_as_string, unit, duration, base_time=None):
"""
Find dates from duration
Eg: 20 days from now
Currently does not support strings like "20 days from last monday".
"""
# Check if query is `2 days before yesterday` or `day before yesterday`
if base_time is not None:
base_date = date_from_adverb(base_date, base_time)
num = convert_string_to_number(number_as_string)
if unit in day_variations:
args = {'days': num}
elif unit in minute_variations:
args = {'minutes': num}
elif unit in week_variations:
args = {'weeks': num}
elif unit in month_variations:
args = {'days': 365 * num / 12}
elif unit in year_variations:
args = {'years': num}
if duration == 'ago' or duration == 'before' or duration == 'earlier':
if 'years' in args:
return datetime(base_date.year - args['years'], base_date.month, base_date.day)
return base_date - timedelta(**args)
elif duration == 'after' or duration == 'later' or duration == 'from now':
if 'years' in args:
return datetime(base_date.year + args['years'], base_date.month, base_date.day)
return base_date + timedelta(**args)
|
python
|
def date_from_duration(base_date, number_as_string, unit, duration, base_time=None):
"""
Find dates from duration
Eg: 20 days from now
Currently does not support strings like "20 days from last monday".
"""
# Check if query is `2 days before yesterday` or `day before yesterday`
if base_time is not None:
base_date = date_from_adverb(base_date, base_time)
num = convert_string_to_number(number_as_string)
if unit in day_variations:
args = {'days': num}
elif unit in minute_variations:
args = {'minutes': num}
elif unit in week_variations:
args = {'weeks': num}
elif unit in month_variations:
args = {'days': 365 * num / 12}
elif unit in year_variations:
args = {'years': num}
if duration == 'ago' or duration == 'before' or duration == 'earlier':
if 'years' in args:
return datetime(base_date.year - args['years'], base_date.month, base_date.day)
return base_date - timedelta(**args)
elif duration == 'after' or duration == 'later' or duration == 'from now':
if 'years' in args:
return datetime(base_date.year + args['years'], base_date.month, base_date.day)
return base_date + timedelta(**args)
|
[
"def",
"date_from_duration",
"(",
"base_date",
",",
"number_as_string",
",",
"unit",
",",
"duration",
",",
"base_time",
"=",
"None",
")",
":",
"# Check if query is `2 days before yesterday` or `day before yesterday`",
"if",
"base_time",
"is",
"not",
"None",
":",
"base_date",
"=",
"date_from_adverb",
"(",
"base_date",
",",
"base_time",
")",
"num",
"=",
"convert_string_to_number",
"(",
"number_as_string",
")",
"if",
"unit",
"in",
"day_variations",
":",
"args",
"=",
"{",
"'days'",
":",
"num",
"}",
"elif",
"unit",
"in",
"minute_variations",
":",
"args",
"=",
"{",
"'minutes'",
":",
"num",
"}",
"elif",
"unit",
"in",
"week_variations",
":",
"args",
"=",
"{",
"'weeks'",
":",
"num",
"}",
"elif",
"unit",
"in",
"month_variations",
":",
"args",
"=",
"{",
"'days'",
":",
"365",
"*",
"num",
"/",
"12",
"}",
"elif",
"unit",
"in",
"year_variations",
":",
"args",
"=",
"{",
"'years'",
":",
"num",
"}",
"if",
"duration",
"==",
"'ago'",
"or",
"duration",
"==",
"'before'",
"or",
"duration",
"==",
"'earlier'",
":",
"if",
"'years'",
"in",
"args",
":",
"return",
"datetime",
"(",
"base_date",
".",
"year",
"-",
"args",
"[",
"'years'",
"]",
",",
"base_date",
".",
"month",
",",
"base_date",
".",
"day",
")",
"return",
"base_date",
"-",
"timedelta",
"(",
"*",
"*",
"args",
")",
"elif",
"duration",
"==",
"'after'",
"or",
"duration",
"==",
"'later'",
"or",
"duration",
"==",
"'from now'",
":",
"if",
"'years'",
"in",
"args",
":",
"return",
"datetime",
"(",
"base_date",
".",
"year",
"+",
"args",
"[",
"'years'",
"]",
",",
"base_date",
".",
"month",
",",
"base_date",
".",
"day",
")",
"return",
"base_date",
"+",
"timedelta",
"(",
"*",
"*",
"args",
")"
] |
Find dates from duration
Eg: 20 days from now
Currently does not support strings like "20 days from last monday".
|
[
"Find",
"dates",
"from",
"duration",
"Eg",
":",
"20",
"days",
"from",
"now",
"Currently",
"does",
"not",
"support",
"strings",
"like",
"20",
"days",
"from",
"last",
"monday",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/parsing.py#L655-L682
|
train
|
gunthercox/ChatterBot
|
chatterbot/parsing.py
|
this_week_day
|
def this_week_day(base_date, weekday):
"""
Finds coming weekday
"""
day_of_week = base_date.weekday()
# If today is Tuesday and the query is `this monday`
# We should output the next_week monday
if day_of_week > weekday:
return next_week_day(base_date, weekday)
start_of_this_week = base_date - timedelta(days=day_of_week + 1)
day = start_of_this_week + timedelta(days=1)
while day.weekday() != weekday:
day = day + timedelta(days=1)
return day
|
python
|
def this_week_day(base_date, weekday):
"""
Finds coming weekday
"""
day_of_week = base_date.weekday()
# If today is Tuesday and the query is `this monday`
# We should output the next_week monday
if day_of_week > weekday:
return next_week_day(base_date, weekday)
start_of_this_week = base_date - timedelta(days=day_of_week + 1)
day = start_of_this_week + timedelta(days=1)
while day.weekday() != weekday:
day = day + timedelta(days=1)
return day
|
[
"def",
"this_week_day",
"(",
"base_date",
",",
"weekday",
")",
":",
"day_of_week",
"=",
"base_date",
".",
"weekday",
"(",
")",
"# If today is Tuesday and the query is `this monday`",
"# We should output the next_week monday",
"if",
"day_of_week",
">",
"weekday",
":",
"return",
"next_week_day",
"(",
"base_date",
",",
"weekday",
")",
"start_of_this_week",
"=",
"base_date",
"-",
"timedelta",
"(",
"days",
"=",
"day_of_week",
"+",
"1",
")",
"day",
"=",
"start_of_this_week",
"+",
"timedelta",
"(",
"days",
"=",
"1",
")",
"while",
"day",
".",
"weekday",
"(",
")",
"!=",
"weekday",
":",
"day",
"=",
"day",
"+",
"timedelta",
"(",
"days",
"=",
"1",
")",
"return",
"day"
] |
Finds coming weekday
|
[
"Finds",
"coming",
"weekday"
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/parsing.py#L685-L698
|
train
|
gunthercox/ChatterBot
|
chatterbot/parsing.py
|
previous_week_day
|
def previous_week_day(base_date, weekday):
"""
Finds previous weekday
"""
day = base_date - timedelta(days=1)
while day.weekday() != weekday:
day = day - timedelta(days=1)
return day
|
python
|
def previous_week_day(base_date, weekday):
"""
Finds previous weekday
"""
day = base_date - timedelta(days=1)
while day.weekday() != weekday:
day = day - timedelta(days=1)
return day
|
[
"def",
"previous_week_day",
"(",
"base_date",
",",
"weekday",
")",
":",
"day",
"=",
"base_date",
"-",
"timedelta",
"(",
"days",
"=",
"1",
")",
"while",
"day",
".",
"weekday",
"(",
")",
"!=",
"weekday",
":",
"day",
"=",
"day",
"-",
"timedelta",
"(",
"days",
"=",
"1",
")",
"return",
"day"
] |
Finds previous weekday
|
[
"Finds",
"previous",
"weekday"
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/parsing.py#L701-L708
|
train
|
gunthercox/ChatterBot
|
chatterbot/parsing.py
|
next_week_day
|
def next_week_day(base_date, weekday):
"""
Finds next weekday
"""
day_of_week = base_date.weekday()
end_of_this_week = base_date + timedelta(days=6 - day_of_week)
day = end_of_this_week + timedelta(days=1)
while day.weekday() != weekday:
day = day + timedelta(days=1)
return day
|
python
|
def next_week_day(base_date, weekday):
"""
Finds next weekday
"""
day_of_week = base_date.weekday()
end_of_this_week = base_date + timedelta(days=6 - day_of_week)
day = end_of_this_week + timedelta(days=1)
while day.weekday() != weekday:
day = day + timedelta(days=1)
return day
|
[
"def",
"next_week_day",
"(",
"base_date",
",",
"weekday",
")",
":",
"day_of_week",
"=",
"base_date",
".",
"weekday",
"(",
")",
"end_of_this_week",
"=",
"base_date",
"+",
"timedelta",
"(",
"days",
"=",
"6",
"-",
"day_of_week",
")",
"day",
"=",
"end_of_this_week",
"+",
"timedelta",
"(",
"days",
"=",
"1",
")",
"while",
"day",
".",
"weekday",
"(",
")",
"!=",
"weekday",
":",
"day",
"=",
"day",
"+",
"timedelta",
"(",
"days",
"=",
"1",
")",
"return",
"day"
] |
Finds next weekday
|
[
"Finds",
"next",
"weekday"
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/parsing.py#L711-L720
|
train
|
gunthercox/ChatterBot
|
chatterbot/parsing.py
|
datetime_parsing
|
def datetime_parsing(text, base_date=datetime.now()):
"""
Extract datetime objects from a string of text.
"""
matches = []
found_array = []
# Find the position in the string
for expression, function in regex:
for match in expression.finditer(text):
matches.append((match.group(), function(match, base_date), match.span()))
# Wrap the matched text with TAG element to prevent nested selections
for match, value, spans in matches:
subn = re.subn(
'(?!<TAG[^>]*?>)' + match + '(?![^<]*?</TAG>)', '<TAG>' + match + '</TAG>', text
)
text = subn[0]
is_substituted = subn[1]
if is_substituted != 0:
found_array.append((match, value, spans))
# To preserve order of the match, sort based on the start position
return sorted(found_array, key=lambda match: match and match[2][0])
|
python
|
def datetime_parsing(text, base_date=datetime.now()):
"""
Extract datetime objects from a string of text.
"""
matches = []
found_array = []
# Find the position in the string
for expression, function in regex:
for match in expression.finditer(text):
matches.append((match.group(), function(match, base_date), match.span()))
# Wrap the matched text with TAG element to prevent nested selections
for match, value, spans in matches:
subn = re.subn(
'(?!<TAG[^>]*?>)' + match + '(?![^<]*?</TAG>)', '<TAG>' + match + '</TAG>', text
)
text = subn[0]
is_substituted = subn[1]
if is_substituted != 0:
found_array.append((match, value, spans))
# To preserve order of the match, sort based on the start position
return sorted(found_array, key=lambda match: match and match[2][0])
|
[
"def",
"datetime_parsing",
"(",
"text",
",",
"base_date",
"=",
"datetime",
".",
"now",
"(",
")",
")",
":",
"matches",
"=",
"[",
"]",
"found_array",
"=",
"[",
"]",
"# Find the position in the string",
"for",
"expression",
",",
"function",
"in",
"regex",
":",
"for",
"match",
"in",
"expression",
".",
"finditer",
"(",
"text",
")",
":",
"matches",
".",
"append",
"(",
"(",
"match",
".",
"group",
"(",
")",
",",
"function",
"(",
"match",
",",
"base_date",
")",
",",
"match",
".",
"span",
"(",
")",
")",
")",
"# Wrap the matched text with TAG element to prevent nested selections",
"for",
"match",
",",
"value",
",",
"spans",
"in",
"matches",
":",
"subn",
"=",
"re",
".",
"subn",
"(",
"'(?!<TAG[^>]*?>)'",
"+",
"match",
"+",
"'(?![^<]*?</TAG>)'",
",",
"'<TAG>'",
"+",
"match",
"+",
"'</TAG>'",
",",
"text",
")",
"text",
"=",
"subn",
"[",
"0",
"]",
"is_substituted",
"=",
"subn",
"[",
"1",
"]",
"if",
"is_substituted",
"!=",
"0",
":",
"found_array",
".",
"append",
"(",
"(",
"match",
",",
"value",
",",
"spans",
")",
")",
"# To preserve order of the match, sort based on the start position",
"return",
"sorted",
"(",
"found_array",
",",
"key",
"=",
"lambda",
"match",
":",
"match",
"and",
"match",
"[",
"2",
"]",
"[",
"0",
"]",
")"
] |
Extract datetime objects from a string of text.
|
[
"Extract",
"datetime",
"objects",
"from",
"a",
"string",
"of",
"text",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/parsing.py#L723-L746
|
train
|
gunthercox/ChatterBot
|
chatterbot/search.py
|
IndexedTextSearch.search
|
def search(self, input_statement, **additional_parameters):
"""
Search for close matches to the input. Confidence scores for
subsequent results will order of increasing value.
:param input_statement: A statement.
:type input_statement: chatterbot.conversation.Statement
:param **additional_parameters: Additional parameters to be passed
to the ``filter`` method of the storage adapter when searching.
:rtype: Generator yielding one closest matching statement at a time.
"""
self.chatbot.logger.info('Beginning search for close text match')
input_search_text = input_statement.search_text
if not input_statement.search_text:
self.chatbot.logger.warn(
'No value for search_text was available on the provided input'
)
input_search_text = self.chatbot.storage.tagger.get_bigram_pair_string(
input_statement.text
)
search_parameters = {
'search_text_contains': input_search_text,
'persona_not_startswith': 'bot:',
'page_size': self.search_page_size
}
if additional_parameters:
search_parameters.update(additional_parameters)
statement_list = self.chatbot.storage.filter(**search_parameters)
closest_match = Statement(text='')
closest_match.confidence = 0
self.chatbot.logger.info('Processing search results')
# Find the closest matching known statement
for statement in statement_list:
confidence = self.compare_statements(input_statement, statement)
if confidence > closest_match.confidence:
statement.confidence = confidence
closest_match = statement
self.chatbot.logger.info('Similar text found: {} {}'.format(
closest_match.text, confidence
))
yield closest_match
|
python
|
def search(self, input_statement, **additional_parameters):
"""
Search for close matches to the input. Confidence scores for
subsequent results will order of increasing value.
:param input_statement: A statement.
:type input_statement: chatterbot.conversation.Statement
:param **additional_parameters: Additional parameters to be passed
to the ``filter`` method of the storage adapter when searching.
:rtype: Generator yielding one closest matching statement at a time.
"""
self.chatbot.logger.info('Beginning search for close text match')
input_search_text = input_statement.search_text
if not input_statement.search_text:
self.chatbot.logger.warn(
'No value for search_text was available on the provided input'
)
input_search_text = self.chatbot.storage.tagger.get_bigram_pair_string(
input_statement.text
)
search_parameters = {
'search_text_contains': input_search_text,
'persona_not_startswith': 'bot:',
'page_size': self.search_page_size
}
if additional_parameters:
search_parameters.update(additional_parameters)
statement_list = self.chatbot.storage.filter(**search_parameters)
closest_match = Statement(text='')
closest_match.confidence = 0
self.chatbot.logger.info('Processing search results')
# Find the closest matching known statement
for statement in statement_list:
confidence = self.compare_statements(input_statement, statement)
if confidence > closest_match.confidence:
statement.confidence = confidence
closest_match = statement
self.chatbot.logger.info('Similar text found: {} {}'.format(
closest_match.text, confidence
))
yield closest_match
|
[
"def",
"search",
"(",
"self",
",",
"input_statement",
",",
"*",
"*",
"additional_parameters",
")",
":",
"self",
".",
"chatbot",
".",
"logger",
".",
"info",
"(",
"'Beginning search for close text match'",
")",
"input_search_text",
"=",
"input_statement",
".",
"search_text",
"if",
"not",
"input_statement",
".",
"search_text",
":",
"self",
".",
"chatbot",
".",
"logger",
".",
"warn",
"(",
"'No value for search_text was available on the provided input'",
")",
"input_search_text",
"=",
"self",
".",
"chatbot",
".",
"storage",
".",
"tagger",
".",
"get_bigram_pair_string",
"(",
"input_statement",
".",
"text",
")",
"search_parameters",
"=",
"{",
"'search_text_contains'",
":",
"input_search_text",
",",
"'persona_not_startswith'",
":",
"'bot:'",
",",
"'page_size'",
":",
"self",
".",
"search_page_size",
"}",
"if",
"additional_parameters",
":",
"search_parameters",
".",
"update",
"(",
"additional_parameters",
")",
"statement_list",
"=",
"self",
".",
"chatbot",
".",
"storage",
".",
"filter",
"(",
"*",
"*",
"search_parameters",
")",
"closest_match",
"=",
"Statement",
"(",
"text",
"=",
"''",
")",
"closest_match",
".",
"confidence",
"=",
"0",
"self",
".",
"chatbot",
".",
"logger",
".",
"info",
"(",
"'Processing search results'",
")",
"# Find the closest matching known statement",
"for",
"statement",
"in",
"statement_list",
":",
"confidence",
"=",
"self",
".",
"compare_statements",
"(",
"input_statement",
",",
"statement",
")",
"if",
"confidence",
">",
"closest_match",
".",
"confidence",
":",
"statement",
".",
"confidence",
"=",
"confidence",
"closest_match",
"=",
"statement",
"self",
".",
"chatbot",
".",
"logger",
".",
"info",
"(",
"'Similar text found: {} {}'",
".",
"format",
"(",
"closest_match",
".",
"text",
",",
"confidence",
")",
")",
"yield",
"closest_match"
] |
Search for close matches to the input. Confidence scores for
subsequent results will order of increasing value.
:param input_statement: A statement.
:type input_statement: chatterbot.conversation.Statement
:param **additional_parameters: Additional parameters to be passed
to the ``filter`` method of the storage adapter when searching.
:rtype: Generator yielding one closest matching statement at a time.
|
[
"Search",
"for",
"close",
"matches",
"to",
"the",
"input",
".",
"Confidence",
"scores",
"for",
"subsequent",
"results",
"will",
"order",
"of",
"increasing",
"value",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/search.py#L35-L89
|
train
|
gunthercox/ChatterBot
|
examples/tkinter_gui.py
|
TkinterGUIExample.initialize
|
def initialize(self):
"""
Set window layout.
"""
self.grid()
self.respond = ttk.Button(self, text='Get Response', command=self.get_response)
self.respond.grid(column=0, row=0, sticky='nesw', padx=3, pady=3)
self.usr_input = ttk.Entry(self, state='normal')
self.usr_input.grid(column=1, row=0, sticky='nesw', padx=3, pady=3)
self.conversation_lbl = ttk.Label(self, anchor=tk.E, text='Conversation:')
self.conversation_lbl.grid(column=0, row=1, sticky='nesw', padx=3, pady=3)
self.conversation = ScrolledText.ScrolledText(self, state='disabled')
self.conversation.grid(column=0, row=2, columnspan=2, sticky='nesw', padx=3, pady=3)
|
python
|
def initialize(self):
"""
Set window layout.
"""
self.grid()
self.respond = ttk.Button(self, text='Get Response', command=self.get_response)
self.respond.grid(column=0, row=0, sticky='nesw', padx=3, pady=3)
self.usr_input = ttk.Entry(self, state='normal')
self.usr_input.grid(column=1, row=0, sticky='nesw', padx=3, pady=3)
self.conversation_lbl = ttk.Label(self, anchor=tk.E, text='Conversation:')
self.conversation_lbl.grid(column=0, row=1, sticky='nesw', padx=3, pady=3)
self.conversation = ScrolledText.ScrolledText(self, state='disabled')
self.conversation.grid(column=0, row=2, columnspan=2, sticky='nesw', padx=3, pady=3)
|
[
"def",
"initialize",
"(",
"self",
")",
":",
"self",
".",
"grid",
"(",
")",
"self",
".",
"respond",
"=",
"ttk",
".",
"Button",
"(",
"self",
",",
"text",
"=",
"'Get Response'",
",",
"command",
"=",
"self",
".",
"get_response",
")",
"self",
".",
"respond",
".",
"grid",
"(",
"column",
"=",
"0",
",",
"row",
"=",
"0",
",",
"sticky",
"=",
"'nesw'",
",",
"padx",
"=",
"3",
",",
"pady",
"=",
"3",
")",
"self",
".",
"usr_input",
"=",
"ttk",
".",
"Entry",
"(",
"self",
",",
"state",
"=",
"'normal'",
")",
"self",
".",
"usr_input",
".",
"grid",
"(",
"column",
"=",
"1",
",",
"row",
"=",
"0",
",",
"sticky",
"=",
"'nesw'",
",",
"padx",
"=",
"3",
",",
"pady",
"=",
"3",
")",
"self",
".",
"conversation_lbl",
"=",
"ttk",
".",
"Label",
"(",
"self",
",",
"anchor",
"=",
"tk",
".",
"E",
",",
"text",
"=",
"'Conversation:'",
")",
"self",
".",
"conversation_lbl",
".",
"grid",
"(",
"column",
"=",
"0",
",",
"row",
"=",
"1",
",",
"sticky",
"=",
"'nesw'",
",",
"padx",
"=",
"3",
",",
"pady",
"=",
"3",
")",
"self",
".",
"conversation",
"=",
"ScrolledText",
".",
"ScrolledText",
"(",
"self",
",",
"state",
"=",
"'disabled'",
")",
"self",
".",
"conversation",
".",
"grid",
"(",
"column",
"=",
"0",
",",
"row",
"=",
"2",
",",
"columnspan",
"=",
"2",
",",
"sticky",
"=",
"'nesw'",
",",
"padx",
"=",
"3",
",",
"pady",
"=",
"3",
")"
] |
Set window layout.
|
[
"Set",
"window",
"layout",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/examples/tkinter_gui.py#L33-L49
|
train
|
gunthercox/ChatterBot
|
examples/tkinter_gui.py
|
TkinterGUIExample.get_response
|
def get_response(self):
"""
Get a response from the chatbot and display it.
"""
user_input = self.usr_input.get()
self.usr_input.delete(0, tk.END)
response = self.chatbot.get_response(user_input)
self.conversation['state'] = 'normal'
self.conversation.insert(
tk.END, "Human: " + user_input + "\n" + "ChatBot: " + str(response.text) + "\n"
)
self.conversation['state'] = 'disabled'
time.sleep(0.5)
|
python
|
def get_response(self):
"""
Get a response from the chatbot and display it.
"""
user_input = self.usr_input.get()
self.usr_input.delete(0, tk.END)
response = self.chatbot.get_response(user_input)
self.conversation['state'] = 'normal'
self.conversation.insert(
tk.END, "Human: " + user_input + "\n" + "ChatBot: " + str(response.text) + "\n"
)
self.conversation['state'] = 'disabled'
time.sleep(0.5)
|
[
"def",
"get_response",
"(",
"self",
")",
":",
"user_input",
"=",
"self",
".",
"usr_input",
".",
"get",
"(",
")",
"self",
".",
"usr_input",
".",
"delete",
"(",
"0",
",",
"tk",
".",
"END",
")",
"response",
"=",
"self",
".",
"chatbot",
".",
"get_response",
"(",
"user_input",
")",
"self",
".",
"conversation",
"[",
"'state'",
"]",
"=",
"'normal'",
"self",
".",
"conversation",
".",
"insert",
"(",
"tk",
".",
"END",
",",
"\"Human: \"",
"+",
"user_input",
"+",
"\"\\n\"",
"+",
"\"ChatBot: \"",
"+",
"str",
"(",
"response",
".",
"text",
")",
"+",
"\"\\n\"",
")",
"self",
".",
"conversation",
"[",
"'state'",
"]",
"=",
"'disabled'",
"time",
".",
"sleep",
"(",
"0.5",
")"
] |
Get a response from the chatbot and display it.
|
[
"Get",
"a",
"response",
"from",
"the",
"chatbot",
"and",
"display",
"it",
"."
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/examples/tkinter_gui.py#L51-L66
|
train
|
gunthercox/ChatterBot
|
chatterbot/ext/django_chatterbot/abstract_models.py
|
AbstractBaseStatement.add_tags
|
def add_tags(self, *tags):
"""
Add a list of strings to the statement as tags.
(Overrides the method from StatementMixin)
"""
for _tag in tags:
self.tags.get_or_create(name=_tag)
|
python
|
def add_tags(self, *tags):
"""
Add a list of strings to the statement as tags.
(Overrides the method from StatementMixin)
"""
for _tag in tags:
self.tags.get_or_create(name=_tag)
|
[
"def",
"add_tags",
"(",
"self",
",",
"*",
"tags",
")",
":",
"for",
"_tag",
"in",
"tags",
":",
"self",
".",
"tags",
".",
"get_or_create",
"(",
"name",
"=",
"_tag",
")"
] |
Add a list of strings to the statement as tags.
(Overrides the method from StatementMixin)
|
[
"Add",
"a",
"list",
"of",
"strings",
"to",
"the",
"statement",
"as",
"tags",
".",
"(",
"Overrides",
"the",
"method",
"from",
"StatementMixin",
")"
] |
1a03dcb45cba7bdc24d3db5e750582e0cb1518e2
|
https://github.com/gunthercox/ChatterBot/blob/1a03dcb45cba7bdc24d3db5e750582e0cb1518e2/chatterbot/ext/django_chatterbot/abstract_models.py#L110-L116
|
train
|
tensorflow/lucid
|
lucid/scratch/web/svelte.py
|
SvelteComponent
|
def SvelteComponent(name, path):
"""Display svelte components in iPython.
Args:
name: name of svelte component (must match component filename when built)
path: path to compile svelte .js file or source svelte .html file.
(If html file, we try to call svelte and build the file.)
Returns:
A function mapping data to a rendered svelte component in ipython.
"""
if path[-3:] == ".js":
js_path = path
elif path[-5:] == ".html":
print("Trying to build svelte component from html...")
js_path = build_svelte(path)
js_content = read(js_path, mode='r')
def inner(data):
id_str = js_id(name)
html = _template \
.replace("$js", js_content) \
.replace("$name", name) \
.replace("$data", json.dumps(data)) \
.replace("$id", id_str)
_display_html(html)
return inner
|
python
|
def SvelteComponent(name, path):
"""Display svelte components in iPython.
Args:
name: name of svelte component (must match component filename when built)
path: path to compile svelte .js file or source svelte .html file.
(If html file, we try to call svelte and build the file.)
Returns:
A function mapping data to a rendered svelte component in ipython.
"""
if path[-3:] == ".js":
js_path = path
elif path[-5:] == ".html":
print("Trying to build svelte component from html...")
js_path = build_svelte(path)
js_content = read(js_path, mode='r')
def inner(data):
id_str = js_id(name)
html = _template \
.replace("$js", js_content) \
.replace("$name", name) \
.replace("$data", json.dumps(data)) \
.replace("$id", id_str)
_display_html(html)
return inner
|
[
"def",
"SvelteComponent",
"(",
"name",
",",
"path",
")",
":",
"if",
"path",
"[",
"-",
"3",
":",
"]",
"==",
"\".js\"",
":",
"js_path",
"=",
"path",
"elif",
"path",
"[",
"-",
"5",
":",
"]",
"==",
"\".html\"",
":",
"print",
"(",
"\"Trying to build svelte component from html...\"",
")",
"js_path",
"=",
"build_svelte",
"(",
"path",
")",
"js_content",
"=",
"read",
"(",
"js_path",
",",
"mode",
"=",
"'r'",
")",
"def",
"inner",
"(",
"data",
")",
":",
"id_str",
"=",
"js_id",
"(",
"name",
")",
"html",
"=",
"_template",
".",
"replace",
"(",
"\"$js\"",
",",
"js_content",
")",
".",
"replace",
"(",
"\"$name\"",
",",
"name",
")",
".",
"replace",
"(",
"\"$data\"",
",",
"json",
".",
"dumps",
"(",
"data",
")",
")",
".",
"replace",
"(",
"\"$id\"",
",",
"id_str",
")",
"_display_html",
"(",
"html",
")",
"return",
"inner"
] |
Display svelte components in iPython.
Args:
name: name of svelte component (must match component filename when built)
path: path to compile svelte .js file or source svelte .html file.
(If html file, we try to call svelte and build the file.)
Returns:
A function mapping data to a rendered svelte component in ipython.
|
[
"Display",
"svelte",
"components",
"in",
"iPython",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/scratch/web/svelte.py#L43-L68
|
train
|
tensorflow/lucid
|
lucid/misc/io/saving.py
|
save_json
|
def save_json(object, handle, indent=2):
"""Save object as json on CNS."""
obj_json = json.dumps(object, indent=indent, cls=NumpyJSONEncoder)
handle.write(obj_json)
|
python
|
def save_json(object, handle, indent=2):
"""Save object as json on CNS."""
obj_json = json.dumps(object, indent=indent, cls=NumpyJSONEncoder)
handle.write(obj_json)
|
[
"def",
"save_json",
"(",
"object",
",",
"handle",
",",
"indent",
"=",
"2",
")",
":",
"obj_json",
"=",
"json",
".",
"dumps",
"(",
"object",
",",
"indent",
"=",
"indent",
",",
"cls",
"=",
"NumpyJSONEncoder",
")",
"handle",
".",
"write",
"(",
"obj_json",
")"
] |
Save object as json on CNS.
|
[
"Save",
"object",
"as",
"json",
"on",
"CNS",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/misc/io/saving.py#L58-L61
|
train
|
tensorflow/lucid
|
lucid/misc/io/saving.py
|
save_npz
|
def save_npz(object, handle):
"""Save dict of numpy array as npz file."""
# there is a bug where savez doesn't actually accept a file handle.
log.warning("Saving npz files currently only works locally. :/")
path = handle.name
handle.close()
if type(object) is dict:
np.savez(path, **object)
elif type(object) is list:
np.savez(path, *object)
else:
log.warning("Saving non dict or list as npz file, did you maybe want npy?")
np.savez(path, object)
|
python
|
def save_npz(object, handle):
"""Save dict of numpy array as npz file."""
# there is a bug where savez doesn't actually accept a file handle.
log.warning("Saving npz files currently only works locally. :/")
path = handle.name
handle.close()
if type(object) is dict:
np.savez(path, **object)
elif type(object) is list:
np.savez(path, *object)
else:
log.warning("Saving non dict or list as npz file, did you maybe want npy?")
np.savez(path, object)
|
[
"def",
"save_npz",
"(",
"object",
",",
"handle",
")",
":",
"# there is a bug where savez doesn't actually accept a file handle.",
"log",
".",
"warning",
"(",
"\"Saving npz files currently only works locally. :/\"",
")",
"path",
"=",
"handle",
".",
"name",
"handle",
".",
"close",
"(",
")",
"if",
"type",
"(",
"object",
")",
"is",
"dict",
":",
"np",
".",
"savez",
"(",
"path",
",",
"*",
"*",
"object",
")",
"elif",
"type",
"(",
"object",
")",
"is",
"list",
":",
"np",
".",
"savez",
"(",
"path",
",",
"*",
"object",
")",
"else",
":",
"log",
".",
"warning",
"(",
"\"Saving non dict or list as npz file, did you maybe want npy?\"",
")",
"np",
".",
"savez",
"(",
"path",
",",
"object",
")"
] |
Save dict of numpy array as npz file.
|
[
"Save",
"dict",
"of",
"numpy",
"array",
"as",
"npz",
"file",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/misc/io/saving.py#L69-L81
|
train
|
tensorflow/lucid
|
lucid/misc/io/saving.py
|
save_img
|
def save_img(object, handle, **kwargs):
"""Save numpy array as image file on CNS."""
if isinstance(object, np.ndarray):
normalized = _normalize_array(object)
object = PIL.Image.fromarray(normalized)
if isinstance(object, PIL.Image.Image):
object.save(handle, **kwargs) # will infer format from handle's url ext.
else:
raise ValueError("Can only save_img for numpy arrays or PIL.Images!")
|
python
|
def save_img(object, handle, **kwargs):
"""Save numpy array as image file on CNS."""
if isinstance(object, np.ndarray):
normalized = _normalize_array(object)
object = PIL.Image.fromarray(normalized)
if isinstance(object, PIL.Image.Image):
object.save(handle, **kwargs) # will infer format from handle's url ext.
else:
raise ValueError("Can only save_img for numpy arrays or PIL.Images!")
|
[
"def",
"save_img",
"(",
"object",
",",
"handle",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"isinstance",
"(",
"object",
",",
"np",
".",
"ndarray",
")",
":",
"normalized",
"=",
"_normalize_array",
"(",
"object",
")",
"object",
"=",
"PIL",
".",
"Image",
".",
"fromarray",
"(",
"normalized",
")",
"if",
"isinstance",
"(",
"object",
",",
"PIL",
".",
"Image",
".",
"Image",
")",
":",
"object",
".",
"save",
"(",
"handle",
",",
"*",
"*",
"kwargs",
")",
"# will infer format from handle's url ext.",
"else",
":",
"raise",
"ValueError",
"(",
"\"Can only save_img for numpy arrays or PIL.Images!\"",
")"
] |
Save numpy array as image file on CNS.
|
[
"Save",
"numpy",
"array",
"as",
"image",
"file",
"on",
"CNS",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/misc/io/saving.py#L84-L94
|
train
|
tensorflow/lucid
|
lucid/misc/io/saving.py
|
save
|
def save(thing, url_or_handle, **kwargs):
"""Save object to file on CNS.
File format is inferred from path. Use save_img(), save_npy(), or save_json()
if you need to force a particular format.
Args:
obj: object to save.
path: CNS path.
Raises:
RuntimeError: If file extension not supported.
"""
is_handle = hasattr(url_or_handle, "write") and hasattr(url_or_handle, "name")
if is_handle:
_, ext = os.path.splitext(url_or_handle.name)
else:
_, ext = os.path.splitext(url_or_handle)
if not ext:
raise RuntimeError("No extension in URL: " + url_or_handle)
if ext in savers:
saver = savers[ext]
if is_handle:
saver(thing, url_or_handle, **kwargs)
else:
with write_handle(url_or_handle) as handle:
saver(thing, handle, **kwargs)
else:
saver_names = [(key, fn.__name__) for (key, fn) in savers.items()]
message = "Unknown extension '{}', supports {}."
raise ValueError(message.format(ext, saver_names))
|
python
|
def save(thing, url_or_handle, **kwargs):
"""Save object to file on CNS.
File format is inferred from path. Use save_img(), save_npy(), or save_json()
if you need to force a particular format.
Args:
obj: object to save.
path: CNS path.
Raises:
RuntimeError: If file extension not supported.
"""
is_handle = hasattr(url_or_handle, "write") and hasattr(url_or_handle, "name")
if is_handle:
_, ext = os.path.splitext(url_or_handle.name)
else:
_, ext = os.path.splitext(url_or_handle)
if not ext:
raise RuntimeError("No extension in URL: " + url_or_handle)
if ext in savers:
saver = savers[ext]
if is_handle:
saver(thing, url_or_handle, **kwargs)
else:
with write_handle(url_or_handle) as handle:
saver(thing, handle, **kwargs)
else:
saver_names = [(key, fn.__name__) for (key, fn) in savers.items()]
message = "Unknown extension '{}', supports {}."
raise ValueError(message.format(ext, saver_names))
|
[
"def",
"save",
"(",
"thing",
",",
"url_or_handle",
",",
"*",
"*",
"kwargs",
")",
":",
"is_handle",
"=",
"hasattr",
"(",
"url_or_handle",
",",
"\"write\"",
")",
"and",
"hasattr",
"(",
"url_or_handle",
",",
"\"name\"",
")",
"if",
"is_handle",
":",
"_",
",",
"ext",
"=",
"os",
".",
"path",
".",
"splitext",
"(",
"url_or_handle",
".",
"name",
")",
"else",
":",
"_",
",",
"ext",
"=",
"os",
".",
"path",
".",
"splitext",
"(",
"url_or_handle",
")",
"if",
"not",
"ext",
":",
"raise",
"RuntimeError",
"(",
"\"No extension in URL: \"",
"+",
"url_or_handle",
")",
"if",
"ext",
"in",
"savers",
":",
"saver",
"=",
"savers",
"[",
"ext",
"]",
"if",
"is_handle",
":",
"saver",
"(",
"thing",
",",
"url_or_handle",
",",
"*",
"*",
"kwargs",
")",
"else",
":",
"with",
"write_handle",
"(",
"url_or_handle",
")",
"as",
"handle",
":",
"saver",
"(",
"thing",
",",
"handle",
",",
"*",
"*",
"kwargs",
")",
"else",
":",
"saver_names",
"=",
"[",
"(",
"key",
",",
"fn",
".",
"__name__",
")",
"for",
"(",
"key",
",",
"fn",
")",
"in",
"savers",
".",
"items",
"(",
")",
"]",
"message",
"=",
"\"Unknown extension '{}', supports {}.\"",
"raise",
"ValueError",
"(",
"message",
".",
"format",
"(",
"ext",
",",
"saver_names",
")",
")"
] |
Save object to file on CNS.
File format is inferred from path. Use save_img(), save_npy(), or save_json()
if you need to force a particular format.
Args:
obj: object to save.
path: CNS path.
Raises:
RuntimeError: If file extension not supported.
|
[
"Save",
"object",
"to",
"file",
"on",
"CNS",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/misc/io/saving.py#L135-L166
|
train
|
tensorflow/lucid
|
lucid/misc/gl/meshutil.py
|
frustum
|
def frustum(left, right, bottom, top, znear, zfar):
"""Create view frustum matrix."""
assert right != left
assert bottom != top
assert znear != zfar
M = np.zeros((4, 4), dtype=np.float32)
M[0, 0] = +2.0 * znear / (right - left)
M[2, 0] = (right + left) / (right - left)
M[1, 1] = +2.0 * znear / (top - bottom)
M[3, 1] = (top + bottom) / (top - bottom)
M[2, 2] = -(zfar + znear) / (zfar - znear)
M[3, 2] = -2.0 * znear * zfar / (zfar - znear)
M[2, 3] = -1.0
return M
|
python
|
def frustum(left, right, bottom, top, znear, zfar):
"""Create view frustum matrix."""
assert right != left
assert bottom != top
assert znear != zfar
M = np.zeros((4, 4), dtype=np.float32)
M[0, 0] = +2.0 * znear / (right - left)
M[2, 0] = (right + left) / (right - left)
M[1, 1] = +2.0 * znear / (top - bottom)
M[3, 1] = (top + bottom) / (top - bottom)
M[2, 2] = -(zfar + znear) / (zfar - znear)
M[3, 2] = -2.0 * znear * zfar / (zfar - znear)
M[2, 3] = -1.0
return M
|
[
"def",
"frustum",
"(",
"left",
",",
"right",
",",
"bottom",
",",
"top",
",",
"znear",
",",
"zfar",
")",
":",
"assert",
"right",
"!=",
"left",
"assert",
"bottom",
"!=",
"top",
"assert",
"znear",
"!=",
"zfar",
"M",
"=",
"np",
".",
"zeros",
"(",
"(",
"4",
",",
"4",
")",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
"M",
"[",
"0",
",",
"0",
"]",
"=",
"+",
"2.0",
"*",
"znear",
"/",
"(",
"right",
"-",
"left",
")",
"M",
"[",
"2",
",",
"0",
"]",
"=",
"(",
"right",
"+",
"left",
")",
"/",
"(",
"right",
"-",
"left",
")",
"M",
"[",
"1",
",",
"1",
"]",
"=",
"+",
"2.0",
"*",
"znear",
"/",
"(",
"top",
"-",
"bottom",
")",
"M",
"[",
"3",
",",
"1",
"]",
"=",
"(",
"top",
"+",
"bottom",
")",
"/",
"(",
"top",
"-",
"bottom",
")",
"M",
"[",
"2",
",",
"2",
"]",
"=",
"-",
"(",
"zfar",
"+",
"znear",
")",
"/",
"(",
"zfar",
"-",
"znear",
")",
"M",
"[",
"3",
",",
"2",
"]",
"=",
"-",
"2.0",
"*",
"znear",
"*",
"zfar",
"/",
"(",
"zfar",
"-",
"znear",
")",
"M",
"[",
"2",
",",
"3",
"]",
"=",
"-",
"1.0",
"return",
"M"
] |
Create view frustum matrix.
|
[
"Create",
"view",
"frustum",
"matrix",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/misc/gl/meshutil.py#L8-L22
|
train
|
tensorflow/lucid
|
lucid/misc/gl/meshutil.py
|
anorm
|
def anorm(x, axis=None, keepdims=False):
"""Compute L2 norms alogn specified axes."""
return np.sqrt((x*x).sum(axis=axis, keepdims=keepdims))
|
python
|
def anorm(x, axis=None, keepdims=False):
"""Compute L2 norms alogn specified axes."""
return np.sqrt((x*x).sum(axis=axis, keepdims=keepdims))
|
[
"def",
"anorm",
"(",
"x",
",",
"axis",
"=",
"None",
",",
"keepdims",
"=",
"False",
")",
":",
"return",
"np",
".",
"sqrt",
"(",
"(",
"x",
"*",
"x",
")",
".",
"sum",
"(",
"axis",
"=",
"axis",
",",
"keepdims",
"=",
"keepdims",
")",
")"
] |
Compute L2 norms alogn specified axes.
|
[
"Compute",
"L2",
"norms",
"alogn",
"specified",
"axes",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/misc/gl/meshutil.py#L33-L35
|
train
|
tensorflow/lucid
|
lucid/misc/gl/meshutil.py
|
normalize
|
def normalize(v, axis=None, eps=1e-10):
"""L2 Normalize along specified axes."""
return v / max(anorm(v, axis=axis, keepdims=True), eps)
|
python
|
def normalize(v, axis=None, eps=1e-10):
"""L2 Normalize along specified axes."""
return v / max(anorm(v, axis=axis, keepdims=True), eps)
|
[
"def",
"normalize",
"(",
"v",
",",
"axis",
"=",
"None",
",",
"eps",
"=",
"1e-10",
")",
":",
"return",
"v",
"/",
"max",
"(",
"anorm",
"(",
"v",
",",
"axis",
"=",
"axis",
",",
"keepdims",
"=",
"True",
")",
",",
"eps",
")"
] |
L2 Normalize along specified axes.
|
[
"L2",
"Normalize",
"along",
"specified",
"axes",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/misc/gl/meshutil.py#L38-L40
|
train
|
tensorflow/lucid
|
lucid/misc/gl/meshutil.py
|
lookat
|
def lookat(eye, target=[0, 0, 0], up=[0, 1, 0]):
"""Generate LookAt modelview matrix."""
eye = np.float32(eye)
forward = normalize(target - eye)
side = normalize(np.cross(forward, up))
up = np.cross(side, forward)
M = np.eye(4, dtype=np.float32)
R = M[:3, :3]
R[:] = [side, up, -forward]
M[:3, 3] = -R.dot(eye)
return M
|
python
|
def lookat(eye, target=[0, 0, 0], up=[0, 1, 0]):
"""Generate LookAt modelview matrix."""
eye = np.float32(eye)
forward = normalize(target - eye)
side = normalize(np.cross(forward, up))
up = np.cross(side, forward)
M = np.eye(4, dtype=np.float32)
R = M[:3, :3]
R[:] = [side, up, -forward]
M[:3, 3] = -R.dot(eye)
return M
|
[
"def",
"lookat",
"(",
"eye",
",",
"target",
"=",
"[",
"0",
",",
"0",
",",
"0",
"]",
",",
"up",
"=",
"[",
"0",
",",
"1",
",",
"0",
"]",
")",
":",
"eye",
"=",
"np",
".",
"float32",
"(",
"eye",
")",
"forward",
"=",
"normalize",
"(",
"target",
"-",
"eye",
")",
"side",
"=",
"normalize",
"(",
"np",
".",
"cross",
"(",
"forward",
",",
"up",
")",
")",
"up",
"=",
"np",
".",
"cross",
"(",
"side",
",",
"forward",
")",
"M",
"=",
"np",
".",
"eye",
"(",
"4",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
"R",
"=",
"M",
"[",
":",
"3",
",",
":",
"3",
"]",
"R",
"[",
":",
"]",
"=",
"[",
"side",
",",
"up",
",",
"-",
"forward",
"]",
"M",
"[",
":",
"3",
",",
"3",
"]",
"=",
"-",
"R",
".",
"dot",
"(",
"eye",
")",
"return",
"M"
] |
Generate LookAt modelview matrix.
|
[
"Generate",
"LookAt",
"modelview",
"matrix",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/misc/gl/meshutil.py#L43-L53
|
train
|
tensorflow/lucid
|
lucid/misc/gl/meshutil.py
|
sample_view
|
def sample_view(min_dist, max_dist=None):
'''Sample random camera position.
Sample origin directed camera position in given distance
range from the origin. ModelView matrix is returned.
'''
if max_dist is None:
max_dist = min_dist
dist = np.random.uniform(min_dist, max_dist)
eye = np.random.normal(size=3)
eye = normalize(eye)*dist
return lookat(eye)
|
python
|
def sample_view(min_dist, max_dist=None):
'''Sample random camera position.
Sample origin directed camera position in given distance
range from the origin. ModelView matrix is returned.
'''
if max_dist is None:
max_dist = min_dist
dist = np.random.uniform(min_dist, max_dist)
eye = np.random.normal(size=3)
eye = normalize(eye)*dist
return lookat(eye)
|
[
"def",
"sample_view",
"(",
"min_dist",
",",
"max_dist",
"=",
"None",
")",
":",
"if",
"max_dist",
"is",
"None",
":",
"max_dist",
"=",
"min_dist",
"dist",
"=",
"np",
".",
"random",
".",
"uniform",
"(",
"min_dist",
",",
"max_dist",
")",
"eye",
"=",
"np",
".",
"random",
".",
"normal",
"(",
"size",
"=",
"3",
")",
"eye",
"=",
"normalize",
"(",
"eye",
")",
"*",
"dist",
"return",
"lookat",
"(",
"eye",
")"
] |
Sample random camera position.
Sample origin directed camera position in given distance
range from the origin. ModelView matrix is returned.
|
[
"Sample",
"random",
"camera",
"position",
".",
"Sample",
"origin",
"directed",
"camera",
"position",
"in",
"given",
"distance",
"range",
"from",
"the",
"origin",
".",
"ModelView",
"matrix",
"is",
"returned",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/misc/gl/meshutil.py#L56-L67
|
train
|
tensorflow/lucid
|
lucid/misc/gl/meshutil.py
|
_parse_vertex_tuple
|
def _parse_vertex_tuple(s):
"""Parse vertex indices in '/' separated form (like 'i/j/k', 'i//k' ...)."""
vt = [0, 0, 0]
for i, c in enumerate(s.split('/')):
if c:
vt[i] = int(c)
return tuple(vt)
|
python
|
def _parse_vertex_tuple(s):
"""Parse vertex indices in '/' separated form (like 'i/j/k', 'i//k' ...)."""
vt = [0, 0, 0]
for i, c in enumerate(s.split('/')):
if c:
vt[i] = int(c)
return tuple(vt)
|
[
"def",
"_parse_vertex_tuple",
"(",
"s",
")",
":",
"vt",
"=",
"[",
"0",
",",
"0",
",",
"0",
"]",
"for",
"i",
",",
"c",
"in",
"enumerate",
"(",
"s",
".",
"split",
"(",
"'/'",
")",
")",
":",
"if",
"c",
":",
"vt",
"[",
"i",
"]",
"=",
"int",
"(",
"c",
")",
"return",
"tuple",
"(",
"vt",
")"
] |
Parse vertex indices in '/' separated form (like 'i/j/k', 'i//k' ...).
|
[
"Parse",
"vertex",
"indices",
"in",
"/",
"separated",
"form",
"(",
"like",
"i",
"/",
"j",
"/",
"k",
"i",
"//",
"k",
"...",
")",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/misc/gl/meshutil.py#L78-L84
|
train
|
tensorflow/lucid
|
lucid/misc/gl/meshutil.py
|
_unify_rows
|
def _unify_rows(a):
"""Unify lengths of each row of a."""
lens = np.fromiter(map(len, a), np.int32)
if not (lens[0] == lens).all():
out = np.zeros((len(a), lens.max()), np.float32)
for i, row in enumerate(a):
out[i, :lens[i]] = row
else:
out = np.float32(a)
return out
|
python
|
def _unify_rows(a):
"""Unify lengths of each row of a."""
lens = np.fromiter(map(len, a), np.int32)
if not (lens[0] == lens).all():
out = np.zeros((len(a), lens.max()), np.float32)
for i, row in enumerate(a):
out[i, :lens[i]] = row
else:
out = np.float32(a)
return out
|
[
"def",
"_unify_rows",
"(",
"a",
")",
":",
"lens",
"=",
"np",
".",
"fromiter",
"(",
"map",
"(",
"len",
",",
"a",
")",
",",
"np",
".",
"int32",
")",
"if",
"not",
"(",
"lens",
"[",
"0",
"]",
"==",
"lens",
")",
".",
"all",
"(",
")",
":",
"out",
"=",
"np",
".",
"zeros",
"(",
"(",
"len",
"(",
"a",
")",
",",
"lens",
".",
"max",
"(",
")",
")",
",",
"np",
".",
"float32",
")",
"for",
"i",
",",
"row",
"in",
"enumerate",
"(",
"a",
")",
":",
"out",
"[",
"i",
",",
":",
"lens",
"[",
"i",
"]",
"]",
"=",
"row",
"else",
":",
"out",
"=",
"np",
".",
"float32",
"(",
"a",
")",
"return",
"out"
] |
Unify lengths of each row of a.
|
[
"Unify",
"lengths",
"of",
"each",
"row",
"of",
"a",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/misc/gl/meshutil.py#L87-L96
|
train
|
tensorflow/lucid
|
lucid/misc/gl/meshutil.py
|
load_obj
|
def load_obj(fn):
"""Load 3d mesh form .obj' file.
Args:
fn: Input file name or file-like object.
Returns:
dictionary with the following keys (some of which may be missing):
position: np.float32, (n, 3) array, vertex positions
uv: np.float32, (n, 2) array, vertex uv coordinates
normal: np.float32, (n, 3) array, vertex uv normals
face: np.int32, (k*3,) traingular face indices
"""
position = [np.zeros(3, dtype=np.float32)]
normal = [np.zeros(3, dtype=np.float32)]
uv = [np.zeros(2, dtype=np.float32)]
tuple2idx = OrderedDict()
trinagle_indices = []
input_file = open(fn) if isinstance(fn, str) else fn
for line in input_file:
line = line.strip()
if not line or line[0] == '#':
continue
line = line.split(' ', 1)
tag = line[0]
if len(line) > 1:
line = line[1]
else:
line = ''
if tag == 'v':
position.append(np.fromstring(line, sep=' '))
elif tag == 'vt':
uv.append(np.fromstring(line, sep=' '))
elif tag == 'vn':
normal.append(np.fromstring(line, sep=' '))
elif tag == 'f':
output_face_indices = []
for chunk in line.split():
# tuple order: pos_idx, uv_idx, normal_idx
vt = _parse_vertex_tuple(chunk)
if vt not in tuple2idx: # create a new output vertex?
tuple2idx[vt] = len(tuple2idx)
output_face_indices.append(tuple2idx[vt])
# generate face triangles
for i in range(1, len(output_face_indices)-1):
for vi in [0, i, i+1]:
trinagle_indices.append(output_face_indices[vi])
outputs = {}
outputs['face'] = np.int32(trinagle_indices)
pos_idx, uv_idx, normal_idx = np.int32(list(tuple2idx)).T
if np.any(pos_idx):
outputs['position'] = _unify_rows(position)[pos_idx]
if np.any(uv_idx):
outputs['uv'] = _unify_rows(uv)[uv_idx]
if np.any(normal_idx):
outputs['normal'] = _unify_rows(normal)[normal_idx]
return outputs
|
python
|
def load_obj(fn):
"""Load 3d mesh form .obj' file.
Args:
fn: Input file name or file-like object.
Returns:
dictionary with the following keys (some of which may be missing):
position: np.float32, (n, 3) array, vertex positions
uv: np.float32, (n, 2) array, vertex uv coordinates
normal: np.float32, (n, 3) array, vertex uv normals
face: np.int32, (k*3,) traingular face indices
"""
position = [np.zeros(3, dtype=np.float32)]
normal = [np.zeros(3, dtype=np.float32)]
uv = [np.zeros(2, dtype=np.float32)]
tuple2idx = OrderedDict()
trinagle_indices = []
input_file = open(fn) if isinstance(fn, str) else fn
for line in input_file:
line = line.strip()
if not line or line[0] == '#':
continue
line = line.split(' ', 1)
tag = line[0]
if len(line) > 1:
line = line[1]
else:
line = ''
if tag == 'v':
position.append(np.fromstring(line, sep=' '))
elif tag == 'vt':
uv.append(np.fromstring(line, sep=' '))
elif tag == 'vn':
normal.append(np.fromstring(line, sep=' '))
elif tag == 'f':
output_face_indices = []
for chunk in line.split():
# tuple order: pos_idx, uv_idx, normal_idx
vt = _parse_vertex_tuple(chunk)
if vt not in tuple2idx: # create a new output vertex?
tuple2idx[vt] = len(tuple2idx)
output_face_indices.append(tuple2idx[vt])
# generate face triangles
for i in range(1, len(output_face_indices)-1):
for vi in [0, i, i+1]:
trinagle_indices.append(output_face_indices[vi])
outputs = {}
outputs['face'] = np.int32(trinagle_indices)
pos_idx, uv_idx, normal_idx = np.int32(list(tuple2idx)).T
if np.any(pos_idx):
outputs['position'] = _unify_rows(position)[pos_idx]
if np.any(uv_idx):
outputs['uv'] = _unify_rows(uv)[uv_idx]
if np.any(normal_idx):
outputs['normal'] = _unify_rows(normal)[normal_idx]
return outputs
|
[
"def",
"load_obj",
"(",
"fn",
")",
":",
"position",
"=",
"[",
"np",
".",
"zeros",
"(",
"3",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
"]",
"normal",
"=",
"[",
"np",
".",
"zeros",
"(",
"3",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
"]",
"uv",
"=",
"[",
"np",
".",
"zeros",
"(",
"2",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
"]",
"tuple2idx",
"=",
"OrderedDict",
"(",
")",
"trinagle_indices",
"=",
"[",
"]",
"input_file",
"=",
"open",
"(",
"fn",
")",
"if",
"isinstance",
"(",
"fn",
",",
"str",
")",
"else",
"fn",
"for",
"line",
"in",
"input_file",
":",
"line",
"=",
"line",
".",
"strip",
"(",
")",
"if",
"not",
"line",
"or",
"line",
"[",
"0",
"]",
"==",
"'#'",
":",
"continue",
"line",
"=",
"line",
".",
"split",
"(",
"' '",
",",
"1",
")",
"tag",
"=",
"line",
"[",
"0",
"]",
"if",
"len",
"(",
"line",
")",
">",
"1",
":",
"line",
"=",
"line",
"[",
"1",
"]",
"else",
":",
"line",
"=",
"''",
"if",
"tag",
"==",
"'v'",
":",
"position",
".",
"append",
"(",
"np",
".",
"fromstring",
"(",
"line",
",",
"sep",
"=",
"' '",
")",
")",
"elif",
"tag",
"==",
"'vt'",
":",
"uv",
".",
"append",
"(",
"np",
".",
"fromstring",
"(",
"line",
",",
"sep",
"=",
"' '",
")",
")",
"elif",
"tag",
"==",
"'vn'",
":",
"normal",
".",
"append",
"(",
"np",
".",
"fromstring",
"(",
"line",
",",
"sep",
"=",
"' '",
")",
")",
"elif",
"tag",
"==",
"'f'",
":",
"output_face_indices",
"=",
"[",
"]",
"for",
"chunk",
"in",
"line",
".",
"split",
"(",
")",
":",
"# tuple order: pos_idx, uv_idx, normal_idx",
"vt",
"=",
"_parse_vertex_tuple",
"(",
"chunk",
")",
"if",
"vt",
"not",
"in",
"tuple2idx",
":",
"# create a new output vertex?",
"tuple2idx",
"[",
"vt",
"]",
"=",
"len",
"(",
"tuple2idx",
")",
"output_face_indices",
".",
"append",
"(",
"tuple2idx",
"[",
"vt",
"]",
")",
"# generate face triangles",
"for",
"i",
"in",
"range",
"(",
"1",
",",
"len",
"(",
"output_face_indices",
")",
"-",
"1",
")",
":",
"for",
"vi",
"in",
"[",
"0",
",",
"i",
",",
"i",
"+",
"1",
"]",
":",
"trinagle_indices",
".",
"append",
"(",
"output_face_indices",
"[",
"vi",
"]",
")",
"outputs",
"=",
"{",
"}",
"outputs",
"[",
"'face'",
"]",
"=",
"np",
".",
"int32",
"(",
"trinagle_indices",
")",
"pos_idx",
",",
"uv_idx",
",",
"normal_idx",
"=",
"np",
".",
"int32",
"(",
"list",
"(",
"tuple2idx",
")",
")",
".",
"T",
"if",
"np",
".",
"any",
"(",
"pos_idx",
")",
":",
"outputs",
"[",
"'position'",
"]",
"=",
"_unify_rows",
"(",
"position",
")",
"[",
"pos_idx",
"]",
"if",
"np",
".",
"any",
"(",
"uv_idx",
")",
":",
"outputs",
"[",
"'uv'",
"]",
"=",
"_unify_rows",
"(",
"uv",
")",
"[",
"uv_idx",
"]",
"if",
"np",
".",
"any",
"(",
"normal_idx",
")",
":",
"outputs",
"[",
"'normal'",
"]",
"=",
"_unify_rows",
"(",
"normal",
")",
"[",
"normal_idx",
"]",
"return",
"outputs"
] |
Load 3d mesh form .obj' file.
Args:
fn: Input file name or file-like object.
Returns:
dictionary with the following keys (some of which may be missing):
position: np.float32, (n, 3) array, vertex positions
uv: np.float32, (n, 2) array, vertex uv coordinates
normal: np.float32, (n, 3) array, vertex uv normals
face: np.int32, (k*3,) traingular face indices
|
[
"Load",
"3d",
"mesh",
"form",
".",
"obj",
"file",
".",
"Args",
":",
"fn",
":",
"Input",
"file",
"name",
"or",
"file",
"-",
"like",
"object",
".",
"Returns",
":",
"dictionary",
"with",
"the",
"following",
"keys",
"(",
"some",
"of",
"which",
"may",
"be",
"missing",
")",
":",
"position",
":",
"np",
".",
"float32",
"(",
"n",
"3",
")",
"array",
"vertex",
"positions",
"uv",
":",
"np",
".",
"float32",
"(",
"n",
"2",
")",
"array",
"vertex",
"uv",
"coordinates",
"normal",
":",
"np",
".",
"float32",
"(",
"n",
"3",
")",
"array",
"vertex",
"uv",
"normals",
"face",
":",
"np",
".",
"int32",
"(",
"k",
"*",
"3",
")",
"traingular",
"face",
"indices"
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/misc/gl/meshutil.py#L99-L158
|
train
|
tensorflow/lucid
|
lucid/misc/gl/meshutil.py
|
normalize_mesh
|
def normalize_mesh(mesh):
'''Scale mesh to fit into -1..1 cube'''
mesh = dict(mesh)
pos = mesh['position'][:,:3].copy()
pos -= (pos.max(0)+pos.min(0)) / 2.0
pos /= np.abs(pos).max()
mesh['position'] = pos
return mesh
|
python
|
def normalize_mesh(mesh):
'''Scale mesh to fit into -1..1 cube'''
mesh = dict(mesh)
pos = mesh['position'][:,:3].copy()
pos -= (pos.max(0)+pos.min(0)) / 2.0
pos /= np.abs(pos).max()
mesh['position'] = pos
return mesh
|
[
"def",
"normalize_mesh",
"(",
"mesh",
")",
":",
"mesh",
"=",
"dict",
"(",
"mesh",
")",
"pos",
"=",
"mesh",
"[",
"'position'",
"]",
"[",
":",
",",
":",
"3",
"]",
".",
"copy",
"(",
")",
"pos",
"-=",
"(",
"pos",
".",
"max",
"(",
"0",
")",
"+",
"pos",
".",
"min",
"(",
"0",
")",
")",
"/",
"2.0",
"pos",
"/=",
"np",
".",
"abs",
"(",
"pos",
")",
".",
"max",
"(",
")",
"mesh",
"[",
"'position'",
"]",
"=",
"pos",
"return",
"mesh"
] |
Scale mesh to fit into -1..1 cube
|
[
"Scale",
"mesh",
"to",
"fit",
"into",
"-",
"1",
"..",
"1",
"cube"
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/misc/gl/meshutil.py#L161-L168
|
train
|
tensorflow/lucid
|
lucid/modelzoo/vision_base.py
|
Layer.activations
|
def activations(self):
"""Loads sampled activations, which requires network access."""
if self._activations is None:
self._activations = _get_aligned_activations(self)
return self._activations
|
python
|
def activations(self):
"""Loads sampled activations, which requires network access."""
if self._activations is None:
self._activations = _get_aligned_activations(self)
return self._activations
|
[
"def",
"activations",
"(",
"self",
")",
":",
"if",
"self",
".",
"_activations",
"is",
"None",
":",
"self",
".",
"_activations",
"=",
"_get_aligned_activations",
"(",
"self",
")",
"return",
"self",
".",
"_activations"
] |
Loads sampled activations, which requires network access.
|
[
"Loads",
"sampled",
"activations",
"which",
"requires",
"network",
"access",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/modelzoo/vision_base.py#L71-L75
|
train
|
tensorflow/lucid
|
lucid/modelzoo/vision_base.py
|
Model.create_input
|
def create_input(self, t_input=None, forget_xy_shape=True):
"""Create input tensor."""
if t_input is None:
t_input = tf.placeholder(tf.float32, self.image_shape)
t_prep_input = t_input
if len(t_prep_input.shape) == 3:
t_prep_input = tf.expand_dims(t_prep_input, 0)
if forget_xy_shape:
t_prep_input = model_util.forget_xy(t_prep_input)
if hasattr(self, "is_BGR") and self.is_BGR is True:
t_prep_input = tf.reverse(t_prep_input, [-1])
lo, hi = self.image_value_range
t_prep_input = lo + t_prep_input * (hi - lo)
return t_input, t_prep_input
|
python
|
def create_input(self, t_input=None, forget_xy_shape=True):
"""Create input tensor."""
if t_input is None:
t_input = tf.placeholder(tf.float32, self.image_shape)
t_prep_input = t_input
if len(t_prep_input.shape) == 3:
t_prep_input = tf.expand_dims(t_prep_input, 0)
if forget_xy_shape:
t_prep_input = model_util.forget_xy(t_prep_input)
if hasattr(self, "is_BGR") and self.is_BGR is True:
t_prep_input = tf.reverse(t_prep_input, [-1])
lo, hi = self.image_value_range
t_prep_input = lo + t_prep_input * (hi - lo)
return t_input, t_prep_input
|
[
"def",
"create_input",
"(",
"self",
",",
"t_input",
"=",
"None",
",",
"forget_xy_shape",
"=",
"True",
")",
":",
"if",
"t_input",
"is",
"None",
":",
"t_input",
"=",
"tf",
".",
"placeholder",
"(",
"tf",
".",
"float32",
",",
"self",
".",
"image_shape",
")",
"t_prep_input",
"=",
"t_input",
"if",
"len",
"(",
"t_prep_input",
".",
"shape",
")",
"==",
"3",
":",
"t_prep_input",
"=",
"tf",
".",
"expand_dims",
"(",
"t_prep_input",
",",
"0",
")",
"if",
"forget_xy_shape",
":",
"t_prep_input",
"=",
"model_util",
".",
"forget_xy",
"(",
"t_prep_input",
")",
"if",
"hasattr",
"(",
"self",
",",
"\"is_BGR\"",
")",
"and",
"self",
".",
"is_BGR",
"is",
"True",
":",
"t_prep_input",
"=",
"tf",
".",
"reverse",
"(",
"t_prep_input",
",",
"[",
"-",
"1",
"]",
")",
"lo",
",",
"hi",
"=",
"self",
".",
"image_value_range",
"t_prep_input",
"=",
"lo",
"+",
"t_prep_input",
"*",
"(",
"hi",
"-",
"lo",
")",
"return",
"t_input",
",",
"t_prep_input"
] |
Create input tensor.
|
[
"Create",
"input",
"tensor",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/modelzoo/vision_base.py#L161-L174
|
train
|
tensorflow/lucid
|
lucid/modelzoo/vision_base.py
|
Model.import_graph
|
def import_graph(self, t_input=None, scope='import', forget_xy_shape=True):
"""Import model GraphDef into the current graph."""
graph = tf.get_default_graph()
assert graph.unique_name(scope, False) == scope, (
'Scope "%s" already exists. Provide explicit scope names when '
'importing multiple instances of the model.') % scope
t_input, t_prep_input = self.create_input(t_input, forget_xy_shape)
tf.import_graph_def(
self.graph_def, {self.input_name: t_prep_input}, name=scope)
self.post_import(scope)
|
python
|
def import_graph(self, t_input=None, scope='import', forget_xy_shape=True):
"""Import model GraphDef into the current graph."""
graph = tf.get_default_graph()
assert graph.unique_name(scope, False) == scope, (
'Scope "%s" already exists. Provide explicit scope names when '
'importing multiple instances of the model.') % scope
t_input, t_prep_input = self.create_input(t_input, forget_xy_shape)
tf.import_graph_def(
self.graph_def, {self.input_name: t_prep_input}, name=scope)
self.post_import(scope)
|
[
"def",
"import_graph",
"(",
"self",
",",
"t_input",
"=",
"None",
",",
"scope",
"=",
"'import'",
",",
"forget_xy_shape",
"=",
"True",
")",
":",
"graph",
"=",
"tf",
".",
"get_default_graph",
"(",
")",
"assert",
"graph",
".",
"unique_name",
"(",
"scope",
",",
"False",
")",
"==",
"scope",
",",
"(",
"'Scope \"%s\" already exists. Provide explicit scope names when '",
"'importing multiple instances of the model.'",
")",
"%",
"scope",
"t_input",
",",
"t_prep_input",
"=",
"self",
".",
"create_input",
"(",
"t_input",
",",
"forget_xy_shape",
")",
"tf",
".",
"import_graph_def",
"(",
"self",
".",
"graph_def",
",",
"{",
"self",
".",
"input_name",
":",
"t_prep_input",
"}",
",",
"name",
"=",
"scope",
")",
"self",
".",
"post_import",
"(",
"scope",
")"
] |
Import model GraphDef into the current graph.
|
[
"Import",
"model",
"GraphDef",
"into",
"the",
"current",
"graph",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/modelzoo/vision_base.py#L176-L185
|
train
|
tensorflow/lucid
|
lucid/recipes/activation_atlas/layout.py
|
normalize_layout
|
def normalize_layout(layout, min_percentile=1, max_percentile=99, relative_margin=0.1):
"""Removes outliers and scales layout to between [0,1]."""
# compute percentiles
mins = np.percentile(layout, min_percentile, axis=(0))
maxs = np.percentile(layout, max_percentile, axis=(0))
# add margins
mins -= relative_margin * (maxs - mins)
maxs += relative_margin * (maxs - mins)
# `clip` broadcasts, `[None]`s added only for readability
clipped = np.clip(layout, mins, maxs)
# embed within [0,1] along both axes
clipped -= clipped.min(axis=0)
clipped /= clipped.max(axis=0)
return clipped
|
python
|
def normalize_layout(layout, min_percentile=1, max_percentile=99, relative_margin=0.1):
"""Removes outliers and scales layout to between [0,1]."""
# compute percentiles
mins = np.percentile(layout, min_percentile, axis=(0))
maxs = np.percentile(layout, max_percentile, axis=(0))
# add margins
mins -= relative_margin * (maxs - mins)
maxs += relative_margin * (maxs - mins)
# `clip` broadcasts, `[None]`s added only for readability
clipped = np.clip(layout, mins, maxs)
# embed within [0,1] along both axes
clipped -= clipped.min(axis=0)
clipped /= clipped.max(axis=0)
return clipped
|
[
"def",
"normalize_layout",
"(",
"layout",
",",
"min_percentile",
"=",
"1",
",",
"max_percentile",
"=",
"99",
",",
"relative_margin",
"=",
"0.1",
")",
":",
"# compute percentiles",
"mins",
"=",
"np",
".",
"percentile",
"(",
"layout",
",",
"min_percentile",
",",
"axis",
"=",
"(",
"0",
")",
")",
"maxs",
"=",
"np",
".",
"percentile",
"(",
"layout",
",",
"max_percentile",
",",
"axis",
"=",
"(",
"0",
")",
")",
"# add margins",
"mins",
"-=",
"relative_margin",
"*",
"(",
"maxs",
"-",
"mins",
")",
"maxs",
"+=",
"relative_margin",
"*",
"(",
"maxs",
"-",
"mins",
")",
"# `clip` broadcasts, `[None]`s added only for readability",
"clipped",
"=",
"np",
".",
"clip",
"(",
"layout",
",",
"mins",
",",
"maxs",
")",
"# embed within [0,1] along both axes",
"clipped",
"-=",
"clipped",
".",
"min",
"(",
"axis",
"=",
"0",
")",
"clipped",
"/=",
"clipped",
".",
"max",
"(",
"axis",
"=",
"0",
")",
"return",
"clipped"
] |
Removes outliers and scales layout to between [0,1].
|
[
"Removes",
"outliers",
"and",
"scales",
"layout",
"to",
"between",
"[",
"0",
"1",
"]",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/recipes/activation_atlas/layout.py#L25-L43
|
train
|
tensorflow/lucid
|
lucid/recipes/activation_atlas/layout.py
|
aligned_umap
|
def aligned_umap(activations, umap_options={}, normalize=True, verbose=False):
"""`activations` can be a list of ndarrays. In that case a list of layouts is returned."""
umap_defaults = dict(
n_components=2, n_neighbors=50, min_dist=0.05, verbose=verbose, metric="cosine"
)
umap_defaults.update(umap_options)
# if passed a list of activations, we combine them and later split the layouts
if type(activations) is list or type(activations) is tuple:
num_activation_groups = len(activations)
combined_activations = np.concatenate(activations)
else:
num_activation_groups = 1
combined_activations = activations
try:
layout = UMAP(**umap_defaults).fit_transform(combined_activations)
except (RecursionError, SystemError) as exception:
log.error("UMAP failed to fit these activations. We're not yet sure why this sometimes occurs.")
raise ValueError("UMAP failed to fit activations: %s", exception)
if normalize:
layout = normalize_layout(layout)
if num_activation_groups > 1:
layouts = np.split(layout, num_activation_groups, axis=0)
return layouts
else:
return layout
|
python
|
def aligned_umap(activations, umap_options={}, normalize=True, verbose=False):
"""`activations` can be a list of ndarrays. In that case a list of layouts is returned."""
umap_defaults = dict(
n_components=2, n_neighbors=50, min_dist=0.05, verbose=verbose, metric="cosine"
)
umap_defaults.update(umap_options)
# if passed a list of activations, we combine them and later split the layouts
if type(activations) is list or type(activations) is tuple:
num_activation_groups = len(activations)
combined_activations = np.concatenate(activations)
else:
num_activation_groups = 1
combined_activations = activations
try:
layout = UMAP(**umap_defaults).fit_transform(combined_activations)
except (RecursionError, SystemError) as exception:
log.error("UMAP failed to fit these activations. We're not yet sure why this sometimes occurs.")
raise ValueError("UMAP failed to fit activations: %s", exception)
if normalize:
layout = normalize_layout(layout)
if num_activation_groups > 1:
layouts = np.split(layout, num_activation_groups, axis=0)
return layouts
else:
return layout
|
[
"def",
"aligned_umap",
"(",
"activations",
",",
"umap_options",
"=",
"{",
"}",
",",
"normalize",
"=",
"True",
",",
"verbose",
"=",
"False",
")",
":",
"umap_defaults",
"=",
"dict",
"(",
"n_components",
"=",
"2",
",",
"n_neighbors",
"=",
"50",
",",
"min_dist",
"=",
"0.05",
",",
"verbose",
"=",
"verbose",
",",
"metric",
"=",
"\"cosine\"",
")",
"umap_defaults",
".",
"update",
"(",
"umap_options",
")",
"# if passed a list of activations, we combine them and later split the layouts",
"if",
"type",
"(",
"activations",
")",
"is",
"list",
"or",
"type",
"(",
"activations",
")",
"is",
"tuple",
":",
"num_activation_groups",
"=",
"len",
"(",
"activations",
")",
"combined_activations",
"=",
"np",
".",
"concatenate",
"(",
"activations",
")",
"else",
":",
"num_activation_groups",
"=",
"1",
"combined_activations",
"=",
"activations",
"try",
":",
"layout",
"=",
"UMAP",
"(",
"*",
"*",
"umap_defaults",
")",
".",
"fit_transform",
"(",
"combined_activations",
")",
"except",
"(",
"RecursionError",
",",
"SystemError",
")",
"as",
"exception",
":",
"log",
".",
"error",
"(",
"\"UMAP failed to fit these activations. We're not yet sure why this sometimes occurs.\"",
")",
"raise",
"ValueError",
"(",
"\"UMAP failed to fit activations: %s\"",
",",
"exception",
")",
"if",
"normalize",
":",
"layout",
"=",
"normalize_layout",
"(",
"layout",
")",
"if",
"num_activation_groups",
">",
"1",
":",
"layouts",
"=",
"np",
".",
"split",
"(",
"layout",
",",
"num_activation_groups",
",",
"axis",
"=",
"0",
")",
"return",
"layouts",
"else",
":",
"return",
"layout"
] |
`activations` can be a list of ndarrays. In that case a list of layouts is returned.
|
[
"activations",
"can",
"be",
"a",
"list",
"of",
"ndarrays",
".",
"In",
"that",
"case",
"a",
"list",
"of",
"layouts",
"is",
"returned",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/recipes/activation_atlas/layout.py#L46-L74
|
train
|
tensorflow/lucid
|
lucid/scratch/atlas_pipeline/render_tile.py
|
render_tile
|
def render_tile(cells, ti, tj, render, params, metadata, layout, summary):
"""
Render each cell in the tile and stitch it into a single image
"""
image_size = params["cell_size"] * params["n_tile"]
tile = Image.new("RGB", (image_size, image_size), (255,255,255))
keys = cells.keys()
for i,key in enumerate(keys):
print("cell", i+1, "/", len(keys), end='\r')
cell_image = render(cells[key], params, metadata, layout, summary)
# stitch this rendering into the tile image
ci = key[0] % params["n_tile"]
cj = key[1] % params["n_tile"]
xmin = ci*params["cell_size"]
ymin = cj*params["cell_size"]
xmax = (ci+1)*params["cell_size"]
ymax = (cj+1)*params["cell_size"]
if params.get("scale_density", False):
density = len(cells[key]["gi"])
# scale = density/summary["max_density"]
scale = math.log(density)/(math.log(summary["max_density"]) or 1)
owidth = xmax - xmin
width = int(round(owidth * scale))
if(width < 1):
width = 1
offsetL = int(round((owidth - width)/2))
offsetR = owidth - width - offsetL # handle odd numbers
# print("\n")
# print("width", width, offsetL, offsetR)
box = [xmin + offsetL, ymin + offsetL, xmax - offsetR, ymax - offsetR]
resample = params.get("scale_type", Image.NEAREST)
cell_image = cell_image.resize(size=(width,width), resample=resample)
# print(cell_image)
else:
box = [xmin, ymin, xmax, ymax]
# print("box", box)
tile.paste(cell_image, box)
print("\n")
return tile
|
python
|
def render_tile(cells, ti, tj, render, params, metadata, layout, summary):
"""
Render each cell in the tile and stitch it into a single image
"""
image_size = params["cell_size"] * params["n_tile"]
tile = Image.new("RGB", (image_size, image_size), (255,255,255))
keys = cells.keys()
for i,key in enumerate(keys):
print("cell", i+1, "/", len(keys), end='\r')
cell_image = render(cells[key], params, metadata, layout, summary)
# stitch this rendering into the tile image
ci = key[0] % params["n_tile"]
cj = key[1] % params["n_tile"]
xmin = ci*params["cell_size"]
ymin = cj*params["cell_size"]
xmax = (ci+1)*params["cell_size"]
ymax = (cj+1)*params["cell_size"]
if params.get("scale_density", False):
density = len(cells[key]["gi"])
# scale = density/summary["max_density"]
scale = math.log(density)/(math.log(summary["max_density"]) or 1)
owidth = xmax - xmin
width = int(round(owidth * scale))
if(width < 1):
width = 1
offsetL = int(round((owidth - width)/2))
offsetR = owidth - width - offsetL # handle odd numbers
# print("\n")
# print("width", width, offsetL, offsetR)
box = [xmin + offsetL, ymin + offsetL, xmax - offsetR, ymax - offsetR]
resample = params.get("scale_type", Image.NEAREST)
cell_image = cell_image.resize(size=(width,width), resample=resample)
# print(cell_image)
else:
box = [xmin, ymin, xmax, ymax]
# print("box", box)
tile.paste(cell_image, box)
print("\n")
return tile
|
[
"def",
"render_tile",
"(",
"cells",
",",
"ti",
",",
"tj",
",",
"render",
",",
"params",
",",
"metadata",
",",
"layout",
",",
"summary",
")",
":",
"image_size",
"=",
"params",
"[",
"\"cell_size\"",
"]",
"*",
"params",
"[",
"\"n_tile\"",
"]",
"tile",
"=",
"Image",
".",
"new",
"(",
"\"RGB\"",
",",
"(",
"image_size",
",",
"image_size",
")",
",",
"(",
"255",
",",
"255",
",",
"255",
")",
")",
"keys",
"=",
"cells",
".",
"keys",
"(",
")",
"for",
"i",
",",
"key",
"in",
"enumerate",
"(",
"keys",
")",
":",
"print",
"(",
"\"cell\"",
",",
"i",
"+",
"1",
",",
"\"/\"",
",",
"len",
"(",
"keys",
")",
",",
"end",
"=",
"'\\r'",
")",
"cell_image",
"=",
"render",
"(",
"cells",
"[",
"key",
"]",
",",
"params",
",",
"metadata",
",",
"layout",
",",
"summary",
")",
"# stitch this rendering into the tile image",
"ci",
"=",
"key",
"[",
"0",
"]",
"%",
"params",
"[",
"\"n_tile\"",
"]",
"cj",
"=",
"key",
"[",
"1",
"]",
"%",
"params",
"[",
"\"n_tile\"",
"]",
"xmin",
"=",
"ci",
"*",
"params",
"[",
"\"cell_size\"",
"]",
"ymin",
"=",
"cj",
"*",
"params",
"[",
"\"cell_size\"",
"]",
"xmax",
"=",
"(",
"ci",
"+",
"1",
")",
"*",
"params",
"[",
"\"cell_size\"",
"]",
"ymax",
"=",
"(",
"cj",
"+",
"1",
")",
"*",
"params",
"[",
"\"cell_size\"",
"]",
"if",
"params",
".",
"get",
"(",
"\"scale_density\"",
",",
"False",
")",
":",
"density",
"=",
"len",
"(",
"cells",
"[",
"key",
"]",
"[",
"\"gi\"",
"]",
")",
"# scale = density/summary[\"max_density\"]",
"scale",
"=",
"math",
".",
"log",
"(",
"density",
")",
"/",
"(",
"math",
".",
"log",
"(",
"summary",
"[",
"\"max_density\"",
"]",
")",
"or",
"1",
")",
"owidth",
"=",
"xmax",
"-",
"xmin",
"width",
"=",
"int",
"(",
"round",
"(",
"owidth",
"*",
"scale",
")",
")",
"if",
"(",
"width",
"<",
"1",
")",
":",
"width",
"=",
"1",
"offsetL",
"=",
"int",
"(",
"round",
"(",
"(",
"owidth",
"-",
"width",
")",
"/",
"2",
")",
")",
"offsetR",
"=",
"owidth",
"-",
"width",
"-",
"offsetL",
"# handle odd numbers",
"# print(\"\\n\")",
"# print(\"width\", width, offsetL, offsetR)",
"box",
"=",
"[",
"xmin",
"+",
"offsetL",
",",
"ymin",
"+",
"offsetL",
",",
"xmax",
"-",
"offsetR",
",",
"ymax",
"-",
"offsetR",
"]",
"resample",
"=",
"params",
".",
"get",
"(",
"\"scale_type\"",
",",
"Image",
".",
"NEAREST",
")",
"cell_image",
"=",
"cell_image",
".",
"resize",
"(",
"size",
"=",
"(",
"width",
",",
"width",
")",
",",
"resample",
"=",
"resample",
")",
"# print(cell_image)",
"else",
":",
"box",
"=",
"[",
"xmin",
",",
"ymin",
",",
"xmax",
",",
"ymax",
"]",
"# print(\"box\", box)",
"tile",
".",
"paste",
"(",
"cell_image",
",",
"box",
")",
"print",
"(",
"\"\\n\"",
")",
"return",
"tile"
] |
Render each cell in the tile and stitch it into a single image
|
[
"Render",
"each",
"cell",
"in",
"the",
"tile",
"and",
"stitch",
"it",
"into",
"a",
"single",
"image"
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/scratch/atlas_pipeline/render_tile.py#L11-L51
|
train
|
tensorflow/lucid
|
lucid/scratch/atlas_pipeline/render_tile.py
|
aggregate_tile
|
def aggregate_tile(cells, ti, tj, aggregate, params, metadata, layout, summary):
"""
Call the user defined aggregation function on each cell and combine into a single json object
"""
tile = []
keys = cells.keys()
for i,key in enumerate(keys):
print("cell", i+1, "/", len(keys), end='\r')
cell_json = aggregate(cells[key], params, metadata, layout, summary)
tile.append({"aggregate":cell_json, "i":int(key[0]), "j":int(key[1])})
return tile
|
python
|
def aggregate_tile(cells, ti, tj, aggregate, params, metadata, layout, summary):
"""
Call the user defined aggregation function on each cell and combine into a single json object
"""
tile = []
keys = cells.keys()
for i,key in enumerate(keys):
print("cell", i+1, "/", len(keys), end='\r')
cell_json = aggregate(cells[key], params, metadata, layout, summary)
tile.append({"aggregate":cell_json, "i":int(key[0]), "j":int(key[1])})
return tile
|
[
"def",
"aggregate_tile",
"(",
"cells",
",",
"ti",
",",
"tj",
",",
"aggregate",
",",
"params",
",",
"metadata",
",",
"layout",
",",
"summary",
")",
":",
"tile",
"=",
"[",
"]",
"keys",
"=",
"cells",
".",
"keys",
"(",
")",
"for",
"i",
",",
"key",
"in",
"enumerate",
"(",
"keys",
")",
":",
"print",
"(",
"\"cell\"",
",",
"i",
"+",
"1",
",",
"\"/\"",
",",
"len",
"(",
"keys",
")",
",",
"end",
"=",
"'\\r'",
")",
"cell_json",
"=",
"aggregate",
"(",
"cells",
"[",
"key",
"]",
",",
"params",
",",
"metadata",
",",
"layout",
",",
"summary",
")",
"tile",
".",
"append",
"(",
"{",
"\"aggregate\"",
":",
"cell_json",
",",
"\"i\"",
":",
"int",
"(",
"key",
"[",
"0",
"]",
")",
",",
"\"j\"",
":",
"int",
"(",
"key",
"[",
"1",
"]",
")",
"}",
")",
"return",
"tile"
] |
Call the user defined aggregation function on each cell and combine into a single json object
|
[
"Call",
"the",
"user",
"defined",
"aggregation",
"function",
"on",
"each",
"cell",
"and",
"combine",
"into",
"a",
"single",
"json",
"object"
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/scratch/atlas_pipeline/render_tile.py#L54-L64
|
train
|
tensorflow/lucid
|
lucid/misc/gl/glcontext.py
|
create_opengl_context
|
def create_opengl_context(surface_size=(640, 480)):
"""Create offscreen OpenGL context and make it current.
Users are expected to directly use EGL API in case more advanced
context management is required.
Args:
surface_size: (width, height), size of the offscreen rendering surface.
"""
egl_display = egl.eglGetDisplay(egl.EGL_DEFAULT_DISPLAY)
major, minor = egl.EGLint(), egl.EGLint()
egl.eglInitialize(egl_display, pointer(major), pointer(minor))
config_attribs = [
egl.EGL_SURFACE_TYPE, egl.EGL_PBUFFER_BIT, egl.EGL_BLUE_SIZE, 8,
egl.EGL_GREEN_SIZE, 8, egl.EGL_RED_SIZE, 8, egl.EGL_DEPTH_SIZE, 24,
egl.EGL_RENDERABLE_TYPE, egl.EGL_OPENGL_BIT, egl.EGL_NONE
]
config_attribs = (egl.EGLint * len(config_attribs))(*config_attribs)
num_configs = egl.EGLint()
egl_cfg = egl.EGLConfig()
egl.eglChooseConfig(egl_display, config_attribs, pointer(egl_cfg), 1,
pointer(num_configs))
width, height = surface_size
pbuffer_attribs = [
egl.EGL_WIDTH,
width,
egl.EGL_HEIGHT,
height,
egl.EGL_NONE,
]
pbuffer_attribs = (egl.EGLint * len(pbuffer_attribs))(*pbuffer_attribs)
egl_surf = egl.eglCreatePbufferSurface(egl_display, egl_cfg, pbuffer_attribs)
egl.eglBindAPI(egl.EGL_OPENGL_API)
egl_context = egl.eglCreateContext(egl_display, egl_cfg, egl.EGL_NO_CONTEXT,
None)
egl.eglMakeCurrent(egl_display, egl_surf, egl_surf, egl_context)
|
python
|
def create_opengl_context(surface_size=(640, 480)):
"""Create offscreen OpenGL context and make it current.
Users are expected to directly use EGL API in case more advanced
context management is required.
Args:
surface_size: (width, height), size of the offscreen rendering surface.
"""
egl_display = egl.eglGetDisplay(egl.EGL_DEFAULT_DISPLAY)
major, minor = egl.EGLint(), egl.EGLint()
egl.eglInitialize(egl_display, pointer(major), pointer(minor))
config_attribs = [
egl.EGL_SURFACE_TYPE, egl.EGL_PBUFFER_BIT, egl.EGL_BLUE_SIZE, 8,
egl.EGL_GREEN_SIZE, 8, egl.EGL_RED_SIZE, 8, egl.EGL_DEPTH_SIZE, 24,
egl.EGL_RENDERABLE_TYPE, egl.EGL_OPENGL_BIT, egl.EGL_NONE
]
config_attribs = (egl.EGLint * len(config_attribs))(*config_attribs)
num_configs = egl.EGLint()
egl_cfg = egl.EGLConfig()
egl.eglChooseConfig(egl_display, config_attribs, pointer(egl_cfg), 1,
pointer(num_configs))
width, height = surface_size
pbuffer_attribs = [
egl.EGL_WIDTH,
width,
egl.EGL_HEIGHT,
height,
egl.EGL_NONE,
]
pbuffer_attribs = (egl.EGLint * len(pbuffer_attribs))(*pbuffer_attribs)
egl_surf = egl.eglCreatePbufferSurface(egl_display, egl_cfg, pbuffer_attribs)
egl.eglBindAPI(egl.EGL_OPENGL_API)
egl_context = egl.eglCreateContext(egl_display, egl_cfg, egl.EGL_NO_CONTEXT,
None)
egl.eglMakeCurrent(egl_display, egl_surf, egl_surf, egl_context)
|
[
"def",
"create_opengl_context",
"(",
"surface_size",
"=",
"(",
"640",
",",
"480",
")",
")",
":",
"egl_display",
"=",
"egl",
".",
"eglGetDisplay",
"(",
"egl",
".",
"EGL_DEFAULT_DISPLAY",
")",
"major",
",",
"minor",
"=",
"egl",
".",
"EGLint",
"(",
")",
",",
"egl",
".",
"EGLint",
"(",
")",
"egl",
".",
"eglInitialize",
"(",
"egl_display",
",",
"pointer",
"(",
"major",
")",
",",
"pointer",
"(",
"minor",
")",
")",
"config_attribs",
"=",
"[",
"egl",
".",
"EGL_SURFACE_TYPE",
",",
"egl",
".",
"EGL_PBUFFER_BIT",
",",
"egl",
".",
"EGL_BLUE_SIZE",
",",
"8",
",",
"egl",
".",
"EGL_GREEN_SIZE",
",",
"8",
",",
"egl",
".",
"EGL_RED_SIZE",
",",
"8",
",",
"egl",
".",
"EGL_DEPTH_SIZE",
",",
"24",
",",
"egl",
".",
"EGL_RENDERABLE_TYPE",
",",
"egl",
".",
"EGL_OPENGL_BIT",
",",
"egl",
".",
"EGL_NONE",
"]",
"config_attribs",
"=",
"(",
"egl",
".",
"EGLint",
"*",
"len",
"(",
"config_attribs",
")",
")",
"(",
"*",
"config_attribs",
")",
"num_configs",
"=",
"egl",
".",
"EGLint",
"(",
")",
"egl_cfg",
"=",
"egl",
".",
"EGLConfig",
"(",
")",
"egl",
".",
"eglChooseConfig",
"(",
"egl_display",
",",
"config_attribs",
",",
"pointer",
"(",
"egl_cfg",
")",
",",
"1",
",",
"pointer",
"(",
"num_configs",
")",
")",
"width",
",",
"height",
"=",
"surface_size",
"pbuffer_attribs",
"=",
"[",
"egl",
".",
"EGL_WIDTH",
",",
"width",
",",
"egl",
".",
"EGL_HEIGHT",
",",
"height",
",",
"egl",
".",
"EGL_NONE",
",",
"]",
"pbuffer_attribs",
"=",
"(",
"egl",
".",
"EGLint",
"*",
"len",
"(",
"pbuffer_attribs",
")",
")",
"(",
"*",
"pbuffer_attribs",
")",
"egl_surf",
"=",
"egl",
".",
"eglCreatePbufferSurface",
"(",
"egl_display",
",",
"egl_cfg",
",",
"pbuffer_attribs",
")",
"egl",
".",
"eglBindAPI",
"(",
"egl",
".",
"EGL_OPENGL_API",
")",
"egl_context",
"=",
"egl",
".",
"eglCreateContext",
"(",
"egl_display",
",",
"egl_cfg",
",",
"egl",
".",
"EGL_NO_CONTEXT",
",",
"None",
")",
"egl",
".",
"eglMakeCurrent",
"(",
"egl_display",
",",
"egl_surf",
",",
"egl_surf",
",",
"egl_context",
")"
] |
Create offscreen OpenGL context and make it current.
Users are expected to directly use EGL API in case more advanced
context management is required.
Args:
surface_size: (width, height), size of the offscreen rendering surface.
|
[
"Create",
"offscreen",
"OpenGL",
"context",
"and",
"make",
"it",
"current",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/misc/gl/glcontext.py#L79-L120
|
train
|
tensorflow/lucid
|
lucid/optvis/param/resize_bilinear_nd.py
|
collapse_shape
|
def collapse_shape(shape, a, b):
"""Collapse `shape` outside the interval (`a`,`b`).
This function collapses `shape` outside the interval (`a`,`b`) by
multiplying the dimensions before `a` into a single dimension,
and mutliplying the dimensions after `b` into a single dimension.
Args:
shape: a tensor shape
a: integer, position in shape
b: integer, position in shape
Returns:
The collapsed shape, represented as a list.
Examples:
[1, 2, 3, 4, 5], (a=0, b=2) => [1, 1, 2, 60]
[1, 2, 3, 4, 5], (a=1, b=3) => [1, 2, 3, 20]
[1, 2, 3, 4, 5], (a=2, b=4) => [2, 3, 4, 5 ]
[1, 2, 3, 4, 5], (a=3, b=5) => [6, 4, 5, 1 ]
"""
shape = list(shape)
if a < 0:
n_pad = -a
pad = n_pad * [1]
return collapse_shape(pad + shape, a + n_pad, b + n_pad)
if b > len(shape):
n_pad = b - len(shape)
pad = n_pad * [1]
return collapse_shape(shape + pad, a, b)
return [product(shape[:a])] + shape[a:b] + [product(shape[b:])]
|
python
|
def collapse_shape(shape, a, b):
"""Collapse `shape` outside the interval (`a`,`b`).
This function collapses `shape` outside the interval (`a`,`b`) by
multiplying the dimensions before `a` into a single dimension,
and mutliplying the dimensions after `b` into a single dimension.
Args:
shape: a tensor shape
a: integer, position in shape
b: integer, position in shape
Returns:
The collapsed shape, represented as a list.
Examples:
[1, 2, 3, 4, 5], (a=0, b=2) => [1, 1, 2, 60]
[1, 2, 3, 4, 5], (a=1, b=3) => [1, 2, 3, 20]
[1, 2, 3, 4, 5], (a=2, b=4) => [2, 3, 4, 5 ]
[1, 2, 3, 4, 5], (a=3, b=5) => [6, 4, 5, 1 ]
"""
shape = list(shape)
if a < 0:
n_pad = -a
pad = n_pad * [1]
return collapse_shape(pad + shape, a + n_pad, b + n_pad)
if b > len(shape):
n_pad = b - len(shape)
pad = n_pad * [1]
return collapse_shape(shape + pad, a, b)
return [product(shape[:a])] + shape[a:b] + [product(shape[b:])]
|
[
"def",
"collapse_shape",
"(",
"shape",
",",
"a",
",",
"b",
")",
":",
"shape",
"=",
"list",
"(",
"shape",
")",
"if",
"a",
"<",
"0",
":",
"n_pad",
"=",
"-",
"a",
"pad",
"=",
"n_pad",
"*",
"[",
"1",
"]",
"return",
"collapse_shape",
"(",
"pad",
"+",
"shape",
",",
"a",
"+",
"n_pad",
",",
"b",
"+",
"n_pad",
")",
"if",
"b",
">",
"len",
"(",
"shape",
")",
":",
"n_pad",
"=",
"b",
"-",
"len",
"(",
"shape",
")",
"pad",
"=",
"n_pad",
"*",
"[",
"1",
"]",
"return",
"collapse_shape",
"(",
"shape",
"+",
"pad",
",",
"a",
",",
"b",
")",
"return",
"[",
"product",
"(",
"shape",
"[",
":",
"a",
"]",
")",
"]",
"+",
"shape",
"[",
"a",
":",
"b",
"]",
"+",
"[",
"product",
"(",
"shape",
"[",
"b",
":",
"]",
")",
"]"
] |
Collapse `shape` outside the interval (`a`,`b`).
This function collapses `shape` outside the interval (`a`,`b`) by
multiplying the dimensions before `a` into a single dimension,
and mutliplying the dimensions after `b` into a single dimension.
Args:
shape: a tensor shape
a: integer, position in shape
b: integer, position in shape
Returns:
The collapsed shape, represented as a list.
Examples:
[1, 2, 3, 4, 5], (a=0, b=2) => [1, 1, 2, 60]
[1, 2, 3, 4, 5], (a=1, b=3) => [1, 2, 3, 20]
[1, 2, 3, 4, 5], (a=2, b=4) => [2, 3, 4, 5 ]
[1, 2, 3, 4, 5], (a=3, b=5) => [6, 4, 5, 1 ]
|
[
"Collapse",
"shape",
"outside",
"the",
"interval",
"(",
"a",
"b",
")",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/param/resize_bilinear_nd.py#L35-L65
|
train
|
tensorflow/lucid
|
lucid/optvis/param/resize_bilinear_nd.py
|
resize_bilinear_nd
|
def resize_bilinear_nd(t, target_shape):
"""Bilinear resizes a tensor t to have shape target_shape.
This function bilinearly resizes a n-dimensional tensor by iteratively
applying tf.image.resize_bilinear (which can only resize 2 dimensions).
For bilinear interpolation, the order in which it is applied does not matter.
Args:
t: tensor to be resized
target_shape: the desired shape of the new tensor.
Returns:
The resized tensor
"""
shape = t.get_shape().as_list()
target_shape = list(target_shape)
assert len(shape) == len(target_shape)
# We progressively move through the shape, resizing dimensions...
d = 0
while d < len(shape):
# If we don't need to deal with the next dimesnion, step over it
if shape[d] == target_shape[d]:
d += 1
continue
# Otherwise, we'll resize the next two dimensions...
# If d+2 doesn't need to be resized, this will just be a null op for it
new_shape = shape[:]
new_shape[d : d+2] = target_shape[d : d+2]
# The helper collapse_shape() makes our shapes 4-dimensional with
# the two dimesnions we want to deal with in the middle.
shape_ = collapse_shape(shape, d, d+2)
new_shape_ = collapse_shape(new_shape, d, d+2)
# We can then reshape and use the 2d tf.image.resize_bilinear() on the
# inner two dimesions.
t_ = tf.reshape(t, shape_)
t_ = tf.image.resize_bilinear(t_, new_shape_[1:3])
# And then reshape back to our uncollapsed version, having finished resizing
# two more dimensions in our shape.
t = tf.reshape(t_, new_shape)
shape = new_shape
d += 2
return t
|
python
|
def resize_bilinear_nd(t, target_shape):
"""Bilinear resizes a tensor t to have shape target_shape.
This function bilinearly resizes a n-dimensional tensor by iteratively
applying tf.image.resize_bilinear (which can only resize 2 dimensions).
For bilinear interpolation, the order in which it is applied does not matter.
Args:
t: tensor to be resized
target_shape: the desired shape of the new tensor.
Returns:
The resized tensor
"""
shape = t.get_shape().as_list()
target_shape = list(target_shape)
assert len(shape) == len(target_shape)
# We progressively move through the shape, resizing dimensions...
d = 0
while d < len(shape):
# If we don't need to deal with the next dimesnion, step over it
if shape[d] == target_shape[d]:
d += 1
continue
# Otherwise, we'll resize the next two dimensions...
# If d+2 doesn't need to be resized, this will just be a null op for it
new_shape = shape[:]
new_shape[d : d+2] = target_shape[d : d+2]
# The helper collapse_shape() makes our shapes 4-dimensional with
# the two dimesnions we want to deal with in the middle.
shape_ = collapse_shape(shape, d, d+2)
new_shape_ = collapse_shape(new_shape, d, d+2)
# We can then reshape and use the 2d tf.image.resize_bilinear() on the
# inner two dimesions.
t_ = tf.reshape(t, shape_)
t_ = tf.image.resize_bilinear(t_, new_shape_[1:3])
# And then reshape back to our uncollapsed version, having finished resizing
# two more dimensions in our shape.
t = tf.reshape(t_, new_shape)
shape = new_shape
d += 2
return t
|
[
"def",
"resize_bilinear_nd",
"(",
"t",
",",
"target_shape",
")",
":",
"shape",
"=",
"t",
".",
"get_shape",
"(",
")",
".",
"as_list",
"(",
")",
"target_shape",
"=",
"list",
"(",
"target_shape",
")",
"assert",
"len",
"(",
"shape",
")",
"==",
"len",
"(",
"target_shape",
")",
"# We progressively move through the shape, resizing dimensions...",
"d",
"=",
"0",
"while",
"d",
"<",
"len",
"(",
"shape",
")",
":",
"# If we don't need to deal with the next dimesnion, step over it",
"if",
"shape",
"[",
"d",
"]",
"==",
"target_shape",
"[",
"d",
"]",
":",
"d",
"+=",
"1",
"continue",
"# Otherwise, we'll resize the next two dimensions...",
"# If d+2 doesn't need to be resized, this will just be a null op for it",
"new_shape",
"=",
"shape",
"[",
":",
"]",
"new_shape",
"[",
"d",
":",
"d",
"+",
"2",
"]",
"=",
"target_shape",
"[",
"d",
":",
"d",
"+",
"2",
"]",
"# The helper collapse_shape() makes our shapes 4-dimensional with",
"# the two dimesnions we want to deal with in the middle.",
"shape_",
"=",
"collapse_shape",
"(",
"shape",
",",
"d",
",",
"d",
"+",
"2",
")",
"new_shape_",
"=",
"collapse_shape",
"(",
"new_shape",
",",
"d",
",",
"d",
"+",
"2",
")",
"# We can then reshape and use the 2d tf.image.resize_bilinear() on the",
"# inner two dimesions.",
"t_",
"=",
"tf",
".",
"reshape",
"(",
"t",
",",
"shape_",
")",
"t_",
"=",
"tf",
".",
"image",
".",
"resize_bilinear",
"(",
"t_",
",",
"new_shape_",
"[",
"1",
":",
"3",
"]",
")",
"# And then reshape back to our uncollapsed version, having finished resizing",
"# two more dimensions in our shape.",
"t",
"=",
"tf",
".",
"reshape",
"(",
"t_",
",",
"new_shape",
")",
"shape",
"=",
"new_shape",
"d",
"+=",
"2",
"return",
"t"
] |
Bilinear resizes a tensor t to have shape target_shape.
This function bilinearly resizes a n-dimensional tensor by iteratively
applying tf.image.resize_bilinear (which can only resize 2 dimensions).
For bilinear interpolation, the order in which it is applied does not matter.
Args:
t: tensor to be resized
target_shape: the desired shape of the new tensor.
Returns:
The resized tensor
|
[
"Bilinear",
"resizes",
"a",
"tensor",
"t",
"to",
"have",
"shape",
"target_shape",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/param/resize_bilinear_nd.py#L68-L116
|
train
|
tensorflow/lucid
|
lucid/modelzoo/aligned_activations.py
|
get_aligned_activations
|
def get_aligned_activations(layer):
"""Downloads 100k activations of the specified layer sampled from iterating over
ImageNet. Activations of all layers where sampled at the same spatial positions for
each image, allowing the calculation of correlations."""
activation_paths = [
PATH_TEMPLATE.format(
sanitize(layer.model_class.name), sanitize(layer.name), page
)
for page in range(NUMBER_OF_PAGES)
]
activations = np.vstack([load(path) for path in activation_paths])
assert np.all(np.isfinite(activations))
return activations
|
python
|
def get_aligned_activations(layer):
"""Downloads 100k activations of the specified layer sampled from iterating over
ImageNet. Activations of all layers where sampled at the same spatial positions for
each image, allowing the calculation of correlations."""
activation_paths = [
PATH_TEMPLATE.format(
sanitize(layer.model_class.name), sanitize(layer.name), page
)
for page in range(NUMBER_OF_PAGES)
]
activations = np.vstack([load(path) for path in activation_paths])
assert np.all(np.isfinite(activations))
return activations
|
[
"def",
"get_aligned_activations",
"(",
"layer",
")",
":",
"activation_paths",
"=",
"[",
"PATH_TEMPLATE",
".",
"format",
"(",
"sanitize",
"(",
"layer",
".",
"model_class",
".",
"name",
")",
",",
"sanitize",
"(",
"layer",
".",
"name",
")",
",",
"page",
")",
"for",
"page",
"in",
"range",
"(",
"NUMBER_OF_PAGES",
")",
"]",
"activations",
"=",
"np",
".",
"vstack",
"(",
"[",
"load",
"(",
"path",
")",
"for",
"path",
"in",
"activation_paths",
"]",
")",
"assert",
"np",
".",
"all",
"(",
"np",
".",
"isfinite",
"(",
"activations",
")",
")",
"return",
"activations"
] |
Downloads 100k activations of the specified layer sampled from iterating over
ImageNet. Activations of all layers where sampled at the same spatial positions for
each image, allowing the calculation of correlations.
|
[
"Downloads",
"100k",
"activations",
"of",
"the",
"specified",
"layer",
"sampled",
"from",
"iterating",
"over",
"ImageNet",
".",
"Activations",
"of",
"all",
"layers",
"where",
"sampled",
"at",
"the",
"same",
"spatial",
"positions",
"for",
"each",
"image",
"allowing",
"the",
"calculation",
"of",
"correlations",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/modelzoo/aligned_activations.py#L35-L47
|
train
|
tensorflow/lucid
|
lucid/modelzoo/aligned_activations.py
|
layer_covariance
|
def layer_covariance(layer1, layer2=None):
"""Computes the covariance matrix between the neurons of two layers. If only one
layer is passed, computes the symmetric covariance matrix of that layer."""
layer2 = layer2 or layer1
act1, act2 = layer1.activations, layer2.activations
num_datapoints = act1.shape[0] # cast to avoid numpy type promotion during division
return np.matmul(act1.T, act2) / float(num_datapoints)
|
python
|
def layer_covariance(layer1, layer2=None):
"""Computes the covariance matrix between the neurons of two layers. If only one
layer is passed, computes the symmetric covariance matrix of that layer."""
layer2 = layer2 or layer1
act1, act2 = layer1.activations, layer2.activations
num_datapoints = act1.shape[0] # cast to avoid numpy type promotion during division
return np.matmul(act1.T, act2) / float(num_datapoints)
|
[
"def",
"layer_covariance",
"(",
"layer1",
",",
"layer2",
"=",
"None",
")",
":",
"layer2",
"=",
"layer2",
"or",
"layer1",
"act1",
",",
"act2",
"=",
"layer1",
".",
"activations",
",",
"layer2",
".",
"activations",
"num_datapoints",
"=",
"act1",
".",
"shape",
"[",
"0",
"]",
"# cast to avoid numpy type promotion during division",
"return",
"np",
".",
"matmul",
"(",
"act1",
".",
"T",
",",
"act2",
")",
"/",
"float",
"(",
"num_datapoints",
")"
] |
Computes the covariance matrix between the neurons of two layers. If only one
layer is passed, computes the symmetric covariance matrix of that layer.
|
[
"Computes",
"the",
"covariance",
"matrix",
"between",
"the",
"neurons",
"of",
"two",
"layers",
".",
"If",
"only",
"one",
"layer",
"is",
"passed",
"computes",
"the",
"symmetric",
"covariance",
"matrix",
"of",
"that",
"layer",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/modelzoo/aligned_activations.py#L51-L57
|
train
|
tensorflow/lucid
|
lucid/modelzoo/aligned_activations.py
|
push_activations
|
def push_activations(activations, from_layer, to_layer):
"""Push activations from one model to another using prerecorded correlations"""
inverse_covariance_matrix = layer_inverse_covariance(from_layer)
activations_decorrelated = np.dot(inverse_covariance_matrix, activations.T).T
covariance_matrix = layer_covariance(from_layer, to_layer)
activation_recorrelated = np.dot(activations_decorrelated, covariance_matrix)
return activation_recorrelated
|
python
|
def push_activations(activations, from_layer, to_layer):
"""Push activations from one model to another using prerecorded correlations"""
inverse_covariance_matrix = layer_inverse_covariance(from_layer)
activations_decorrelated = np.dot(inverse_covariance_matrix, activations.T).T
covariance_matrix = layer_covariance(from_layer, to_layer)
activation_recorrelated = np.dot(activations_decorrelated, covariance_matrix)
return activation_recorrelated
|
[
"def",
"push_activations",
"(",
"activations",
",",
"from_layer",
",",
"to_layer",
")",
":",
"inverse_covariance_matrix",
"=",
"layer_inverse_covariance",
"(",
"from_layer",
")",
"activations_decorrelated",
"=",
"np",
".",
"dot",
"(",
"inverse_covariance_matrix",
",",
"activations",
".",
"T",
")",
".",
"T",
"covariance_matrix",
"=",
"layer_covariance",
"(",
"from_layer",
",",
"to_layer",
")",
"activation_recorrelated",
"=",
"np",
".",
"dot",
"(",
"activations_decorrelated",
",",
"covariance_matrix",
")",
"return",
"activation_recorrelated"
] |
Push activations from one model to another using prerecorded correlations
|
[
"Push",
"activations",
"from",
"one",
"model",
"to",
"another",
"using",
"prerecorded",
"correlations"
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/modelzoo/aligned_activations.py#L66-L72
|
train
|
tensorflow/lucid
|
lucid/recipes/image_interpolation_params.py
|
multi_interpolation_basis
|
def multi_interpolation_basis(n_objectives=6, n_interp_steps=5, width=128,
channels=3):
"""A paramaterization for interpolating between each pair of N objectives.
Sometimes you want to interpolate between optimizing a bunch of objectives,
in a paramaterization that encourages images to align.
Args:
n_objectives: number of objectives you want interpolate between
n_interp_steps: number of interpolation steps
width: width of intepolated images
channel
Returns:
A [n_objectives, n_objectives, n_interp_steps, width, width, channel]
shaped tensor, t, where the final [width, width, channel] should be
seen as images, such that the following properties hold:
t[a, b] = t[b, a, ::-1]
t[a, i, 0] = t[a, j, 0] for all i, j
t[a, a, i] = t[a, a, j] for all i, j
t[a, b, i] = t[b, a, -i] for all i
"""
N, M, W, Ch = n_objectives, n_interp_steps, width, channels
const_term = sum([lowres_tensor([W, W, Ch], [W//k, W//k, Ch])
for k in [1, 2, 4, 8]])
const_term = tf.reshape(const_term, [1, 1, 1, W, W, Ch])
example_interps = [
sum([lowres_tensor([M, W, W, Ch], [2, W//k, W//k, Ch])
for k in [1, 2, 4, 8]])
for _ in range(N)]
example_basis = []
for n in range(N):
col = []
for m in range(N):
interp = example_interps[n] + example_interps[m][::-1]
col.append(interp)
example_basis.append(col)
interp_basis = []
for n in range(N):
col = [interp_basis[m][N-n][::-1] for m in range(n)]
col.append(tf.zeros([M, W, W, 3]))
for m in range(n+1, N):
interp = sum([lowres_tensor([M, W, W, Ch], [M, W//k, W//k, Ch])
for k in [1, 2]])
col.append(interp)
interp_basis.append(col)
basis = []
for n in range(N):
col_ex = tf.stack(example_basis[n])
col_in = tf.stack(interp_basis[n])
basis.append(col_ex + col_in)
basis = tf.stack(basis)
return basis + const_term
|
python
|
def multi_interpolation_basis(n_objectives=6, n_interp_steps=5, width=128,
channels=3):
"""A paramaterization for interpolating between each pair of N objectives.
Sometimes you want to interpolate between optimizing a bunch of objectives,
in a paramaterization that encourages images to align.
Args:
n_objectives: number of objectives you want interpolate between
n_interp_steps: number of interpolation steps
width: width of intepolated images
channel
Returns:
A [n_objectives, n_objectives, n_interp_steps, width, width, channel]
shaped tensor, t, where the final [width, width, channel] should be
seen as images, such that the following properties hold:
t[a, b] = t[b, a, ::-1]
t[a, i, 0] = t[a, j, 0] for all i, j
t[a, a, i] = t[a, a, j] for all i, j
t[a, b, i] = t[b, a, -i] for all i
"""
N, M, W, Ch = n_objectives, n_interp_steps, width, channels
const_term = sum([lowres_tensor([W, W, Ch], [W//k, W//k, Ch])
for k in [1, 2, 4, 8]])
const_term = tf.reshape(const_term, [1, 1, 1, W, W, Ch])
example_interps = [
sum([lowres_tensor([M, W, W, Ch], [2, W//k, W//k, Ch])
for k in [1, 2, 4, 8]])
for _ in range(N)]
example_basis = []
for n in range(N):
col = []
for m in range(N):
interp = example_interps[n] + example_interps[m][::-1]
col.append(interp)
example_basis.append(col)
interp_basis = []
for n in range(N):
col = [interp_basis[m][N-n][::-1] for m in range(n)]
col.append(tf.zeros([M, W, W, 3]))
for m in range(n+1, N):
interp = sum([lowres_tensor([M, W, W, Ch], [M, W//k, W//k, Ch])
for k in [1, 2]])
col.append(interp)
interp_basis.append(col)
basis = []
for n in range(N):
col_ex = tf.stack(example_basis[n])
col_in = tf.stack(interp_basis[n])
basis.append(col_ex + col_in)
basis = tf.stack(basis)
return basis + const_term
|
[
"def",
"multi_interpolation_basis",
"(",
"n_objectives",
"=",
"6",
",",
"n_interp_steps",
"=",
"5",
",",
"width",
"=",
"128",
",",
"channels",
"=",
"3",
")",
":",
"N",
",",
"M",
",",
"W",
",",
"Ch",
"=",
"n_objectives",
",",
"n_interp_steps",
",",
"width",
",",
"channels",
"const_term",
"=",
"sum",
"(",
"[",
"lowres_tensor",
"(",
"[",
"W",
",",
"W",
",",
"Ch",
"]",
",",
"[",
"W",
"//",
"k",
",",
"W",
"//",
"k",
",",
"Ch",
"]",
")",
"for",
"k",
"in",
"[",
"1",
",",
"2",
",",
"4",
",",
"8",
"]",
"]",
")",
"const_term",
"=",
"tf",
".",
"reshape",
"(",
"const_term",
",",
"[",
"1",
",",
"1",
",",
"1",
",",
"W",
",",
"W",
",",
"Ch",
"]",
")",
"example_interps",
"=",
"[",
"sum",
"(",
"[",
"lowres_tensor",
"(",
"[",
"M",
",",
"W",
",",
"W",
",",
"Ch",
"]",
",",
"[",
"2",
",",
"W",
"//",
"k",
",",
"W",
"//",
"k",
",",
"Ch",
"]",
")",
"for",
"k",
"in",
"[",
"1",
",",
"2",
",",
"4",
",",
"8",
"]",
"]",
")",
"for",
"_",
"in",
"range",
"(",
"N",
")",
"]",
"example_basis",
"=",
"[",
"]",
"for",
"n",
"in",
"range",
"(",
"N",
")",
":",
"col",
"=",
"[",
"]",
"for",
"m",
"in",
"range",
"(",
"N",
")",
":",
"interp",
"=",
"example_interps",
"[",
"n",
"]",
"+",
"example_interps",
"[",
"m",
"]",
"[",
":",
":",
"-",
"1",
"]",
"col",
".",
"append",
"(",
"interp",
")",
"example_basis",
".",
"append",
"(",
"col",
")",
"interp_basis",
"=",
"[",
"]",
"for",
"n",
"in",
"range",
"(",
"N",
")",
":",
"col",
"=",
"[",
"interp_basis",
"[",
"m",
"]",
"[",
"N",
"-",
"n",
"]",
"[",
":",
":",
"-",
"1",
"]",
"for",
"m",
"in",
"range",
"(",
"n",
")",
"]",
"col",
".",
"append",
"(",
"tf",
".",
"zeros",
"(",
"[",
"M",
",",
"W",
",",
"W",
",",
"3",
"]",
")",
")",
"for",
"m",
"in",
"range",
"(",
"n",
"+",
"1",
",",
"N",
")",
":",
"interp",
"=",
"sum",
"(",
"[",
"lowres_tensor",
"(",
"[",
"M",
",",
"W",
",",
"W",
",",
"Ch",
"]",
",",
"[",
"M",
",",
"W",
"//",
"k",
",",
"W",
"//",
"k",
",",
"Ch",
"]",
")",
"for",
"k",
"in",
"[",
"1",
",",
"2",
"]",
"]",
")",
"col",
".",
"append",
"(",
"interp",
")",
"interp_basis",
".",
"append",
"(",
"col",
")",
"basis",
"=",
"[",
"]",
"for",
"n",
"in",
"range",
"(",
"N",
")",
":",
"col_ex",
"=",
"tf",
".",
"stack",
"(",
"example_basis",
"[",
"n",
"]",
")",
"col_in",
"=",
"tf",
".",
"stack",
"(",
"interp_basis",
"[",
"n",
"]",
")",
"basis",
".",
"append",
"(",
"col_ex",
"+",
"col_in",
")",
"basis",
"=",
"tf",
".",
"stack",
"(",
"basis",
")",
"return",
"basis",
"+",
"const_term"
] |
A paramaterization for interpolating between each pair of N objectives.
Sometimes you want to interpolate between optimizing a bunch of objectives,
in a paramaterization that encourages images to align.
Args:
n_objectives: number of objectives you want interpolate between
n_interp_steps: number of interpolation steps
width: width of intepolated images
channel
Returns:
A [n_objectives, n_objectives, n_interp_steps, width, width, channel]
shaped tensor, t, where the final [width, width, channel] should be
seen as images, such that the following properties hold:
t[a, b] = t[b, a, ::-1]
t[a, i, 0] = t[a, j, 0] for all i, j
t[a, a, i] = t[a, a, j] for all i, j
t[a, b, i] = t[b, a, -i] for all i
|
[
"A",
"paramaterization",
"for",
"interpolating",
"between",
"each",
"pair",
"of",
"N",
"objectives",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/recipes/image_interpolation_params.py#L22-L82
|
train
|
tensorflow/lucid
|
lucid/optvis/overrides/gradient_override.py
|
register_to_random_name
|
def register_to_random_name(grad_f):
"""Register a gradient function to a random string.
In order to use a custom gradient in TensorFlow, it must be registered to a
string. This is both a hassle, and -- because only one function can every be
registered to a string -- annoying to iterate on in an interactive
environemnt.
This function registers a function to a unique random string of the form:
{FUNCTION_NAME}_{RANDOM_SALT}
And then returns the random string. This is a helper in creating more
convenient gradient overrides.
Args:
grad_f: gradient function to register. Should map (op, grad) -> grad(s)
Returns:
String that gradient function was registered to.
"""
grad_f_name = grad_f.__name__ + "_" + str(uuid.uuid4())
tf.RegisterGradient(grad_f_name)(grad_f)
return grad_f_name
|
python
|
def register_to_random_name(grad_f):
"""Register a gradient function to a random string.
In order to use a custom gradient in TensorFlow, it must be registered to a
string. This is both a hassle, and -- because only one function can every be
registered to a string -- annoying to iterate on in an interactive
environemnt.
This function registers a function to a unique random string of the form:
{FUNCTION_NAME}_{RANDOM_SALT}
And then returns the random string. This is a helper in creating more
convenient gradient overrides.
Args:
grad_f: gradient function to register. Should map (op, grad) -> grad(s)
Returns:
String that gradient function was registered to.
"""
grad_f_name = grad_f.__name__ + "_" + str(uuid.uuid4())
tf.RegisterGradient(grad_f_name)(grad_f)
return grad_f_name
|
[
"def",
"register_to_random_name",
"(",
"grad_f",
")",
":",
"grad_f_name",
"=",
"grad_f",
".",
"__name__",
"+",
"\"_\"",
"+",
"str",
"(",
"uuid",
".",
"uuid4",
"(",
")",
")",
"tf",
".",
"RegisterGradient",
"(",
"grad_f_name",
")",
"(",
"grad_f",
")",
"return",
"grad_f_name"
] |
Register a gradient function to a random string.
In order to use a custom gradient in TensorFlow, it must be registered to a
string. This is both a hassle, and -- because only one function can every be
registered to a string -- annoying to iterate on in an interactive
environemnt.
This function registers a function to a unique random string of the form:
{FUNCTION_NAME}_{RANDOM_SALT}
And then returns the random string. This is a helper in creating more
convenient gradient overrides.
Args:
grad_f: gradient function to register. Should map (op, grad) -> grad(s)
Returns:
String that gradient function was registered to.
|
[
"Register",
"a",
"gradient",
"function",
"to",
"a",
"random",
"string",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/overrides/gradient_override.py#L50-L73
|
train
|
tensorflow/lucid
|
lucid/optvis/overrides/gradient_override.py
|
gradient_override_map
|
def gradient_override_map(override_dict):
"""Convenience wrapper for graph.gradient_override_map().
This functions provides two conveniences over normal tensorflow gradient
overrides: it auomatically uses the default graph instead of you needing to
find the graph, and it automatically
Example:
def _foo_grad_alt(op, grad): ...
with gradient_override({"Foo": _foo_grad_alt}):
Args:
override_dict: A dictionary describing how to override the gradient.
keys: strings correponding to the op type that should have their gradient
overriden.
values: functions or strings registered to gradient functions
"""
override_dict_by_name = {}
for (op_name, grad_f) in override_dict.items():
if isinstance(grad_f, str):
override_dict_by_name[op_name] = grad_f
else:
override_dict_by_name[op_name] = register_to_random_name(grad_f)
with tf.get_default_graph().gradient_override_map(override_dict_by_name):
yield
|
python
|
def gradient_override_map(override_dict):
"""Convenience wrapper for graph.gradient_override_map().
This functions provides two conveniences over normal tensorflow gradient
overrides: it auomatically uses the default graph instead of you needing to
find the graph, and it automatically
Example:
def _foo_grad_alt(op, grad): ...
with gradient_override({"Foo": _foo_grad_alt}):
Args:
override_dict: A dictionary describing how to override the gradient.
keys: strings correponding to the op type that should have their gradient
overriden.
values: functions or strings registered to gradient functions
"""
override_dict_by_name = {}
for (op_name, grad_f) in override_dict.items():
if isinstance(grad_f, str):
override_dict_by_name[op_name] = grad_f
else:
override_dict_by_name[op_name] = register_to_random_name(grad_f)
with tf.get_default_graph().gradient_override_map(override_dict_by_name):
yield
|
[
"def",
"gradient_override_map",
"(",
"override_dict",
")",
":",
"override_dict_by_name",
"=",
"{",
"}",
"for",
"(",
"op_name",
",",
"grad_f",
")",
"in",
"override_dict",
".",
"items",
"(",
")",
":",
"if",
"isinstance",
"(",
"grad_f",
",",
"str",
")",
":",
"override_dict_by_name",
"[",
"op_name",
"]",
"=",
"grad_f",
"else",
":",
"override_dict_by_name",
"[",
"op_name",
"]",
"=",
"register_to_random_name",
"(",
"grad_f",
")",
"with",
"tf",
".",
"get_default_graph",
"(",
")",
".",
"gradient_override_map",
"(",
"override_dict_by_name",
")",
":",
"yield"
] |
Convenience wrapper for graph.gradient_override_map().
This functions provides two conveniences over normal tensorflow gradient
overrides: it auomatically uses the default graph instead of you needing to
find the graph, and it automatically
Example:
def _foo_grad_alt(op, grad): ...
with gradient_override({"Foo": _foo_grad_alt}):
Args:
override_dict: A dictionary describing how to override the gradient.
keys: strings correponding to the op type that should have their gradient
overriden.
values: functions or strings registered to gradient functions
|
[
"Convenience",
"wrapper",
"for",
"graph",
".",
"gradient_override_map",
"()",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/overrides/gradient_override.py#L77-L104
|
train
|
tensorflow/lucid
|
lucid/optvis/overrides/gradient_override.py
|
use_gradient
|
def use_gradient(grad_f):
"""Decorator for easily setting custom gradients for TensorFlow functions.
* DO NOT use this function if you need to serialize your graph.
* This function will cause the decorated function to run slower.
Example:
def _foo_grad(op, grad): ...
@use_gradient(_foo_grad)
def foo(x1, x2, x3): ...
Args:
grad_f: function to use as gradient.
Returns:
A decorator to apply to the function you wish to override the gradient of.
"""
grad_f_name = register_to_random_name(grad_f)
def function_wrapper(f):
def inner(*inputs):
# TensorFlow only supports (as of writing) overriding the gradient of
# individual ops. In order to override the gardient of `f`, we need to
# somehow make it appear to be an individual TensorFlow op.
#
# Our solution is to create a PyFunc that mimics `f`.
#
# In particular, we construct a graph for `f` and run it, then use a
# stateful PyFunc to stash it's results in Python. Then we have another
# PyFunc mimic it by taking all the same inputs and returning the stashed
# output.
#
# I wish we could do this without PyFunc, but I don't see a way to have
# it be fully general.
state = {"out_value": None}
# First, we need to run `f` and store it's output.
out = f(*inputs)
def store_out(out_value):
"""Store the value of out to a python variable."""
state["out_value"] = out_value
store_name = "store_" + f.__name__
store = tf.py_func(store_out, [out], (), stateful=True, name=store_name)
# Next, we create the mock function, with an overriden gradient.
# Note that we need to make sure store gets evaluated before the mock
# runs.
def mock_f(*inputs):
"""Mimic f by retrieving the stored value of out."""
return state["out_value"]
with tf.control_dependencies([store]):
with gradient_override_map({"PyFunc": grad_f_name}):
mock_name = "mock_" + f.__name__
mock_out = tf.py_func(mock_f, inputs, out.dtype, stateful=True,
name=mock_name)
mock_out.set_shape(out.get_shape())
# Finally, we can return the mock.
return mock_out
return inner
return function_wrapper
|
python
|
def use_gradient(grad_f):
"""Decorator for easily setting custom gradients for TensorFlow functions.
* DO NOT use this function if you need to serialize your graph.
* This function will cause the decorated function to run slower.
Example:
def _foo_grad(op, grad): ...
@use_gradient(_foo_grad)
def foo(x1, x2, x3): ...
Args:
grad_f: function to use as gradient.
Returns:
A decorator to apply to the function you wish to override the gradient of.
"""
grad_f_name = register_to_random_name(grad_f)
def function_wrapper(f):
def inner(*inputs):
# TensorFlow only supports (as of writing) overriding the gradient of
# individual ops. In order to override the gardient of `f`, we need to
# somehow make it appear to be an individual TensorFlow op.
#
# Our solution is to create a PyFunc that mimics `f`.
#
# In particular, we construct a graph for `f` and run it, then use a
# stateful PyFunc to stash it's results in Python. Then we have another
# PyFunc mimic it by taking all the same inputs and returning the stashed
# output.
#
# I wish we could do this without PyFunc, but I don't see a way to have
# it be fully general.
state = {"out_value": None}
# First, we need to run `f` and store it's output.
out = f(*inputs)
def store_out(out_value):
"""Store the value of out to a python variable."""
state["out_value"] = out_value
store_name = "store_" + f.__name__
store = tf.py_func(store_out, [out], (), stateful=True, name=store_name)
# Next, we create the mock function, with an overriden gradient.
# Note that we need to make sure store gets evaluated before the mock
# runs.
def mock_f(*inputs):
"""Mimic f by retrieving the stored value of out."""
return state["out_value"]
with tf.control_dependencies([store]):
with gradient_override_map({"PyFunc": grad_f_name}):
mock_name = "mock_" + f.__name__
mock_out = tf.py_func(mock_f, inputs, out.dtype, stateful=True,
name=mock_name)
mock_out.set_shape(out.get_shape())
# Finally, we can return the mock.
return mock_out
return inner
return function_wrapper
|
[
"def",
"use_gradient",
"(",
"grad_f",
")",
":",
"grad_f_name",
"=",
"register_to_random_name",
"(",
"grad_f",
")",
"def",
"function_wrapper",
"(",
"f",
")",
":",
"def",
"inner",
"(",
"*",
"inputs",
")",
":",
"# TensorFlow only supports (as of writing) overriding the gradient of",
"# individual ops. In order to override the gardient of `f`, we need to",
"# somehow make it appear to be an individual TensorFlow op.",
"#",
"# Our solution is to create a PyFunc that mimics `f`.",
"#",
"# In particular, we construct a graph for `f` and run it, then use a",
"# stateful PyFunc to stash it's results in Python. Then we have another",
"# PyFunc mimic it by taking all the same inputs and returning the stashed",
"# output.",
"#",
"# I wish we could do this without PyFunc, but I don't see a way to have",
"# it be fully general.",
"state",
"=",
"{",
"\"out_value\"",
":",
"None",
"}",
"# First, we need to run `f` and store it's output.",
"out",
"=",
"f",
"(",
"*",
"inputs",
")",
"def",
"store_out",
"(",
"out_value",
")",
":",
"\"\"\"Store the value of out to a python variable.\"\"\"",
"state",
"[",
"\"out_value\"",
"]",
"=",
"out_value",
"store_name",
"=",
"\"store_\"",
"+",
"f",
".",
"__name__",
"store",
"=",
"tf",
".",
"py_func",
"(",
"store_out",
",",
"[",
"out",
"]",
",",
"(",
")",
",",
"stateful",
"=",
"True",
",",
"name",
"=",
"store_name",
")",
"# Next, we create the mock function, with an overriden gradient.",
"# Note that we need to make sure store gets evaluated before the mock",
"# runs.",
"def",
"mock_f",
"(",
"*",
"inputs",
")",
":",
"\"\"\"Mimic f by retrieving the stored value of out.\"\"\"",
"return",
"state",
"[",
"\"out_value\"",
"]",
"with",
"tf",
".",
"control_dependencies",
"(",
"[",
"store",
"]",
")",
":",
"with",
"gradient_override_map",
"(",
"{",
"\"PyFunc\"",
":",
"grad_f_name",
"}",
")",
":",
"mock_name",
"=",
"\"mock_\"",
"+",
"f",
".",
"__name__",
"mock_out",
"=",
"tf",
".",
"py_func",
"(",
"mock_f",
",",
"inputs",
",",
"out",
".",
"dtype",
",",
"stateful",
"=",
"True",
",",
"name",
"=",
"mock_name",
")",
"mock_out",
".",
"set_shape",
"(",
"out",
".",
"get_shape",
"(",
")",
")",
"# Finally, we can return the mock.",
"return",
"mock_out",
"return",
"inner",
"return",
"function_wrapper"
] |
Decorator for easily setting custom gradients for TensorFlow functions.
* DO NOT use this function if you need to serialize your graph.
* This function will cause the decorated function to run slower.
Example:
def _foo_grad(op, grad): ...
@use_gradient(_foo_grad)
def foo(x1, x2, x3): ...
Args:
grad_f: function to use as gradient.
Returns:
A decorator to apply to the function you wish to override the gradient of.
|
[
"Decorator",
"for",
"easily",
"setting",
"custom",
"gradients",
"for",
"TensorFlow",
"functions",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/overrides/gradient_override.py#L107-L178
|
train
|
tensorflow/lucid
|
lucid/optvis/param/spatial.py
|
pixel_image
|
def pixel_image(shape, sd=None, init_val=None):
"""A naive, pixel-based image parameterization.
Defaults to a random initialization, but can take a supplied init_val argument
instead.
Args:
shape: shape of resulting image, [batch, width, height, channels].
sd: standard deviation of param initialization noise.
init_val: an initial value to use instead of a random initialization. Needs
to have the same shape as the supplied shape argument.
Returns:
tensor with shape from first argument.
"""
if sd is not None and init_val is not None:
warnings.warn(
"`pixel_image` received both an initial value and a sd argument. Ignoring sd in favor of the supplied initial value."
)
sd = sd or 0.01
init_val = init_val or np.random.normal(size=shape, scale=sd).astype(np.float32)
return tf.Variable(init_val)
|
python
|
def pixel_image(shape, sd=None, init_val=None):
"""A naive, pixel-based image parameterization.
Defaults to a random initialization, but can take a supplied init_val argument
instead.
Args:
shape: shape of resulting image, [batch, width, height, channels].
sd: standard deviation of param initialization noise.
init_val: an initial value to use instead of a random initialization. Needs
to have the same shape as the supplied shape argument.
Returns:
tensor with shape from first argument.
"""
if sd is not None and init_val is not None:
warnings.warn(
"`pixel_image` received both an initial value and a sd argument. Ignoring sd in favor of the supplied initial value."
)
sd = sd or 0.01
init_val = init_val or np.random.normal(size=shape, scale=sd).astype(np.float32)
return tf.Variable(init_val)
|
[
"def",
"pixel_image",
"(",
"shape",
",",
"sd",
"=",
"None",
",",
"init_val",
"=",
"None",
")",
":",
"if",
"sd",
"is",
"not",
"None",
"and",
"init_val",
"is",
"not",
"None",
":",
"warnings",
".",
"warn",
"(",
"\"`pixel_image` received both an initial value and a sd argument. Ignoring sd in favor of the supplied initial value.\"",
")",
"sd",
"=",
"sd",
"or",
"0.01",
"init_val",
"=",
"init_val",
"or",
"np",
".",
"random",
".",
"normal",
"(",
"size",
"=",
"shape",
",",
"scale",
"=",
"sd",
")",
".",
"astype",
"(",
"np",
".",
"float32",
")",
"return",
"tf",
".",
"Variable",
"(",
"init_val",
")"
] |
A naive, pixel-based image parameterization.
Defaults to a random initialization, but can take a supplied init_val argument
instead.
Args:
shape: shape of resulting image, [batch, width, height, channels].
sd: standard deviation of param initialization noise.
init_val: an initial value to use instead of a random initialization. Needs
to have the same shape as the supplied shape argument.
Returns:
tensor with shape from first argument.
|
[
"A",
"naive",
"pixel",
"-",
"based",
"image",
"parameterization",
".",
"Defaults",
"to",
"a",
"random",
"initialization",
"but",
"can",
"take",
"a",
"supplied",
"init_val",
"argument",
"instead",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/param/spatial.py#L24-L45
|
train
|
tensorflow/lucid
|
lucid/optvis/param/spatial.py
|
rfft2d_freqs
|
def rfft2d_freqs(h, w):
"""Computes 2D spectrum frequencies."""
fy = np.fft.fftfreq(h)[:, None]
# when we have an odd input dimension we need to keep one additional
# frequency and later cut off 1 pixel
if w % 2 == 1:
fx = np.fft.fftfreq(w)[: w // 2 + 2]
else:
fx = np.fft.fftfreq(w)[: w // 2 + 1]
return np.sqrt(fx * fx + fy * fy)
|
python
|
def rfft2d_freqs(h, w):
"""Computes 2D spectrum frequencies."""
fy = np.fft.fftfreq(h)[:, None]
# when we have an odd input dimension we need to keep one additional
# frequency and later cut off 1 pixel
if w % 2 == 1:
fx = np.fft.fftfreq(w)[: w // 2 + 2]
else:
fx = np.fft.fftfreq(w)[: w // 2 + 1]
return np.sqrt(fx * fx + fy * fy)
|
[
"def",
"rfft2d_freqs",
"(",
"h",
",",
"w",
")",
":",
"fy",
"=",
"np",
".",
"fft",
".",
"fftfreq",
"(",
"h",
")",
"[",
":",
",",
"None",
"]",
"# when we have an odd input dimension we need to keep one additional",
"# frequency and later cut off 1 pixel",
"if",
"w",
"%",
"2",
"==",
"1",
":",
"fx",
"=",
"np",
".",
"fft",
".",
"fftfreq",
"(",
"w",
")",
"[",
":",
"w",
"//",
"2",
"+",
"2",
"]",
"else",
":",
"fx",
"=",
"np",
".",
"fft",
".",
"fftfreq",
"(",
"w",
")",
"[",
":",
"w",
"//",
"2",
"+",
"1",
"]",
"return",
"np",
".",
"sqrt",
"(",
"fx",
"*",
"fx",
"+",
"fy",
"*",
"fy",
")"
] |
Computes 2D spectrum frequencies.
|
[
"Computes",
"2D",
"spectrum",
"frequencies",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/param/spatial.py#L48-L58
|
train
|
tensorflow/lucid
|
lucid/optvis/param/spatial.py
|
fft_image
|
def fft_image(shape, sd=None, decay_power=1):
"""An image paramaterization using 2D Fourier coefficients."""
sd = sd or 0.01
batch, h, w, ch = shape
freqs = rfft2d_freqs(h, w)
init_val_size = (2, ch) + freqs.shape
images = []
for _ in range(batch):
# Create a random variable holding the actual 2D fourier coefficients
init_val = np.random.normal(size=init_val_size, scale=sd).astype(np.float32)
spectrum_real_imag_t = tf.Variable(init_val)
spectrum_t = tf.complex(spectrum_real_imag_t[0], spectrum_real_imag_t[1])
# Scale the spectrum. First normalize energy, then scale by the square-root
# of the number of pixels to get a unitary transformation.
# This allows to use similar leanring rates to pixel-wise optimisation.
scale = 1.0 / np.maximum(freqs, 1.0 / max(w, h)) ** decay_power
scale *= np.sqrt(w * h)
scaled_spectrum_t = scale * spectrum_t
# convert complex scaled spectrum to shape (h, w, ch) image tensor
# needs to transpose because irfft2d returns channels first
image_t = tf.transpose(tf.spectral.irfft2d(scaled_spectrum_t), (1, 2, 0))
# in case of odd spatial input dimensions we need to crop
image_t = image_t[:h, :w, :ch]
images.append(image_t)
batched_image_t = tf.stack(images) / 4.0 # TODO: is that a magic constant?
return batched_image_t
|
python
|
def fft_image(shape, sd=None, decay_power=1):
"""An image paramaterization using 2D Fourier coefficients."""
sd = sd or 0.01
batch, h, w, ch = shape
freqs = rfft2d_freqs(h, w)
init_val_size = (2, ch) + freqs.shape
images = []
for _ in range(batch):
# Create a random variable holding the actual 2D fourier coefficients
init_val = np.random.normal(size=init_val_size, scale=sd).astype(np.float32)
spectrum_real_imag_t = tf.Variable(init_val)
spectrum_t = tf.complex(spectrum_real_imag_t[0], spectrum_real_imag_t[1])
# Scale the spectrum. First normalize energy, then scale by the square-root
# of the number of pixels to get a unitary transformation.
# This allows to use similar leanring rates to pixel-wise optimisation.
scale = 1.0 / np.maximum(freqs, 1.0 / max(w, h)) ** decay_power
scale *= np.sqrt(w * h)
scaled_spectrum_t = scale * spectrum_t
# convert complex scaled spectrum to shape (h, w, ch) image tensor
# needs to transpose because irfft2d returns channels first
image_t = tf.transpose(tf.spectral.irfft2d(scaled_spectrum_t), (1, 2, 0))
# in case of odd spatial input dimensions we need to crop
image_t = image_t[:h, :w, :ch]
images.append(image_t)
batched_image_t = tf.stack(images) / 4.0 # TODO: is that a magic constant?
return batched_image_t
|
[
"def",
"fft_image",
"(",
"shape",
",",
"sd",
"=",
"None",
",",
"decay_power",
"=",
"1",
")",
":",
"sd",
"=",
"sd",
"or",
"0.01",
"batch",
",",
"h",
",",
"w",
",",
"ch",
"=",
"shape",
"freqs",
"=",
"rfft2d_freqs",
"(",
"h",
",",
"w",
")",
"init_val_size",
"=",
"(",
"2",
",",
"ch",
")",
"+",
"freqs",
".",
"shape",
"images",
"=",
"[",
"]",
"for",
"_",
"in",
"range",
"(",
"batch",
")",
":",
"# Create a random variable holding the actual 2D fourier coefficients",
"init_val",
"=",
"np",
".",
"random",
".",
"normal",
"(",
"size",
"=",
"init_val_size",
",",
"scale",
"=",
"sd",
")",
".",
"astype",
"(",
"np",
".",
"float32",
")",
"spectrum_real_imag_t",
"=",
"tf",
".",
"Variable",
"(",
"init_val",
")",
"spectrum_t",
"=",
"tf",
".",
"complex",
"(",
"spectrum_real_imag_t",
"[",
"0",
"]",
",",
"spectrum_real_imag_t",
"[",
"1",
"]",
")",
"# Scale the spectrum. First normalize energy, then scale by the square-root",
"# of the number of pixels to get a unitary transformation.",
"# This allows to use similar leanring rates to pixel-wise optimisation.",
"scale",
"=",
"1.0",
"/",
"np",
".",
"maximum",
"(",
"freqs",
",",
"1.0",
"/",
"max",
"(",
"w",
",",
"h",
")",
")",
"**",
"decay_power",
"scale",
"*=",
"np",
".",
"sqrt",
"(",
"w",
"*",
"h",
")",
"scaled_spectrum_t",
"=",
"scale",
"*",
"spectrum_t",
"# convert complex scaled spectrum to shape (h, w, ch) image tensor",
"# needs to transpose because irfft2d returns channels first",
"image_t",
"=",
"tf",
".",
"transpose",
"(",
"tf",
".",
"spectral",
".",
"irfft2d",
"(",
"scaled_spectrum_t",
")",
",",
"(",
"1",
",",
"2",
",",
"0",
")",
")",
"# in case of odd spatial input dimensions we need to crop",
"image_t",
"=",
"image_t",
"[",
":",
"h",
",",
":",
"w",
",",
":",
"ch",
"]",
"images",
".",
"append",
"(",
"image_t",
")",
"batched_image_t",
"=",
"tf",
".",
"stack",
"(",
"images",
")",
"/",
"4.0",
"# TODO: is that a magic constant?",
"return",
"batched_image_t"
] |
An image paramaterization using 2D Fourier coefficients.
|
[
"An",
"image",
"paramaterization",
"using",
"2D",
"Fourier",
"coefficients",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/param/spatial.py#L61-L93
|
train
|
tensorflow/lucid
|
lucid/optvis/param/spatial.py
|
laplacian_pyramid_image
|
def laplacian_pyramid_image(shape, n_levels=4, sd=None):
"""Simple laplacian pyramid paramaterization of an image.
For more flexibility, use a sum of lowres_tensor()s.
Args:
shape: shape of resulting image, [batch, width, height, channels].
n_levels: number of levels of laplacian pyarmid.
sd: standard deviation of param initialization.
Returns:
tensor with shape from first argument.
"""
batch_dims = shape[:-3]
w, h, ch = shape[-3:]
pyramid = 0
for n in range(n_levels):
k = 2 ** n
pyramid += lowres_tensor(shape, batch_dims + (w // k, h // k, ch), sd=sd)
return pyramid
|
python
|
def laplacian_pyramid_image(shape, n_levels=4, sd=None):
"""Simple laplacian pyramid paramaterization of an image.
For more flexibility, use a sum of lowres_tensor()s.
Args:
shape: shape of resulting image, [batch, width, height, channels].
n_levels: number of levels of laplacian pyarmid.
sd: standard deviation of param initialization.
Returns:
tensor with shape from first argument.
"""
batch_dims = shape[:-3]
w, h, ch = shape[-3:]
pyramid = 0
for n in range(n_levels):
k = 2 ** n
pyramid += lowres_tensor(shape, batch_dims + (w // k, h // k, ch), sd=sd)
return pyramid
|
[
"def",
"laplacian_pyramid_image",
"(",
"shape",
",",
"n_levels",
"=",
"4",
",",
"sd",
"=",
"None",
")",
":",
"batch_dims",
"=",
"shape",
"[",
":",
"-",
"3",
"]",
"w",
",",
"h",
",",
"ch",
"=",
"shape",
"[",
"-",
"3",
":",
"]",
"pyramid",
"=",
"0",
"for",
"n",
"in",
"range",
"(",
"n_levels",
")",
":",
"k",
"=",
"2",
"**",
"n",
"pyramid",
"+=",
"lowres_tensor",
"(",
"shape",
",",
"batch_dims",
"+",
"(",
"w",
"//",
"k",
",",
"h",
"//",
"k",
",",
"ch",
")",
",",
"sd",
"=",
"sd",
")",
"return",
"pyramid"
] |
Simple laplacian pyramid paramaterization of an image.
For more flexibility, use a sum of lowres_tensor()s.
Args:
shape: shape of resulting image, [batch, width, height, channels].
n_levels: number of levels of laplacian pyarmid.
sd: standard deviation of param initialization.
Returns:
tensor with shape from first argument.
|
[
"Simple",
"laplacian",
"pyramid",
"paramaterization",
"of",
"an",
"image",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/param/spatial.py#L96-L115
|
train
|
tensorflow/lucid
|
lucid/optvis/param/spatial.py
|
bilinearly_sampled_image
|
def bilinearly_sampled_image(texture, uv):
"""Build bilinear texture sampling graph.
Coordinate transformation rules match OpenGL GL_REPEAT wrapping and GL_LINEAR
interpolation modes.
Args:
texture: [tex_h, tex_w, channel_n] tensor.
uv: [frame_h, frame_h, 2] tensor with per-pixel UV coordinates in range [0..1]
Returns:
[frame_h, frame_h, channel_n] tensor with per-pixel sampled values.
"""
h, w = tf.unstack(tf.shape(texture)[:2])
u, v = tf.split(uv, 2, axis=-1)
v = 1.0 - v # vertical flip to match GL convention
u, v = u * tf.to_float(w) - 0.5, v * tf.to_float(h) - 0.5
u0, u1 = tf.floor(u), tf.ceil(u)
v0, v1 = tf.floor(v), tf.ceil(v)
uf, vf = u - u0, v - v0
u0, u1, v0, v1 = map(tf.to_int32, [u0, u1, v0, v1])
def sample(u, v):
vu = tf.concat([v % h, u % w], axis=-1)
return tf.gather_nd(texture, vu)
s00, s01 = sample(u0, v0), sample(u0, v1)
s10, s11 = sample(u1, v0), sample(u1, v1)
s0 = s00 * (1.0 - vf) + s01 * vf
s1 = s10 * (1.0 - vf) + s11 * vf
s = s0 * (1.0 - uf) + s1 * uf
return s
|
python
|
def bilinearly_sampled_image(texture, uv):
"""Build bilinear texture sampling graph.
Coordinate transformation rules match OpenGL GL_REPEAT wrapping and GL_LINEAR
interpolation modes.
Args:
texture: [tex_h, tex_w, channel_n] tensor.
uv: [frame_h, frame_h, 2] tensor with per-pixel UV coordinates in range [0..1]
Returns:
[frame_h, frame_h, channel_n] tensor with per-pixel sampled values.
"""
h, w = tf.unstack(tf.shape(texture)[:2])
u, v = tf.split(uv, 2, axis=-1)
v = 1.0 - v # vertical flip to match GL convention
u, v = u * tf.to_float(w) - 0.5, v * tf.to_float(h) - 0.5
u0, u1 = tf.floor(u), tf.ceil(u)
v0, v1 = tf.floor(v), tf.ceil(v)
uf, vf = u - u0, v - v0
u0, u1, v0, v1 = map(tf.to_int32, [u0, u1, v0, v1])
def sample(u, v):
vu = tf.concat([v % h, u % w], axis=-1)
return tf.gather_nd(texture, vu)
s00, s01 = sample(u0, v0), sample(u0, v1)
s10, s11 = sample(u1, v0), sample(u1, v1)
s0 = s00 * (1.0 - vf) + s01 * vf
s1 = s10 * (1.0 - vf) + s11 * vf
s = s0 * (1.0 - uf) + s1 * uf
return s
|
[
"def",
"bilinearly_sampled_image",
"(",
"texture",
",",
"uv",
")",
":",
"h",
",",
"w",
"=",
"tf",
".",
"unstack",
"(",
"tf",
".",
"shape",
"(",
"texture",
")",
"[",
":",
"2",
"]",
")",
"u",
",",
"v",
"=",
"tf",
".",
"split",
"(",
"uv",
",",
"2",
",",
"axis",
"=",
"-",
"1",
")",
"v",
"=",
"1.0",
"-",
"v",
"# vertical flip to match GL convention",
"u",
",",
"v",
"=",
"u",
"*",
"tf",
".",
"to_float",
"(",
"w",
")",
"-",
"0.5",
",",
"v",
"*",
"tf",
".",
"to_float",
"(",
"h",
")",
"-",
"0.5",
"u0",
",",
"u1",
"=",
"tf",
".",
"floor",
"(",
"u",
")",
",",
"tf",
".",
"ceil",
"(",
"u",
")",
"v0",
",",
"v1",
"=",
"tf",
".",
"floor",
"(",
"v",
")",
",",
"tf",
".",
"ceil",
"(",
"v",
")",
"uf",
",",
"vf",
"=",
"u",
"-",
"u0",
",",
"v",
"-",
"v0",
"u0",
",",
"u1",
",",
"v0",
",",
"v1",
"=",
"map",
"(",
"tf",
".",
"to_int32",
",",
"[",
"u0",
",",
"u1",
",",
"v0",
",",
"v1",
"]",
")",
"def",
"sample",
"(",
"u",
",",
"v",
")",
":",
"vu",
"=",
"tf",
".",
"concat",
"(",
"[",
"v",
"%",
"h",
",",
"u",
"%",
"w",
"]",
",",
"axis",
"=",
"-",
"1",
")",
"return",
"tf",
".",
"gather_nd",
"(",
"texture",
",",
"vu",
")",
"s00",
",",
"s01",
"=",
"sample",
"(",
"u0",
",",
"v0",
")",
",",
"sample",
"(",
"u0",
",",
"v1",
")",
"s10",
",",
"s11",
"=",
"sample",
"(",
"u1",
",",
"v0",
")",
",",
"sample",
"(",
"u1",
",",
"v1",
")",
"s0",
"=",
"s00",
"*",
"(",
"1.0",
"-",
"vf",
")",
"+",
"s01",
"*",
"vf",
"s1",
"=",
"s10",
"*",
"(",
"1.0",
"-",
"vf",
")",
"+",
"s11",
"*",
"vf",
"s",
"=",
"s0",
"*",
"(",
"1.0",
"-",
"uf",
")",
"+",
"s1",
"*",
"uf",
"return",
"s"
] |
Build bilinear texture sampling graph.
Coordinate transformation rules match OpenGL GL_REPEAT wrapping and GL_LINEAR
interpolation modes.
Args:
texture: [tex_h, tex_w, channel_n] tensor.
uv: [frame_h, frame_h, 2] tensor with per-pixel UV coordinates in range [0..1]
Returns:
[frame_h, frame_h, channel_n] tensor with per-pixel sampled values.
|
[
"Build",
"bilinear",
"texture",
"sampling",
"graph",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/param/spatial.py#L118-L149
|
train
|
tensorflow/lucid
|
lucid/optvis/param/color.py
|
_linear_decorelate_color
|
def _linear_decorelate_color(t):
"""Multiply input by sqrt of emperical (ImageNet) color correlation matrix.
If you interpret t's innermost dimension as describing colors in a
decorrelated version of the color space (which is a very natural way to
describe colors -- see discussion in Feature Visualization article) the way
to map back to normal colors is multiply the square root of your color
correlations.
"""
# check that inner dimension is 3?
t_flat = tf.reshape(t, [-1, 3])
color_correlation_normalized = color_correlation_svd_sqrt / max_norm_svd_sqrt
t_flat = tf.matmul(t_flat, color_correlation_normalized.T)
t = tf.reshape(t_flat, tf.shape(t))
return t
|
python
|
def _linear_decorelate_color(t):
"""Multiply input by sqrt of emperical (ImageNet) color correlation matrix.
If you interpret t's innermost dimension as describing colors in a
decorrelated version of the color space (which is a very natural way to
describe colors -- see discussion in Feature Visualization article) the way
to map back to normal colors is multiply the square root of your color
correlations.
"""
# check that inner dimension is 3?
t_flat = tf.reshape(t, [-1, 3])
color_correlation_normalized = color_correlation_svd_sqrt / max_norm_svd_sqrt
t_flat = tf.matmul(t_flat, color_correlation_normalized.T)
t = tf.reshape(t_flat, tf.shape(t))
return t
|
[
"def",
"_linear_decorelate_color",
"(",
"t",
")",
":",
"# check that inner dimension is 3?",
"t_flat",
"=",
"tf",
".",
"reshape",
"(",
"t",
",",
"[",
"-",
"1",
",",
"3",
"]",
")",
"color_correlation_normalized",
"=",
"color_correlation_svd_sqrt",
"/",
"max_norm_svd_sqrt",
"t_flat",
"=",
"tf",
".",
"matmul",
"(",
"t_flat",
",",
"color_correlation_normalized",
".",
"T",
")",
"t",
"=",
"tf",
".",
"reshape",
"(",
"t_flat",
",",
"tf",
".",
"shape",
"(",
"t",
")",
")",
"return",
"t"
] |
Multiply input by sqrt of emperical (ImageNet) color correlation matrix.
If you interpret t's innermost dimension as describing colors in a
decorrelated version of the color space (which is a very natural way to
describe colors -- see discussion in Feature Visualization article) the way
to map back to normal colors is multiply the square root of your color
correlations.
|
[
"Multiply",
"input",
"by",
"sqrt",
"of",
"emperical",
"(",
"ImageNet",
")",
"color",
"correlation",
"matrix",
".",
"If",
"you",
"interpret",
"t",
"s",
"innermost",
"dimension",
"as",
"describing",
"colors",
"in",
"a",
"decorrelated",
"version",
"of",
"the",
"color",
"space",
"(",
"which",
"is",
"a",
"very",
"natural",
"way",
"to",
"describe",
"colors",
"--",
"see",
"discussion",
"in",
"Feature",
"Visualization",
"article",
")",
"the",
"way",
"to",
"map",
"back",
"to",
"normal",
"colors",
"is",
"multiply",
"the",
"square",
"root",
"of",
"your",
"color",
"correlations",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/param/color.py#L32-L46
|
train
|
tensorflow/lucid
|
lucid/optvis/param/color.py
|
to_valid_rgb
|
def to_valid_rgb(t, decorrelate=False, sigmoid=True):
"""Transform inner dimension of t to valid rgb colors.
In practice this consistes of two parts:
(1) If requested, transform the colors from a decorrelated color space to RGB.
(2) Constrain the color channels to be in [0,1], either using a sigmoid
function or clipping.
Args:
t: input tensor, innermost dimension will be interpreted as colors
and transformed/constrained.
decorrelate: should the input tensor's colors be interpreted as coming from
a whitened space or not?
sigmoid: should the colors be constrained using sigmoid (if True) or
clipping (if False).
Returns:
t with the innermost dimension transformed.
"""
if decorrelate:
t = _linear_decorelate_color(t)
if decorrelate and not sigmoid:
t += color_mean
if sigmoid:
return tf.nn.sigmoid(t)
else:
return constrain_L_inf(2*t-1)/2 + 0.5
|
python
|
def to_valid_rgb(t, decorrelate=False, sigmoid=True):
"""Transform inner dimension of t to valid rgb colors.
In practice this consistes of two parts:
(1) If requested, transform the colors from a decorrelated color space to RGB.
(2) Constrain the color channels to be in [0,1], either using a sigmoid
function or clipping.
Args:
t: input tensor, innermost dimension will be interpreted as colors
and transformed/constrained.
decorrelate: should the input tensor's colors be interpreted as coming from
a whitened space or not?
sigmoid: should the colors be constrained using sigmoid (if True) or
clipping (if False).
Returns:
t with the innermost dimension transformed.
"""
if decorrelate:
t = _linear_decorelate_color(t)
if decorrelate and not sigmoid:
t += color_mean
if sigmoid:
return tf.nn.sigmoid(t)
else:
return constrain_L_inf(2*t-1)/2 + 0.5
|
[
"def",
"to_valid_rgb",
"(",
"t",
",",
"decorrelate",
"=",
"False",
",",
"sigmoid",
"=",
"True",
")",
":",
"if",
"decorrelate",
":",
"t",
"=",
"_linear_decorelate_color",
"(",
"t",
")",
"if",
"decorrelate",
"and",
"not",
"sigmoid",
":",
"t",
"+=",
"color_mean",
"if",
"sigmoid",
":",
"return",
"tf",
".",
"nn",
".",
"sigmoid",
"(",
"t",
")",
"else",
":",
"return",
"constrain_L_inf",
"(",
"2",
"*",
"t",
"-",
"1",
")",
"/",
"2",
"+",
"0.5"
] |
Transform inner dimension of t to valid rgb colors.
In practice this consistes of two parts:
(1) If requested, transform the colors from a decorrelated color space to RGB.
(2) Constrain the color channels to be in [0,1], either using a sigmoid
function or clipping.
Args:
t: input tensor, innermost dimension will be interpreted as colors
and transformed/constrained.
decorrelate: should the input tensor's colors be interpreted as coming from
a whitened space or not?
sigmoid: should the colors be constrained using sigmoid (if True) or
clipping (if False).
Returns:
t with the innermost dimension transformed.
|
[
"Transform",
"inner",
"dimension",
"of",
"t",
"to",
"valid",
"rgb",
"colors",
".",
"In",
"practice",
"this",
"consistes",
"of",
"two",
"parts",
":",
"(",
"1",
")",
"If",
"requested",
"transform",
"the",
"colors",
"from",
"a",
"decorrelated",
"color",
"space",
"to",
"RGB",
".",
"(",
"2",
")",
"Constrain",
"the",
"color",
"channels",
"to",
"be",
"in",
"[",
"0",
"1",
"]",
"either",
"using",
"a",
"sigmoid",
"function",
"or",
"clipping",
".",
"Args",
":",
"t",
":",
"input",
"tensor",
"innermost",
"dimension",
"will",
"be",
"interpreted",
"as",
"colors",
"and",
"transformed",
"/",
"constrained",
".",
"decorrelate",
":",
"should",
"the",
"input",
"tensor",
"s",
"colors",
"be",
"interpreted",
"as",
"coming",
"from",
"a",
"whitened",
"space",
"or",
"not?",
"sigmoid",
":",
"should",
"the",
"colors",
"be",
"constrained",
"using",
"sigmoid",
"(",
"if",
"True",
")",
"or",
"clipping",
"(",
"if",
"False",
")",
".",
"Returns",
":",
"t",
"with",
"the",
"innermost",
"dimension",
"transformed",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/param/color.py#L49-L75
|
train
|
tensorflow/lucid
|
lucid/modelzoo/other_models/InceptionV1.py
|
_populate_inception_bottlenecks
|
def _populate_inception_bottlenecks(scope):
"""Add Inception bottlenecks and their pre-Relu versions to the graph."""
graph = tf.get_default_graph()
for op in graph.get_operations():
if op.name.startswith(scope+'/') and 'Concat' in op.type:
name = op.name.split('/')[1]
pre_relus = []
for tower in op.inputs[1:]:
if tower.op.type == 'Relu':
tower = tower.op.inputs[0]
pre_relus.append(tower)
concat_name = scope + '/' + name + '_pre_relu'
_ = tf.concat(pre_relus, -1, name=concat_name)
|
python
|
def _populate_inception_bottlenecks(scope):
"""Add Inception bottlenecks and their pre-Relu versions to the graph."""
graph = tf.get_default_graph()
for op in graph.get_operations():
if op.name.startswith(scope+'/') and 'Concat' in op.type:
name = op.name.split('/')[1]
pre_relus = []
for tower in op.inputs[1:]:
if tower.op.type == 'Relu':
tower = tower.op.inputs[0]
pre_relus.append(tower)
concat_name = scope + '/' + name + '_pre_relu'
_ = tf.concat(pre_relus, -1, name=concat_name)
|
[
"def",
"_populate_inception_bottlenecks",
"(",
"scope",
")",
":",
"graph",
"=",
"tf",
".",
"get_default_graph",
"(",
")",
"for",
"op",
"in",
"graph",
".",
"get_operations",
"(",
")",
":",
"if",
"op",
".",
"name",
".",
"startswith",
"(",
"scope",
"+",
"'/'",
")",
"and",
"'Concat'",
"in",
"op",
".",
"type",
":",
"name",
"=",
"op",
".",
"name",
".",
"split",
"(",
"'/'",
")",
"[",
"1",
"]",
"pre_relus",
"=",
"[",
"]",
"for",
"tower",
"in",
"op",
".",
"inputs",
"[",
"1",
":",
"]",
":",
"if",
"tower",
".",
"op",
".",
"type",
"==",
"'Relu'",
":",
"tower",
"=",
"tower",
".",
"op",
".",
"inputs",
"[",
"0",
"]",
"pre_relus",
".",
"append",
"(",
"tower",
")",
"concat_name",
"=",
"scope",
"+",
"'/'",
"+",
"name",
"+",
"'_pre_relu'",
"_",
"=",
"tf",
".",
"concat",
"(",
"pre_relus",
",",
"-",
"1",
",",
"name",
"=",
"concat_name",
")"
] |
Add Inception bottlenecks and their pre-Relu versions to the graph.
|
[
"Add",
"Inception",
"bottlenecks",
"and",
"their",
"pre",
"-",
"Relu",
"versions",
"to",
"the",
"graph",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/modelzoo/other_models/InceptionV1.py#L22-L34
|
train
|
tensorflow/lucid
|
lucid/optvis/objectives.py
|
wrap_objective
|
def wrap_objective(f, *args, **kwds):
"""Decorator for creating Objective factories.
Changes f from the closure: (args) => () => TF Tensor
into an Obejective factory: (args) => Objective
while perserving function name, arg info, docs... for interactive python.
"""
objective_func = f(*args, **kwds)
objective_name = f.__name__
args_str = " [" + ", ".join([_make_arg_str(arg) for arg in args]) + "]"
description = objective_name.title() + args_str
return Objective(objective_func, objective_name, description)
|
python
|
def wrap_objective(f, *args, **kwds):
"""Decorator for creating Objective factories.
Changes f from the closure: (args) => () => TF Tensor
into an Obejective factory: (args) => Objective
while perserving function name, arg info, docs... for interactive python.
"""
objective_func = f(*args, **kwds)
objective_name = f.__name__
args_str = " [" + ", ".join([_make_arg_str(arg) for arg in args]) + "]"
description = objective_name.title() + args_str
return Objective(objective_func, objective_name, description)
|
[
"def",
"wrap_objective",
"(",
"f",
",",
"*",
"args",
",",
"*",
"*",
"kwds",
")",
":",
"objective_func",
"=",
"f",
"(",
"*",
"args",
",",
"*",
"*",
"kwds",
")",
"objective_name",
"=",
"f",
".",
"__name__",
"args_str",
"=",
"\" [\"",
"+",
"\", \"",
".",
"join",
"(",
"[",
"_make_arg_str",
"(",
"arg",
")",
"for",
"arg",
"in",
"args",
"]",
")",
"+",
"\"]\"",
"description",
"=",
"objective_name",
".",
"title",
"(",
")",
"+",
"args_str",
"return",
"Objective",
"(",
"objective_func",
",",
"objective_name",
",",
"description",
")"
] |
Decorator for creating Objective factories.
Changes f from the closure: (args) => () => TF Tensor
into an Obejective factory: (args) => Objective
while perserving function name, arg info, docs... for interactive python.
|
[
"Decorator",
"for",
"creating",
"Objective",
"factories",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/objectives.py#L117-L129
|
train
|
tensorflow/lucid
|
lucid/optvis/objectives.py
|
neuron
|
def neuron(layer_name, channel_n, x=None, y=None, batch=None):
"""Visualize a single neuron of a single channel.
Defaults to the center neuron. When width and height are even numbers, we
choose the neuron in the bottom right of the center 2x2 neurons.
Odd width & height: Even width & height:
+---+---+---+ +---+---+---+---+
| | | | | | | | |
+---+---+---+ +---+---+---+---+
| | X | | | | | | |
+---+---+---+ +---+---+---+---+
| | | | | | | X | |
+---+---+---+ +---+---+---+---+
| | | | |
+---+---+---+---+
"""
def inner(T):
layer = T(layer_name)
shape = tf.shape(layer)
x_ = shape[1] // 2 if x is None else x
y_ = shape[2] // 2 if y is None else y
if batch is None:
return layer[:, x_, y_, channel_n]
else:
return layer[batch, x_, y_, channel_n]
return inner
|
python
|
def neuron(layer_name, channel_n, x=None, y=None, batch=None):
"""Visualize a single neuron of a single channel.
Defaults to the center neuron. When width and height are even numbers, we
choose the neuron in the bottom right of the center 2x2 neurons.
Odd width & height: Even width & height:
+---+---+---+ +---+---+---+---+
| | | | | | | | |
+---+---+---+ +---+---+---+---+
| | X | | | | | | |
+---+---+---+ +---+---+---+---+
| | | | | | | X | |
+---+---+---+ +---+---+---+---+
| | | | |
+---+---+---+---+
"""
def inner(T):
layer = T(layer_name)
shape = tf.shape(layer)
x_ = shape[1] // 2 if x is None else x
y_ = shape[2] // 2 if y is None else y
if batch is None:
return layer[:, x_, y_, channel_n]
else:
return layer[batch, x_, y_, channel_n]
return inner
|
[
"def",
"neuron",
"(",
"layer_name",
",",
"channel_n",
",",
"x",
"=",
"None",
",",
"y",
"=",
"None",
",",
"batch",
"=",
"None",
")",
":",
"def",
"inner",
"(",
"T",
")",
":",
"layer",
"=",
"T",
"(",
"layer_name",
")",
"shape",
"=",
"tf",
".",
"shape",
"(",
"layer",
")",
"x_",
"=",
"shape",
"[",
"1",
"]",
"//",
"2",
"if",
"x",
"is",
"None",
"else",
"x",
"y_",
"=",
"shape",
"[",
"2",
"]",
"//",
"2",
"if",
"y",
"is",
"None",
"else",
"y",
"if",
"batch",
"is",
"None",
":",
"return",
"layer",
"[",
":",
",",
"x_",
",",
"y_",
",",
"channel_n",
"]",
"else",
":",
"return",
"layer",
"[",
"batch",
",",
"x_",
",",
"y_",
",",
"channel_n",
"]",
"return",
"inner"
] |
Visualize a single neuron of a single channel.
Defaults to the center neuron. When width and height are even numbers, we
choose the neuron in the bottom right of the center 2x2 neurons.
Odd width & height: Even width & height:
+---+---+---+ +---+---+---+---+
| | | | | | | | |
+---+---+---+ +---+---+---+---+
| | X | | | | | | |
+---+---+---+ +---+---+---+---+
| | | | | | | X | |
+---+---+---+ +---+---+---+---+
| | | | |
+---+---+---+---+
|
[
"Visualize",
"a",
"single",
"neuron",
"of",
"a",
"single",
"channel",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/objectives.py#L133-L161
|
train
|
tensorflow/lucid
|
lucid/optvis/objectives.py
|
channel
|
def channel(layer, n_channel, batch=None):
"""Visualize a single channel"""
if batch is None:
return lambda T: tf.reduce_mean(T(layer)[..., n_channel])
else:
return lambda T: tf.reduce_mean(T(layer)[batch, ..., n_channel])
|
python
|
def channel(layer, n_channel, batch=None):
"""Visualize a single channel"""
if batch is None:
return lambda T: tf.reduce_mean(T(layer)[..., n_channel])
else:
return lambda T: tf.reduce_mean(T(layer)[batch, ..., n_channel])
|
[
"def",
"channel",
"(",
"layer",
",",
"n_channel",
",",
"batch",
"=",
"None",
")",
":",
"if",
"batch",
"is",
"None",
":",
"return",
"lambda",
"T",
":",
"tf",
".",
"reduce_mean",
"(",
"T",
"(",
"layer",
")",
"[",
"...",
",",
"n_channel",
"]",
")",
"else",
":",
"return",
"lambda",
"T",
":",
"tf",
".",
"reduce_mean",
"(",
"T",
"(",
"layer",
")",
"[",
"batch",
",",
"...",
",",
"n_channel",
"]",
")"
] |
Visualize a single channel
|
[
"Visualize",
"a",
"single",
"channel"
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/objectives.py#L165-L170
|
train
|
tensorflow/lucid
|
lucid/optvis/objectives.py
|
direction
|
def direction(layer, vec, batch=None, cossim_pow=0):
"""Visualize a direction"""
if batch is None:
vec = vec[None, None, None]
return lambda T: _dot_cossim(T(layer), vec)
else:
vec = vec[None, None]
return lambda T: _dot_cossim(T(layer)[batch], vec)
|
python
|
def direction(layer, vec, batch=None, cossim_pow=0):
"""Visualize a direction"""
if batch is None:
vec = vec[None, None, None]
return lambda T: _dot_cossim(T(layer), vec)
else:
vec = vec[None, None]
return lambda T: _dot_cossim(T(layer)[batch], vec)
|
[
"def",
"direction",
"(",
"layer",
",",
"vec",
",",
"batch",
"=",
"None",
",",
"cossim_pow",
"=",
"0",
")",
":",
"if",
"batch",
"is",
"None",
":",
"vec",
"=",
"vec",
"[",
"None",
",",
"None",
",",
"None",
"]",
"return",
"lambda",
"T",
":",
"_dot_cossim",
"(",
"T",
"(",
"layer",
")",
",",
"vec",
")",
"else",
":",
"vec",
"=",
"vec",
"[",
"None",
",",
"None",
"]",
"return",
"lambda",
"T",
":",
"_dot_cossim",
"(",
"T",
"(",
"layer",
")",
"[",
"batch",
"]",
",",
"vec",
")"
] |
Visualize a direction
|
[
"Visualize",
"a",
"direction"
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/objectives.py#L189-L196
|
train
|
tensorflow/lucid
|
lucid/optvis/objectives.py
|
direction_neuron
|
def direction_neuron(layer_name, vec, batch=None, x=None, y=None, cossim_pow=0):
"""Visualize a single (x, y) position along the given direction"""
def inner(T):
layer = T(layer_name)
shape = tf.shape(layer)
x_ = shape[1] // 2 if x is None else x
y_ = shape[2] // 2 if y is None else y
if batch is None:
return _dot_cossim(layer[:, x_, y_], vec[None], cossim_pow=cossim_pow)
else:
return _dot_cossim(layer[batch, x_, y_], vec, cossim_pow=cossim_pow)
return inner
|
python
|
def direction_neuron(layer_name, vec, batch=None, x=None, y=None, cossim_pow=0):
"""Visualize a single (x, y) position along the given direction"""
def inner(T):
layer = T(layer_name)
shape = tf.shape(layer)
x_ = shape[1] // 2 if x is None else x
y_ = shape[2] // 2 if y is None else y
if batch is None:
return _dot_cossim(layer[:, x_, y_], vec[None], cossim_pow=cossim_pow)
else:
return _dot_cossim(layer[batch, x_, y_], vec, cossim_pow=cossim_pow)
return inner
|
[
"def",
"direction_neuron",
"(",
"layer_name",
",",
"vec",
",",
"batch",
"=",
"None",
",",
"x",
"=",
"None",
",",
"y",
"=",
"None",
",",
"cossim_pow",
"=",
"0",
")",
":",
"def",
"inner",
"(",
"T",
")",
":",
"layer",
"=",
"T",
"(",
"layer_name",
")",
"shape",
"=",
"tf",
".",
"shape",
"(",
"layer",
")",
"x_",
"=",
"shape",
"[",
"1",
"]",
"//",
"2",
"if",
"x",
"is",
"None",
"else",
"x",
"y_",
"=",
"shape",
"[",
"2",
"]",
"//",
"2",
"if",
"y",
"is",
"None",
"else",
"y",
"if",
"batch",
"is",
"None",
":",
"return",
"_dot_cossim",
"(",
"layer",
"[",
":",
",",
"x_",
",",
"y_",
"]",
",",
"vec",
"[",
"None",
"]",
",",
"cossim_pow",
"=",
"cossim_pow",
")",
"else",
":",
"return",
"_dot_cossim",
"(",
"layer",
"[",
"batch",
",",
"x_",
",",
"y_",
"]",
",",
"vec",
",",
"cossim_pow",
"=",
"cossim_pow",
")",
"return",
"inner"
] |
Visualize a single (x, y) position along the given direction
|
[
"Visualize",
"a",
"single",
"(",
"x",
"y",
")",
"position",
"along",
"the",
"given",
"direction"
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/objectives.py#L200-L211
|
train
|
tensorflow/lucid
|
lucid/optvis/objectives.py
|
direction_cossim
|
def direction_cossim(layer, vec, batch=None):
"""Visualize a direction (cossine similarity)"""
def inner(T):
act_mags = tf.sqrt(tf.reduce_sum(T(layer)**2, -1, keepdims=True))
vec_mag = tf.sqrt(tf.reduce_sum(vec**2))
mags = act_mags * vec_mag
if batch is None:
return tf.reduce_mean(T(layer) * vec.reshape([1, 1, 1, -1]) / mags)
else:
return tf.reduce_mean(T(layer)[batch] * vec.reshape([1, 1, -1]) / mags)
return inner
|
python
|
def direction_cossim(layer, vec, batch=None):
"""Visualize a direction (cossine similarity)"""
def inner(T):
act_mags = tf.sqrt(tf.reduce_sum(T(layer)**2, -1, keepdims=True))
vec_mag = tf.sqrt(tf.reduce_sum(vec**2))
mags = act_mags * vec_mag
if batch is None:
return tf.reduce_mean(T(layer) * vec.reshape([1, 1, 1, -1]) / mags)
else:
return tf.reduce_mean(T(layer)[batch] * vec.reshape([1, 1, -1]) / mags)
return inner
|
[
"def",
"direction_cossim",
"(",
"layer",
",",
"vec",
",",
"batch",
"=",
"None",
")",
":",
"def",
"inner",
"(",
"T",
")",
":",
"act_mags",
"=",
"tf",
".",
"sqrt",
"(",
"tf",
".",
"reduce_sum",
"(",
"T",
"(",
"layer",
")",
"**",
"2",
",",
"-",
"1",
",",
"keepdims",
"=",
"True",
")",
")",
"vec_mag",
"=",
"tf",
".",
"sqrt",
"(",
"tf",
".",
"reduce_sum",
"(",
"vec",
"**",
"2",
")",
")",
"mags",
"=",
"act_mags",
"*",
"vec_mag",
"if",
"batch",
"is",
"None",
":",
"return",
"tf",
".",
"reduce_mean",
"(",
"T",
"(",
"layer",
")",
"*",
"vec",
".",
"reshape",
"(",
"[",
"1",
",",
"1",
",",
"1",
",",
"-",
"1",
"]",
")",
"/",
"mags",
")",
"else",
":",
"return",
"tf",
".",
"reduce_mean",
"(",
"T",
"(",
"layer",
")",
"[",
"batch",
"]",
"*",
"vec",
".",
"reshape",
"(",
"[",
"1",
",",
"1",
",",
"-",
"1",
"]",
")",
"/",
"mags",
")",
"return",
"inner"
] |
Visualize a direction (cossine similarity)
|
[
"Visualize",
"a",
"direction",
"(",
"cossine",
"similarity",
")"
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/objectives.py#L214-L224
|
train
|
tensorflow/lucid
|
lucid/optvis/objectives.py
|
L1
|
def L1(layer="input", constant=0, batch=None):
"""L1 norm of layer. Generally used as penalty."""
if batch is None:
return lambda T: tf.reduce_sum(tf.abs(T(layer) - constant))
else:
return lambda T: tf.reduce_sum(tf.abs(T(layer)[batch] - constant))
|
python
|
def L1(layer="input", constant=0, batch=None):
"""L1 norm of layer. Generally used as penalty."""
if batch is None:
return lambda T: tf.reduce_sum(tf.abs(T(layer) - constant))
else:
return lambda T: tf.reduce_sum(tf.abs(T(layer)[batch] - constant))
|
[
"def",
"L1",
"(",
"layer",
"=",
"\"input\"",
",",
"constant",
"=",
"0",
",",
"batch",
"=",
"None",
")",
":",
"if",
"batch",
"is",
"None",
":",
"return",
"lambda",
"T",
":",
"tf",
".",
"reduce_sum",
"(",
"tf",
".",
"abs",
"(",
"T",
"(",
"layer",
")",
"-",
"constant",
")",
")",
"else",
":",
"return",
"lambda",
"T",
":",
"tf",
".",
"reduce_sum",
"(",
"tf",
".",
"abs",
"(",
"T",
"(",
"layer",
")",
"[",
"batch",
"]",
"-",
"constant",
")",
")"
] |
L1 norm of layer. Generally used as penalty.
|
[
"L1",
"norm",
"of",
"layer",
".",
"Generally",
"used",
"as",
"penalty",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/objectives.py#L247-L252
|
train
|
tensorflow/lucid
|
lucid/optvis/objectives.py
|
L2
|
def L2(layer="input", constant=0, epsilon=1e-6, batch=None):
"""L2 norm of layer. Generally used as penalty."""
if batch is None:
return lambda T: tf.sqrt(epsilon + tf.reduce_sum((T(layer) - constant) ** 2))
else:
return lambda T: tf.sqrt(epsilon + tf.reduce_sum((T(layer)[batch] - constant) ** 2))
|
python
|
def L2(layer="input", constant=0, epsilon=1e-6, batch=None):
"""L2 norm of layer. Generally used as penalty."""
if batch is None:
return lambda T: tf.sqrt(epsilon + tf.reduce_sum((T(layer) - constant) ** 2))
else:
return lambda T: tf.sqrt(epsilon + tf.reduce_sum((T(layer)[batch] - constant) ** 2))
|
[
"def",
"L2",
"(",
"layer",
"=",
"\"input\"",
",",
"constant",
"=",
"0",
",",
"epsilon",
"=",
"1e-6",
",",
"batch",
"=",
"None",
")",
":",
"if",
"batch",
"is",
"None",
":",
"return",
"lambda",
"T",
":",
"tf",
".",
"sqrt",
"(",
"epsilon",
"+",
"tf",
".",
"reduce_sum",
"(",
"(",
"T",
"(",
"layer",
")",
"-",
"constant",
")",
"**",
"2",
")",
")",
"else",
":",
"return",
"lambda",
"T",
":",
"tf",
".",
"sqrt",
"(",
"epsilon",
"+",
"tf",
".",
"reduce_sum",
"(",
"(",
"T",
"(",
"layer",
")",
"[",
"batch",
"]",
"-",
"constant",
")",
"**",
"2",
")",
")"
] |
L2 norm of layer. Generally used as penalty.
|
[
"L2",
"norm",
"of",
"layer",
".",
"Generally",
"used",
"as",
"penalty",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/objectives.py#L256-L261
|
train
|
tensorflow/lucid
|
lucid/optvis/objectives.py
|
blur_input_each_step
|
def blur_input_each_step():
"""Minimizing this objective is equivelant to blurring input each step.
Optimizing (-k)*blur_input_each_step() is equivelant to:
input <- (1-k)*input + k*blur(input)
An operation that was used in early feature visualization work.
See Nguyen, et al., 2015.
"""
def inner(T):
t_input = T("input")
t_input_blurred = tf.stop_gradient(_tf_blur(t_input))
return 0.5*tf.reduce_sum((t_input - t_input_blurred)**2)
return inner
|
python
|
def blur_input_each_step():
"""Minimizing this objective is equivelant to blurring input each step.
Optimizing (-k)*blur_input_each_step() is equivelant to:
input <- (1-k)*input + k*blur(input)
An operation that was used in early feature visualization work.
See Nguyen, et al., 2015.
"""
def inner(T):
t_input = T("input")
t_input_blurred = tf.stop_gradient(_tf_blur(t_input))
return 0.5*tf.reduce_sum((t_input - t_input_blurred)**2)
return inner
|
[
"def",
"blur_input_each_step",
"(",
")",
":",
"def",
"inner",
"(",
"T",
")",
":",
"t_input",
"=",
"T",
"(",
"\"input\"",
")",
"t_input_blurred",
"=",
"tf",
".",
"stop_gradient",
"(",
"_tf_blur",
"(",
"t_input",
")",
")",
"return",
"0.5",
"*",
"tf",
".",
"reduce_sum",
"(",
"(",
"t_input",
"-",
"t_input_blurred",
")",
"**",
"2",
")",
"return",
"inner"
] |
Minimizing this objective is equivelant to blurring input each step.
Optimizing (-k)*blur_input_each_step() is equivelant to:
input <- (1-k)*input + k*blur(input)
An operation that was used in early feature visualization work.
See Nguyen, et al., 2015.
|
[
"Minimizing",
"this",
"objective",
"is",
"equivelant",
"to",
"blurring",
"input",
"each",
"step",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/objectives.py#L277-L291
|
train
|
tensorflow/lucid
|
lucid/optvis/objectives.py
|
channel_interpolate
|
def channel_interpolate(layer1, n_channel1, layer2, n_channel2):
"""Interpolate between layer1, n_channel1 and layer2, n_channel2.
Optimize for a convex combination of layer1, n_channel1 and
layer2, n_channel2, transitioning across the batch.
Args:
layer1: layer to optimize 100% at batch=0.
n_channel1: neuron index to optimize 100% at batch=0.
layer2: layer to optimize 100% at batch=N.
n_channel2: neuron index to optimize 100% at batch=N.
Returns:
Objective
"""
def inner(T):
batch_n = T(layer1).get_shape().as_list()[0]
arr1 = T(layer1)[..., n_channel1]
arr2 = T(layer2)[..., n_channel2]
weights = (np.arange(batch_n)/float(batch_n-1))
S = 0
for n in range(batch_n):
S += (1-weights[n]) * tf.reduce_mean(arr1[n])
S += weights[n] * tf.reduce_mean(arr2[n])
return S
return inner
|
python
|
def channel_interpolate(layer1, n_channel1, layer2, n_channel2):
"""Interpolate between layer1, n_channel1 and layer2, n_channel2.
Optimize for a convex combination of layer1, n_channel1 and
layer2, n_channel2, transitioning across the batch.
Args:
layer1: layer to optimize 100% at batch=0.
n_channel1: neuron index to optimize 100% at batch=0.
layer2: layer to optimize 100% at batch=N.
n_channel2: neuron index to optimize 100% at batch=N.
Returns:
Objective
"""
def inner(T):
batch_n = T(layer1).get_shape().as_list()[0]
arr1 = T(layer1)[..., n_channel1]
arr2 = T(layer2)[..., n_channel2]
weights = (np.arange(batch_n)/float(batch_n-1))
S = 0
for n in range(batch_n):
S += (1-weights[n]) * tf.reduce_mean(arr1[n])
S += weights[n] * tf.reduce_mean(arr2[n])
return S
return inner
|
[
"def",
"channel_interpolate",
"(",
"layer1",
",",
"n_channel1",
",",
"layer2",
",",
"n_channel2",
")",
":",
"def",
"inner",
"(",
"T",
")",
":",
"batch_n",
"=",
"T",
"(",
"layer1",
")",
".",
"get_shape",
"(",
")",
".",
"as_list",
"(",
")",
"[",
"0",
"]",
"arr1",
"=",
"T",
"(",
"layer1",
")",
"[",
"...",
",",
"n_channel1",
"]",
"arr2",
"=",
"T",
"(",
"layer2",
")",
"[",
"...",
",",
"n_channel2",
"]",
"weights",
"=",
"(",
"np",
".",
"arange",
"(",
"batch_n",
")",
"/",
"float",
"(",
"batch_n",
"-",
"1",
")",
")",
"S",
"=",
"0",
"for",
"n",
"in",
"range",
"(",
"batch_n",
")",
":",
"S",
"+=",
"(",
"1",
"-",
"weights",
"[",
"n",
"]",
")",
"*",
"tf",
".",
"reduce_mean",
"(",
"arr1",
"[",
"n",
"]",
")",
"S",
"+=",
"weights",
"[",
"n",
"]",
"*",
"tf",
".",
"reduce_mean",
"(",
"arr2",
"[",
"n",
"]",
")",
"return",
"S",
"return",
"inner"
] |
Interpolate between layer1, n_channel1 and layer2, n_channel2.
Optimize for a convex combination of layer1, n_channel1 and
layer2, n_channel2, transitioning across the batch.
Args:
layer1: layer to optimize 100% at batch=0.
n_channel1: neuron index to optimize 100% at batch=0.
layer2: layer to optimize 100% at batch=N.
n_channel2: neuron index to optimize 100% at batch=N.
Returns:
Objective
|
[
"Interpolate",
"between",
"layer1",
"n_channel1",
"and",
"layer2",
"n_channel2",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/objectives.py#L303-L328
|
train
|
tensorflow/lucid
|
lucid/optvis/objectives.py
|
penalize_boundary_complexity
|
def penalize_boundary_complexity(shp, w=20, mask=None, C=0.5):
"""Encourage the boundaries of an image to have less variation and of color C.
Args:
shp: shape of T("input") because this may not be known.
w: width of boundary to penalize. Ignored if mask is set.
mask: mask describing what area should be penalized.
Returns:
Objective.
"""
def inner(T):
arr = T("input")
# print shp
if mask is None:
mask_ = np.ones(shp)
mask_[:, w:-w, w:-w] = 0
else:
mask_ = mask
blur = _tf_blur(arr, w=5)
diffs = (blur-arr)**2
diffs += 0.8*(arr-C)**2
return -tf.reduce_sum(diffs*mask_)
return inner
|
python
|
def penalize_boundary_complexity(shp, w=20, mask=None, C=0.5):
"""Encourage the boundaries of an image to have less variation and of color C.
Args:
shp: shape of T("input") because this may not be known.
w: width of boundary to penalize. Ignored if mask is set.
mask: mask describing what area should be penalized.
Returns:
Objective.
"""
def inner(T):
arr = T("input")
# print shp
if mask is None:
mask_ = np.ones(shp)
mask_[:, w:-w, w:-w] = 0
else:
mask_ = mask
blur = _tf_blur(arr, w=5)
diffs = (blur-arr)**2
diffs += 0.8*(arr-C)**2
return -tf.reduce_sum(diffs*mask_)
return inner
|
[
"def",
"penalize_boundary_complexity",
"(",
"shp",
",",
"w",
"=",
"20",
",",
"mask",
"=",
"None",
",",
"C",
"=",
"0.5",
")",
":",
"def",
"inner",
"(",
"T",
")",
":",
"arr",
"=",
"T",
"(",
"\"input\"",
")",
"# print shp",
"if",
"mask",
"is",
"None",
":",
"mask_",
"=",
"np",
".",
"ones",
"(",
"shp",
")",
"mask_",
"[",
":",
",",
"w",
":",
"-",
"w",
",",
"w",
":",
"-",
"w",
"]",
"=",
"0",
"else",
":",
"mask_",
"=",
"mask",
"blur",
"=",
"_tf_blur",
"(",
"arr",
",",
"w",
"=",
"5",
")",
"diffs",
"=",
"(",
"blur",
"-",
"arr",
")",
"**",
"2",
"diffs",
"+=",
"0.8",
"*",
"(",
"arr",
"-",
"C",
")",
"**",
"2",
"return",
"-",
"tf",
".",
"reduce_sum",
"(",
"diffs",
"*",
"mask_",
")",
"return",
"inner"
] |
Encourage the boundaries of an image to have less variation and of color C.
Args:
shp: shape of T("input") because this may not be known.
w: width of boundary to penalize. Ignored if mask is set.
mask: mask describing what area should be penalized.
Returns:
Objective.
|
[
"Encourage",
"the",
"boundaries",
"of",
"an",
"image",
"to",
"have",
"less",
"variation",
"and",
"of",
"color",
"C",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/objectives.py#L332-L358
|
train
|
tensorflow/lucid
|
lucid/optvis/objectives.py
|
alignment
|
def alignment(layer, decay_ratio=2):
"""Encourage neighboring images to be similar.
When visualizing the interpolation between two objectives, it's often
desireable to encourage analagous boejcts to be drawn in the same position,
to make them more comparable.
This term penalizes L2 distance between neighboring images, as evaluated at
layer.
In general, we find this most effective if used with a paramaterization that
shares across the batch. (In fact, that works quite well by iteself, so this
function may just be obselete.)
Args:
layer: layer to penalize at.
decay_ratio: how much to decay penalty as images move apart in batch.
Returns:
Objective.
"""
def inner(T):
batch_n = T(layer).get_shape().as_list()[0]
arr = T(layer)
accum = 0
for d in [1, 2, 3, 4]:
for i in range(batch_n - d):
a, b = i, i+d
arr1, arr2 = arr[a], arr[b]
accum += tf.reduce_mean((arr1-arr2)**2) / decay_ratio**float(d)
return -accum
return inner
|
python
|
def alignment(layer, decay_ratio=2):
"""Encourage neighboring images to be similar.
When visualizing the interpolation between two objectives, it's often
desireable to encourage analagous boejcts to be drawn in the same position,
to make them more comparable.
This term penalizes L2 distance between neighboring images, as evaluated at
layer.
In general, we find this most effective if used with a paramaterization that
shares across the batch. (In fact, that works quite well by iteself, so this
function may just be obselete.)
Args:
layer: layer to penalize at.
decay_ratio: how much to decay penalty as images move apart in batch.
Returns:
Objective.
"""
def inner(T):
batch_n = T(layer).get_shape().as_list()[0]
arr = T(layer)
accum = 0
for d in [1, 2, 3, 4]:
for i in range(batch_n - d):
a, b = i, i+d
arr1, arr2 = arr[a], arr[b]
accum += tf.reduce_mean((arr1-arr2)**2) / decay_ratio**float(d)
return -accum
return inner
|
[
"def",
"alignment",
"(",
"layer",
",",
"decay_ratio",
"=",
"2",
")",
":",
"def",
"inner",
"(",
"T",
")",
":",
"batch_n",
"=",
"T",
"(",
"layer",
")",
".",
"get_shape",
"(",
")",
".",
"as_list",
"(",
")",
"[",
"0",
"]",
"arr",
"=",
"T",
"(",
"layer",
")",
"accum",
"=",
"0",
"for",
"d",
"in",
"[",
"1",
",",
"2",
",",
"3",
",",
"4",
"]",
":",
"for",
"i",
"in",
"range",
"(",
"batch_n",
"-",
"d",
")",
":",
"a",
",",
"b",
"=",
"i",
",",
"i",
"+",
"d",
"arr1",
",",
"arr2",
"=",
"arr",
"[",
"a",
"]",
",",
"arr",
"[",
"b",
"]",
"accum",
"+=",
"tf",
".",
"reduce_mean",
"(",
"(",
"arr1",
"-",
"arr2",
")",
"**",
"2",
")",
"/",
"decay_ratio",
"**",
"float",
"(",
"d",
")",
"return",
"-",
"accum",
"return",
"inner"
] |
Encourage neighboring images to be similar.
When visualizing the interpolation between two objectives, it's often
desireable to encourage analagous boejcts to be drawn in the same position,
to make them more comparable.
This term penalizes L2 distance between neighboring images, as evaluated at
layer.
In general, we find this most effective if used with a paramaterization that
shares across the batch. (In fact, that works quite well by iteself, so this
function may just be obselete.)
Args:
layer: layer to penalize at.
decay_ratio: how much to decay penalty as images move apart in batch.
Returns:
Objective.
|
[
"Encourage",
"neighboring",
"images",
"to",
"be",
"similar",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/objectives.py#L362-L393
|
train
|
tensorflow/lucid
|
lucid/optvis/objectives.py
|
diversity
|
def diversity(layer):
"""Encourage diversity between each batch element.
A neural net feature often responds to multiple things, but naive feature
visualization often only shows us one. If you optimize a batch of images,
this objective will encourage them all to be different.
In particular, it caculuates the correlation matrix of activations at layer
for each image, and then penalizes cossine similarity between them. This is
very similar to ideas in style transfer, except we're *penalizing* style
similarity instead of encouraging it.
Args:
layer: layer to evaluate activation correlations on.
Returns:
Objective.
"""
def inner(T):
layer_t = T(layer)
batch_n, _, _, channels = layer_t.get_shape().as_list()
flattened = tf.reshape(layer_t, [batch_n, -1, channels])
grams = tf.matmul(flattened, flattened, transpose_a=True)
grams = tf.nn.l2_normalize(grams, axis=[1,2], epsilon=1e-10)
return sum([ sum([ tf.reduce_sum(grams[i]*grams[j])
for j in range(batch_n) if j != i])
for i in range(batch_n)]) / batch_n
return inner
|
python
|
def diversity(layer):
"""Encourage diversity between each batch element.
A neural net feature often responds to multiple things, but naive feature
visualization often only shows us one. If you optimize a batch of images,
this objective will encourage them all to be different.
In particular, it caculuates the correlation matrix of activations at layer
for each image, and then penalizes cossine similarity between them. This is
very similar to ideas in style transfer, except we're *penalizing* style
similarity instead of encouraging it.
Args:
layer: layer to evaluate activation correlations on.
Returns:
Objective.
"""
def inner(T):
layer_t = T(layer)
batch_n, _, _, channels = layer_t.get_shape().as_list()
flattened = tf.reshape(layer_t, [batch_n, -1, channels])
grams = tf.matmul(flattened, flattened, transpose_a=True)
grams = tf.nn.l2_normalize(grams, axis=[1,2], epsilon=1e-10)
return sum([ sum([ tf.reduce_sum(grams[i]*grams[j])
for j in range(batch_n) if j != i])
for i in range(batch_n)]) / batch_n
return inner
|
[
"def",
"diversity",
"(",
"layer",
")",
":",
"def",
"inner",
"(",
"T",
")",
":",
"layer_t",
"=",
"T",
"(",
"layer",
")",
"batch_n",
",",
"_",
",",
"_",
",",
"channels",
"=",
"layer_t",
".",
"get_shape",
"(",
")",
".",
"as_list",
"(",
")",
"flattened",
"=",
"tf",
".",
"reshape",
"(",
"layer_t",
",",
"[",
"batch_n",
",",
"-",
"1",
",",
"channels",
"]",
")",
"grams",
"=",
"tf",
".",
"matmul",
"(",
"flattened",
",",
"flattened",
",",
"transpose_a",
"=",
"True",
")",
"grams",
"=",
"tf",
".",
"nn",
".",
"l2_normalize",
"(",
"grams",
",",
"axis",
"=",
"[",
"1",
",",
"2",
"]",
",",
"epsilon",
"=",
"1e-10",
")",
"return",
"sum",
"(",
"[",
"sum",
"(",
"[",
"tf",
".",
"reduce_sum",
"(",
"grams",
"[",
"i",
"]",
"*",
"grams",
"[",
"j",
"]",
")",
"for",
"j",
"in",
"range",
"(",
"batch_n",
")",
"if",
"j",
"!=",
"i",
"]",
")",
"for",
"i",
"in",
"range",
"(",
"batch_n",
")",
"]",
")",
"/",
"batch_n",
"return",
"inner"
] |
Encourage diversity between each batch element.
A neural net feature often responds to multiple things, but naive feature
visualization often only shows us one. If you optimize a batch of images,
this objective will encourage them all to be different.
In particular, it caculuates the correlation matrix of activations at layer
for each image, and then penalizes cossine similarity between them. This is
very similar to ideas in style transfer, except we're *penalizing* style
similarity instead of encouraging it.
Args:
layer: layer to evaluate activation correlations on.
Returns:
Objective.
|
[
"Encourage",
"diversity",
"between",
"each",
"batch",
"element",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/objectives.py#L396-L425
|
train
|
tensorflow/lucid
|
lucid/optvis/objectives.py
|
input_diff
|
def input_diff(orig_img):
"""Average L2 difference between optimized image and orig_img.
This objective is usually mutliplied by a negative number and used as a
penalty in making advarsarial counterexamples.
"""
def inner(T):
diff = T("input") - orig_img
return tf.sqrt(tf.reduce_mean(diff**2))
return inner
|
python
|
def input_diff(orig_img):
"""Average L2 difference between optimized image and orig_img.
This objective is usually mutliplied by a negative number and used as a
penalty in making advarsarial counterexamples.
"""
def inner(T):
diff = T("input") - orig_img
return tf.sqrt(tf.reduce_mean(diff**2))
return inner
|
[
"def",
"input_diff",
"(",
"orig_img",
")",
":",
"def",
"inner",
"(",
"T",
")",
":",
"diff",
"=",
"T",
"(",
"\"input\"",
")",
"-",
"orig_img",
"return",
"tf",
".",
"sqrt",
"(",
"tf",
".",
"reduce_mean",
"(",
"diff",
"**",
"2",
")",
")",
"return",
"inner"
] |
Average L2 difference between optimized image and orig_img.
This objective is usually mutliplied by a negative number and used as a
penalty in making advarsarial counterexamples.
|
[
"Average",
"L2",
"difference",
"between",
"optimized",
"image",
"and",
"orig_img",
"."
] |
d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e
|
https://github.com/tensorflow/lucid/blob/d1a1e2e4fd4be61b89b8cba20dc425a5ae34576e/lucid/optvis/objectives.py#L429-L438
|
train
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.