<commit_before>#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 17 14:52:36 2017

@author: mattjohnson
"""
import os
os.chdir('..')

from rmgpy.molecule.molecule import Molecule
from molecule.adjacency import Adjacency
from lines.line import getAdjMatrix, LineSegment
from image_processing import get_hough_lines
from lines_to_graph import lines_to_graph

def getLoss(files,params):
    loss = 0.0
    for fname in files:
        if test(fname,params):
            pass
        else:
            loss += 1.0
    return loss

def test(fname,params):
    bname = os.path.basename(os.path.normpath(fname))
    smiles = bname.split('.')[0]
    m = Molecule().fromSMILES(smiles)
    
    lines = get_hough_lines(fname)
    imdim = max([x.length for x in lines])

    # normalize all the lines
    for i,l in enumerate(lines):
        pt1 = l.pts[0]
        pt2 = l.pts[1]
        pt1.x /= imdim
        pt2.x /= imdim
        pt1.y /= imdim
        pt2.y /= imdim
        lines[i] = LineSegment([pt1,pt2])
    
    lines = lines_to_graph(lines, params)
    
    node_radius = params[6]
    
    adj = getAdjMatrix(lines,node_radius)
    atomnames = ['C']*adj.shape[0]
    molecule = Adjacency(adj, atomnames)
    molecule.addHydrogens()
    rmgmol = molecule.toRMGmol()
    
    return m.isIsomorphic(rmgmol)<commit_msg>add comments to loss functions<commit_after>#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 17 14:52:36 2017

@author: mattjohnson
"""
import os
os.chdir('..')

from rmgpy.molecule.molecule import Molecule
from molecule.adjacency import Adjacency
from lines.line import getAdjMatrix, LineSegment
from image_processing import get_hough_lines
from lines_to_graph import lines_to_graph

def getLoss(files,params):
    """
    calculates the loss for images in files and the set of parameters params
    """
    loss = 0.0
    for fname in files:
        if test(fname,params):
            pass
        else:
            loss += 1.0
    return loss

def test(fname,params):
    """
    checks if the given parameters allow proper identification of the image in fname
    assumes fname is of the form '/any/any/any/smilesString.any'
    for example /stuff/CC[O].thing 
    """
    bname = os.path.basename(os.path.normpath(fname)) #get rid of all but the final name
    smiles = bname.split('.')[0]
    m = Molecule().fromSMILES(smiles) #make actual molecule
    
    lines = get_hough_lines(fname)
    imdim = max([x.length for x in lines])

    # normalize all the lines
    for i,l in enumerate(lines):
        pt1 = l.pts[0]
        pt2 = l.pts[1]
        pt1.x /= imdim
        pt2.x /= imdim
        pt1.y /= imdim
        pt2.y /= imdim
        lines[i] = LineSegment([pt1,pt2])
    
    lines = lines_to_graph(lines, params)
    
    node_radius = params[6]
    
    adj = getAdjMatrix(lines,node_radius)
    atomnames = ['C']*adj.shape[0]
    molecule = Adjacency(adj, atomnames)
    molecule.addHydrogens()
    rmgmol = molecule.toRMGmol()
    
    return m.isIsomorphic(rmgmol)
{'fraction_non_alphanumeric': 0.11246300559026635, 'fraction_numerical': 0.020388030253206183, 'mean_word_length': 2.6344086021505375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3348732', 'n_tokens_mistral': 1115, 'n_tokens_neox': 1048, 'n_words': 266}