1

I'm currently working on a project that requires using the following python code to plot a sigma profile

"""
A small script that shows how to extract and plot the 
NHB, OH, OT portions of the sigma profiles
A part of usnistgov/COSMOSAC
"""
import os
import timeit
import json

import numpy as np
import matplotlib.pyplot as plt
import cCOSMO

here = os.path.abspath(os.path.dirname(__file__))
dbVT = cCOSMO.VirginiaTechProfileDatabase(
    here+"/profiles/VT2005/Sigma_Profile_Database_Index_v2.txt", 
    here+"/profiles/VT2005/Sigma_Profiles_v2/")
dbUD = cCOSMO.DelawareProfileDatabase(
    here+"/profiles/UD/complist.txt", 
    here+"/profiles/UD/sigma3/")

# Load the fluids into both databases (they start empty)
names = [ "METHANOL", "ETHANOL" ]
for iden in names:
    dbUD.add_profile(dbUD.normalize_identifier(iden))
    dbVT.add_profile(dbVT.normalize_identifier(iden))

# Plot the profiles
import matplotlib.pyplot as plt

# Iterate over the databases
for db, dbname in zip([dbVT, dbUD],['VT','UD']):
    # Iterate over the names
    for name, dashes in zip(names,[[2,2], []]):
        # Get the sigma profiles
        prof = db.get_profile(db.normalize_identifier(name))
        # Iterate over the profiles to be plotted
        for key in ['nhb', 'oh', 'ot']:
            profile = getattr(prof.profiles, key)
            PA = np.sum(profile.psigmaA)
            if PA > 0:
               plt.plot(profile.sigma, profile.psigmaA/PA, dashes=dashes, label=dbname+':'+name+':'+key)
            print(dbname, name, key, PA)

# Labeling and saving
plt.legend(loc='best')
plt.xlabel(r'$\sigma$ / e/$\AA^2$')
plt.ylabel(r'$p(\sigma)$ ')
plt.savefig('methanol_ethanol_profiles.pdf')
plt.close()

# Iterate over the databases
for db, dbname in zip([dbVT, dbUD],['VT','UD']):
    # Iterate over the names
    for name, dashes in zip(names,[[2,2], []]):
        # Get the sigma profiles
        profset = db.get_profile(db.normalize_identifier(name))
        total_profile = 0.0
        # Iterate over the profiles to be plotted
        for key in ['nhb', 'oh', 'ot']:
            profile = getattr(profset.profiles, key)
            PA = np.sum(profile.psigmaA)
            if PA > 0:
                total_profile += profile.psigmaA
        plt.plot(profset.profiles.nhb.sigma, total_profile/np.sum(total_profile), dashes=dashes, label=dbname+':'+name)

# Labeling and saving
plt.legend(loc='best')
plt.xlabel(r'$\sigma$ / e/$\AA^2$')
plt.ylabel(r'$p(\sigma)$ ')
plt.savefig('methanol_ethanol_summed_profiles.pdf')
plt.show()

Before getting to this point, I generated a file (.sigma) which contains all the data needed to generate a sigma profile. Because this code taps into different databases, I have been having difficulty figuring out how to run this using my own files.

The following is one of my attempts at getting this to work

"""
A small script that shows how to extract and plot the 
NHB, OH, OT portions of the sigma profiles

A part of usnistgov/COSMOSAC
"""
import os
import timeit
import json

import numpy as np
import matplotlib.pyplot as plt
import cCOSMO

here = os.path.abspath(os.path.dirname(__file__))
dbVT = cCOSMO.VirginiaTechProfileDatabase(
    here+"/profiles/VT2005/Sigma_Profile_Database_Index_v2.txt", 
    here+"/profiles/VT2005/Sigma_Profiles_v2/")
dbUD = cCOSMO.DelawareProfileDatabase(
    here+"/profiles/UD/complist.txt", 
    here+"/profiles/UD/sigma3/")
dbGR = here+"/profiles/GR/sigma"

# Load the fluids into both databases (they start empty)
names = [ "METHANOL", "ETHANOL" ]
for iden in names:
    dbUD.add_profile(dbUD.normalize_identifier(iden))
    dbVT.add_profile(dbVT.normalize_identifier(iden))

# Plot the profiles
import matplotlib.pyplot as plt

# Iterate over the databases
for db, dbname in zip([dbGR],['GR']):
    # Iterate over the names
    for name, dashes in zip("ACETIC_ACID",[[2,2], []]):
        # Get the sigma profiles
        prof = db.get_profile(db.normalize_identifier(name))
        # Iterate over the profiles to be plotted
        for key in ['nhb', 'oh', 'ot']:
            profile = getattr(prof.profiles, key)
            PA = np.sum(profile.psigmaA)
            if PA > 0:
               plt.plot(profile.sigma, profile.psigmaA/PA, dashes=dashes, label=dbname+':'+name+':'+key)
            print(dbname, name, key, PA)

# Labeling and saving
plt.legend(loc='best')
plt.xlabel(r'$\sigma$ / e/$\AA^2$')
plt.ylabel(r'$p(\sigma)$ ')
plt.savefig('methanol_ethanol_profiles.pdf')
plt.close()

# Iterate over the databases
for db, dbname in zip([dbGR],['GR']):
    # Iterate over the names
    for name, dashes in zip("ACETIC_ACID",[[2,2], []]):
        # Get the sigma profiles
        profset = db.get_profile(db.normalize_identifier(name))
        total_profile = 0.0
        # Iterate over the profiles to be plotted
        for key in ['nhb', 'oh', 'ot']:
            profile = getattr(profset.profiles, key)
            PA = np.sum(profile.psigmaA)
            if PA > 0:
                total_profile += profile.psigmaA
        plt.plot(profset.profiles.nhb.sigma, total_profile/np.sum(total_profile), dashes=dashes, label=dbname+':'+name)

# Labeling and saving
plt.legend(loc='best')
plt.xlabel(r'$\sigma$ / e/$\AA^2$')
plt.ylabel(r'$p(\sigma)$ ')
plt.savefig('methanol_ethanol_summed_profiles.pdf')
plt.show()

Here dbGR is my "database" which is simply a folder containing my .sigma file and ACETIC_ACID is the name of the file. When I attempt to run this I get the following:

AttributeError: 'str' object has no attribute 'get_profile'

This error is associated with the line

prof = db.get_profile(db.normalize_identifier(name))

I dont have much experience with python (or coding in general), so any help would be much appreciated.

Also, if you think I have left out some crucial information please let me know.

k95g
  • 11
  • 1

1 Answers1

1

The error is because the variable db you're trying to call the .get_profile() method on is a string defined previously as here+"/profiles/GR/sigma". It also looks like you may have some stuff missing when you define dbGR. The module cCOSMO likely has some function that you will need to define your database. Note that the example variables dbVT & dbUD are defined using the functions cCOSMO.VirginiaTechProfileDatabase() & cCOSMO.DelawareProfileDatabase(), respectively. It's also worth noting that those functions take two arguments, and it looks like you may have only expected to define the second argument of the two. If you have documentation on the module or access to someone who has used it in the past, they may be able to help you further. You can also try opening your python console and running the line:

>>> help('cCOSMO')

Good luck!

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 16 '23 at 09:56