forked from ontoportal/ontologies_linked_data
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract the submission metrics generation step to a file
- Loading branch information
1 parent
a2eb2c0
commit 70f314e
Showing
3 changed files
with
120 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...gies_linked_data/services/submission_process/operations/submission_mertrics_calculator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
module LinkedData | ||
module Services | ||
class SubmissionMetricsCalculator < OntologySubmissionProcess | ||
def process(logger, options = nil) | ||
process_metrics(logger) | ||
end | ||
|
||
def generate_umls_metrics_file(tr_file_path=nil) | ||
tr_file_path ||= @submission.triples_file_path | ||
class_count = 0 | ||
indiv_count = 0 | ||
prop_count = 0 | ||
|
||
File.foreach(tr_file_path) do |line| | ||
class_count += 1 if line =~ /owl:Class/ | ||
indiv_count += 1 if line =~ /owl:NamedIndividual/ | ||
prop_count += 1 if line =~ /owl:ObjectProperty/ | ||
prop_count += 1 if line =~ /owl:DatatypeProperty/ | ||
end | ||
generate_metrics_file(class_count, indiv_count, prop_count) | ||
end | ||
|
||
private | ||
|
||
def process_metrics(logger) | ||
status = LinkedData::Models::SubmissionStatus.find('METRICS').first | ||
begin | ||
compute_metrics(logger) | ||
@submission.add_submission_status(status) | ||
rescue StandardError => e | ||
logger.error("#{e.class}: #{e.message}\n#{e.backtrace.join("\n\t")}") | ||
logger.flush | ||
@submission.metrics = nil | ||
@submission.add_submission_status(status.get_error_status) | ||
ensure | ||
@submission.save | ||
end | ||
end | ||
|
||
def compute_metrics(logger) | ||
metrics = metrics_for_submission(logger) | ||
metrics.id = RDF::URI.new(@submission.id.to_s + '/metrics') | ||
exist_metrics = LinkedData::Models::Metric.find(metrics.id).first | ||
exist_metrics.delete if exist_metrics | ||
metrics.save | ||
@submission.metrics = metrics | ||
@submission | ||
end | ||
|
||
def metrics_for_submission(logger) | ||
logger.info('metrics_for_submission start') | ||
logger.flush | ||
begin | ||
@submission.bring(:submissionStatus) if @submission.bring?(:submissionStatus) | ||
cls_metrics = LinkedData::Metrics.class_metrics(@submission, logger) | ||
logger.info('class_metrics finished') | ||
logger.flush | ||
metrics = LinkedData::Models::Metric.new | ||
|
||
cls_metrics.each do |k,v| | ||
unless v.instance_of?(Integer) | ||
begin | ||
v = Integer(v) | ||
rescue ArgumentError | ||
v = 0 | ||
rescue TypeError | ||
v = 0 | ||
end | ||
end | ||
metrics.send("#{k}=",v) | ||
end | ||
indiv_count = LinkedData::Metrics.number_individuals(logger, @submission) | ||
metrics.individuals = indiv_count | ||
logger.info('individuals finished') | ||
logger.flush | ||
prop_count = LinkedData::Metrics.number_properties(logger, @submission) | ||
metrics.properties = prop_count | ||
logger.info('properties finished') | ||
logger.flush | ||
# re-generate metrics file | ||
generate_metrics_file(cls_metrics[:classes], indiv_count, prop_count) | ||
logger.info('generation of metrics file finished') | ||
logger.flush | ||
rescue StandardError => e | ||
logger.error(e.message) | ||
logger.error(e) | ||
logger.flush | ||
metrics = nil | ||
end | ||
metrics | ||
end | ||
|
||
def generate_metrics_file(class_count, indiv_count, prop_count) | ||
CSV.open(@submission.metrics_path, 'wb') do |csv| | ||
csv << ['Class Count', 'Individual Count', 'Property Count'] | ||
csv << [class_count, indiv_count, prop_count] | ||
end | ||
end | ||
|
||
# TODO to find usage in NCBO code | ||
def generate_metrics_file2(class_count, indiv_count, prop_count, max_depth) | ||
CSV.open(self.metrics_path, "wb") do |csv| | ||
csv << ["Class Count", "Individual Count", "Property Count", "Max Depth"] | ||
csv << [class_count, indiv_count, prop_count, max_depth] | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |